| OLD | NEW |
| (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 services.completion.suggestion.builder; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:analysis_server/src/protocol_server.dart' | |
| 10 hide Element, ElementKind; | |
| 11 import 'package:analysis_server/src/services/completion/dart/suggestion_builder.
dart' | |
| 12 show createSuggestion; | |
| 13 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | |
| 14 import 'package:analyzer/dart/element/element.dart'; | |
| 15 import 'package:analyzer/src/generated/ast.dart'; | |
| 16 | |
| 17 export 'package:analysis_server/src/services/completion/dart/suggestion_builder.
dart' | |
| 18 show createSuggestion; | |
| 19 | |
| 20 const String DYNAMIC = 'dynamic'; | |
| 21 | |
| 22 /** | |
| 23 * Call the given function with each non-null non-empty inherited type name | |
| 24 * that is defined in the given class. | |
| 25 */ | |
| 26 visitInheritedTypeNames(ClassDeclaration node, void inherited(String name)) { | |
| 27 void visit(TypeName type) { | |
| 28 if (type != null) { | |
| 29 Identifier id = type.name; | |
| 30 if (id != null) { | |
| 31 String name = id.name; | |
| 32 if (name != null && name.length > 0) { | |
| 33 inherited(name); | |
| 34 } | |
| 35 } | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 ExtendsClause extendsClause = node.extendsClause; | |
| 40 if (extendsClause != null) { | |
| 41 visit(extendsClause.superclass); | |
| 42 } | |
| 43 ImplementsClause implementsClause = node.implementsClause; | |
| 44 if (implementsClause != null) { | |
| 45 NodeList<TypeName> interfaces = implementsClause.interfaces; | |
| 46 if (interfaces != null) { | |
| 47 interfaces.forEach((TypeName type) { | |
| 48 visit(type); | |
| 49 }); | |
| 50 } | |
| 51 } | |
| 52 WithClause withClause = node.withClause; | |
| 53 if (withClause != null) { | |
| 54 NodeList<TypeName> mixinTypes = withClause.mixinTypes; | |
| 55 if (mixinTypes != null) { | |
| 56 mixinTypes.forEach((TypeName type) { | |
| 57 visit(type); | |
| 58 }); | |
| 59 } | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * Starting with the given class node, traverse the inheritance hierarchy | |
| 65 * calling the given functions with each non-null non-empty inherited class | |
| 66 * declaration. For each locally defined declaration, call [localDeclaration]. | |
| 67 * For each class identifier in the hierarchy that is not defined locally, | |
| 68 * call the [importedTypeName] function. | |
| 69 */ | |
| 70 void visitInheritedTypes(ClassDeclaration node, | |
| 71 {void localDeclaration(ClassDeclaration classNode), | |
| 72 void importedTypeName(String typeName)}) { | |
| 73 CompilationUnit unit = node.getAncestor((p) => p is CompilationUnit); | |
| 74 List<ClassDeclaration> todo = new List<ClassDeclaration>(); | |
| 75 todo.add(node); | |
| 76 Set<String> visited = new Set<String>(); | |
| 77 while (todo.length > 0) { | |
| 78 node = todo.removeLast(); | |
| 79 visitInheritedTypeNames(node, (String name) { | |
| 80 if (visited.add(name)) { | |
| 81 var classNode = unit.declarations.firstWhere((member) { | |
| 82 if (member is ClassDeclaration) { | |
| 83 SimpleIdentifier id = member.name; | |
| 84 if (id != null && id.name == name) { | |
| 85 return true; | |
| 86 } | |
| 87 } | |
| 88 return false; | |
| 89 }, orElse: () => null); | |
| 90 if (classNode is ClassDeclaration) { | |
| 91 if (localDeclaration != null) { | |
| 92 localDeclaration(classNode); | |
| 93 } | |
| 94 todo.add(classNode); | |
| 95 } else { | |
| 96 if (importedTypeName != null) { | |
| 97 importedTypeName(name); | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 }); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Common mixin for sharing behavior | |
| 107 */ | |
| 108 abstract class ElementSuggestionBuilder { | |
| 109 /** | |
| 110 * Return the kind of suggestions that should be built. | |
| 111 */ | |
| 112 CompletionSuggestionKind get kind; | |
| 113 | |
| 114 /** | |
| 115 * Return the request on which the builder is operating. | |
| 116 */ | |
| 117 DartCompletionRequest get request; | |
| 118 | |
| 119 /** | |
| 120 * Add a suggestion based upon the given element. | |
| 121 */ | |
| 122 void addSuggestion(Element element, | |
| 123 {String prefix, int relevance: DART_RELEVANCE_DEFAULT}) { | |
| 124 if (element.isPrivate) { | |
| 125 LibraryElement elementLibrary = element.library; | |
| 126 CompilationUnitElement unitElem = request.unit.element; | |
| 127 if (unitElem == null) { | |
| 128 return; | |
| 129 } | |
| 130 LibraryElement unitLibrary = unitElem.library; | |
| 131 if (elementLibrary != unitLibrary) { | |
| 132 return; | |
| 133 } | |
| 134 } | |
| 135 if (prefix == null && element.isSynthetic) { | |
| 136 if ((element is PropertyAccessorElement) || | |
| 137 element is FieldElement && !_isSpecialEnumField(element)) { | |
| 138 return; | |
| 139 } | |
| 140 } | |
| 141 String completion = element.displayName; | |
| 142 if (prefix != null && prefix.length > 0) { | |
| 143 if (completion == null || completion.length <= 0) { | |
| 144 completion = prefix; | |
| 145 } else { | |
| 146 completion = '$prefix.$completion'; | |
| 147 } | |
| 148 } | |
| 149 if (completion == null || completion.length <= 0) { | |
| 150 return; | |
| 151 } | |
| 152 CompletionSuggestion suggestion = createSuggestion(element, | |
| 153 completion: completion, kind: kind, relevance: relevance); | |
| 154 if (suggestion != null) { | |
| 155 request.addSuggestion(suggestion); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 /** | |
| 160 * Determine if the given element is one of the synthetic enum accessors | |
| 161 * for which we should generate a suggestion. | |
| 162 */ | |
| 163 bool _isSpecialEnumField(FieldElement element) { | |
| 164 Element parent = element.enclosingElement; | |
| 165 if (parent is ClassElement && parent.isEnum) { | |
| 166 if (element.name == 'values') { | |
| 167 return true; | |
| 168 } | |
| 169 } | |
| 170 return false; | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 /** | |
| 175 * Common interface implemented by suggestion builders. | |
| 176 */ | |
| 177 abstract class SuggestionBuilder { | |
| 178 /** | |
| 179 * Compute suggestions and return `true` if building is complete, | |
| 180 * or `false` if [computeFull] should be called. | |
| 181 */ | |
| 182 bool computeFast(AstNode node); | |
| 183 | |
| 184 /** | |
| 185 * Return a future that computes the suggestions given a fully resolved AST. | |
| 186 * The future returns `true` if suggestions were added, else `false`. | |
| 187 */ | |
| 188 Future<bool> computeFull(AstNode node); | |
| 189 } | |
| OLD | NEW |