Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: pkg/analysis_server/lib/src/computer/computer_navigation.dart

Issue 315053005: Split computers.dart and use parsed JSON objects for navigation tests. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rollback debug output Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library computer.navigation;
6
7 import 'package:analyzer/src/generated/ast.dart';
8 import 'package:analyzer/src/generated/element.dart';
9 import 'package:analyzer/src/generated/scanner.dart';
10 import 'package:analyzer/src/generated/source.dart';
11
12
13 /**
14 * A computer for navigation regions in a Dart [CompilationUnit].
15 */
16 class DartUnitNavigationComputer {
17 final CompilationUnit _unit;
18
19 List<Map<String, Object>> _regions = [];
20
21 DartUnitNavigationComputer(this._unit);
22
23 /**
24 * Returns the computed navigation regions, not `null`.
25 */
26 List<Map<String, Object>> compute() {
27 _unit.accept(new _DartUnitNavigationComputerVisitor(this));
28 return new List.from(_regions);
29 }
30
31 void _addRegion(int offset, int length, Element element) {
32 Map<String, Object> target = _createTarget(element);
33 if (target == null) {
34 return;
35 }
36 _regions.add({
37 'offset': offset,
38 'length': length,
39 'targets': [target]
40 });
41 }
42
43 void _addRegion_nodeStart_nodeEnd(AstNode a, AstNode b, Element element) {
44 int offset = a.offset;
45 int length = b.end - offset;
46 _addRegion(offset, length, element);
47 }
48
49 void _addRegion_nodeStart_nodeStart(AstNode a, AstNode b, Element element) {
50 int offset = a.offset;
51 int length = b.offset - offset;
52 _addRegion(offset, length, element);
53 }
54
55 void _addRegion_tokenStart_nodeEnd(Token a, AstNode b, Element element) {
56 int offset = a.offset;
57 int length = b.end - offset;
58 _addRegion(offset, length, element);
59 }
60
61 void _addRegionForNode(AstNode node, Element element) {
62 int offset = node.offset;
63 int length = node.length;
64 _addRegion(offset, length, element);
65 }
66
67 void _addRegionForToken(Token token, Element element) {
68 int offset = token.offset;
69 int length = token.length;
70 _addRegion(offset, length, element);
71 }
72
73 /**
74 * Returns the JSON for the given [Element], maybe `null` if `null` was given.
75 */
76 Map<String, Object> _createTarget(Element element) {
77 if (element == null) {
78 return null;
79 }
80 if (element is FieldFormalParameterElement) {
81 element = (element as FieldFormalParameterElement).field;
82 }
83 // prepare Source
84 Source source = element.source;
85 if (source == null) {
86 return null;
87 }
88 // prepare location
89 int offset = element.nameOffset;
90 int length = element.displayName.length;
91 if (element is CompilationUnitElement) {
92 offset = 0;
93 length = 0;
94 }
95 // return as JSON
96 return {
97 'file': source.fullName,
98 'offset': offset,
99 'length': length,
100 'elementId': element.location.encoding
101 };
102 }
103 }
104
105
106
107 class _DartUnitNavigationComputerVisitor extends RecursiveAstVisitor {
108 final DartUnitNavigationComputer computer;
109
110 _DartUnitNavigationComputerVisitor(this.computer);
111
112 @override
113 visitAssignmentExpression(AssignmentExpression node) {
114 computer._addRegionForToken(node.operator, node.bestElement);
115 return super.visitAssignmentExpression(node);
116 }
117
118 @override
119 visitBinaryExpression(BinaryExpression node) {
120 computer._addRegionForToken(node.operator, node.bestElement);
121 return super.visitBinaryExpression(node);
122 }
123
124 @override
125 visitConstructorDeclaration(ConstructorDeclaration node) {
126 // associate constructor with "T" or "T.name"
127 {
128 AstNode firstNode = node.returnType;
129 AstNode lastNode = node.name;
130 if (lastNode == null) {
131 lastNode = firstNode;
132 }
133 if (firstNode != null && lastNode != null) {
134 computer._addRegion_nodeStart_nodeEnd(firstNode, lastNode, node.element) ;
135 }
136 }
137 return super.visitConstructorDeclaration(node);
138 }
139
140 @override
141 visitExportDirective(ExportDirective node) {
142 ExportElement exportElement = node.element;
143 if (exportElement != null) {
144 Element element = exportElement.exportedLibrary;
145 computer._addRegion_tokenStart_nodeEnd(node.keyword, node.uri, element);
146 }
147 return super.visitExportDirective(node);
148 }
149
150 @override
151 visitImportDirective(ImportDirective node) {
152 ImportElement importElement = node.element;
153 if (importElement != null) {
154 Element element = importElement.importedLibrary;
155 computer._addRegion_tokenStart_nodeEnd(node.keyword, node.uri, element);
156 }
157 return super.visitImportDirective(node);
158 }
159
160 @override
161 visitIndexExpression(IndexExpression node) {
162 computer._addRegionForToken(node.rightBracket, node.bestElement);
163 return super.visitIndexExpression(node);
164 }
165
166 @override
167 visitInstanceCreationExpression(InstanceCreationExpression node) {
168 Element element = node.staticElement;
169 if (element != null && element.isSynthetic) {
170 element = element.enclosingElement;
171 }
172 computer._addRegion_nodeStart_nodeStart(node, node.argumentList, element);
173 return super.visitInstanceCreationExpression(node);
174 }
175
176 @override
177 visitPartDirective(PartDirective node) {
178 computer._addRegion_tokenStart_nodeEnd(node.keyword, node.uri, node.element) ;
179 return super.visitPartDirective(node);
180 }
181
182 @override
183 visitPartOfDirective(PartOfDirective node) {
184 computer._addRegion_tokenStart_nodeEnd(node.keyword, node.libraryName, node. element);
185 return super.visitPartOfDirective(node);
186 }
187
188 @override
189 visitPostfixExpression(PostfixExpression node) {
190 computer._addRegionForToken(node.operator, node.bestElement);
191 return super.visitPostfixExpression(node);
192 }
193
194 @override
195 visitPrefixExpression(PrefixExpression node) {
196 computer._addRegionForToken(node.operator, node.bestElement);
197 return super.visitPrefixExpression(node);
198 }
199
200 @override
201 visitSimpleIdentifier(SimpleIdentifier node) {
202 if (node.parent is ConstructorDeclaration) {
203 } else {
204 computer._addRegionForNode(node, node.bestElement);
205 }
206 return super.visitSimpleIdentifier(node);
207 }
208 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/computer/computer_highlights.dart ('k') | pkg/analysis_server/lib/src/computers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698