Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: pkg/analysis_server/lib/src/protocol2.dart

Issue 479683005: Make more use of generated code in analysis server. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 protocol2; 5 library protocol2;
6 6
7 import 'dart:convert'; 7 import 'dart:convert';
8 8
9 import 'package:analysis_server/src/computer/element.dart' show
10 elementFromEngine;
11 import 'package:analysis_server/src/search/search_result.dart' show
12 searchResultFromMatch;
9 import 'package:analysis_server/src/services/json.dart'; 13 import 'package:analysis_server/src/services/json.dart';
14 import 'package:analysis_server/src/services/search/search_engine.dart' as
15 engine;
10 import 'package:analyzer/src/generated/element.dart' as engine; 16 import 'package:analyzer/src/generated/element.dart' as engine;
11 import 'package:analyzer/src/generated/engine.dart' as engine; 17 import 'package:analyzer/src/generated/engine.dart' as engine;
12 import 'package:analyzer/src/generated/error.dart' as engine; 18 import 'package:analyzer/src/generated/error.dart' as engine;
13 import 'package:analyzer/src/generated/source.dart' as engine; 19 import 'package:analyzer/src/generated/source.dart' as engine;
14 20
15 import 'protocol.dart'; 21 import 'protocol.dart';
16 22
17 part 'generated_protocol.dart'; 23 part 'generated_protocol.dart';
18 24
19 /** 25 /**
20 * Translate the input [map], applying [keyCallback] to all its keys, and 26 * Translate the input [map], applying [keyCallback] to all its keys, and
21 * [valueCallback] to all its values. 27 * [valueCallback] to all its values.
22 */ 28 */
23 mapMap(Map map, {dynamic keyCallback(key), dynamic valueCallback(value)}) { 29 mapMap(Map map, {dynamic keyCallback(key), dynamic valueCallback(value)}) {
24 Map result = {}; 30 Map result = {};
25 map.forEach((key, value) { 31 map.forEach((key, value) {
26 if (keyCallback != null) { 32 if (keyCallback != null) {
27 key = keyCallback(key); 33 key = keyCallback(key);
28 } 34 }
29 if (valueCallback != null) { 35 if (valueCallback != null) {
30 value = valueCallback(value); 36 value = valueCallback(value);
31 } 37 }
32 result[key] = value; 38 result[key] = value;
33 }); 39 });
34 return result; 40 return result;
35 } 41 }
36 42
37 /** 43 /**
44 * Adds the given [sourceEdit] to the list in [sourceFileEdit].
45 */
46 void _addEditForSource(SourceFileEdit sourceFileEdit, SourceEdit sourceEdit) {
47 List<SourceEdit> edits = sourceFileEdit.edits;
48 int index = 0;
49 while (index < edits.length && edits[index].offset > sourceEdit.offset) {
50 index++;
51 }
52 edits.insert(index, sourceEdit);
53 }
54
55 /**
56 * Adds the given [sourceEdits] to the list in [sourceFileEdit].
57 */
58 void _addAllEditsForSource(SourceFileEdit sourceFileEdit,
59 Iterable<SourceEdit> edits) {
60 edits.forEach(sourceFileEdit.add);
61 }
62
63 /**
38 * Create an AnalysisError based on error information from the analyzer 64 * Create an AnalysisError based on error information from the analyzer
39 * engine. Access via AnalysisError.fromEngine(). 65 * engine. Access via AnalysisError.fromEngine().
40 */ 66 */
41 AnalysisError _analysisErrorFromEngine(engine.LineInfo 67 AnalysisError _analysisErrorFromEngine(engine.LineInfo
42 lineInfo, engine.AnalysisError error) { 68 lineInfo, engine.AnalysisError error) {
43 engine.ErrorCode errorCode = error.errorCode; 69 engine.ErrorCode errorCode = error.errorCode;
44 // prepare location 70 // prepare location
45 Location location; 71 Location location;
46 { 72 {
47 String file = error.source.fullName; 73 String file = error.source.fullName;
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 } 174 }
149 for (int i = 0; i < listA.length; i++) { 175 for (int i = 0; i < listA.length; i++) {
150 if (!itemEqual(listA[i], listB[i])) { 176 if (!itemEqual(listA[i], listB[i])) {
151 return false; 177 return false;
152 } 178 }
153 } 179 }
154 return true; 180 return true;
155 } 181 }
156 182
157 /** 183 /**
184 * Create a Location based on an element from the analyzer engine.
185 */
186 Location _locationFromElement(engine.Element element) {
187 engine.Source source = element.source;
188 engine.LineInfo lineInfo = element.context.getLineInfo(source);
189 String name = element.displayName;
190 // prepare location
191 int offset = element.nameOffset;
192 int length = name != null ? name.length : 0;
193 engine.LineInfo_Location lineLocation = lineInfo.getLocation(offset);
194 int startLine = lineLocation.lineNumber;
195 int startColumn = lineLocation.columnNumber;
196 if (element is engine.CompilationUnitElement) {
197 offset = 0;
198 length = 0;
199 startLine = 1;
200 startColumn = 1;
201 }
202 // done
203 return new Location(
204 source.fullName,
205 offset,
206 length,
207 startLine,
208 startColumn);
209 }
210
211 /**
212 * Create a Location based on an element and offset from the analyzer engine.
213 */
214 Location _locationFromOffset(engine.Element element, int offset, int length) {
215 engine.Source source = element.source;
216 engine.LineInfo lineInfo = element.context.getLineInfo(source);
217 // prepare location
218 engine.LineInfo_Location lineLocation = lineInfo.getLocation(offset);
219 int startLine = lineLocation.lineNumber;
220 int startColumn = lineLocation.columnNumber;
221 // done
222 return new Location(
223 source.fullName,
224 offset,
225 length,
226 startLine,
227 startColumn);
228 }
229
230 /**
158 * Compare the maps [mapA] and [mapB], using [valueEqual] to compare map 231 * Compare the maps [mapA] and [mapB], using [valueEqual] to compare map
159 * values. 232 * values.
160 */ 233 */
161 bool _mapEqual(Map mapA, Map mapB, bool valueEqual(a, b)) { 234 bool _mapEqual(Map mapA, Map mapB, bool valueEqual(a, b)) {
162 if (mapA.length != mapB.length) { 235 if (mapA.length != mapB.length) {
163 return false; 236 return false;
164 } 237 }
165 for (var key in mapA.keys) { 238 for (var key in mapA.keys) {
166 if (!mapB.containsKey(key)) { 239 if (!mapB.containsKey(key)) {
167 return false; 240 return false;
168 } 241 }
169 if (!valueEqual(mapA[key], mapB[key])) { 242 if (!valueEqual(mapA[key], mapB[key])) {
170 return false; 243 return false;
171 } 244 }
172 } 245 }
173 return true; 246 return true;
174 } 247 }
175 248
249 /**
250 * Create an OverriddenMember based on an element from the analyzer engine.
251 */
252 OverriddenMember _overriddenMemberFromEngine(engine.Element member) {
253 Element element = elementFromEngine(member);
254 String className = member.enclosingElement.displayName;
255 return new OverriddenMember(element, className);
256 }
257
258 /**
259 * Create a SearchResultKind based on a value from the search engine.
260 */
261 SearchResultKind _searchResultKindFromEngine(engine.MatchKind kind) {
262 if (kind == engine.MatchKind.DECLARATION) {
263 return SearchResultKind.DECLARATION;
264 }
265 if (kind == engine.MatchKind.READ) {
266 return SearchResultKind.READ;
267 }
268 if (kind == engine.MatchKind.READ_WRITE) {
269 return SearchResultKind.READ_WRITE;
270 }
271 if (kind == engine.MatchKind.WRITE) {
272 return SearchResultKind.WRITE;
273 }
274 if (kind == engine.MatchKind.INVOCATION) {
275 return SearchResultKind.INVOCATION;
276 }
277 if (kind == engine.MatchKind.REFERENCE) {
278 return SearchResultKind.REFERENCE;
279 }
280 return SearchResultKind.UNKNOWN;
281 }
282
176 283
177 /** 284 /**
178 * Type of callbacks used to decode parts of JSON objects. [jsonPath] is a 285 * Type of callbacks used to decode parts of JSON objects. [jsonPath] is a
179 * string describing the part of the JSON object being decoded, and [value] is 286 * string describing the part of the JSON object being decoded, and [value] is
180 * the part to decode. 287 * the part to decode.
181 */ 288 */
182 typedef Object JsonDecoderCallback(String jsonPath, Object value); 289 typedef Object JsonDecoderCallback(String jsonPath, Object value);
183 290
184 /** 291 /**
185 * Base class for decoding JSON objects. The derived class must implement 292 * Base class for decoding JSON objects. The derived class must implement
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); 475 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
369 hash = hash ^ (hash >> 11); 476 hash = hash ^ (hash >> 11);
370 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); 477 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
371 } 478 }
372 479
373 static int hash2(a, b) => finish(combine(combine(0, a), b)); 480 static int hash2(a, b) => finish(combine(combine(0, a), b));
374 481
375 static int hash4(a, b, c, d) => finish(combine(combine(combine(combine(0, a), 482 static int hash4(a, b, c, d) => finish(combine(combine(combine(combine(0, a),
376 b), c), d)); 483 b), c), d));
377 } 484 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698