| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library computers; | |
| 6 | |
| 7 import 'package:analyzer/src/generated/ast.dart'; | |
| 8 import 'package:analyzer/src/generated/element.dart'; | |
| 9 import 'package:analyzer/src/generated/scanner.dart'; | |
| 10 import 'package:analyzer/src/generated/source.dart'; | |
| 11 | |
| 12 | |
| 13 /** | |
| 14 * A computer for [HighlightRegion]s in a Dart [CompilationUnit]. | |
| 15 */ | |
| 16 class DartUnitHighlightsComputer { | |
| 17 final CompilationUnit _unit; | |
| 18 | |
| 19 final List<Map<String, Object>> _regions = <Map<String, Object>>[]; | |
| 20 | |
| 21 DartUnitHighlightsComputer(this._unit); | |
| 22 | |
| 23 /** | |
| 24 * Returns the computed highlight regions, not `null`. | |
| 25 */ | |
| 26 List<Map<String, Object>> compute() { | |
| 27 _unit.accept(new _DartUnitHighlightsComputerVisitor(this)); | |
| 28 return _regions; | |
| 29 } | |
| 30 | |
| 31 void _addIdentifierRegion(SimpleIdentifier node) { | |
| 32 if (_addIdentifierRegion_keyword(node)) { | |
| 33 return; | |
| 34 } | |
| 35 if (_addIdentifierRegion_class(node)) { | |
| 36 return; | |
| 37 } | |
| 38 if (_addIdentifierRegion_constructor(node)) { | |
| 39 return; | |
| 40 } | |
| 41 if (_addIdentifierRegion_dynamicType(node)) { | |
| 42 return; | |
| 43 } | |
| 44 if (_addIdentifierRegion_getterSetterDeclaration(node)) { | |
| 45 return; | |
| 46 } | |
| 47 if (_addIdentifierRegion_field(node)) { | |
| 48 return; | |
| 49 } | |
| 50 if (_addIdentifierRegion_function(node)) { | |
| 51 return; | |
| 52 } | |
| 53 if (_addIdentifierRegion_functionTypeAlias(node)) { | |
| 54 return; | |
| 55 } | |
| 56 if (_addIdentifierRegion_importPrefix(node)) { | |
| 57 return; | |
| 58 } | |
| 59 if (_addIdentifierRegion_localVariable(node)) { | |
| 60 return; | |
| 61 } | |
| 62 if (_addIdentifierRegion_method(node)) { | |
| 63 return; | |
| 64 } | |
| 65 if (_addIdentifierRegion_parameter(node)) { | |
| 66 return; | |
| 67 } | |
| 68 if (_addIdentifierRegion_topLevelVariable(node)) { | |
| 69 return; | |
| 70 } | |
| 71 if (_addIdentifierRegion_typeParameter(node)) { | |
| 72 return; | |
| 73 } | |
| 74 _addRegion_node(node, HighlightType.IDENTIFIER_DEFAULT); | |
| 75 } | |
| 76 | |
| 77 void _addIdentifierRegion_annotation(Annotation node) { | |
| 78 ArgumentList arguments = node.arguments; | |
| 79 if (arguments == null) { | |
| 80 _addRegion_node(node, HighlightType.ANNOTATION); | |
| 81 } else { | |
| 82 _addRegion_nodeStart_tokenEnd(node, arguments.beginToken, HighlightType.AN
NOTATION); | |
| 83 _addRegion_token(arguments.endToken, HighlightType.ANNOTATION); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 bool _addIdentifierRegion_class(SimpleIdentifier node) { | |
| 88 Element element = node.staticElement; | |
| 89 if (element is! ClassElement) { | |
| 90 return false; | |
| 91 } | |
| 92 return _addRegion_node(node, HighlightType.CLASS); | |
| 93 } | |
| 94 | |
| 95 bool _addIdentifierRegion_constructor(SimpleIdentifier node) { | |
| 96 Element element = node.staticElement; | |
| 97 if (element is! ConstructorElement) { | |
| 98 return false; | |
| 99 } | |
| 100 return _addRegion_node(node, HighlightType.CONSTRUCTOR); | |
| 101 } | |
| 102 | |
| 103 bool _addIdentifierRegion_dynamicType(SimpleIdentifier node) { | |
| 104 // should be variable | |
| 105 Element element = node.staticElement; | |
| 106 if (element is! VariableElement) { | |
| 107 return false; | |
| 108 } | |
| 109 // has propagated type | |
| 110 if (node.propagatedType != null) { | |
| 111 return false; | |
| 112 } | |
| 113 // has dynamic static type | |
| 114 DartType staticType = node.staticType; | |
| 115 if (staticType == null || !staticType.isDynamic) { | |
| 116 return false; | |
| 117 } | |
| 118 // OK | |
| 119 return _addRegion_node(node, HighlightType.DYNAMIC_TYPE); | |
| 120 } | |
| 121 | |
| 122 bool _addIdentifierRegion_field(SimpleIdentifier node) { | |
| 123 Element element = node.bestElement; | |
| 124 if (element is FieldFormalParameterElement) { | |
| 125 element = (element as FieldFormalParameterElement).field; | |
| 126 } | |
| 127 if (element is FieldElement) { | |
| 128 if ((element as FieldElement).isStatic) { | |
| 129 return _addRegion_node(node, HighlightType.FIELD_STATIC); | |
| 130 } else { | |
| 131 return _addRegion_node(node, HighlightType.FIELD); | |
| 132 } | |
| 133 } | |
| 134 if (element is PropertyAccessorElement) { | |
| 135 if ((element as PropertyAccessorElement).isStatic) { | |
| 136 return _addRegion_node(node, HighlightType.FIELD_STATIC); | |
| 137 } else { | |
| 138 return _addRegion_node(node, HighlightType.FIELD); | |
| 139 } | |
| 140 } | |
| 141 return false; | |
| 142 } | |
| 143 | |
| 144 bool _addIdentifierRegion_function(SimpleIdentifier node) { | |
| 145 Element element = node.staticElement; | |
| 146 if (element is! FunctionElement) { | |
| 147 return false; | |
| 148 } | |
| 149 HighlightType type; | |
| 150 if (node.inDeclarationContext()) { | |
| 151 type = HighlightType.FUNCTION_DECLARATION; | |
| 152 } else { | |
| 153 type = HighlightType.FUNCTION; | |
| 154 } | |
| 155 return _addRegion_node(node, type); | |
| 156 } | |
| 157 | |
| 158 bool _addIdentifierRegion_functionTypeAlias(SimpleIdentifier node) { | |
| 159 Element element = node.staticElement; | |
| 160 if (element is! FunctionTypeAliasElement) { | |
| 161 return false; | |
| 162 } | |
| 163 return _addRegion_node(node, HighlightType.FUNCTION_TYPE_ALIAS); | |
| 164 } | |
| 165 | |
| 166 bool _addIdentifierRegion_getterSetterDeclaration(SimpleIdentifier node) { | |
| 167 // should be declaration | |
| 168 AstNode parent = node.parent; | |
| 169 if (!(parent is MethodDeclaration || parent is FunctionDeclaration)) { | |
| 170 return false; | |
| 171 } | |
| 172 // should be property accessor | |
| 173 Element element = node.staticElement; | |
| 174 if (element is! PropertyAccessorElement) { | |
| 175 return false; | |
| 176 } | |
| 177 // getter or setter | |
| 178 PropertyAccessorElement propertyAccessorElement = element as PropertyAccesso
rElement; | |
| 179 if (propertyAccessorElement.isGetter) { | |
| 180 return _addRegion_node(node, HighlightType.GETTER_DECLARATION); | |
| 181 } else { | |
| 182 return _addRegion_node(node, HighlightType.SETTER_DECLARATION); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 bool _addIdentifierRegion_importPrefix(SimpleIdentifier node) { | |
| 187 Element element = node.staticElement; | |
| 188 if (element is! PrefixElement) { | |
| 189 return false; | |
| 190 } | |
| 191 return _addRegion_node(node, HighlightType.IMPORT_PREFIX); | |
| 192 } | |
| 193 | |
| 194 bool _addIdentifierRegion_keyword(SimpleIdentifier node) { | |
| 195 String name = node.name; | |
| 196 if (name == "void") { | |
| 197 return _addRegion_node(node, HighlightType.KEYWORD); | |
| 198 } | |
| 199 return false; | |
| 200 } | |
| 201 | |
| 202 bool _addIdentifierRegion_localVariable(SimpleIdentifier node) { | |
| 203 Element element = node.staticElement; | |
| 204 if (element is! LocalVariableElement) { | |
| 205 return false; | |
| 206 } | |
| 207 // OK | |
| 208 HighlightType type; | |
| 209 if (node.inDeclarationContext()) { | |
| 210 type = HighlightType.LOCAL_VARIABLE_DECLARATION; | |
| 211 } else { | |
| 212 type = HighlightType.LOCAL_VARIABLE; | |
| 213 } | |
| 214 return _addRegion_node(node, type); | |
| 215 } | |
| 216 | |
| 217 bool _addIdentifierRegion_method(SimpleIdentifier node) { | |
| 218 Element element = node.bestElement; | |
| 219 if (element is! MethodElement) { | |
| 220 return false; | |
| 221 } | |
| 222 MethodElement methodElement = element as MethodElement; | |
| 223 bool isStatic = methodElement.isStatic; | |
| 224 // OK | |
| 225 HighlightType type; | |
| 226 if (node.inDeclarationContext()) { | |
| 227 if (isStatic) { | |
| 228 type = HighlightType.METHOD_DECLARATION_STATIC; | |
| 229 } else { | |
| 230 type = HighlightType.METHOD_DECLARATION; | |
| 231 } | |
| 232 } else { | |
| 233 if (isStatic) { | |
| 234 type = HighlightType.METHOD_STATIC; | |
| 235 } else { | |
| 236 type = HighlightType.METHOD; | |
| 237 } | |
| 238 } | |
| 239 return _addRegion_node(node, type); | |
| 240 } | |
| 241 | |
| 242 bool _addIdentifierRegion_parameter(SimpleIdentifier node) { | |
| 243 Element element = node.staticElement; | |
| 244 if (element is! ParameterElement) { | |
| 245 return false; | |
| 246 } | |
| 247 return _addRegion_node(node, HighlightType.PARAMETER); | |
| 248 } | |
| 249 | |
| 250 bool _addIdentifierRegion_topLevelVariable(SimpleIdentifier node) { | |
| 251 Element element = node.staticElement; | |
| 252 if (element is! TopLevelVariableElement) { | |
| 253 return false; | |
| 254 } | |
| 255 return _addRegion_node(node, HighlightType.TOP_LEVEL_VARIABLE); | |
| 256 } | |
| 257 | |
| 258 bool _addIdentifierRegion_typeParameter(SimpleIdentifier node) { | |
| 259 Element element = node.staticElement; | |
| 260 if (element is! TypeParameterElement) { | |
| 261 return false; | |
| 262 } | |
| 263 return _addRegion_node(node, HighlightType.TYPE_PARAMETER); | |
| 264 } | |
| 265 | |
| 266 void _addRegion(int offset, int length, HighlightType type) { | |
| 267 _regions.add({'offset': offset, 'length': length, 'type': type.name}); | |
| 268 } | |
| 269 | |
| 270 bool _addRegion_node(AstNode node, HighlightType type) { | |
| 271 int offset = node.offset; | |
| 272 int length = node.length; | |
| 273 _addRegion(offset, length, type); | |
| 274 return true; | |
| 275 } | |
| 276 | |
| 277 void _addRegion_nodeStart_tokenEnd(AstNode a, Token b, HighlightType type) { | |
| 278 int offset = a.offset; | |
| 279 int end = b.end; | |
| 280 _addRegion(offset, end - offset, type); | |
| 281 } | |
| 282 | |
| 283 void _addRegion_token(Token token, HighlightType type) { | |
| 284 if (token != null) { | |
| 285 int offset = token.offset; | |
| 286 int length = token.length; | |
| 287 _addRegion(offset, length, type); | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 void _addRegion_tokenStart_tokenEnd(Token a, Token b, HighlightType type) { | |
| 292 int offset = a.offset; | |
| 293 int end = b.end; | |
| 294 _addRegion(offset, end - offset, type); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 | |
| 299 /** | |
| 300 * An AST visitor for [DartUnitHighlightsComputer]. | |
| 301 */ | |
| 302 class _DartUnitHighlightsComputerVisitor extends RecursiveAstVisitor<Object> { | |
| 303 final DartUnitHighlightsComputer computer; | |
| 304 | |
| 305 _DartUnitHighlightsComputerVisitor(this.computer); | |
| 306 | |
| 307 @override | |
| 308 Object visitAnnotation(Annotation node) { | |
| 309 computer._addIdentifierRegion_annotation(node); | |
| 310 return super.visitAnnotation(node); | |
| 311 } | |
| 312 | |
| 313 @override | |
| 314 Object visitAsExpression(AsExpression node) { | |
| 315 computer._addRegion_token(node.asOperator, HighlightType.BUILT_IN); | |
| 316 return super.visitAsExpression(node); | |
| 317 } | |
| 318 | |
| 319 @override | |
| 320 Object visitBooleanLiteral(BooleanLiteral node) { | |
| 321 computer._addRegion_node(node, HighlightType.LITERAL_BOOLEAN); | |
| 322 return super.visitBooleanLiteral(node); | |
| 323 } | |
| 324 | |
| 325 @override | |
| 326 Object visitCatchClause(CatchClause node) { | |
| 327 computer._addRegion_token(node.onKeyword, HighlightType.BUILT_IN); | |
| 328 return super.visitCatchClause(node); | |
| 329 } | |
| 330 | |
| 331 @override | |
| 332 Object visitClassDeclaration(ClassDeclaration node) { | |
| 333 computer._addRegion_token(node.abstractKeyword, HighlightType.BUILT_IN); | |
| 334 return super.visitClassDeclaration(node); | |
| 335 } | |
| 336 | |
| 337 @override | |
| 338 Object visitConstructorDeclaration(ConstructorDeclaration node) { | |
| 339 computer._addRegion_token(node.externalKeyword, HighlightType.BUILT_IN); | |
| 340 computer._addRegion_token(node.factoryKeyword, HighlightType.BUILT_IN); | |
| 341 return super.visitConstructorDeclaration(node); | |
| 342 } | |
| 343 | |
| 344 @override | |
| 345 Object visitDoubleLiteral(DoubleLiteral node) { | |
| 346 computer._addRegion_node(node, HighlightType.LITERAL_DOUBLE); | |
| 347 return super.visitDoubleLiteral(node); | |
| 348 } | |
| 349 | |
| 350 @override | |
| 351 Object visitExportDirective(ExportDirective node) { | |
| 352 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); | |
| 353 return super.visitExportDirective(node); | |
| 354 } | |
| 355 | |
| 356 @override | |
| 357 Object visitFieldDeclaration(FieldDeclaration node) { | |
| 358 computer._addRegion_token(node.staticKeyword, HighlightType.BUILT_IN); | |
| 359 return super.visitFieldDeclaration(node); | |
| 360 } | |
| 361 | |
| 362 @override | |
| 363 Object visitFunctionDeclaration(FunctionDeclaration node) { | |
| 364 computer._addRegion_token(node.externalKeyword, HighlightType.BUILT_IN); | |
| 365 computer._addRegion_token(node.propertyKeyword, HighlightType.BUILT_IN); | |
| 366 return super.visitFunctionDeclaration(node); | |
| 367 } | |
| 368 | |
| 369 @override | |
| 370 Object visitFunctionTypeAlias(FunctionTypeAlias node) { | |
| 371 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); | |
| 372 return super.visitFunctionTypeAlias(node); | |
| 373 } | |
| 374 | |
| 375 @override | |
| 376 Object visitHideCombinator(HideCombinator node) { | |
| 377 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); | |
| 378 return super.visitHideCombinator(node); | |
| 379 } | |
| 380 | |
| 381 @override | |
| 382 Object visitImplementsClause(ImplementsClause node) { | |
| 383 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); | |
| 384 return super.visitImplementsClause(node); | |
| 385 } | |
| 386 | |
| 387 @override | |
| 388 Object visitImportDirective(ImportDirective node) { | |
| 389 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); | |
| 390 computer._addRegion_token(node.deferredToken, HighlightType.BUILT_IN); | |
| 391 computer._addRegion_token(node.asToken, HighlightType.BUILT_IN); | |
| 392 return super.visitImportDirective(node); | |
| 393 } | |
| 394 | |
| 395 @override | |
| 396 Object visitIntegerLiteral(IntegerLiteral node) { | |
| 397 computer._addRegion_node(node, HighlightType.LITERAL_INTEGER); | |
| 398 return super.visitIntegerLiteral(node); | |
| 399 } | |
| 400 | |
| 401 @override | |
| 402 Object visitLibraryDirective(LibraryDirective node) { | |
| 403 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); | |
| 404 return super.visitLibraryDirective(node); | |
| 405 } | |
| 406 | |
| 407 @override | |
| 408 Object visitMethodDeclaration(MethodDeclaration node) { | |
| 409 computer._addRegion_token(node.externalKeyword, HighlightType.BUILT_IN); | |
| 410 computer._addRegion_token(node.modifierKeyword, HighlightType.BUILT_IN); | |
| 411 computer._addRegion_token(node.operatorKeyword, HighlightType.BUILT_IN); | |
| 412 computer._addRegion_token(node.propertyKeyword, HighlightType.BUILT_IN); | |
| 413 return super.visitMethodDeclaration(node); | |
| 414 } | |
| 415 | |
| 416 @override | |
| 417 Object visitNativeClause(NativeClause node) { | |
| 418 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); | |
| 419 return super.visitNativeClause(node); | |
| 420 } | |
| 421 | |
| 422 @override | |
| 423 Object visitNativeFunctionBody(NativeFunctionBody node) { | |
| 424 computer._addRegion_token(node.nativeToken, HighlightType.BUILT_IN); | |
| 425 return super.visitNativeFunctionBody(node); | |
| 426 } | |
| 427 | |
| 428 @override | |
| 429 Object visitPartDirective(PartDirective node) { | |
| 430 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); | |
| 431 return super.visitPartDirective(node); | |
| 432 } | |
| 433 | |
| 434 @override | |
| 435 Object visitPartOfDirective(PartOfDirective node) { | |
| 436 computer._addRegion_tokenStart_tokenEnd(node.partToken, node.ofToken, Highli
ghtType.BUILT_IN); | |
| 437 return super.visitPartOfDirective(node); | |
| 438 } | |
| 439 | |
| 440 @override | |
| 441 Object visitShowCombinator(ShowCombinator node) { | |
| 442 computer._addRegion_token(node.keyword, HighlightType.BUILT_IN); | |
| 443 return super.visitShowCombinator(node); | |
| 444 } | |
| 445 | |
| 446 @override | |
| 447 Object visitSimpleIdentifier(SimpleIdentifier node) { | |
| 448 computer._addIdentifierRegion(node); | |
| 449 return super.visitSimpleIdentifier(node); | |
| 450 } | |
| 451 | |
| 452 @override | |
| 453 Object visitSimpleStringLiteral(SimpleStringLiteral node) { | |
| 454 computer._addRegion_node(node, HighlightType.LITERAL_STRING); | |
| 455 return super.visitSimpleStringLiteral(node); | |
| 456 } | |
| 457 | |
| 458 @override | |
| 459 Object visitTypeName(TypeName node) { | |
| 460 DartType type = node.type; | |
| 461 if (type != null) { | |
| 462 if (type.isDynamic && node.name.name == "dynamic") { | |
| 463 computer._addRegion_node(node, HighlightType.TYPE_NAME_DYNAMIC); | |
| 464 return null; | |
| 465 } | |
| 466 } | |
| 467 return super.visitTypeName(node); | |
| 468 } | |
| 469 } | |
| 470 | |
| 471 | |
| 472 /** | |
| 473 * Highlighting kinds constants. | |
| 474 */ | |
| 475 class HighlightType { | |
| 476 static const HighlightType ANNOTATION = const HighlightType('ANNOTATION'); | |
| 477 static const HighlightType BUILT_IN = const HighlightType('BUILT_IN'); | |
| 478 static const HighlightType CLASS = const HighlightType('CLASS'); | |
| 479 static const HighlightType COMMENT_BLOCK = const HighlightType('COMMENT_BLOCK'
); | |
| 480 static const HighlightType COMMENT_DOCUMENTATION = const HighlightType('COMMEN
T_DOCUMENTATION'); | |
| 481 static const HighlightType COMMENT_END_OF_LINE = const HighlightType('COMMENT_
END_OF_LINE'); | |
| 482 static const HighlightType CONSTRUCTOR = const HighlightType('CONSTRUCTOR'); | |
| 483 static const HighlightType DIRECTIVE = const HighlightType('DIRECTIVE'); | |
| 484 static const HighlightType DYNAMIC_TYPE = const HighlightType('DYNAMIC_TYPE'); | |
| 485 static const HighlightType FIELD = const HighlightType('FIELD'); | |
| 486 static const HighlightType FIELD_STATIC = const HighlightType('FIELD_STATIC'); | |
| 487 static const HighlightType FUNCTION_DECLARATION = const HighlightType('FUNCTIO
N_DECLARATION'); | |
| 488 static const HighlightType FUNCTION = const HighlightType('FUNCTION'); | |
| 489 static const HighlightType FUNCTION_TYPE_ALIAS = const HighlightType('FUNCTION
_TYPE_ALIAS'); | |
| 490 static const HighlightType GETTER_DECLARATION = const HighlightType('GETTER_DE
CLARATION'); | |
| 491 static const HighlightType KEYWORD = const HighlightType('KEYWORD'); | |
| 492 static const HighlightType IDENTIFIER_DEFAULT = const HighlightType('IDENTIFIE
R_DEFAULT'); | |
| 493 static const HighlightType IMPORT_PREFIX = const HighlightType('IMPORT_PREFIX'
); | |
| 494 static const HighlightType LITERAL_BOOLEAN = const HighlightType('LITERAL_BOOL
EAN'); | |
| 495 static const HighlightType LITERAL_DOUBLE = const HighlightType('LITERAL_DOUBL
E'); | |
| 496 static const HighlightType LITERAL_INTEGER = const HighlightType('LITERAL_INTE
GER'); | |
| 497 static const HighlightType LITERAL_LIST = const HighlightType('LITERAL_LIST'); | |
| 498 static const HighlightType LITERAL_MAP = const HighlightType('LITERAL_MAP'); | |
| 499 static const HighlightType LITERAL_STRING = const HighlightType('LITERAL_STRIN
G'); | |
| 500 static const HighlightType LOCAL_VARIABLE_DECLARATION = const HighlightType('L
OCAL_VARIABLE_DECLARATION'); | |
| 501 static const HighlightType LOCAL_VARIABLE = const HighlightType('LOCAL_VARIABL
E'); | |
| 502 static const HighlightType METHOD_DECLARATION = const HighlightType('METHOD_DE
CLARATION'); | |
| 503 static const HighlightType METHOD_DECLARATION_STATIC = const HighlightType('ME
THOD_DECLARATION_STATIC'); | |
| 504 static const HighlightType METHOD = const HighlightType('METHOD'); | |
| 505 static const HighlightType METHOD_STATIC = const HighlightType('METHOD_STATIC'
); | |
| 506 static const HighlightType PARAMETER = const HighlightType('PARAMETER'); | |
| 507 static const HighlightType SETTER_DECLARATION = const HighlightType('SETTER_DE
CLARATION'); | |
| 508 static const HighlightType TOP_LEVEL_VARIABLE = const HighlightType('TOP_LEVEL
_VARIABLE'); | |
| 509 static const HighlightType TYPE_NAME_DYNAMIC = const HighlightType('TYPE_NAME_
DYNAMIC'); | |
| 510 static const HighlightType TYPE_PARAMETER = const HighlightType('TYPE_PARAMETE
R'); | |
| 511 | |
| 512 final String name; | |
| 513 | |
| 514 @override | |
| 515 String toString() => name; | |
| 516 | |
| 517 const HighlightType(this.name); | |
| 518 } | |
| 519 | |
| 520 | |
| 521 /** | |
| 522 * A computer for navigation regions in a Dart [CompilationUnit]. | |
| 523 */ | |
| 524 class DartUnitNavigationComputer { | |
| 525 final CompilationUnit _unit; | |
| 526 | |
| 527 List<Map<String, Object>> _regions = []; | |
| 528 | |
| 529 DartUnitNavigationComputer(this._unit); | |
| 530 | |
| 531 /** | |
| 532 * Returns the computed navigation regions, not `null`. | |
| 533 */ | |
| 534 List<Map<String, Object>> compute() { | |
| 535 _unit.accept(new _DartUnitNavigationComputerVisitor(this)); | |
| 536 return new List.from(_regions); | |
| 537 } | |
| 538 | |
| 539 void _addRegion(int offset, int length, Element element) { | |
| 540 Map<String, Object> target = _createTarget(element); | |
| 541 if (target == null) { | |
| 542 return; | |
| 543 } | |
| 544 _regions.add({ | |
| 545 'offset': offset, | |
| 546 'length': length, | |
| 547 'targets': [target] | |
| 548 }); | |
| 549 } | |
| 550 | |
| 551 void _addRegion_nodeStart_nodeEnd(AstNode a, AstNode b, Element element) { | |
| 552 int offset = a.offset; | |
| 553 int length = b.end - offset; | |
| 554 _addRegion(offset, length, element); | |
| 555 } | |
| 556 | |
| 557 void _addRegion_nodeStart_nodeStart(AstNode a, AstNode b, Element element) { | |
| 558 int offset = a.offset; | |
| 559 int length = b.offset - offset; | |
| 560 _addRegion(offset, length, element); | |
| 561 } | |
| 562 | |
| 563 void _addRegion_tokenStart_nodeEnd(Token a, AstNode b, Element element) { | |
| 564 int offset = a.offset; | |
| 565 int length = b.end - offset; | |
| 566 _addRegion(offset, length, element); | |
| 567 } | |
| 568 | |
| 569 void _addRegionForNode(AstNode node, Element element) { | |
| 570 int offset = node.offset; | |
| 571 int length = node.length; | |
| 572 _addRegion(offset, length, element); | |
| 573 } | |
| 574 | |
| 575 void _addRegionForToken(Token token, Element element) { | |
| 576 int offset = token.offset; | |
| 577 int length = token.length; | |
| 578 _addRegion(offset, length, element); | |
| 579 } | |
| 580 | |
| 581 /** | |
| 582 * Returns the JSON for the given [Element], maybe `null` if `null` was given. | |
| 583 */ | |
| 584 Map<String, Object> _createTarget(Element element) { | |
| 585 if (element == null) { | |
| 586 return null; | |
| 587 } | |
| 588 if (element is FieldFormalParameterElement) { | |
| 589 element = (element as FieldFormalParameterElement).field; | |
| 590 } | |
| 591 // prepare Source | |
| 592 Source source = element.source; | |
| 593 if (source == null) { | |
| 594 return null; | |
| 595 } | |
| 596 // prepare location | |
| 597 int offset = element.nameOffset; | |
| 598 int length = element.displayName.length; | |
| 599 if (element is CompilationUnitElement) { | |
| 600 offset = 0; | |
| 601 length = 0; | |
| 602 } | |
| 603 // return as JSON | |
| 604 return { | |
| 605 'file': source.fullName, | |
| 606 'offset': offset, | |
| 607 'length': length, | |
| 608 'elementId': element.location.encoding | |
| 609 }; | |
| 610 } | |
| 611 } | |
| 612 | |
| 613 | |
| 614 | |
| 615 class _DartUnitNavigationComputerVisitor extends RecursiveAstVisitor { | |
| 616 final DartUnitNavigationComputer computer; | |
| 617 | |
| 618 _DartUnitNavigationComputerVisitor(this.computer); | |
| 619 | |
| 620 @override | |
| 621 visitAssignmentExpression(AssignmentExpression node) { | |
| 622 computer._addRegionForToken(node.operator, node.bestElement); | |
| 623 return super.visitAssignmentExpression(node); | |
| 624 } | |
| 625 | |
| 626 @override | |
| 627 visitBinaryExpression(BinaryExpression node) { | |
| 628 computer._addRegionForToken(node.operator, node.bestElement); | |
| 629 return super.visitBinaryExpression(node); | |
| 630 } | |
| 631 | |
| 632 @override | |
| 633 visitConstructorDeclaration(ConstructorDeclaration node) { | |
| 634 // associate constructor with "T" or "T.name" | |
| 635 { | |
| 636 AstNode firstNode = node.returnType; | |
| 637 AstNode lastNode = node.name; | |
| 638 if (lastNode == null) { | |
| 639 lastNode = firstNode; | |
| 640 } | |
| 641 if (firstNode != null && lastNode != null) { | |
| 642 computer._addRegion_nodeStart_nodeEnd(firstNode, lastNode, node.element)
; | |
| 643 } | |
| 644 } | |
| 645 return super.visitConstructorDeclaration(node); | |
| 646 } | |
| 647 | |
| 648 @override | |
| 649 visitExportDirective(ExportDirective node) { | |
| 650 ExportElement exportElement = node.element; | |
| 651 if (exportElement != null) { | |
| 652 Element element = exportElement.exportedLibrary; | |
| 653 computer._addRegion_tokenStart_nodeEnd(node.keyword, node.uri, element); | |
| 654 } | |
| 655 return super.visitExportDirective(node); | |
| 656 } | |
| 657 | |
| 658 @override | |
| 659 visitImportDirective(ImportDirective node) { | |
| 660 ImportElement importElement = node.element; | |
| 661 if (importElement != null) { | |
| 662 Element element = importElement.importedLibrary; | |
| 663 computer._addRegion_tokenStart_nodeEnd(node.keyword, node.uri, element); | |
| 664 } | |
| 665 return super.visitImportDirective(node); | |
| 666 } | |
| 667 | |
| 668 @override | |
| 669 visitIndexExpression(IndexExpression node) { | |
| 670 computer._addRegionForToken(node.rightBracket, node.bestElement); | |
| 671 return super.visitIndexExpression(node); | |
| 672 } | |
| 673 | |
| 674 @override | |
| 675 visitInstanceCreationExpression(InstanceCreationExpression node) { | |
| 676 Element element = node.staticElement; | |
| 677 if (element != null && element.isSynthetic) { | |
| 678 element = element.enclosingElement; | |
| 679 } | |
| 680 computer._addRegion_nodeStart_nodeStart(node, node.argumentList, element); | |
| 681 return super.visitInstanceCreationExpression(node); | |
| 682 } | |
| 683 | |
| 684 @override | |
| 685 visitPartDirective(PartDirective node) { | |
| 686 computer._addRegion_tokenStart_nodeEnd(node.keyword, node.uri, node.element)
; | |
| 687 return super.visitPartDirective(node); | |
| 688 } | |
| 689 | |
| 690 @override | |
| 691 visitPartOfDirective(PartOfDirective node) { | |
| 692 computer._addRegion_tokenStart_nodeEnd(node.keyword, node.libraryName, node.
element); | |
| 693 return super.visitPartOfDirective(node); | |
| 694 } | |
| 695 | |
| 696 @override | |
| 697 visitPostfixExpression(PostfixExpression node) { | |
| 698 computer._addRegionForToken(node.operator, node.bestElement); | |
| 699 return super.visitPostfixExpression(node); | |
| 700 } | |
| 701 | |
| 702 @override | |
| 703 visitPrefixExpression(PrefixExpression node) { | |
| 704 computer._addRegionForToken(node.operator, node.bestElement); | |
| 705 return super.visitPrefixExpression(node); | |
| 706 } | |
| 707 | |
| 708 @override | |
| 709 visitSimpleIdentifier(SimpleIdentifier node) { | |
| 710 if (node.parent is ConstructorDeclaration) { | |
| 711 } else { | |
| 712 computer._addRegionForNode(node, node.bestElement); | |
| 713 } | |
| 714 return super.visitSimpleIdentifier(node); | |
| 715 } | |
| 716 } | |
| OLD | NEW |