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

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

Issue 3002303002: Reduce duplicated code (Closed)
Patch Set: Created 3 years, 3 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
« no previous file with comments | « no previous file | pkg/analysis_server/lib/src/services/completion/dart/completion_manager.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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/src/collections.dart'; 5 import 'package:analysis_server/src/collections.dart';
6 import 'package:analyzer/dart/ast/ast.dart'; 6 import 'package:analyzer/dart/ast/ast.dart';
7 import 'package:analyzer/dart/ast/visitor.dart'; 7 import 'package:analyzer/dart/ast/visitor.dart';
8 import 'package:analyzer/dart/element/element.dart' as engine; 8 import 'package:analyzer/dart/element/element.dart' as engine;
9 import 'package:analyzer/dart/element/type.dart' as engine; 9 import 'package:analyzer/dart/element/type.dart' as engine;
10 import 'package:analyzer/src/generated/source.dart'; 10 import 'package:analyzer/src/generated/source.dart';
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 Location _getLocationOffsetLength(int offset, int length) { 105 Location _getLocationOffsetLength(int offset, int length) {
106 LineInfo_Location lineLocation = lineInfo.getLocation(offset); 106 LineInfo_Location lineLocation = lineInfo.getLocation(offset);
107 int startLine = lineLocation.lineNumber; 107 int startLine = lineLocation.lineNumber;
108 int startColumn = lineLocation.columnNumber; 108 int startColumn = lineLocation.columnNumber;
109 return new Location(file, offset, length, startLine, startColumn); 109 return new Location(file, offset, length, startLine, startColumn);
110 } 110 }
111 111
112 /** 112 /**
113 * Returns the [AstNode]'s source region. 113 * Returns the [AstNode]'s source region.
114 */ 114 */
115 _SourceRegion _getSourceRegion(AstNode node) { 115 SourceRange _getSourceRange(AstNode node) {
116 int endOffset = node.end; 116 int endOffset = node.end;
117 // prepare position of the node among its siblings 117 // prepare position of the node among its siblings
118 int firstOffset; 118 int firstOffset;
119 List<AstNode> siblings; 119 List<AstNode> siblings;
120 AstNode parent = node.parent; 120 AstNode parent = node.parent;
121 // field 121 // field
122 if (parent is VariableDeclarationList) { 122 if (parent is VariableDeclarationList) {
123 VariableDeclarationList variableList = parent as VariableDeclarationList; 123 VariableDeclarationList variableList = parent as VariableDeclarationList;
124 List<VariableDeclaration> variables = variableList.variables; 124 List<VariableDeclaration> variables = variableList.variables;
125 int variableIndex = variables.indexOf(node); 125 int variableIndex = variables.indexOf(node);
126 if (variableIndex == variables.length - 1) { 126 if (variableIndex == variables.length - 1) {
127 endOffset = variableList.parent.end; 127 endOffset = variableList.parent.end;
128 } 128 }
129 if (variableIndex == 0) { 129 if (variableIndex == 0) {
130 node = parent.parent; 130 node = parent.parent;
131 parent = node.parent; 131 parent = node.parent;
132 } else if (variableIndex >= 1) { 132 } else if (variableIndex >= 1) {
133 firstOffset = variables[variableIndex - 1].end; 133 firstOffset = variables[variableIndex - 1].end;
134 return new _SourceRegion(firstOffset, endOffset - firstOffset); 134 return new SourceRange(firstOffset, endOffset - firstOffset);
135 } 135 }
136 } 136 }
137 // unit or class member 137 // unit or class member
138 if (parent is CompilationUnit) { 138 if (parent is CompilationUnit) {
139 firstOffset = node.offset; 139 firstOffset = node.offset;
140 siblings = parent.declarations; 140 siblings = parent.declarations;
141 } else if (parent is ClassDeclaration) { 141 } else if (parent is ClassDeclaration) {
142 firstOffset = parent.leftBracket.end; 142 firstOffset = parent.leftBracket.end;
143 siblings = parent.members; 143 siblings = parent.members;
144 } else { 144 } else {
145 int offset = node.offset; 145 int offset = node.offset;
146 return new _SourceRegion(offset, endOffset - offset); 146 return new SourceRange(offset, endOffset - offset);
147 } 147 }
148 // first child: [endOfParent, endOfNode] 148 // first child: [endOfParent, endOfNode]
149 int index = siblings.indexOf(node); 149 int index = siblings.indexOf(node);
150 if (index == 0) { 150 if (index == 0) {
151 return new _SourceRegion(firstOffset, endOffset - firstOffset); 151 return new SourceRange(firstOffset, endOffset - firstOffset);
152 } 152 }
153 // not first child: [endOfPreviousSibling, endOfNode] 153 // not first child: [endOfPreviousSibling, endOfNode]
154 int prevSiblingEnd = siblings[index - 1].end; 154 int prevSiblingEnd = siblings[index - 1].end;
155 return new _SourceRegion(prevSiblingEnd, endOffset - prevSiblingEnd); 155 return new SourceRange(prevSiblingEnd, endOffset - prevSiblingEnd);
156 } 156 }
157 157
158 Outline _newClassOutline(ClassDeclaration node, List<Outline> classContents) { 158 Outline _newClassOutline(ClassDeclaration node, List<Outline> classContents) {
159 SimpleIdentifier nameNode = node.name; 159 SimpleIdentifier nameNode = node.name;
160 String name = nameNode.name; 160 String name = nameNode.name;
161 _SourceRegion sourceRegion = _getSourceRegion(node); 161 SourceRange range = _getSourceRange(node);
162 Element element = new Element( 162 Element element = new Element(
163 ElementKind.CLASS, 163 ElementKind.CLASS,
164 name, 164 name,
165 Element.makeFlags( 165 Element.makeFlags(
166 isPrivate: Identifier.isPrivateName(name), 166 isPrivate: Identifier.isPrivateName(name),
167 isDeprecated: _isDeprecated(node), 167 isDeprecated: _isDeprecated(node),
168 isAbstract: node.isAbstract), 168 isAbstract: node.isAbstract),
169 location: _getLocationNode(nameNode), 169 location: _getLocationNode(nameNode),
170 typeParameters: _getTypeParametersStr(node.typeParameters)); 170 typeParameters: _getTypeParametersStr(node.typeParameters));
171 return new Outline(element, sourceRegion.offset, sourceRegion.length, 171 return new Outline(element, range.offset, range.length,
172 children: nullIfEmpty(classContents)); 172 children: nullIfEmpty(classContents));
173 } 173 }
174 174
175 Outline _newClassTypeAlias(ClassTypeAlias node) { 175 Outline _newClassTypeAlias(ClassTypeAlias node) {
176 SimpleIdentifier nameNode = node.name; 176 SimpleIdentifier nameNode = node.name;
177 String name = nameNode.name; 177 String name = nameNode.name;
178 _SourceRegion sourceRegion = _getSourceRegion(node); 178 SourceRange range = _getSourceRange(node);
179 Element element = new Element( 179 Element element = new Element(
180 ElementKind.CLASS_TYPE_ALIAS, 180 ElementKind.CLASS_TYPE_ALIAS,
181 name, 181 name,
182 Element.makeFlags( 182 Element.makeFlags(
183 isPrivate: Identifier.isPrivateName(name), 183 isPrivate: Identifier.isPrivateName(name),
184 isDeprecated: _isDeprecated(node), 184 isDeprecated: _isDeprecated(node),
185 isAbstract: node.isAbstract), 185 isAbstract: node.isAbstract),
186 location: _getLocationNode(nameNode), 186 location: _getLocationNode(nameNode),
187 typeParameters: _getTypeParametersStr(node.typeParameters)); 187 typeParameters: _getTypeParametersStr(node.typeParameters));
188 return new Outline(element, sourceRegion.offset, sourceRegion.length); 188 return new Outline(element, range.offset, range.length);
189 } 189 }
190 190
191 Outline _newConstructorOutline(ConstructorDeclaration constructor) { 191 Outline _newConstructorOutline(ConstructorDeclaration constructor) {
192 Identifier returnType = constructor.returnType; 192 Identifier returnType = constructor.returnType;
193 String name = returnType.name; 193 String name = returnType.name;
194 int offset = returnType.offset; 194 int offset = returnType.offset;
195 int length = returnType.length; 195 int length = returnType.length;
196 SimpleIdentifier constructorNameNode = constructor.name; 196 SimpleIdentifier constructorNameNode = constructor.name;
197 bool isPrivate = false; 197 bool isPrivate = false;
198 if (constructorNameNode != null) { 198 if (constructorNameNode != null) {
199 String constructorName = constructorNameNode.name; 199 String constructorName = constructorNameNode.name;
200 isPrivate = Identifier.isPrivateName(constructorName); 200 isPrivate = Identifier.isPrivateName(constructorName);
201 name += '.$constructorName'; 201 name += '.$constructorName';
202 offset = constructorNameNode.offset; 202 offset = constructorNameNode.offset;
203 length = constructorNameNode.length; 203 length = constructorNameNode.length;
204 } 204 }
205 _SourceRegion sourceRegion = _getSourceRegion(constructor); 205 SourceRange range = _getSourceRange(constructor);
206 FormalParameterList parameters = constructor.parameters; 206 FormalParameterList parameters = constructor.parameters;
207 String parametersStr = _safeToSource(parameters); 207 String parametersStr = _safeToSource(parameters);
208 Element element = new Element( 208 Element element = new Element(
209 ElementKind.CONSTRUCTOR, 209 ElementKind.CONSTRUCTOR,
210 name, 210 name,
211 Element.makeFlags( 211 Element.makeFlags(
212 isPrivate: isPrivate, isDeprecated: _isDeprecated(constructor)), 212 isPrivate: isPrivate, isDeprecated: _isDeprecated(constructor)),
213 location: _getLocationOffsetLength(offset, length), 213 location: _getLocationOffsetLength(offset, length),
214 parameters: parametersStr); 214 parameters: parametersStr);
215 List<Outline> contents = _addLocalFunctionOutlines(constructor.body); 215 List<Outline> contents = _addLocalFunctionOutlines(constructor.body);
216 Outline outline = new Outline( 216 Outline outline = new Outline(element, range.offset, range.length,
217 element, sourceRegion.offset, sourceRegion.length,
218 children: nullIfEmpty(contents)); 217 children: nullIfEmpty(contents));
219 return outline; 218 return outline;
220 } 219 }
221 220
222 Outline _newEnumConstant(EnumConstantDeclaration node) { 221 Outline _newEnumConstant(EnumConstantDeclaration node) {
223 SimpleIdentifier nameNode = node.name; 222 SimpleIdentifier nameNode = node.name;
224 String name = nameNode.name; 223 String name = nameNode.name;
225 _SourceRegion sourceRegion = _getSourceRegion(node); 224 SourceRange range = _getSourceRange(node);
226 Element element = new Element( 225 Element element = new Element(
227 ElementKind.ENUM_CONSTANT, 226 ElementKind.ENUM_CONSTANT,
228 name, 227 name,
229 Element.makeFlags( 228 Element.makeFlags(
230 isPrivate: Identifier.isPrivateName(name), 229 isPrivate: Identifier.isPrivateName(name),
231 isDeprecated: _isDeprecated(node)), 230 isDeprecated: _isDeprecated(node)),
232 location: _getLocationNode(nameNode)); 231 location: _getLocationNode(nameNode));
233 return new Outline(element, sourceRegion.offset, sourceRegion.length); 232 return new Outline(element, range.offset, range.length);
234 } 233 }
235 234
236 Outline _newEnumOutline(EnumDeclaration node, List<Outline> children) { 235 Outline _newEnumOutline(EnumDeclaration node, List<Outline> children) {
237 SimpleIdentifier nameNode = node.name; 236 SimpleIdentifier nameNode = node.name;
238 String name = nameNode.name; 237 String name = nameNode.name;
239 _SourceRegion sourceRegion = _getSourceRegion(node); 238 SourceRange range = _getSourceRange(node);
240 Element element = new Element( 239 Element element = new Element(
241 ElementKind.ENUM, 240 ElementKind.ENUM,
242 name, 241 name,
243 Element.makeFlags( 242 Element.makeFlags(
244 isPrivate: Identifier.isPrivateName(name), 243 isPrivate: Identifier.isPrivateName(name),
245 isDeprecated: _isDeprecated(node)), 244 isDeprecated: _isDeprecated(node)),
246 location: _getLocationNode(nameNode)); 245 location: _getLocationNode(nameNode));
247 return new Outline(element, sourceRegion.offset, sourceRegion.length, 246 return new Outline(element, range.offset, range.length,
248 children: nullIfEmpty(children)); 247 children: nullIfEmpty(children));
249 } 248 }
250 249
251 Outline _newFunctionOutline(FunctionDeclaration function, bool isStatic) { 250 Outline _newFunctionOutline(FunctionDeclaration function, bool isStatic) {
252 TypeAnnotation returnType = function.returnType; 251 TypeAnnotation returnType = function.returnType;
253 SimpleIdentifier nameNode = function.name; 252 SimpleIdentifier nameNode = function.name;
254 String name = nameNode.name; 253 String name = nameNode.name;
255 FunctionExpression functionExpression = function.functionExpression; 254 FunctionExpression functionExpression = function.functionExpression;
256 FormalParameterList parameters = functionExpression.parameters; 255 FormalParameterList parameters = functionExpression.parameters;
257 ElementKind kind; 256 ElementKind kind;
258 if (function.isGetter) { 257 if (function.isGetter) {
259 kind = ElementKind.GETTER; 258 kind = ElementKind.GETTER;
260 } else if (function.isSetter) { 259 } else if (function.isSetter) {
261 kind = ElementKind.SETTER; 260 kind = ElementKind.SETTER;
262 } else { 261 } else {
263 kind = ElementKind.FUNCTION; 262 kind = ElementKind.FUNCTION;
264 } 263 }
265 _SourceRegion sourceRegion = _getSourceRegion(function); 264 SourceRange range = _getSourceRange(function);
266 String parametersStr = _safeToSource(parameters); 265 String parametersStr = _safeToSource(parameters);
267 String returnTypeStr = _safeToSource(returnType); 266 String returnTypeStr = _safeToSource(returnType);
268 Element element = new Element( 267 Element element = new Element(
269 kind, 268 kind,
270 name, 269 name,
271 Element.makeFlags( 270 Element.makeFlags(
272 isPrivate: Identifier.isPrivateName(name), 271 isPrivate: Identifier.isPrivateName(name),
273 isDeprecated: _isDeprecated(function), 272 isDeprecated: _isDeprecated(function),
274 isStatic: isStatic), 273 isStatic: isStatic),
275 location: _getLocationNode(nameNode), 274 location: _getLocationNode(nameNode),
276 parameters: parametersStr, 275 parameters: parametersStr,
277 returnType: returnTypeStr); 276 returnType: returnTypeStr);
278 List<Outline> contents = _addLocalFunctionOutlines(functionExpression.body); 277 List<Outline> contents = _addLocalFunctionOutlines(functionExpression.body);
279 Outline outline = new Outline( 278 Outline outline = new Outline(element, range.offset, range.length,
280 element, sourceRegion.offset, sourceRegion.length,
281 children: nullIfEmpty(contents)); 279 children: nullIfEmpty(contents));
282 return outline; 280 return outline;
283 } 281 }
284 282
285 Outline _newFunctionTypeAliasOutline(FunctionTypeAlias node) { 283 Outline _newFunctionTypeAliasOutline(FunctionTypeAlias node) {
286 TypeAnnotation returnType = node.returnType; 284 TypeAnnotation returnType = node.returnType;
287 SimpleIdentifier nameNode = node.name; 285 SimpleIdentifier nameNode = node.name;
288 String name = nameNode.name; 286 String name = nameNode.name;
289 _SourceRegion sourceRegion = _getSourceRegion(node); 287 SourceRange range = _getSourceRange(node);
290 FormalParameterList parameters = node.parameters; 288 FormalParameterList parameters = node.parameters;
291 String parametersStr = _safeToSource(parameters); 289 String parametersStr = _safeToSource(parameters);
292 String returnTypeStr = _safeToSource(returnType); 290 String returnTypeStr = _safeToSource(returnType);
293 Element element = new Element( 291 Element element = new Element(
294 ElementKind.FUNCTION_TYPE_ALIAS, 292 ElementKind.FUNCTION_TYPE_ALIAS,
295 name, 293 name,
296 Element.makeFlags( 294 Element.makeFlags(
297 isPrivate: Identifier.isPrivateName(name), 295 isPrivate: Identifier.isPrivateName(name),
298 isDeprecated: _isDeprecated(node)), 296 isDeprecated: _isDeprecated(node)),
299 location: _getLocationNode(nameNode), 297 location: _getLocationNode(nameNode),
300 parameters: parametersStr, 298 parameters: parametersStr,
301 returnType: returnTypeStr, 299 returnType: returnTypeStr,
302 typeParameters: _getTypeParametersStr(node.typeParameters)); 300 typeParameters: _getTypeParametersStr(node.typeParameters));
303 return new Outline(element, sourceRegion.offset, sourceRegion.length); 301 return new Outline(element, range.offset, range.length);
304 } 302 }
305 303
306 Outline _newMethodOutline(MethodDeclaration method) { 304 Outline _newMethodOutline(MethodDeclaration method) {
307 TypeAnnotation returnType = method.returnType; 305 TypeAnnotation returnType = method.returnType;
308 SimpleIdentifier nameNode = method.name; 306 SimpleIdentifier nameNode = method.name;
309 String name = nameNode.name; 307 String name = nameNode.name;
310 FormalParameterList parameters = method.parameters; 308 FormalParameterList parameters = method.parameters;
311 ElementKind kind; 309 ElementKind kind;
312 if (method.isGetter) { 310 if (method.isGetter) {
313 kind = ElementKind.GETTER; 311 kind = ElementKind.GETTER;
314 } else if (method.isSetter) { 312 } else if (method.isSetter) {
315 kind = ElementKind.SETTER; 313 kind = ElementKind.SETTER;
316 } else { 314 } else {
317 kind = ElementKind.METHOD; 315 kind = ElementKind.METHOD;
318 } 316 }
319 _SourceRegion sourceRegion = _getSourceRegion(method); 317 SourceRange range = _getSourceRange(method);
320 String parametersStr = parameters?.toSource(); 318 String parametersStr = parameters?.toSource();
321 String returnTypeStr = _safeToSource(returnType); 319 String returnTypeStr = _safeToSource(returnType);
322 Element element = new Element( 320 Element element = new Element(
323 kind, 321 kind,
324 name, 322 name,
325 Element.makeFlags( 323 Element.makeFlags(
326 isPrivate: Identifier.isPrivateName(name), 324 isPrivate: Identifier.isPrivateName(name),
327 isDeprecated: _isDeprecated(method), 325 isDeprecated: _isDeprecated(method),
328 isAbstract: method.isAbstract, 326 isAbstract: method.isAbstract,
329 isStatic: method.isStatic), 327 isStatic: method.isStatic),
330 location: _getLocationNode(nameNode), 328 location: _getLocationNode(nameNode),
331 parameters: parametersStr, 329 parameters: parametersStr,
332 returnType: returnTypeStr); 330 returnType: returnTypeStr);
333 List<Outline> contents = _addLocalFunctionOutlines(method.body); 331 List<Outline> contents = _addLocalFunctionOutlines(method.body);
334 Outline outline = new Outline( 332 Outline outline = new Outline(element, range.offset, range.length,
335 element, sourceRegion.offset, sourceRegion.length,
336 children: nullIfEmpty(contents)); 333 children: nullIfEmpty(contents));
337 return outline; 334 return outline;
338 } 335 }
339 336
340 Outline _newUnitOutline(List<Outline> unitContents) { 337 Outline _newUnitOutline(List<Outline> unitContents) {
341 Element element = new Element( 338 Element element = new Element(
342 ElementKind.COMPILATION_UNIT, '<unit>', Element.makeFlags(), 339 ElementKind.COMPILATION_UNIT, '<unit>', Element.makeFlags(),
343 location: _getLocationNode(unit)); 340 location: _getLocationNode(unit));
344 return new Outline(element, unit.offset, unit.length, 341 return new Outline(element, unit.offset, unit.length,
345 children: nullIfEmpty(unitContents)); 342 children: nullIfEmpty(unitContents));
346 } 343 }
347 344
348 Outline _newVariableOutline(String typeName, ElementKind kind, 345 Outline _newVariableOutline(String typeName, ElementKind kind,
349 VariableDeclaration variable, bool isStatic) { 346 VariableDeclaration variable, bool isStatic) {
350 SimpleIdentifier nameNode = variable.name; 347 SimpleIdentifier nameNode = variable.name;
351 String name = nameNode.name; 348 String name = nameNode.name;
352 _SourceRegion sourceRegion = _getSourceRegion(variable); 349 SourceRange range = _getSourceRange(variable);
353 Element element = new Element( 350 Element element = new Element(
354 kind, 351 kind,
355 name, 352 name,
356 Element.makeFlags( 353 Element.makeFlags(
357 isPrivate: Identifier.isPrivateName(name), 354 isPrivate: Identifier.isPrivateName(name),
358 isDeprecated: _isDeprecated(variable), 355 isDeprecated: _isDeprecated(variable),
359 isStatic: isStatic, 356 isStatic: isStatic,
360 isConst: variable.isConst, 357 isConst: variable.isConst,
361 isFinal: variable.isFinal), 358 isFinal: variable.isFinal),
362 location: _getLocationNode(nameNode), 359 location: _getLocationNode(nameNode),
363 returnType: typeName); 360 returnType: typeName);
364 Outline outline = 361 Outline outline = new Outline(element, range.offset, range.length);
365 new Outline(element, sourceRegion.offset, sourceRegion.length);
366 return outline; 362 return outline;
367 } 363 }
368 364
369 static String _getTypeParametersStr(TypeParameterList parameters) { 365 static String _getTypeParametersStr(TypeParameterList parameters) {
370 if (parameters == null) { 366 if (parameters == null) {
371 return null; 367 return null;
372 } 368 }
373 return parameters.toSource(); 369 return parameters.toSource();
374 } 370 }
375 371
(...skipping 16 matching lines...) Expand all
392 final DartUnitOutlineComputer outlineComputer; 388 final DartUnitOutlineComputer outlineComputer;
393 final List<Outline> contents; 389 final List<Outline> contents;
394 390
395 _LocalFunctionOutlinesVisitor(this.outlineComputer, this.contents); 391 _LocalFunctionOutlinesVisitor(this.outlineComputer, this.contents);
396 392
397 @override 393 @override
398 visitFunctionDeclaration(FunctionDeclaration node) { 394 visitFunctionDeclaration(FunctionDeclaration node) {
399 contents.add(outlineComputer._newFunctionOutline(node, false)); 395 contents.add(outlineComputer._newFunctionOutline(node, false));
400 } 396 }
401 } 397 }
402
403 /**
404 * A range of characters.
405 */
406 class _SourceRegion {
407 final int length;
408 final int offset;
409 _SourceRegion(this.offset, this.length);
410 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/lib/src/services/completion/dart/completion_manager.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698