OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
Brian Wilkerson
2015/09/11 20:26:24
2015
| |
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. | |
4 | |
5 library domains.analysis.occurrences_dart; | |
6 | |
7 import 'package:analysis_server/analysis/occurrences_core.dart'; | |
8 import 'package:analysis_server/src/protocol_server.dart' as protocol; | |
9 import 'package:analyzer/src/generated/ast.dart'; | |
10 import 'package:analyzer/src/generated/element.dart'; | |
11 import 'package:analyzer/src/generated/engine.dart'; | |
12 import 'package:analyzer/src/generated/source.dart'; | |
13 | |
14 /** | |
15 * A computer for occurrences in a Dart [CompilationUnit]. | |
16 */ | |
17 class DartOccurrencesComputer implements OccurrencesContributor { | |
18 @override | |
19 void computeOccurrences( | |
20 OccurrencesCollector collector, AnalysisContext context, Source source) { | |
21 List<Source> libraries = context.getLibrariesContaining(source); | |
22 if (libraries.isNotEmpty) { | |
23 CompilationUnit unit = | |
24 context.getResolvedCompilationUnit2(source, libraries.first); | |
25 if (unit != null) { | |
26 _DartUnitOccurrencesComputerVisitor visitor = | |
27 new _DartUnitOccurrencesComputerVisitor(); | |
28 unit.accept(visitor); | |
29 visitor.elementsOffsets.forEach((engineElement, offsets) { | |
30 int length = engineElement.displayName.length; | |
31 protocol.Element serverElement = | |
32 protocol.newElement_fromEngine(engineElement); | |
33 protocol.Occurrences occurrences = | |
34 new protocol.Occurrences(serverElement, offsets, length); | |
35 collector.addOccurrences(occurrences); | |
36 }); | |
37 } | |
38 } | |
39 } | |
40 } | |
41 | |
42 class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor { | |
43 final Map<Element, List<int>> elementsOffsets = <Element, List<int>>{}; | |
44 | |
45 @override | |
46 visitSimpleIdentifier(SimpleIdentifier node) { | |
47 Element element = node.bestElement; | |
48 if (element != null) { | |
49 _addOccurrence(element, node.offset); | |
50 } | |
51 return super.visitSimpleIdentifier(node); | |
52 } | |
53 | |
54 void _addOccurrence(Element element, int offset) { | |
55 element = _canonicalizeElement(element); | |
56 if (element == null || element == DynamicElementImpl.instance) { | |
57 return; | |
58 } | |
59 List<int> offsets = elementsOffsets[element]; | |
60 if (offsets == null) { | |
61 offsets = <int>[]; | |
62 elementsOffsets[element] = offsets; | |
63 } | |
64 offsets.add(offset); | |
65 } | |
66 | |
67 Element _canonicalizeElement(Element element) { | |
68 if (element is FieldFormalParameterElement) { | |
69 element = (element as FieldFormalParameterElement).field; | |
70 } | |
71 if (element is PropertyAccessorElement) { | |
72 element = (element as PropertyAccessorElement).variable; | |
73 } | |
74 if (element is Member) { | |
75 element = (element as Member).baseElement; | |
76 } | |
77 return element; | |
78 } | |
79 } | |
OLD | NEW |