OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library domain.context; | 5 library domain.context; |
6 | 6 |
7 import 'package:analysis_server/src/analysis_server.dart'; | 7 import 'package:analysis_server/src/analysis_server.dart'; |
8 import 'package:analysis_server/src/protocol.dart'; | 8 import 'package:analysis_server/src/protocol.dart'; |
9 import 'package:analyzer/src/generated/engine.dart'; | 9 import 'package:analyzer/src/generated/engine.dart'; |
10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
11 | 11 |
12 /** | 12 /** |
13 * Instances of the class [ContextDomainHandler] implement a [RequestHandler] | 13 * Instances of the class [ContextDomainHandler] implement a [RequestHandler] |
14 * that handles requests in the context domain. | 14 * that handles requests in the context domain. |
15 */ | 15 */ |
16 class ContextDomainHandler implements RequestHandler { | 16 class ContextDomainHandler implements RequestHandler { |
17 /** | 17 /** |
18 * The name of the context.applyChanges request. | 18 * The name of the context.applyChanges request. |
19 */ | 19 */ |
20 static const String APPLY_CHANGES_NAME = 'context.applyChanges'; | 20 static const String APPLY_CHANGES_NAME = 'context.applyChanges'; |
21 | 21 |
22 /** | 22 /** |
| 23 * The name of the context.getFixes request. |
| 24 */ |
| 25 static const String GET_FIXES_NAME = 'context.getFixes'; |
| 26 |
| 27 /** |
23 * The name of the context.setOptions request. | 28 * The name of the context.setOptions request. |
24 */ | 29 */ |
25 static const String SET_OPTIONS_NAME = 'context.setOptions'; | 30 static const String SET_OPTIONS_NAME = 'context.setOptions'; |
26 | 31 |
27 /** | 32 /** |
28 * The name of the context.setPrioritySources request. | 33 * The name of the context.setPrioritySources request. |
29 */ | 34 */ |
30 static const String SET_PRIORITY_SOURCES_NAME = 'context.setPrioritySources'; | 35 static const String SET_PRIORITY_SOURCES_NAME = 'context.setPrioritySources'; |
31 | 36 |
32 /** | 37 /** |
| 38 * The name of the added parameter. |
| 39 */ |
| 40 static const String ADDED_PARAM = "added"; |
| 41 |
| 42 /** |
33 * The name of the changes parameter. | 43 * The name of the changes parameter. |
34 */ | 44 */ |
35 static const String CHANGES_PARAM = 'changes'; | 45 static const String CHANGES_PARAM = 'changes'; |
36 | 46 |
37 /** | 47 /** |
38 * The name of the contextId parameter. | 48 * The name of the contextId parameter. |
39 */ | 49 */ |
40 static const String CONTEXT_ID_PARAM = 'contextId'; | 50 static const String CONTEXT_ID_PARAM = 'contextId'; |
41 | 51 |
42 /** | 52 /** |
| 53 * The name of the modified parameter. |
| 54 */ |
| 55 static const String MODIFIED_PARAM = "modified"; |
| 56 |
| 57 /** |
43 * The name of the options parameter. | 58 * The name of the options parameter. |
44 */ | 59 */ |
45 static const String OPTIONS_PARAM = 'options'; | 60 static const String OPTIONS_PARAM = 'options'; |
46 | 61 |
47 /** | 62 /** |
| 63 * The name of the removed parameter. |
| 64 */ |
| 65 static const String REMOVED_PARAM = "removed"; |
| 66 |
| 67 /** |
48 * The name of the sources parameter. | 68 * The name of the sources parameter. |
49 */ | 69 */ |
50 static const String SOURCES_PARAM = 'sources'; | 70 static const String SOURCES_PARAM = 'sources'; |
51 | 71 |
52 /** | 72 /** |
53 * The name of the cacheSize option. | 73 * The name of the cacheSize option. |
54 */ | 74 */ |
55 static const String CACHE_SIZE_OPTION = 'cacheSize'; | 75 static const String CACHE_SIZE_OPTION = 'cacheSize'; |
56 | 76 |
57 /** | 77 /** |
(...skipping 30 matching lines...) Expand all Loading... |
88 * Initialize a newly created handler to handle requests for the given [server
]. | 108 * Initialize a newly created handler to handle requests for the given [server
]. |
89 */ | 109 */ |
90 ContextDomainHandler(this.server); | 110 ContextDomainHandler(this.server); |
91 | 111 |
92 @override | 112 @override |
93 Response handleRequest(Request request) { | 113 Response handleRequest(Request request) { |
94 try { | 114 try { |
95 String requestName = request.method; | 115 String requestName = request.method; |
96 if (requestName == APPLY_CHANGES_NAME) { | 116 if (requestName == APPLY_CHANGES_NAME) { |
97 return applyChanges(request); | 117 return applyChanges(request); |
| 118 } else if (requestName == GET_FIXES_NAME) { |
| 119 return getFixes(request); |
98 } else if (requestName == SET_OPTIONS_NAME) { | 120 } else if (requestName == SET_OPTIONS_NAME) { |
99 return setOptions(request); | 121 return setOptions(request); |
100 } else if (requestName == SET_PRIORITY_SOURCES_NAME) { | 122 } else if (requestName == SET_PRIORITY_SOURCES_NAME) { |
101 return setPrioritySources(request); | 123 return setPrioritySources(request); |
102 } | 124 } |
103 } on RequestFailure catch (exception) { | 125 } on RequestFailure catch (exception) { |
104 return exception.response; | 126 return exception.response; |
105 } | 127 } |
106 return null; | 128 return null; |
107 } | 129 } |
108 | 130 |
109 /** | 131 /** |
110 * Inform the specified context that the changes encoded in the change set | 132 * Inform the specified context that the changes encoded in the change set |
111 * have been made. Any invalidated analysis results will be flushed from the | 133 * have been made. Any invalidated analysis results will be flushed from the |
112 * context. | 134 * context. |
113 */ | 135 */ |
114 Response applyChanges(Request request) { | 136 Response applyChanges(Request request) { |
115 AnalysisContext context = getAnalysisContext(request); | 137 AnalysisContext context = getAnalysisContext(request); |
116 Map<String, Object> changesData = request.getRequiredParameter(CHANGES_PARAM
); | 138 Map<String, Object> changesData = request.getRequiredParameter(CHANGES_PARAM
); |
117 ChangeSet changeSet = createChangeSet(changesData); | 139 ChangeSet changeSet = createChangeSet( |
| 140 request, |
| 141 context.sourceFactory, |
| 142 changesData); |
118 | 143 |
119 context.applyChanges(changeSet); | 144 context.applyChanges(changeSet); |
120 Response response = new Response(request.id); | 145 Response response = new Response(request.id); |
121 return response; | 146 return response; |
122 } | 147 } |
123 | 148 |
124 /** | 149 /** |
125 * Convert the given JSON object into a [ChangeSet]. | 150 * Convert the given JSON object into a [ChangeSet], using the given |
| 151 * [sourceFactory] to convert the embedded strings into sources. |
126 */ | 152 */ |
127 ChangeSet createChangeSet(Map<String, Object> jsonData) { | 153 ChangeSet createChangeSet(Request request, SourceFactory sourceFactory, Map<St
ring, Object> jsonData) { |
128 // TODO(brianwilkerson) Implement this. | 154 ChangeSet changeSet = new ChangeSet(); |
129 return new ChangeSet(); | 155 convertSources(request, sourceFactory, jsonData[ADDED_PARAM], (Source source
) { |
| 156 changeSet.addedSource(source); |
| 157 }); |
| 158 convertSources(request, sourceFactory, jsonData[MODIFIED_PARAM], (Source sou
rce) { |
| 159 changeSet.changedSource(source); |
| 160 }); |
| 161 convertSources(request, sourceFactory, jsonData[REMOVED_PARAM], (Source sour
ce) { |
| 162 changeSet.removedSource(source); |
| 163 }); |
| 164 return changeSet; |
130 } | 165 } |
131 | 166 |
132 /** | 167 /** |
| 168 * If the given [sources] is a list of strings, use the given [sourceFactory] |
| 169 * to convert each string into a source and pass the source to the given |
| 170 * [handler]. Otherwise, throw an exception indicating that the data in the |
| 171 * request was not valid. |
| 172 */ |
| 173 void convertSources(Request request, SourceFactory sourceFactory, Object sourc
es, void handler(Source source)) { |
| 174 if (sources is! List<String>) { |
| 175 throw new RequestFailure(new Response(request.id, new RequestError(1, 'Inv
alid sources'))); |
| 176 } |
| 177 convertToSources(sourceFactory, sources).forEach(handler); |
| 178 } |
| 179 |
| 180 /** |
| 181 * Return the list of fixes that are available for problems related to the |
| 182 * given error in the specified context. |
| 183 */ |
| 184 Response getFixes(Request request) { |
| 185 // TODO(brianwilkerson) Implement this. |
| 186 Response response = new Response(request.id); |
| 187 return response; |
| 188 } |
| 189 |
| 190 /** |
133 * Set the options controlling analysis within a context to the given set of | 191 * Set the options controlling analysis within a context to the given set of |
134 * options. | 192 * options. |
135 */ | 193 */ |
136 Response setOptions(Request request) { | 194 Response setOptions(Request request) { |
137 AnalysisContext context = getAnalysisContext(request); | 195 AnalysisContext context = getAnalysisContext(request); |
138 | 196 |
139 context.analysisOptions = createAnalysisOptions(request); | 197 context.analysisOptions = createAnalysisOptions(request); |
140 Response response = new Response(request.id); | 198 Response response = new Response(request.id); |
141 return response; | 199 return response; |
142 } | 200 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 */ | 259 */ |
202 AnalysisContext getAnalysisContext(Request request) { | 260 AnalysisContext getAnalysisContext(Request request) { |
203 String contextId = request.getRequiredParameter(CONTEXT_ID_PARAM); | 261 String contextId = request.getRequiredParameter(CONTEXT_ID_PARAM); |
204 AnalysisContext context = server.contextMap[contextId]; | 262 AnalysisContext context = server.contextMap[contextId]; |
205 if (context == null) { | 263 if (context == null) { |
206 throw new RequestFailure(new Response.contextDoesNotExist(request)); | 264 throw new RequestFailure(new Response.contextDoesNotExist(request)); |
207 } | 265 } |
208 return context; | 266 return context; |
209 } | 267 } |
210 } | 268 } |
OLD | NEW |