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

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

Issue 1941793002: Use null aware operators to clean up code (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 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
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 library computer.outline; 5 library computer.outline;
6 6
7 import 'package:analysis_server/plugin/protocol/protocol.dart'; 7 import 'package:analysis_server/plugin/protocol/protocol.dart';
8 import 'package:analysis_server/src/collections.dart'; 8 import 'package:analysis_server/src/collections.dart';
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';
(...skipping 23 matching lines...) Expand all
34 for (ClassMember classMember in classDeclaration.members) { 34 for (ClassMember classMember in classDeclaration.members) {
35 if (classMember is ConstructorDeclaration) { 35 if (classMember is ConstructorDeclaration) {
36 ConstructorDeclaration constructorDeclaration = classMember; 36 ConstructorDeclaration constructorDeclaration = classMember;
37 classContents.add(_newConstructorOutline(constructorDeclaration)); 37 classContents.add(_newConstructorOutline(constructorDeclaration));
38 } 38 }
39 if (classMember is FieldDeclaration) { 39 if (classMember is FieldDeclaration) {
40 FieldDeclaration fieldDeclaration = classMember; 40 FieldDeclaration fieldDeclaration = classMember;
41 VariableDeclarationList fields = fieldDeclaration.fields; 41 VariableDeclarationList fields = fieldDeclaration.fields;
42 if (fields != null) { 42 if (fields != null) {
43 TypeName fieldType = fields.type; 43 TypeName fieldType = fields.type;
44 String fieldTypeName = 44 String fieldTypeName = _safeToSource(fieldType);
45 fieldType != null ? fieldType.toSource() : '';
46 for (VariableDeclaration field in fields.variables) { 45 for (VariableDeclaration field in fields.variables) {
47 classContents.add(_newVariableOutline(fieldTypeName, 46 classContents.add(_newVariableOutline(fieldTypeName,
48 ElementKind.FIELD, field, fieldDeclaration.isStatic)); 47 ElementKind.FIELD, field, fieldDeclaration.isStatic));
49 } 48 }
50 } 49 }
51 } 50 }
52 if (classMember is MethodDeclaration) { 51 if (classMember is MethodDeclaration) {
53 MethodDeclaration methodDeclaration = classMember; 52 MethodDeclaration methodDeclaration = classMember;
54 classContents.add(_newMethodOutline(methodDeclaration)); 53 classContents.add(_newMethodOutline(methodDeclaration));
55 } 54 }
56 } 55 }
57 unitContents.add(_newClassOutline(classDeclaration, classContents)); 56 unitContents.add(_newClassOutline(classDeclaration, classContents));
58 } 57 }
59 if (unitMember is EnumDeclaration) { 58 if (unitMember is EnumDeclaration) {
60 EnumDeclaration enumDeclaration = unitMember; 59 EnumDeclaration enumDeclaration = unitMember;
61 List<Outline> constantOutlines = <Outline>[]; 60 List<Outline> constantOutlines = <Outline>[];
62 for (EnumConstantDeclaration constant in enumDeclaration.constants) { 61 for (EnumConstantDeclaration constant in enumDeclaration.constants) {
63 constantOutlines.add(_newEnumConstant(constant)); 62 constantOutlines.add(_newEnumConstant(constant));
64 } 63 }
65 unitContents.add(_newEnumOutline(enumDeclaration, constantOutlines)); 64 unitContents.add(_newEnumOutline(enumDeclaration, constantOutlines));
66 } 65 }
67 if (unitMember is TopLevelVariableDeclaration) { 66 if (unitMember is TopLevelVariableDeclaration) {
68 TopLevelVariableDeclaration fieldDeclaration = unitMember; 67 TopLevelVariableDeclaration fieldDeclaration = unitMember;
69 VariableDeclarationList fields = fieldDeclaration.variables; 68 VariableDeclarationList fields = fieldDeclaration.variables;
70 if (fields != null) { 69 if (fields != null) {
71 TypeName fieldType = fields.type; 70 TypeName fieldType = fields.type;
72 String fieldTypeName = fieldType != null ? fieldType.toSource() : ''; 71 String fieldTypeName = _safeToSource(fieldType);
73 for (VariableDeclaration field in fields.variables) { 72 for (VariableDeclaration field in fields.variables) {
74 unitContents.add(_newVariableOutline( 73 unitContents.add(_newVariableOutline(
75 fieldTypeName, ElementKind.TOP_LEVEL_VARIABLE, field, false)); 74 fieldTypeName, ElementKind.TOP_LEVEL_VARIABLE, field, false));
76 } 75 }
77 } 76 }
78 } 77 }
79 if (unitMember is FunctionDeclaration) { 78 if (unitMember is FunctionDeclaration) {
80 FunctionDeclaration functionDeclaration = unitMember; 79 FunctionDeclaration functionDeclaration = unitMember;
81 unitContents.add(_newFunctionOutline(functionDeclaration, true)); 80 unitContents.add(_newFunctionOutline(functionDeclaration, true));
82 } 81 }
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 bool isPrivate = false; 199 bool isPrivate = false;
201 if (constructorNameNode != null) { 200 if (constructorNameNode != null) {
202 String constructorName = constructorNameNode.name; 201 String constructorName = constructorNameNode.name;
203 isPrivate = Identifier.isPrivateName(constructorName); 202 isPrivate = Identifier.isPrivateName(constructorName);
204 name += '.$constructorName'; 203 name += '.$constructorName';
205 offset = constructorNameNode.offset; 204 offset = constructorNameNode.offset;
206 length = constructorNameNode.length; 205 length = constructorNameNode.length;
207 } 206 }
208 _SourceRegion sourceRegion = _getSourceRegion(constructor); 207 _SourceRegion sourceRegion = _getSourceRegion(constructor);
209 FormalParameterList parameters = constructor.parameters; 208 FormalParameterList parameters = constructor.parameters;
210 String parametersStr = parameters != null ? parameters.toSource() : ''; 209 String parametersStr = _safeToSource(parameters);
211 Element element = new Element( 210 Element element = new Element(
212 ElementKind.CONSTRUCTOR, 211 ElementKind.CONSTRUCTOR,
213 name, 212 name,
214 Element.makeFlags( 213 Element.makeFlags(
215 isPrivate: isPrivate, isDeprecated: _isDeprecated(constructor)), 214 isPrivate: isPrivate, isDeprecated: _isDeprecated(constructor)),
216 location: _getLocationOffsetLength(offset, length), 215 location: _getLocationOffsetLength(offset, length),
217 parameters: parametersStr); 216 parameters: parametersStr);
218 List<Outline> contents = _addLocalFunctionOutlines(constructor.body); 217 List<Outline> contents = _addLocalFunctionOutlines(constructor.body);
219 Outline outline = new Outline( 218 Outline outline = new Outline(
220 element, sourceRegion.offset, sourceRegion.length, 219 element, sourceRegion.offset, sourceRegion.length,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 FormalParameterList parameters = functionExpression.parameters; 258 FormalParameterList parameters = functionExpression.parameters;
260 ElementKind kind; 259 ElementKind kind;
261 if (function.isGetter) { 260 if (function.isGetter) {
262 kind = ElementKind.GETTER; 261 kind = ElementKind.GETTER;
263 } else if (function.isSetter) { 262 } else if (function.isSetter) {
264 kind = ElementKind.SETTER; 263 kind = ElementKind.SETTER;
265 } else { 264 } else {
266 kind = ElementKind.FUNCTION; 265 kind = ElementKind.FUNCTION;
267 } 266 }
268 _SourceRegion sourceRegion = _getSourceRegion(function); 267 _SourceRegion sourceRegion = _getSourceRegion(function);
269 String parametersStr = parameters != null ? parameters.toSource() : ''; 268 String parametersStr = _safeToSource(parameters);
270 String returnTypeStr = returnType != null ? returnType.toSource() : ''; 269 String returnTypeStr = _safeToSource(returnType);
271 Element element = new Element( 270 Element element = new Element(
272 kind, 271 kind,
273 name, 272 name,
274 Element.makeFlags( 273 Element.makeFlags(
275 isPrivate: Identifier.isPrivateName(name), 274 isPrivate: Identifier.isPrivateName(name),
276 isDeprecated: _isDeprecated(function), 275 isDeprecated: _isDeprecated(function),
277 isStatic: isStatic), 276 isStatic: isStatic),
278 location: _getLocationNode(nameNode), 277 location: _getLocationNode(nameNode),
279 parameters: parametersStr, 278 parameters: parametersStr,
280 returnType: returnTypeStr); 279 returnType: returnTypeStr);
281 List<Outline> contents = _addLocalFunctionOutlines(functionExpression.body); 280 List<Outline> contents = _addLocalFunctionOutlines(functionExpression.body);
282 Outline outline = new Outline( 281 Outline outline = new Outline(
283 element, sourceRegion.offset, sourceRegion.length, 282 element, sourceRegion.offset, sourceRegion.length,
284 children: nullIfEmpty(contents)); 283 children: nullIfEmpty(contents));
285 return outline; 284 return outline;
286 } 285 }
287 286
288 Outline _newFunctionTypeAliasOutline(FunctionTypeAlias node) { 287 Outline _newFunctionTypeAliasOutline(FunctionTypeAlias node) {
289 TypeName returnType = node.returnType; 288 TypeName returnType = node.returnType;
290 SimpleIdentifier nameNode = node.name; 289 SimpleIdentifier nameNode = node.name;
291 String name = nameNode.name; 290 String name = nameNode.name;
292 _SourceRegion sourceRegion = _getSourceRegion(node); 291 _SourceRegion sourceRegion = _getSourceRegion(node);
293 FormalParameterList parameters = node.parameters; 292 FormalParameterList parameters = node.parameters;
294 String parametersStr = parameters != null ? parameters.toSource() : ''; 293 String parametersStr = _safeToSource(parameters);
295 String returnTypeStr = returnType != null ? returnType.toSource() : ''; 294 String returnTypeStr = _safeToSource(returnType);
296 Element element = new Element( 295 Element element = new Element(
297 ElementKind.FUNCTION_TYPE_ALIAS, 296 ElementKind.FUNCTION_TYPE_ALIAS,
298 name, 297 name,
299 Element.makeFlags( 298 Element.makeFlags(
300 isPrivate: Identifier.isPrivateName(name), 299 isPrivate: Identifier.isPrivateName(name),
301 isDeprecated: _isDeprecated(node)), 300 isDeprecated: _isDeprecated(node)),
302 location: _getLocationNode(nameNode), 301 location: _getLocationNode(nameNode),
303 parameters: parametersStr, 302 parameters: parametersStr,
304 returnType: returnTypeStr, 303 returnType: returnTypeStr,
305 typeParameters: _getTypeParametersStr(node.typeParameters)); 304 typeParameters: _getTypeParametersStr(node.typeParameters));
306 return new Outline(element, sourceRegion.offset, sourceRegion.length); 305 return new Outline(element, sourceRegion.offset, sourceRegion.length);
307 } 306 }
308 307
309 Outline _newMethodOutline(MethodDeclaration method) { 308 Outline _newMethodOutline(MethodDeclaration method) {
310 TypeName returnType = method.returnType; 309 TypeName returnType = method.returnType;
311 SimpleIdentifier nameNode = method.name; 310 SimpleIdentifier nameNode = method.name;
312 String name = nameNode.name; 311 String name = nameNode.name;
313 FormalParameterList parameters = method.parameters; 312 FormalParameterList parameters = method.parameters;
314 ElementKind kind; 313 ElementKind kind;
315 if (method.isGetter) { 314 if (method.isGetter) {
316 kind = ElementKind.GETTER; 315 kind = ElementKind.GETTER;
317 } else if (method.isSetter) { 316 } else if (method.isSetter) {
318 kind = ElementKind.SETTER; 317 kind = ElementKind.SETTER;
319 } else { 318 } else {
320 kind = ElementKind.METHOD; 319 kind = ElementKind.METHOD;
321 } 320 }
322 _SourceRegion sourceRegion = _getSourceRegion(method); 321 _SourceRegion sourceRegion = _getSourceRegion(method);
323 String parametersStr = parameters != null ? parameters.toSource() : null; 322 String parametersStr = parameters?.toSource();
324 String returnTypeStr = returnType != null ? returnType.toSource() : ''; 323 String returnTypeStr = _safeToSource(returnType);
325 Element element = new Element( 324 Element element = new Element(
326 kind, 325 kind,
327 name, 326 name,
328 Element.makeFlags( 327 Element.makeFlags(
329 isPrivate: Identifier.isPrivateName(name), 328 isPrivate: Identifier.isPrivateName(name),
330 isDeprecated: _isDeprecated(method), 329 isDeprecated: _isDeprecated(method),
331 isAbstract: method.isAbstract, 330 isAbstract: method.isAbstract,
332 isStatic: method.isStatic), 331 isStatic: method.isStatic),
333 location: _getLocationNode(nameNode), 332 location: _getLocationNode(nameNode),
334 parameters: parametersStr, 333 parameters: parametersStr,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 return parameters.toSource(); 375 return parameters.toSource();
377 } 376 }
378 377
379 /** 378 /**
380 * Returns `true` if the given [element] is not `null` and deprecated. 379 * Returns `true` if the given [element] is not `null` and deprecated.
381 */ 380 */
382 static bool _isDeprecated(Declaration declaration) { 381 static bool _isDeprecated(Declaration declaration) {
383 engine.Element element = declaration.element; 382 engine.Element element = declaration.element;
384 return element != null && element.isDeprecated; 383 return element != null && element.isDeprecated;
385 } 384 }
385
386 static String _safeToSource(AstNode node) =>
387 node == null ? '' : node.toSource();
scheglov 2016/05/02 15:22:43 node?.toSource() ?? '' Not that it is much better
Brian Wilkerson 2016/05/02 15:28:09 Is that better? Looks to me like this would incur
scheglov 2016/05/02 15:29:57 You're right, it isn't better.
386 } 388 }
387 389
388 /** 390 /**
389 * A visitor for building local function outlines. 391 * A visitor for building local function outlines.
390 */ 392 */
391 class _LocalFunctionOutlinesVisitor extends RecursiveAstVisitor { 393 class _LocalFunctionOutlinesVisitor extends RecursiveAstVisitor {
392 final DartUnitOutlineComputer outlineComputer; 394 final DartUnitOutlineComputer outlineComputer;
393 final List<Outline> contents; 395 final List<Outline> contents;
394 396
395 _LocalFunctionOutlinesVisitor(this.outlineComputer, this.contents); 397 _LocalFunctionOutlinesVisitor(this.outlineComputer, this.contents);
396 398
397 @override 399 @override
398 visitFunctionDeclaration(FunctionDeclaration node) { 400 visitFunctionDeclaration(FunctionDeclaration node) {
399 contents.add(outlineComputer._newFunctionOutline(node, false)); 401 contents.add(outlineComputer._newFunctionOutline(node, false));
400 } 402 }
401 } 403 }
402 404
403 /** 405 /**
404 * A range of characters. 406 * A range of characters.
405 */ 407 */
406 class _SourceRegion { 408 class _SourceRegion {
407 final int length; 409 final int length;
408 final int offset; 410 final int offset;
409 _SourceRegion(this.offset, this.length); 411 _SourceRegion(this.offset, this.length);
410 } 412 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698