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

Side by Side Diff: pkg/analysis_server/lib/src/computer/computer_highlights.dart

Issue 366163003: Add highlight regions for comments. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 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
« no previous file with comments | « no previous file | pkg/analysis_server/test/analysis_notification_highlights_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 computer.highlights; 5 library computer.highlights;
6 6
7 import 'dart:collection';
8
9 import 'package:analysis_server/src/constants.dart'; 7 import 'package:analysis_server/src/constants.dart';
10 import 'package:analyzer/src/generated/ast.dart'; 8 import 'package:analyzer/src/generated/ast.dart';
11 import 'package:analyzer/src/generated/element.dart'; 9 import 'package:analyzer/src/generated/element.dart';
12 import 'package:analyzer/src/generated/scanner.dart'; 10 import 'package:analyzer/src/generated/scanner.dart';
13 11
14 12
15 /** 13 /**
16 * A computer for [HighlightRegion]s in a Dart [CompilationUnit]. 14 * A computer for [HighlightRegion]s in a Dart [CompilationUnit].
17 */ 15 */
18 class DartUnitHighlightsComputer { 16 class DartUnitHighlightsComputer {
19 final CompilationUnit _unit; 17 final CompilationUnit _unit;
20 18
21 final List<Map<String, Object>> _regions = <HashMap<String, Object>>[]; 19 final List<HighlightRegion> _regions = <HighlightRegion>[];
22 20
23 DartUnitHighlightsComputer(this._unit); 21 DartUnitHighlightsComputer(this._unit);
24 22
25 /** 23 /**
26 * Returns the computed highlight regions, not `null`. 24 * Returns the computed highlight regions, not `null`.
27 */ 25 */
28 List<Map<String, Object>> compute() { 26 List<Map<String, Object>> compute() {
29 _unit.accept(new _DartUnitHighlightsComputerVisitor(this)); 27 _unit.accept(new _DartUnitHighlightsComputerVisitor(this));
30 return _regions; 28 _addCommentRanges();
29 return _regions.map((region) => region.toJson()).toList();
30 }
31
32 void _addCommentRanges() {
33 Token token = _unit.beginToken;
34 while (token != null && token.type != TokenType.EOF) {
35 Token commentToken = token.precedingComments;
36 while (commentToken != null) {
37 HighlightType highlightType = null;
38 if (commentToken.type == TokenType.MULTI_LINE_COMMENT) {
39 if (commentToken.lexeme.startsWith('/**')) {
40 highlightType = HighlightType.COMMENT_DOCUMENTATION;
41 } else {
42 highlightType = HighlightType.COMMENT_BLOCK;
43 }
44 }
45 if (commentToken.type == TokenType.SINGLE_LINE_COMMENT) {
46 highlightType = HighlightType.COMMENT_END_OF_LINE;
47 }
48 if (highlightType != null) {
49 _addRegion_token(commentToken, highlightType);
50 }
51 commentToken = commentToken.next;
52 }
53 token = token.next;
54 }
31 } 55 }
32 56
33 void _addIdentifierRegion(SimpleIdentifier node) { 57 void _addIdentifierRegion(SimpleIdentifier node) {
34 if (_addIdentifierRegion_keyword(node)) { 58 if (_addIdentifierRegion_keyword(node)) {
35 return; 59 return;
36 } 60 }
37 if (_addIdentifierRegion_class(node)) { 61 if (_addIdentifierRegion_class(node)) {
38 return; 62 return;
39 } 63 }
40 if (_addIdentifierRegion_constructor(node)) { 64 if (_addIdentifierRegion_constructor(node)) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 return; 98 return;
75 } 99 }
76 _addRegion_node(node, HighlightType.IDENTIFIER_DEFAULT); 100 _addRegion_node(node, HighlightType.IDENTIFIER_DEFAULT);
77 } 101 }
78 102
79 void _addIdentifierRegion_annotation(Annotation node) { 103 void _addIdentifierRegion_annotation(Annotation node) {
80 ArgumentList arguments = node.arguments; 104 ArgumentList arguments = node.arguments;
81 if (arguments == null) { 105 if (arguments == null) {
82 _addRegion_node(node, HighlightType.ANNOTATION); 106 _addRegion_node(node, HighlightType.ANNOTATION);
83 } else { 107 } else {
84 _addRegion_nodeStart_tokenEnd(node, arguments.beginToken, HighlightType.AN NOTATION); 108 _addRegion_nodeStart_tokenEnd(node, arguments.beginToken,
109 HighlightType.ANNOTATION);
85 _addRegion_token(arguments.endToken, HighlightType.ANNOTATION); 110 _addRegion_token(arguments.endToken, HighlightType.ANNOTATION);
86 } 111 }
87 } 112 }
88 113
89 bool _addIdentifierRegion_class(SimpleIdentifier node) { 114 bool _addIdentifierRegion_class(SimpleIdentifier node) {
90 Element element = node.staticElement; 115 Element element = node.staticElement;
91 if (element is! ClassElement) { 116 if (element is! ClassElement) {
92 return false; 117 return false;
93 } 118 }
94 return _addRegion_node(node, HighlightType.CLASS); 119 return _addRegion_node(node, HighlightType.CLASS);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 AstNode parent = node.parent; 195 AstNode parent = node.parent;
171 if (!(parent is MethodDeclaration || parent is FunctionDeclaration)) { 196 if (!(parent is MethodDeclaration || parent is FunctionDeclaration)) {
172 return false; 197 return false;
173 } 198 }
174 // should be property accessor 199 // should be property accessor
175 Element element = node.staticElement; 200 Element element = node.staticElement;
176 if (element is! PropertyAccessorElement) { 201 if (element is! PropertyAccessorElement) {
177 return false; 202 return false;
178 } 203 }
179 // getter or setter 204 // getter or setter
180 PropertyAccessorElement propertyAccessorElement = element as PropertyAccesso rElement; 205 PropertyAccessorElement propertyAccessorElement = element as
206 PropertyAccessorElement;
181 if (propertyAccessorElement.isGetter) { 207 if (propertyAccessorElement.isGetter) {
182 return _addRegion_node(node, HighlightType.GETTER_DECLARATION); 208 return _addRegion_node(node, HighlightType.GETTER_DECLARATION);
183 } else { 209 } else {
184 return _addRegion_node(node, HighlightType.SETTER_DECLARATION); 210 return _addRegion_node(node, HighlightType.SETTER_DECLARATION);
185 } 211 }
186 } 212 }
187 213
188 bool _addIdentifierRegion_importPrefix(SimpleIdentifier node) { 214 bool _addIdentifierRegion_importPrefix(SimpleIdentifier node) {
189 Element element = node.staticElement; 215 Element element = node.staticElement;
190 if (element is! PrefixElement) { 216 if (element is! PrefixElement) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 285
260 bool _addIdentifierRegion_typeParameter(SimpleIdentifier node) { 286 bool _addIdentifierRegion_typeParameter(SimpleIdentifier node) {
261 Element element = node.staticElement; 287 Element element = node.staticElement;
262 if (element is! TypeParameterElement) { 288 if (element is! TypeParameterElement) {
263 return false; 289 return false;
264 } 290 }
265 return _addRegion_node(node, HighlightType.TYPE_PARAMETER); 291 return _addRegion_node(node, HighlightType.TYPE_PARAMETER);
266 } 292 }
267 293
268 void _addRegion(int offset, int length, HighlightType type) { 294 void _addRegion(int offset, int length, HighlightType type) {
269 _regions.add({OFFSET: offset, LENGTH: length, TYPE: type.name}); 295 _regions.add(new HighlightRegion(offset, length, type));
270 } 296 }
271 297
272 bool _addRegion_node(AstNode node, HighlightType type) { 298 bool _addRegion_node(AstNode node, HighlightType type) {
273 int offset = node.offset; 299 int offset = node.offset;
274 int length = node.length; 300 int length = node.length;
275 _addRegion(offset, length, type); 301 _addRegion(offset, length, type);
276 return true; 302 return true;
277 } 303 }
278 304
279 void _addRegion_nodeStart_tokenEnd(AstNode a, Token b, HighlightType type) { 305 void _addRegion_nodeStart_tokenEnd(AstNode a, Token b, HighlightType type) {
(...skipping 11 matching lines...) Expand all
291 } 317 }
292 318
293 void _addRegion_tokenStart_tokenEnd(Token a, Token b, HighlightType type) { 319 void _addRegion_tokenStart_tokenEnd(Token a, Token b, HighlightType type) {
294 int offset = a.offset; 320 int offset = a.offset;
295 int end = b.end; 321 int end = b.end;
296 _addRegion(offset, end - offset, type); 322 _addRegion(offset, end - offset, type);
297 } 323 }
298 } 324 }
299 325
300 326
327 class HighlightRegion {
328 final int offset;
329 final int length;
330 final HighlightType type;
331
332 HighlightRegion(this.offset, this.length, this.type);
333
334 factory HighlightRegion.fromJson(Map<String, Object> map) {
335 HighlightType type = HighlightType.valueOf(map[TYPE]);
336 return new HighlightRegion(map[OFFSET], map[LENGTH], type);
337 }
338
339 Map<String, Object> toJson() {
340 Map<String, Object> json = <String, Object>{};
341 json[OFFSET] = offset;
342 json[LENGTH] = length;
343 json[TYPE] = type.name;
344 return json;
345 }
346
347 @override
348 String toString() => toJson().toString();
349 }
350
351
352 /**
353 * Highlighting kinds constants.
354 */
355 class HighlightType {
356 static const HighlightType ANNOTATION = const HighlightType('ANNOTATION');
357 static const HighlightType BUILT_IN = const HighlightType('BUILT_IN');
358 static const HighlightType CLASS = const HighlightType('CLASS');
359 static const HighlightType COMMENT_BLOCK = const HighlightType(
360 'COMMENT_BLOCK');
361 static const HighlightType COMMENT_DOCUMENTATION = const HighlightType(
362 'COMMENT_DOCUMENTATION');
363 static const HighlightType COMMENT_END_OF_LINE = const HighlightType(
364 'COMMENT_END_OF_LINE');
365 static const HighlightType CONSTRUCTOR = const HighlightType('CONSTRUCTOR');
366 static const HighlightType DIRECTIVE = const HighlightType('DIRECTIVE');
367 static const HighlightType DYNAMIC_TYPE = const HighlightType('DYNAMIC_TYPE');
368 static const HighlightType FIELD = const HighlightType('FIELD');
369 static const HighlightType FIELD_STATIC = const HighlightType('FIELD_STATIC');
370 static const HighlightType FUNCTION_DECLARATION = const HighlightType(
371 'FUNCTION_DECLARATION');
372 static const HighlightType FUNCTION = const HighlightType('FUNCTION');
373 static const HighlightType FUNCTION_TYPE_ALIAS = const HighlightType(
374 'FUNCTION_TYPE_ALIAS');
375 static const HighlightType GETTER_DECLARATION = const HighlightType(
376 'GETTER_DECLARATION');
377 static const HighlightType KEYWORD = const HighlightType('KEYWORD');
378 static const HighlightType IDENTIFIER_DEFAULT = const HighlightType(
379 'IDENTIFIER_DEFAULT');
380 static const HighlightType IMPORT_PREFIX = const HighlightType(
381 'IMPORT_PREFIX');
382 static const HighlightType LITERAL_BOOLEAN = const HighlightType(
383 'LITERAL_BOOLEAN');
384 static const HighlightType LITERAL_DOUBLE = const HighlightType(
385 'LITERAL_DOUBLE');
386 static const HighlightType LITERAL_INTEGER = const HighlightType(
387 'LITERAL_INTEGER');
388 static const HighlightType LITERAL_LIST = const HighlightType('LITERAL_LIST');
389 static const HighlightType LITERAL_MAP = const HighlightType('LITERAL_MAP');
390 static const HighlightType LITERAL_STRING = const HighlightType(
391 'LITERAL_STRING');
392 static const HighlightType LOCAL_VARIABLE_DECLARATION = const HighlightType(
393 'LOCAL_VARIABLE_DECLARATION');
394 static const HighlightType LOCAL_VARIABLE = const HighlightType(
395 'LOCAL_VARIABLE');
396 static const HighlightType METHOD_DECLARATION = const HighlightType(
397 'METHOD_DECLARATION');
398 static const HighlightType METHOD_DECLARATION_STATIC = const HighlightType(
399 'METHOD_DECLARATION_STATIC');
400 static const HighlightType METHOD = const HighlightType('METHOD');
401 static const HighlightType METHOD_STATIC = const HighlightType(
402 'METHOD_STATIC');
403 static const HighlightType PARAMETER = const HighlightType('PARAMETER');
404 static const HighlightType SETTER_DECLARATION = const HighlightType(
405 'SETTER_DECLARATION');
406 static const HighlightType TOP_LEVEL_VARIABLE = const HighlightType(
407 'TOP_LEVEL_VARIABLE');
408 static const HighlightType TYPE_NAME_DYNAMIC = const HighlightType(
409 'TYPE_NAME_DYNAMIC');
410 static const HighlightType TYPE_PARAMETER = const HighlightType(
411 'TYPE_PARAMETER');
412
413 final String name;
414
415 const HighlightType(this.name);
416
417 @override
418 String toString() => name;
419
420 static HighlightType valueOf(String name) {
421 if (ANNOTATION.name == name) return ANNOTATION;
422 if (BUILT_IN.name == name) return BUILT_IN;
423 if (CLASS.name == name) return CLASS;
424 if (COMMENT_BLOCK.name == name) return COMMENT_BLOCK;
425 if (COMMENT_DOCUMENTATION.name == name) return COMMENT_DOCUMENTATION;
426 if (COMMENT_END_OF_LINE.name == name) return COMMENT_END_OF_LINE;
427 if (CONSTRUCTOR.name == name) return CONSTRUCTOR;
428 if (DIRECTIVE.name == name) return DIRECTIVE;
429 if (DYNAMIC_TYPE.name == name) return DYNAMIC_TYPE;
430 if (FIELD.name == name) return FIELD;
431 if (FIELD_STATIC.name == name) return FIELD_STATIC;
432 if (FUNCTION_DECLARATION.name == name) return FUNCTION_DECLARATION;
433 if (FUNCTION.name == name) return FUNCTION;
434 if (FUNCTION_TYPE_ALIAS.name == name) return FUNCTION_TYPE_ALIAS;
435 if (GETTER_DECLARATION.name == name) return GETTER_DECLARATION;
436 if (KEYWORD.name == name) return KEYWORD;
437 if (IDENTIFIER_DEFAULT.name == name) return IDENTIFIER_DEFAULT;
438 if (IMPORT_PREFIX.name == name) return IMPORT_PREFIX;
439 if (LITERAL_BOOLEAN.name == name) return LITERAL_BOOLEAN;
440 if (LITERAL_DOUBLE.name == name) return LITERAL_DOUBLE;
441 if (LITERAL_INTEGER.name == name) return LITERAL_INTEGER;
442 if (LITERAL_LIST.name == name) return LITERAL_LIST;
443 if (LITERAL_MAP.name == name) return LITERAL_MAP;
444 if (LITERAL_STRING.name == name) return LITERAL_STRING;
445 if (LOCAL_VARIABLE_DECLARATION.name == name) return
446 LOCAL_VARIABLE_DECLARATION;
447 if (LOCAL_VARIABLE.name == name) return LOCAL_VARIABLE;
448 if (METHOD_DECLARATION.name == name) return METHOD_DECLARATION;
449 if (METHOD_DECLARATION_STATIC.name == name) return
450 METHOD_DECLARATION_STATIC;
451 if (METHOD.name == name) return METHOD;
452 if (METHOD_STATIC.name == name) return METHOD_STATIC;
453 if (PARAMETER.name == name) return PARAMETER;
454 if (SETTER_DECLARATION.name == name) return SETTER_DECLARATION;
455 if (TOP_LEVEL_VARIABLE.name == name) return TOP_LEVEL_VARIABLE;
456 if (TYPE_NAME_DYNAMIC.name == name) return TYPE_NAME_DYNAMIC;
457 if (TYPE_PARAMETER.name == name) return TYPE_PARAMETER;
458 throw new ArgumentError('Unknown HighlightType: $name');
459 }
460 }
461
462
301 /** 463 /**
302 * An AST visitor for [DartUnitHighlightsComputer]. 464 * An AST visitor for [DartUnitHighlightsComputer].
303 */ 465 */
304 class _DartUnitHighlightsComputerVisitor extends RecursiveAstVisitor<Object> { 466 class _DartUnitHighlightsComputerVisitor extends RecursiveAstVisitor<Object> {
305 final DartUnitHighlightsComputer computer; 467 final DartUnitHighlightsComputer computer;
306 468
307 _DartUnitHighlightsComputerVisitor(this.computer); 469 _DartUnitHighlightsComputerVisitor(this.computer);
308 470
309 @override 471 @override
310 Object visitAnnotation(Annotation node) { 472 Object visitAnnotation(Annotation node) {
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 } 590 }
429 591
430 @override 592 @override
431 Object visitPartDirective(PartDirective node) { 593 Object visitPartDirective(PartDirective node) {
432 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); 594 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN);
433 return super.visitPartDirective(node); 595 return super.visitPartDirective(node);
434 } 596 }
435 597
436 @override 598 @override
437 Object visitPartOfDirective(PartOfDirective node) { 599 Object visitPartOfDirective(PartOfDirective node) {
438 computer._addRegion_tokenStart_tokenEnd(node.partToken, node.ofToken, Highli ghtType.BUILT_IN); 600 computer._addRegion_tokenStart_tokenEnd(node.partToken, node.ofToken,
601 HighlightType.BUILT_IN);
439 return super.visitPartOfDirective(node); 602 return super.visitPartOfDirective(node);
440 } 603 }
441 604
442 @override 605 @override
443 Object visitShowCombinator(ShowCombinator node) { 606 Object visitShowCombinator(ShowCombinator node) {
444 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); 607 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN);
445 return super.visitShowCombinator(node); 608 return super.visitShowCombinator(node);
446 } 609 }
447 610
448 @override 611 @override
(...skipping 13 matching lines...) Expand all
462 DartType type = node.type; 625 DartType type = node.type;
463 if (type != null) { 626 if (type != null) {
464 if (type.isDynamic && node.name.name == "dynamic") { 627 if (type.isDynamic && node.name.name == "dynamic") {
465 computer._addRegion_node(node, HighlightType.TYPE_NAME_DYNAMIC); 628 computer._addRegion_node(node, HighlightType.TYPE_NAME_DYNAMIC);
466 return null; 629 return null;
467 } 630 }
468 } 631 }
469 return super.visitTypeName(node); 632 return super.visitTypeName(node);
470 } 633 }
471 } 634 }
472
473
474 /**
475 * Highlighting kinds constants.
476 */
477 class HighlightType {
478 static const HighlightType ANNOTATION = const HighlightType('ANNOTATION');
479 static const HighlightType BUILT_IN = const HighlightType('BUILT_IN');
480 static const HighlightType CLASS = const HighlightType('CLASS');
481 static const HighlightType COMMENT_BLOCK = const HighlightType('COMMENT_BLOCK' );
482 static const HighlightType COMMENT_DOCUMENTATION = const HighlightType('COMMEN T_DOCUMENTATION');
483 static const HighlightType COMMENT_END_OF_LINE = const HighlightType('COMMENT_ END_OF_LINE');
484 static const HighlightType CONSTRUCTOR = const HighlightType('CONSTRUCTOR');
485 static const HighlightType DIRECTIVE = const HighlightType('DIRECTIVE');
486 static const HighlightType DYNAMIC_TYPE = const HighlightType('DYNAMIC_TYPE');
487 static const HighlightType FIELD = const HighlightType('FIELD');
488 static const HighlightType FIELD_STATIC = const HighlightType('FIELD_STATIC');
489 static const HighlightType FUNCTION_DECLARATION = const HighlightType('FUNCTIO N_DECLARATION');
490 static const HighlightType FUNCTION = const HighlightType('FUNCTION');
491 static const HighlightType FUNCTION_TYPE_ALIAS = const HighlightType('FUNCTION _TYPE_ALIAS');
492 static const HighlightType GETTER_DECLARATION = const HighlightType('GETTER_DE CLARATION');
493 static const HighlightType KEYWORD = const HighlightType('KEYWORD');
494 static const HighlightType IDENTIFIER_DEFAULT = const HighlightType('IDENTIFIE R_DEFAULT');
495 static const HighlightType IMPORT_PREFIX = const HighlightType('IMPORT_PREFIX' );
496 static const HighlightType LITERAL_BOOLEAN = const HighlightType('LITERAL_BOOL EAN');
497 static const HighlightType LITERAL_DOUBLE = const HighlightType('LITERAL_DOUBL E');
498 static const HighlightType LITERAL_INTEGER = const HighlightType('LITERAL_INTE GER');
499 static const HighlightType LITERAL_LIST = const HighlightType('LITERAL_LIST');
500 static const HighlightType LITERAL_MAP = const HighlightType('LITERAL_MAP');
501 static const HighlightType LITERAL_STRING = const HighlightType('LITERAL_STRIN G');
502 static const HighlightType LOCAL_VARIABLE_DECLARATION = const HighlightType('L OCAL_VARIABLE_DECLARATION');
503 static const HighlightType LOCAL_VARIABLE = const HighlightType('LOCAL_VARIABL E');
504 static const HighlightType METHOD_DECLARATION = const HighlightType('METHOD_DE CLARATION');
505 static const HighlightType METHOD_DECLARATION_STATIC = const HighlightType('ME THOD_DECLARATION_STATIC');
506 static const HighlightType METHOD = const HighlightType('METHOD');
507 static const HighlightType METHOD_STATIC = const HighlightType('METHOD_STATIC' );
508 static const HighlightType PARAMETER = const HighlightType('PARAMETER');
509 static const HighlightType SETTER_DECLARATION = const HighlightType('SETTER_DE CLARATION');
510 static const HighlightType TOP_LEVEL_VARIABLE = const HighlightType('TOP_LEVEL _VARIABLE');
511 static const HighlightType TYPE_NAME_DYNAMIC = const HighlightType('TYPE_NAME_ DYNAMIC');
512 static const HighlightType TYPE_PARAMETER = const HighlightType('TYPE_PARAMETE R');
513
514 final String name;
515
516 const HighlightType(this.name);
517 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/analysis_notification_highlights_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698