| OLD | NEW |
| 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 services.completion.suggestion.builder; | 5 library services.completion.suggestion.builder; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol_server.dart' as protocol; | 9 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 10 import 'package:analysis_server/src/protocol_server.dart' hide Element, | 10 import 'package:analysis_server/src/protocol_server.dart' hide Element, |
| 11 ElementKind; | 11 ElementKind; |
| 12 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 12 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 13 import 'package:analyzer/src/generated/ast.dart'; | 13 import 'package:analyzer/src/generated/ast.dart'; |
| 14 import 'package:analyzer/src/generated/element.dart'; | 14 import 'package:analyzer/src/generated/element.dart'; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Create a suggestion based upon the given imported element. |
| 18 */ |
| 19 CompletionSuggestion createElementSuggestion(Element element, |
| 20 {CompletionRelevance relevance: CompletionRelevance.DEFAULT}) { |
| 21 String completion = element.displayName; |
| 22 CompletionSuggestion suggestion = new CompletionSuggestion( |
| 23 CompletionSuggestionKind.INVOCATION, |
| 24 element.isDeprecated ? CompletionRelevance.LOW : relevance, |
| 25 completion, |
| 26 completion.length, |
| 27 0, |
| 28 element.isDeprecated, |
| 29 false); |
| 30 |
| 31 suggestion.element = newElement_fromEngine(element); |
| 32 |
| 33 DartType type; |
| 34 if (element is FunctionElement) { |
| 35 type = element.returnType; |
| 36 } else if (element is PropertyAccessorElement && element.isGetter) { |
| 37 type = element.returnType; |
| 38 } else if (element is TopLevelVariableElement) { |
| 39 type = element.type; |
| 40 } |
| 41 if (type != null) { |
| 42 String name = type.displayName; |
| 43 if (name != null && name.length > 0 && name != 'dynamic') { |
| 44 suggestion.returnType = name; |
| 45 } |
| 46 } |
| 47 return suggestion; |
| 48 } |
| 49 |
| 50 /** |
| 17 * Call the given function with each non-null non-empty inherited type name | 51 * Call the given function with each non-null non-empty inherited type name |
| 18 * that is defined in the given class. | 52 * that is defined in the given class. |
| 19 */ | 53 */ |
| 20 visitInheritedTypeNames(ClassDeclaration node, void inherited(String name)) { | 54 visitInheritedTypeNames(ClassDeclaration node, void inherited(String name)) { |
| 21 | 55 |
| 22 void visit(TypeName type) { | 56 void visit(TypeName type) { |
| 23 if (type != null) { | 57 if (type != null) { |
| 24 Identifier id = type.name; | 58 Identifier id = type.name; |
| 25 if (id != null) { | 59 if (id != null) { |
| 26 String name = id.name; | 60 String name = id.name; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 49 NodeList<TypeName> mixinTypes = withClause.mixinTypes; | 83 NodeList<TypeName> mixinTypes = withClause.mixinTypes; |
| 50 if (mixinTypes != null) { | 84 if (mixinTypes != null) { |
| 51 mixinTypes.forEach((TypeName type) { | 85 mixinTypes.forEach((TypeName type) { |
| 52 visit(type); | 86 visit(type); |
| 53 }); | 87 }); |
| 54 } | 88 } |
| 55 } | 89 } |
| 56 } | 90 } |
| 57 | 91 |
| 58 /** | 92 /** |
| 59 * Call the given functions with each non-null non-empty inherited class | 93 * Starting with the given class node, traverse the inheritence hierarchy |
| 60 * declaration, if the class is defined locally, or type name if it is not | 94 * calling the given functions with each non-null non-empty inherited class |
| 61 * defined locally. | 95 * declaration. For each locally defined class declaration, call [local]. |
| 96 * For each class identifier in the hierarchy that is not defined locally, |
| 97 * call the [imported] function. |
| 62 */ | 98 */ |
| 63 void visitInheritedTypes(ClassDeclaration node, void | 99 void visitInheritedTypes(ClassDeclaration node, void |
| 64 local(ClassDeclaration classNode), void imported(String typeName)) { | 100 local(ClassDeclaration classNode), void imported(String typeName)) { |
| 65 CompilationUnit unit = node.getAncestor((p) => p is CompilationUnit); | 101 CompilationUnit unit = node.getAncestor((p) => p is CompilationUnit); |
| 66 List<ClassDeclaration> todo = new List<ClassDeclaration>(); | 102 List<ClassDeclaration> todo = new List<ClassDeclaration>(); |
| 67 todo.add(node); | 103 todo.add(node); |
| 68 Set<String> visited = new Set<String>(); | 104 Set<String> visited = new Set<String>(); |
| 69 while (todo.length > 0) { | 105 while (todo.length > 0) { |
| 70 node = todo.removeLast(); | 106 node = todo.removeLast(); |
| 71 visitInheritedTypeNames(node, (String name) { | 107 visitInheritedTypeNames(node, (String name) { |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 } | 376 } |
| 341 if (type != null) { | 377 if (type != null) { |
| 342 String typeName = type.displayName; | 378 String typeName = type.displayName; |
| 343 if (typeName != null && typeName.length > 0 && typeName != 'dynamic') { | 379 if (typeName != null && typeName.length > 0 && typeName != 'dynamic') { |
| 344 suggestion.returnType = typeName; | 380 suggestion.returnType = typeName; |
| 345 } | 381 } |
| 346 } | 382 } |
| 347 request.suggestions.add(suggestion); | 383 request.suggestions.add(suggestion); |
| 348 } | 384 } |
| 349 } | 385 } |
| OLD | NEW |