| 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 computer.navigation; | 5 library computer.navigation; |
| 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 import 'package:analyzer/src/generated/source.dart'; | |
| 14 | 11 |
| 15 import 'element.dart' as computer; | 12 import 'element.dart' as computer; |
| 16 | 13 |
| 17 | 14 |
| 18 /** | 15 /** |
| 19 * A computer for navigation regions in a Dart [CompilationUnit]. | 16 * A computer for navigation regions in a Dart [CompilationUnit]. |
| 20 */ | 17 */ |
| 21 class DartUnitNavigationComputer { | 18 class DartUnitNavigationComputer { |
| 22 final CompilationUnit _unit; | 19 final CompilationUnit _unit; |
| 23 | 20 |
| 24 final List<Map<String, Object>> _regions = <HashMap<String, Object>>[]; | 21 final List<Map<String, Object>> _regions = <Map<String, Object>>[]; |
| 25 | 22 |
| 26 DartUnitNavigationComputer(this._unit); | 23 DartUnitNavigationComputer(this._unit); |
| 27 | 24 |
| 28 /** | 25 /** |
| 29 * Returns the computed navigation regions, not `null`. | 26 * Returns the computed navigation regions, not `null`. |
| 30 */ | 27 */ |
| 31 List<Map<String, Object>> compute() { | 28 List<Map<String, Object>> compute() { |
| 32 _unit.accept(new _DartUnitNavigationComputerVisitor(this)); | 29 _unit.accept(new _DartUnitNavigationComputerVisitor(this)); |
| 33 return new List.from(_regions); | 30 return new List.from(_regions); |
| 34 } | 31 } |
| 35 | 32 |
| 36 void _addRegion(int offset, int length, Element element) { | 33 void _addRegion(int offset, int length, Element element) { |
| 37 Map<String, Object> target = _createTarget(element); | 34 if (element == null) { |
| 38 if (target == null) { | |
| 39 return; | 35 return; |
| 40 } | 36 } |
| 37 if (element is FieldFormalParameterElement) { |
| 38 element = (element as FieldFormalParameterElement).field; |
| 39 } |
| 40 var elementJson = new computer.Element.fromEngine(element).toJson(); |
| 41 _regions.add({ | 41 _regions.add({ |
| 42 OFFSET: offset, | 42 OFFSET: offset, |
| 43 LENGTH: length, | 43 LENGTH: length, |
| 44 TARGETS: [target] | 44 TARGETS: [elementJson] |
| 45 }); | 45 }); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void _addRegionForNode(AstNode node, Element element) { | 48 void _addRegionForNode(AstNode node, Element element) { |
| 49 int offset = node.offset; | 49 int offset = node.offset; |
| 50 int length = node.length; | 50 int length = node.length; |
| 51 _addRegion(offset, length, element); | 51 _addRegion(offset, length, element); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void _addRegionForToken(Token token, Element element) { | 54 void _addRegionForToken(Token token, Element element) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 67 int offset = a.offset; | 67 int offset = a.offset; |
| 68 int length = b.offset - offset; | 68 int length = b.offset - offset; |
| 69 _addRegion(offset, length, element); | 69 _addRegion(offset, length, element); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void _addRegion_tokenStart_nodeEnd(Token a, AstNode b, Element element) { | 72 void _addRegion_tokenStart_nodeEnd(Token a, AstNode b, Element element) { |
| 73 int offset = a.offset; | 73 int offset = a.offset; |
| 74 int length = b.end - offset; | 74 int length = b.end - offset; |
| 75 _addRegion(offset, length, element); | 75 _addRegion(offset, length, element); |
| 76 } | 76 } |
| 77 | |
| 78 /** | |
| 79 * Returns the JSON for the given [Element], maybe `null` if `null` was given. | |
| 80 */ | |
| 81 Map<String, Object> _createTarget(Element element) { | |
| 82 if (element == null) { | |
| 83 return null; | |
| 84 } | |
| 85 if (element is FieldFormalParameterElement) { | |
| 86 element = (element as FieldFormalParameterElement).field; | |
| 87 } | |
| 88 // prepare Source | |
| 89 Source source = element.source; | |
| 90 if (source == null) { | |
| 91 return null; | |
| 92 } | |
| 93 // prepare location | |
| 94 int offset = element.nameOffset; | |
| 95 int length = element.displayName.length; | |
| 96 if (element is CompilationUnitElement) { | |
| 97 offset = 0; | |
| 98 length = 0; | |
| 99 } | |
| 100 // return as JSON | |
| 101 return { | |
| 102 FILE: source.fullName, | |
| 103 OFFSET: offset, | |
| 104 LENGTH: length, | |
| 105 ELEMENT: new computer.Element.fromEngine(element).toJson() | |
| 106 }; | |
| 107 } | |
| 108 } | 77 } |
| 109 | 78 |
| 110 | 79 |
| 111 | 80 |
| 112 class _DartUnitNavigationComputerVisitor extends RecursiveAstVisitor { | 81 class _DartUnitNavigationComputerVisitor extends RecursiveAstVisitor { |
| 113 final DartUnitNavigationComputer computer; | 82 final DartUnitNavigationComputer computer; |
| 114 | 83 |
| 115 _DartUnitNavigationComputerVisitor(this.computer); | 84 _DartUnitNavigationComputerVisitor(this.computer); |
| 116 | 85 |
| 117 @override | 86 @override |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 | 176 |
| 208 @override | 177 @override |
| 209 visitSimpleIdentifier(SimpleIdentifier node) { | 178 visitSimpleIdentifier(SimpleIdentifier node) { |
| 210 if (node.parent is ConstructorDeclaration) { | 179 if (node.parent is ConstructorDeclaration) { |
| 211 } else { | 180 } else { |
| 212 computer._addRegionForNode(node, node.bestElement); | 181 computer._addRegionForNode(node, node.bestElement); |
| 213 } | 182 } |
| 214 return super.visitSimpleIdentifier(node); | 183 return super.visitSimpleIdentifier(node); |
| 215 } | 184 } |
| 216 } | 185 } |
| OLD | NEW |