OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/ast/visitor.dart'; | 8 import 'package:analyzer/dart/ast/visitor.dart'; |
9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
10 import 'package:analyzer/dart/element/visitor.dart'; | 10 import 'package:analyzer/dart/element/visitor.dart'; |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 /** | 23 /** |
24 * Returns references to the [element]. | 24 * Returns references to the [element]. |
25 */ | 25 */ |
26 Future<List<SearchResult>> references(Element element) async { | 26 Future<List<SearchResult>> references(Element element) async { |
27 if (element == null) { | 27 if (element == null) { |
28 return const <SearchResult>[]; | 28 return const <SearchResult>[]; |
29 } | 29 } |
30 | 30 |
31 ElementKind kind = element.kind; | 31 ElementKind kind = element.kind; |
32 if (kind == ElementKind.LABEL || kind == ElementKind.LOCAL_VARIABLE) { | 32 if (kind == ElementKind.FUNCTION || kind == ElementKind.METHOD) { |
| 33 if (element.enclosingElement is ExecutableElement) { |
| 34 return _searchReferences_Local(element, (n) => n is Block); |
| 35 } |
| 36 // return _searchReferences_Function(element); |
| 37 } else if (kind == ElementKind.LABEL || |
| 38 kind == ElementKind.LOCAL_VARIABLE) { |
33 return _searchReferences_Local(element, (n) => n is Block); | 39 return _searchReferences_Local(element, (n) => n is Block); |
| 40 } else if (kind == ElementKind.TYPE_PARAMETER) { |
| 41 return _searchReferences_Local( |
| 42 element, (n) => n.parent is CompilationUnit); |
34 } | 43 } |
35 // TODO(scheglov) support other kinds | 44 // TODO(scheglov) support other kinds |
36 return const <SearchResult>[]; | 45 return const <SearchResult>[]; |
37 } | 46 } |
38 | 47 |
39 Future<List<SearchResult>> _searchReferences_Local( | 48 Future<List<SearchResult>> _searchReferences_Local( |
40 Element element, bool isRootNode(AstNode n)) async { | 49 Element element, bool isRootNode(AstNode n)) async { |
41 String path = element.source.fullName; | 50 String path = element.source.fullName; |
42 if (!_driver.addedFiles.contains(path)) { | 51 if (!_driver.addedFiles.contains(path)) { |
43 return const <SearchResult>[]; | 52 return const <SearchResult>[]; |
44 } | 53 } |
45 | 54 |
46 // Prepare the unit. | 55 // Prepare the unit. |
47 AnalysisResult analysisResult = await _driver.getResult(path); | 56 AnalysisResult analysisResult = await _driver.getResult(path); |
48 CompilationUnit unit = analysisResult.unit; | 57 CompilationUnit unit = analysisResult.unit; |
49 if (unit == null) { | 58 if (unit == null) { |
50 return const <SearchResult>[]; | 59 return const <SearchResult>[]; |
51 } | 60 } |
52 | 61 |
53 // Prepare the node. | 62 // Prepare the node. |
54 AstNode node = new NodeLocator(element.nameOffset).searchWithin(unit); | 63 AstNode node = new NodeLocator(element.nameOffset).searchWithin(unit); |
55 if (node == null) { | 64 if (node == null) { |
56 return const <SearchResult>[]; | 65 return const <SearchResult>[]; |
57 } | 66 } |
58 | 67 |
59 // Prepare the enclosing node. | 68 // Prepare the enclosing node. |
60 AstNode enclosingNode = node.getAncestor(isRootNode); | 69 AstNode enclosingNode = node.getAncestor(isRootNode); |
| 70 if (enclosingNode == null) { |
| 71 return const <SearchResult>[]; |
| 72 } |
61 | 73 |
62 // Find the matches. | 74 // Find the matches. |
63 _LocalReferencesVisitor visitor = | 75 _LocalReferencesVisitor visitor = |
64 new _LocalReferencesVisitor(element, unit.element); | 76 new _LocalReferencesVisitor(element, unit.element); |
65 enclosingNode.accept(visitor); | 77 enclosingNode.accept(visitor); |
66 return visitor.results; | 78 return visitor.results; |
67 } | 79 } |
68 } | 80 } |
69 | 81 |
70 /** | 82 /** |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 } | 212 } |
201 | 213 |
202 void _addResult(AstNode node, SearchResultKind kind) { | 214 void _addResult(AstNode node, SearchResultKind kind) { |
203 bool isQualified = node.parent is Label; | 215 bool isQualified = node.parent is Label; |
204 var finder = new _ContainingElementFinder(node.offset); | 216 var finder = new _ContainingElementFinder(node.offset); |
205 enclosingUnitElement.accept(finder); | 217 enclosingUnitElement.accept(finder); |
206 results.add(new SearchResult._(element, finder.containingElement, kind, | 218 results.add(new SearchResult._(element, finder.containingElement, kind, |
207 node.offset, node.length, true, isQualified)); | 219 node.offset, node.length, true, isQualified)); |
208 } | 220 } |
209 } | 221 } |
OLD | NEW |