| 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 import 'package:analysis_server/plugin/analysis/navigation/navigation_core.dart'
; | 5 import 'package:analysis_server/plugin/analysis/navigation/navigation_core.dart'
; |
| 6 import 'package:analysis_server/src/protocol_server.dart' as protocol; | 6 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/token.dart'; | 8 import 'package:analyzer/dart/ast/token.dart'; |
| 9 import 'package:analyzer/dart/ast/visitor.dart'; | 9 import 'package:analyzer/dart/ast/visitor.dart'; |
| 10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
| 11 import 'package:analyzer/dart/element/type.dart'; | 11 import 'package:analyzer/dart/element/type.dart'; |
| 12 import 'package:analyzer/src/dart/ast/utilities.dart'; | 12 import 'package:analyzer/src/dart/ast/utilities.dart'; |
| 13 import 'package:analyzer/src/dart/element/element.dart'; | 13 import 'package:analyzer/src/dart/element/element.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart'; | |
| 15 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 16 | 15 |
| 17 NavigationCollector computeDartNavigation(NavigationCollector collector, | 16 NavigationCollector computeDartNavigation(NavigationCollector collector, |
| 18 CompilationUnit unit, int offset, int length) { | 17 CompilationUnit unit, int offset, int length) { |
| 19 _DartNavigationCollector dartCollector = | 18 _DartNavigationCollector dartCollector = |
| 20 new _DartNavigationCollector(collector); | 19 new _DartNavigationCollector(collector); |
| 21 _DartNavigationComputerVisitor visitor = | 20 _DartNavigationComputerVisitor visitor = |
| 22 new _DartNavigationComputerVisitor(dartCollector); | 21 new _DartNavigationComputerVisitor(dartCollector); |
| 23 if (offset == null || length == null) { | 22 if (offset == null || length == null) { |
| 24 unit.accept(visitor); | 23 unit.accept(visitor); |
| 25 } else { | 24 } else { |
| 26 AstNode node = _getNodeForRange(unit, offset, length); | 25 AstNode node = _getNodeForRange(unit, offset, length); |
| 27 node?.accept(visitor); | 26 node?.accept(visitor); |
| 28 } | 27 } |
| 29 return collector; | 28 return collector; |
| 30 } | 29 } |
| 31 | 30 |
| 32 AstNode _getNodeForRange(CompilationUnit unit, int offset, int length) { | 31 AstNode _getNodeForRange(CompilationUnit unit, int offset, int length) { |
| 33 AstNode node = new NodeLocator(offset, offset + length).searchWithin(unit); | 32 AstNode node = new NodeLocator(offset, offset + length).searchWithin(unit); |
| 34 for (AstNode n = node; n != null; n = n.parent) { | 33 for (AstNode n = node; n != null; n = n.parent) { |
| 35 if (n is Directive) { | 34 if (n is Directive) { |
| 36 return n; | 35 return n; |
| 37 } | 36 } |
| 38 } | 37 } |
| 39 return node; | 38 return node; |
| 40 } | 39 } |
| 41 | 40 |
| 42 /** | 41 /** |
| 43 * A computer for navigation regions in a Dart [CompilationUnit]. | |
| 44 */ | |
| 45 class DartNavigationComputer implements NavigationContributor { | |
| 46 @override | |
| 47 void computeNavigation(NavigationCollector collector, AnalysisContext context, | |
| 48 Source source, int offset, int length) { | |
| 49 List<Source> libraries = context.getLibrariesContaining(source); | |
| 50 if (libraries.isNotEmpty) { | |
| 51 CompilationUnit unit = | |
| 52 context.getResolvedCompilationUnit2(source, libraries.first); | |
| 53 if (unit != null) { | |
| 54 computeDartNavigation(collector, unit, offset, length); | |
| 55 } | |
| 56 } | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * A Dart specific wrapper around [NavigationCollector]. | 42 * A Dart specific wrapper around [NavigationCollector]. |
| 62 */ | 43 */ |
| 63 class _DartNavigationCollector { | 44 class _DartNavigationCollector { |
| 64 final NavigationCollector collector; | 45 final NavigationCollector collector; |
| 65 | 46 |
| 66 _DartNavigationCollector(this.collector); | 47 _DartNavigationCollector(this.collector); |
| 67 | 48 |
| 68 void _addRegion(int offset, int length, Element element) { | 49 void _addRegion(int offset, int length, Element element) { |
| 69 if (element is FieldFormalParameterElement) { | 50 if (element is FieldFormalParameterElement) { |
| 70 element = (element as FieldFormalParameterElement).field; | 51 element = (element as FieldFormalParameterElement).field; |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 */ | 351 */ |
| 371 void _addUriDirectiveRegion(UriBasedDirective node, Element element) { | 352 void _addUriDirectiveRegion(UriBasedDirective node, Element element) { |
| 372 if (element != null) { | 353 if (element != null) { |
| 373 Source source = element.source; | 354 Source source = element.source; |
| 374 if (element.context.exists(source)) { | 355 if (element.context.exists(source)) { |
| 375 computer._addRegionForNode(node.uri, element); | 356 computer._addRegionForNode(node.uri, element); |
| 376 } | 357 } |
| 377 } | 358 } |
| 378 } | 359 } |
| 379 } | 360 } |
| OLD | NEW |