OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 domains.analysis.navigation_dart; | 5 library domains.analysis.navigation_dart; |
6 | 6 |
7 import 'package:analysis_server/analysis/navigation_core.dart'; | 7 import 'package:analysis_server/analysis/navigation_core.dart'; |
8 import 'package:analysis_server/src/protocol_server.dart' as protocol; | 8 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
9 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/src/generated/ast.dart'; |
10 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
(...skipping 22 matching lines...) Expand all Loading... |
33 } else { | 33 } else { |
34 _DartRangeAstVisitor partVisitor = | 34 _DartRangeAstVisitor partVisitor = |
35 new _DartRangeAstVisitor(offset, offset + length, visitor); | 35 new _DartRangeAstVisitor(offset, offset + length, visitor); |
36 unit.accept(partVisitor); | 36 unit.accept(partVisitor); |
37 } | 37 } |
38 } | 38 } |
39 } | 39 } |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
| 43 /** |
| 44 * A Dart specific wrapper around [NavigationCollector]. |
| 45 */ |
| 46 class _DartNavigationCollector { |
| 47 final NavigationCollector collector; |
| 48 |
| 49 _DartNavigationCollector(this.collector); |
| 50 |
| 51 void _addRegion(int offset, int length, Element element) { |
| 52 if (element is FieldFormalParameterElement) { |
| 53 element = (element as FieldFormalParameterElement).field; |
| 54 } |
| 55 if (element == null || element == DynamicElementImpl.instance) { |
| 56 return; |
| 57 } |
| 58 if (element.location == null) { |
| 59 return; |
| 60 } |
| 61 protocol.ElementKind kind = |
| 62 protocol.newElementKind_fromEngine(element.kind); |
| 63 protocol.Location location = protocol.newLocation_fromElement(element); |
| 64 if (location == null) { |
| 65 return; |
| 66 } |
| 67 collector.addRegion(offset, length, kind, location); |
| 68 } |
| 69 |
| 70 void _addRegion_nodeStart_nodeEnd(AstNode a, AstNode b, Element element) { |
| 71 int offset = a.offset; |
| 72 int length = b.end - offset; |
| 73 _addRegion(offset, length, element); |
| 74 } |
| 75 |
| 76 void _addRegionForNode(AstNode node, Element element) { |
| 77 int offset = node.offset; |
| 78 int length = node.length; |
| 79 _addRegion(offset, length, element); |
| 80 } |
| 81 |
| 82 void _addRegionForToken(Token token, Element element) { |
| 83 int offset = token.offset; |
| 84 int length = token.length; |
| 85 _addRegion(offset, length, element); |
| 86 } |
| 87 } |
| 88 |
43 class _DartNavigationComputerVisitor extends RecursiveAstVisitor { | 89 class _DartNavigationComputerVisitor extends RecursiveAstVisitor { |
44 final _DartNavigationCollector computer; | 90 final _DartNavigationCollector computer; |
45 | 91 |
46 _DartNavigationComputerVisitor(this.computer); | 92 _DartNavigationComputerVisitor(this.computer); |
47 | 93 |
48 @override | 94 @override |
49 visitAssignmentExpression(AssignmentExpression node) { | 95 visitAssignmentExpression(AssignmentExpression node) { |
50 _safelyVisit(node.leftHandSide); | 96 _safelyVisit(node.leftHandSide); |
51 computer._addRegionForToken(node.operator, node.bestElement); | 97 computer._addRegionForToken(node.operator, node.bestElement); |
52 _safelyVisit(node.rightHandSide); | 98 _safelyVisit(node.rightHandSide); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 super.visitImportDirective(node); | 169 super.visitImportDirective(node); |
124 } | 170 } |
125 | 171 |
126 @override | 172 @override |
127 visitIndexExpression(IndexExpression node) { | 173 visitIndexExpression(IndexExpression node) { |
128 super.visitIndexExpression(node); | 174 super.visitIndexExpression(node); |
129 computer._addRegionForToken(node.rightBracket, node.bestElement); | 175 computer._addRegionForToken(node.rightBracket, node.bestElement); |
130 } | 176 } |
131 | 177 |
132 @override | 178 @override |
| 179 visitLibraryDirective(LibraryDirective node) { |
| 180 computer._addRegionForNode(node.name, node.element); |
| 181 } |
| 182 |
| 183 @override |
133 visitPartDirective(PartDirective node) { | 184 visitPartDirective(PartDirective node) { |
134 _addUriDirectiveRegion(node, node.element); | 185 _addUriDirectiveRegion(node, node.element); |
135 super.visitPartDirective(node); | 186 super.visitPartDirective(node); |
136 } | 187 } |
137 | 188 |
138 @override | 189 @override |
139 visitPartOfDirective(PartOfDirective node) { | 190 visitPartOfDirective(PartOfDirective node) { |
140 computer._addRegionForNode(node.libraryName, node.element); | 191 computer._addRegionForNode(node.libraryName, node.element); |
141 super.visitPartOfDirective(node); | 192 super.visitPartOfDirective(node); |
142 } | 193 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 } | 267 } |
217 | 268 |
218 void _safelyVisit(AstNode node) { | 269 void _safelyVisit(AstNode node) { |
219 if (node != null) { | 270 if (node != null) { |
220 node.accept(this); | 271 node.accept(this); |
221 } | 272 } |
222 } | 273 } |
223 } | 274 } |
224 | 275 |
225 /** | 276 /** |
226 * A Dart specific wrapper around [NavigationCollector]. | |
227 */ | |
228 class _DartNavigationCollector { | |
229 final NavigationCollector collector; | |
230 | |
231 _DartNavigationCollector(this.collector); | |
232 | |
233 void _addRegion(int offset, int length, Element element) { | |
234 if (element is FieldFormalParameterElement) { | |
235 element = (element as FieldFormalParameterElement).field; | |
236 } | |
237 if (element == null || element == DynamicElementImpl.instance) { | |
238 return; | |
239 } | |
240 if (element.location == null) { | |
241 return; | |
242 } | |
243 protocol.ElementKind kind = | |
244 protocol.newElementKind_fromEngine(element.kind); | |
245 protocol.Location location = protocol.newLocation_fromElement(element); | |
246 if (location == null) { | |
247 return; | |
248 } | |
249 collector.addRegion(offset, length, kind, location); | |
250 } | |
251 | |
252 void _addRegion_nodeStart_nodeEnd(AstNode a, AstNode b, Element element) { | |
253 int offset = a.offset; | |
254 int length = b.end - offset; | |
255 _addRegion(offset, length, element); | |
256 } | |
257 | |
258 void _addRegionForNode(AstNode node, Element element) { | |
259 int offset = node.offset; | |
260 int length = node.length; | |
261 _addRegion(offset, length, element); | |
262 } | |
263 | |
264 void _addRegionForToken(Token token, Element element) { | |
265 int offset = token.offset; | |
266 int length = token.length; | |
267 _addRegion(offset, length, element); | |
268 } | |
269 } | |
270 | |
271 /** | |
272 * An AST visitor that forwards nodes intersecting with the range from | 277 * An AST visitor that forwards nodes intersecting with the range from |
273 * [start] to [end] to the given [visitor]. | 278 * [start] to [end] to the given [visitor]. |
274 */ | 279 */ |
275 class _DartRangeAstVisitor extends UnifyingAstVisitor { | 280 class _DartRangeAstVisitor extends UnifyingAstVisitor { |
276 final int start; | 281 final int start; |
277 final int end; | 282 final int end; |
278 final AstVisitor visitor; | 283 final AstVisitor visitor; |
279 | 284 |
280 _DartRangeAstVisitor(this.start, this.end, this.visitor); | 285 _DartRangeAstVisitor(this.start, this.end, this.visitor); |
281 | 286 |
(...skipping 13 matching lines...) Expand all Loading... |
295 } | 300 } |
296 // The node starts or ends in the range. | 301 // The node starts or ends in the range. |
297 if (isInRange(node.offset) || isInRange(node.end)) { | 302 if (isInRange(node.offset) || isInRange(node.end)) { |
298 node.accept(visitor); | 303 node.accept(visitor); |
299 return; | 304 return; |
300 } | 305 } |
301 // Go deeper. | 306 // Go deeper. |
302 super.visitNode(node); | 307 super.visitNode(node); |
303 } | 308 } |
304 } | 309 } |
OLD | NEW |