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.occurrences_dart; | 5 library domains.analysis.occurrences_dart; |
6 | 6 |
7 import 'package:analysis_server/plugin/analysis/occurrences/occurrences_core.dar
t'; | 7 import 'package:analysis_server/plugin/analysis/occurrences/occurrences_core.dar
t'; |
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/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
10 import 'package:analyzer/dart/ast/visitor.dart'; | 10 import 'package:analyzer/dart/ast/visitor.dart'; |
11 import 'package:analyzer/dart/element/element.dart'; | 11 import 'package:analyzer/dart/element/element.dart'; |
12 import 'package:analyzer/src/dart/element/element.dart'; | 12 import 'package:analyzer/src/dart/element/element.dart'; |
13 import 'package:analyzer/src/dart/element/member.dart'; | 13 import 'package:analyzer/src/dart/element/member.dart'; |
14 import 'package:analyzer/src/generated/engine.dart'; | 14 import 'package:analyzer/src/generated/engine.dart'; |
15 import 'package:analyzer/src/generated/source.dart'; | 15 import 'package:analyzer/src/generated/source.dart'; |
16 | 16 |
| 17 void addDartOccurrences(OccurrencesCollector collector, CompilationUnit unit) { |
| 18 _DartUnitOccurrencesComputerVisitor visitor = |
| 19 new _DartUnitOccurrencesComputerVisitor(); |
| 20 unit.accept(visitor); |
| 21 visitor.elementsOffsets.forEach((engineElement, offsets) { |
| 22 int length = engineElement.nameLength; |
| 23 protocol.Element serverElement = protocol.convertElement(engineElement); |
| 24 protocol.Occurrences occurrences = |
| 25 new protocol.Occurrences(serverElement, offsets, length); |
| 26 collector.addOccurrences(occurrences); |
| 27 }); |
| 28 } |
| 29 |
17 /** | 30 /** |
18 * A computer for occurrences in a Dart [CompilationUnit]. | 31 * A computer for occurrences in a Dart [CompilationUnit]. |
19 */ | 32 */ |
20 class DartOccurrencesComputer implements OccurrencesContributor { | 33 class DartOccurrencesComputer implements OccurrencesContributor { |
21 @override | 34 @override |
22 void computeOccurrences( | 35 void computeOccurrences( |
23 OccurrencesCollector collector, AnalysisContext context, Source source) { | 36 OccurrencesCollector collector, AnalysisContext context, Source source) { |
24 List<Source> libraries = context.getLibrariesContaining(source); | 37 List<Source> libraries = context.getLibrariesContaining(source); |
25 if (libraries.isNotEmpty) { | 38 if (libraries.isNotEmpty) { |
26 CompilationUnit unit = | 39 CompilationUnit unit = |
27 context.getResolvedCompilationUnit2(source, libraries.first); | 40 context.getResolvedCompilationUnit2(source, libraries.first); |
28 if (unit != null) { | 41 if (unit != null) { |
29 _DartUnitOccurrencesComputerVisitor visitor = | 42 addDartOccurrences(collector, unit); |
30 new _DartUnitOccurrencesComputerVisitor(); | |
31 unit.accept(visitor); | |
32 visitor.elementsOffsets.forEach((engineElement, offsets) { | |
33 int length = engineElement.nameLength; | |
34 protocol.Element serverElement = | |
35 protocol.convertElement(engineElement); | |
36 protocol.Occurrences occurrences = | |
37 new protocol.Occurrences(serverElement, offsets, length); | |
38 collector.addOccurrences(occurrences); | |
39 }); | |
40 } | 43 } |
41 } | 44 } |
42 } | 45 } |
43 } | 46 } |
44 | 47 |
45 class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor { | 48 class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor { |
46 final Map<Element, List<int>> elementsOffsets = <Element, List<int>>{}; | 49 final Map<Element, List<int>> elementsOffsets = <Element, List<int>>{}; |
47 | 50 |
48 @override | 51 @override |
49 visitSimpleIdentifier(SimpleIdentifier node) { | 52 visitSimpleIdentifier(SimpleIdentifier node) { |
(...skipping 23 matching lines...) Expand all Loading... |
73 } | 76 } |
74 if (element is PropertyAccessorElement) { | 77 if (element is PropertyAccessorElement) { |
75 element = (element as PropertyAccessorElement).variable; | 78 element = (element as PropertyAccessorElement).variable; |
76 } | 79 } |
77 if (element is Member) { | 80 if (element is Member) { |
78 element = (element as Member).baseElement; | 81 element = (element as Member).baseElement; |
79 } | 82 } |
80 return element; | 83 return element; |
81 } | 84 } |
82 } | 85 } |
OLD | NEW |