| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.dart.suggestion.builder; | 5 library services.completion.dart.suggestion.builder; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol_server.dart' as protocol; | 7 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 8 import 'package:analysis_server/src/protocol_server.dart' | 8 import 'package:analysis_server/src/protocol_server.dart' |
| 9 hide Element, ElementKind; | 9 hide Element, ElementKind; |
| 10 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 * A collection of completion suggestions. | 105 * A collection of completion suggestions. |
| 106 */ | 106 */ |
| 107 final List<CompletionSuggestion> suggestions = <CompletionSuggestion>[]; | 107 final List<CompletionSuggestion> suggestions = <CompletionSuggestion>[]; |
| 108 | 108 |
| 109 /** | 109 /** |
| 110 * Return the kind of suggestions that should be built. | 110 * Return the kind of suggestions that should be built. |
| 111 */ | 111 */ |
| 112 CompletionSuggestionKind get kind; | 112 CompletionSuggestionKind get kind; |
| 113 | 113 |
| 114 /** | 114 /** |
| 115 * Return the request on which the builder is operating. | 115 * Return the library in which the completion is requested. |
| 116 */ | 116 */ |
| 117 DartCompletionRequest get request; | 117 LibraryElement get containingLibrary; |
| 118 | 118 |
| 119 /** | 119 /** |
| 120 * Add a suggestion based upon the given element. | 120 * Add a suggestion based upon the given element. |
| 121 */ | 121 */ |
| 122 void addSuggestion(Element element, | 122 void addSuggestion(Element element, |
| 123 {String prefix, int relevance: DART_RELEVANCE_DEFAULT}) { | 123 {String prefix, int relevance: DART_RELEVANCE_DEFAULT}) { |
| 124 if (element.isPrivate) { | 124 if (element.isPrivate) { |
| 125 LibraryElement elementLibrary = element.library; | 125 if (element.library != containingLibrary) { |
| 126 CompilationUnitElement unitElem = request.target.unit.element; | |
| 127 if (unitElem == null) { | |
| 128 return; | |
| 129 } | |
| 130 LibraryElement unitLibrary = unitElem.library; | |
| 131 if (elementLibrary != unitLibrary) { | |
| 132 return; | 126 return; |
| 133 } | 127 } |
| 134 } | 128 } |
| 135 if (prefix == null && element.isSynthetic) { | 129 if (prefix == null && element.isSynthetic) { |
| 136 if ((element is PropertyAccessorElement) || | 130 if ((element is PropertyAccessorElement) || |
| 137 element is FieldElement && !_isSpecialEnumField(element)) { | 131 element is FieldElement && !_isSpecialEnumField(element)) { |
| 138 return; | 132 return; |
| 139 } | 133 } |
| 140 } | 134 } |
| 141 String completion = element.displayName; | 135 String completion = element.displayName; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 166 if (element.name == 'values') { | 160 if (element.name == 'values') { |
| 167 return true; | 161 return true; |
| 168 } | 162 } |
| 169 } | 163 } |
| 170 return false; | 164 return false; |
| 171 } | 165 } |
| 172 } | 166 } |
| 173 | 167 |
| 174 /** | 168 /** |
| 175 * This class visits elements in a library and provides suggestions based upon | 169 * This class visits elements in a library and provides suggestions based upon |
| 176 * the visible members in that library. Clients should call | 170 * the visible members in that library. |
| 177 * [LibraryElementSuggestionBuilder.suggestionsFor]. | |
| 178 */ | 171 */ |
| 179 class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor | 172 class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor |
| 180 with ElementSuggestionBuilder { | 173 with ElementSuggestionBuilder { |
| 181 final DartCompletionRequest request; | 174 final LibraryElement containingLibrary; |
| 182 final CompletionSuggestionKind kind; | 175 final CompletionSuggestionKind kind; |
| 183 final bool typesOnly; | 176 final bool typesOnly; |
| 184 final bool instCreation; | 177 final bool instCreation; |
| 185 | 178 |
| 186 LibraryElementSuggestionBuilder( | 179 LibraryElementSuggestionBuilder( |
| 187 this.request, this.kind, this.typesOnly, this.instCreation); | 180 this.containingLibrary, this.kind, this.typesOnly, this.instCreation); |
| 188 | 181 |
| 189 @override | 182 @override |
| 190 visitClassElement(ClassElement element) { | 183 visitClassElement(ClassElement element) { |
| 191 if (instCreation) { | 184 if (instCreation) { |
| 192 element.visitChildren(this); | 185 element.visitChildren(this); |
| 193 } else { | 186 } else { |
| 194 addSuggestion(element); | 187 addSuggestion(element); |
| 195 } | 188 } |
| 196 } | 189 } |
| 197 | 190 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 addSuggestion(element); | 230 addSuggestion(element); |
| 238 } | 231 } |
| 239 } | 232 } |
| 240 | 233 |
| 241 @override | 234 @override |
| 242 visitTopLevelVariableElement(TopLevelVariableElement element) { | 235 visitTopLevelVariableElement(TopLevelVariableElement element) { |
| 243 if (!typesOnly) { | 236 if (!typesOnly) { |
| 244 addSuggestion(element); | 237 addSuggestion(element); |
| 245 } | 238 } |
| 246 } | 239 } |
| 247 | |
| 248 /** | |
| 249 * Add suggestions for the visible members in the given library | |
| 250 */ | |
| 251 static void suggestionsFor( | |
| 252 DartCompletionRequest request, | |
| 253 CompletionSuggestionKind kind, | |
| 254 LibraryElement library, | |
| 255 bool typesOnly, | |
| 256 bool instCreation) { | |
| 257 if (library != null) { | |
| 258 library.visitChildren(new LibraryElementSuggestionBuilder( | |
| 259 request, kind, typesOnly, instCreation)); | |
| 260 } | |
| 261 } | |
| 262 } | 240 } |
| OLD | NEW |