| 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 domains.analysis.navigation_dart; | 5 library domains.analysis.navigation_dart; |
| 6 | 6 |
| 7 import 'package:analysis_server/analysis/navigation/navigation_core.dart'; | 7 import 'package:analysis_server/analysis/navigation/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'; |
| 11 import 'package:analyzer/src/generated/engine.dart'; | 11 import 'package:analyzer/src/generated/engine.dart'; |
| 12 import 'package:analyzer/src/generated/scanner.dart'; | 12 import 'package:analyzer/src/generated/scanner.dart'; |
| 13 import 'package:analyzer/src/generated/source.dart'; | 13 import 'package:analyzer/src/generated/source.dart'; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * A computer for navigation regions in a Dart [CompilationUnit]. | 16 * A computer for navigation regions in a Dart [CompilationUnit]. |
| 17 */ | 17 */ |
| 18 class DartNavigationComputer implements NavigationContributor { | 18 class DartNavigationComputer implements NavigationContributor { |
| 19 @override | 19 @override |
| 20 void computeNavigation(NavigationHolder holder, AnalysisContext context, | 20 void computeNavigation(NavigationCollector collector, AnalysisContext context, |
| 21 Source source, int offset, int length) { | 21 Source source, int offset, int length) { |
| 22 List<Source> libraries = context.getLibrariesContaining(source); | 22 List<Source> libraries = context.getLibrariesContaining(source); |
| 23 if (libraries.isNotEmpty) { | 23 if (libraries.isNotEmpty) { |
| 24 CompilationUnit unit = | 24 CompilationUnit unit = |
| 25 context.getResolvedCompilationUnit2(source, libraries.first); | 25 context.getResolvedCompilationUnit2(source, libraries.first); |
| 26 if (unit != null) { | 26 if (unit != null) { |
| 27 _DartNavigationHolder dartHolder = new _DartNavigationHolder(holder); | 27 _DartNavigationCollector dartCollector = |
| 28 new _DartNavigationCollector(collector); |
| 28 _DartNavigationComputerVisitor visitor = | 29 _DartNavigationComputerVisitor visitor = |
| 29 new _DartNavigationComputerVisitor(dartHolder); | 30 new _DartNavigationComputerVisitor(dartCollector); |
| 30 if (offset == null || length == null) { | 31 if (offset == null || length == null) { |
| 31 unit.accept(visitor); | 32 unit.accept(visitor); |
| 32 } else { | 33 } else { |
| 33 _DartRangeAstVisitor partVisitor = | 34 _DartRangeAstVisitor partVisitor = |
| 34 new _DartRangeAstVisitor(offset, offset + length, visitor); | 35 new _DartRangeAstVisitor(offset, offset + length, visitor); |
| 35 unit.accept(partVisitor); | 36 unit.accept(partVisitor); |
| 36 } | 37 } |
| 37 } | 38 } |
| 38 } | 39 } |
| 39 } | 40 } |
| 40 } | 41 } |
| 41 | 42 |
| 42 class _DartNavigationComputerVisitor extends RecursiveAstVisitor { | 43 class _DartNavigationComputerVisitor extends RecursiveAstVisitor { |
| 43 final _DartNavigationHolder computer; | 44 final _DartNavigationCollector computer; |
| 44 | 45 |
| 45 _DartNavigationComputerVisitor(this.computer); | 46 _DartNavigationComputerVisitor(this.computer); |
| 46 | 47 |
| 47 @override | 48 @override |
| 48 visitAssignmentExpression(AssignmentExpression node) { | 49 visitAssignmentExpression(AssignmentExpression node) { |
| 49 _safelyVisit(node.leftHandSide); | 50 _safelyVisit(node.leftHandSide); |
| 50 computer._addRegionForToken(node.operator, node.bestElement); | 51 computer._addRegionForToken(node.operator, node.bestElement); |
| 51 _safelyVisit(node.rightHandSide); | 52 _safelyVisit(node.rightHandSide); |
| 52 } | 53 } |
| 53 | 54 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 } | 217 } |
| 217 | 218 |
| 218 void _safelyVisit(AstNode node) { | 219 void _safelyVisit(AstNode node) { |
| 219 if (node != null) { | 220 if (node != null) { |
| 220 node.accept(this); | 221 node.accept(this); |
| 221 } | 222 } |
| 222 } | 223 } |
| 223 } | 224 } |
| 224 | 225 |
| 225 /** | 226 /** |
| 226 * A Dart specific wrapper around [NavigationHolder]. | 227 * A Dart specific wrapper around [NavigationCollector]. |
| 227 */ | 228 */ |
| 228 class _DartNavigationHolder { | 229 class _DartNavigationCollector { |
| 229 final NavigationHolder holder; | 230 final NavigationCollector collector; |
| 230 | 231 |
| 231 _DartNavigationHolder(this.holder); | 232 _DartNavigationCollector(this.collector); |
| 232 | 233 |
| 233 void _addRegion(int offset, int length, Element element) { | 234 void _addRegion(int offset, int length, Element element) { |
| 234 if (element is FieldFormalParameterElement) { | 235 if (element is FieldFormalParameterElement) { |
| 235 element = (element as FieldFormalParameterElement).field; | 236 element = (element as FieldFormalParameterElement).field; |
| 236 } | 237 } |
| 237 if (element == null || element == DynamicElementImpl.instance) { | 238 if (element == null || element == DynamicElementImpl.instance) { |
| 238 return; | 239 return; |
| 239 } | 240 } |
| 240 if (element.location == null) { | 241 if (element.location == null) { |
| 241 return; | 242 return; |
| 242 } | 243 } |
| 243 protocol.ElementKind kind = | 244 protocol.ElementKind kind = |
| 244 protocol.newElementKind_fromEngine(element.kind); | 245 protocol.newElementKind_fromEngine(element.kind); |
| 245 protocol.Location location = protocol.newLocation_fromElement(element); | 246 protocol.Location location = protocol.newLocation_fromElement(element); |
| 246 if (location == null) { | 247 if (location == null) { |
| 247 return; | 248 return; |
| 248 } | 249 } |
| 249 holder.addRegion(offset, length, kind, location); | 250 collector.addRegion(offset, length, kind, location); |
| 250 } | 251 } |
| 251 | 252 |
| 252 void _addRegion_nodeStart_nodeEnd(AstNode a, AstNode b, Element element) { | 253 void _addRegion_nodeStart_nodeEnd(AstNode a, AstNode b, Element element) { |
| 253 int offset = a.offset; | 254 int offset = a.offset; |
| 254 int length = b.end - offset; | 255 int length = b.end - offset; |
| 255 _addRegion(offset, length, element); | 256 _addRegion(offset, length, element); |
| 256 } | 257 } |
| 257 | 258 |
| 258 void _addRegion_tokenStart_nodeEnd(Token a, AstNode b, Element element) { | 259 void _addRegion_tokenStart_nodeEnd(Token a, AstNode b, Element element) { |
| 259 int offset = a.offset; | 260 int offset = a.offset; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 } | 302 } |
| 302 // The node starts or ends in the range. | 303 // The node starts or ends in the range. |
| 303 if (isInRange(node.offset) || isInRange(node.end)) { | 304 if (isInRange(node.offset) || isInRange(node.end)) { |
| 304 node.accept(visitor); | 305 node.accept(visitor); |
| 305 return; | 306 return; |
| 306 } | 307 } |
| 307 // Go deeper. | 308 // Go deeper. |
| 308 super.visitNode(node); | 309 super.visitNode(node); |
| 309 } | 310 } |
| 310 } | 311 } |
| OLD | NEW |