| 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 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'package:analysis_server/src/protocol_server.dart' as protocol; | 10 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 }); | 158 }); |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 | 161 |
| 162 /** | 162 /** |
| 163 * Common mixin for sharing behavior | 163 * Common mixin for sharing behavior |
| 164 */ | 164 */ |
| 165 abstract class ElementSuggestionBuilder { | 165 abstract class ElementSuggestionBuilder { |
| 166 | 166 |
| 167 /** | 167 /** |
| 168 * Internal collection of completions to prevent duplicate completions. | |
| 169 */ | |
| 170 final Set<String> _completions = new Set<String>(); | |
| 171 | |
| 172 /** | |
| 173 * Return the kind of suggestions that should be built. | 168 * Return the kind of suggestions that should be built. |
| 174 */ | 169 */ |
| 175 CompletionSuggestionKind get kind; | 170 CompletionSuggestionKind get kind; |
| 176 | 171 |
| 177 /** | 172 /** |
| 178 * Return the request on which the builder is operating. | 173 * Return the request on which the builder is operating. |
| 179 */ | 174 */ |
| 180 DartCompletionRequest get request; | 175 DartCompletionRequest get request; |
| 181 | 176 |
| 182 /** | 177 /** |
| 183 * Add a suggestion based upon the given element. | 178 * Add a suggestion based upon the given element. |
| 184 */ | 179 */ |
| 185 void addSuggestion(Element element, {int relevance: DART_RELEVANCE_DEFAULT}) { | 180 void addSuggestion(Element element, {int relevance: DART_RELEVANCE_DEFAULT}) { |
| 186 if (element.isPrivate) { | 181 if (element.isPrivate) { |
| 187 LibraryElement elementLibrary = element.library; | 182 LibraryElement elementLibrary = element.library; |
| 188 LibraryElement unitLibrary = request.unit.element.library; | 183 LibraryElement unitLibrary = request.unit.element.library; |
| 189 if (elementLibrary != unitLibrary) { | 184 if (elementLibrary != unitLibrary) { |
| 190 return; | 185 return; |
| 191 } | 186 } |
| 192 } | 187 } |
| 193 if (element.isSynthetic) { | 188 if (element.isSynthetic) { |
| 194 if (element is PropertyAccessorElement || element is FieldElement) { | 189 if (element is PropertyAccessorElement || element is FieldElement) { |
| 195 return; | 190 return; |
| 196 } | 191 } |
| 197 } | 192 } |
| 198 String completion = element.displayName; | 193 String completion = element.displayName; |
| 199 if (completion == null || | 194 if (completion == null || completion.length <= 0) { |
| 200 completion.length <= 0 || | |
| 201 !_completions.add(completion)) { | |
| 202 return; | 195 return; |
| 203 } | 196 } |
| 204 CompletionSuggestion suggestion = | 197 CompletionSuggestion suggestion = |
| 205 createSuggestion(element, kind: kind, relevance: relevance); | 198 createSuggestion(element, kind: kind, relevance: relevance); |
| 206 if (suggestion != null) { | 199 if (suggestion != null) { |
| 207 request.suggestions.add(suggestion); | 200 request.addSuggestion(suggestion); |
| 208 } | 201 } |
| 209 } | 202 } |
| 210 } | 203 } |
| 211 | 204 |
| 212 /** | 205 /** |
| 213 * This class provides suggestions based upon the visible instance members in | 206 * This class provides suggestions based upon the visible instance members in |
| 214 * an interface type. Clients should call | 207 * an interface type. Clients should call |
| 215 * [InterfaceTypeSuggestionBuilder.suggestionsFor]. | 208 * [InterfaceTypeSuggestionBuilder.suggestionsFor]. |
| 216 */ | 209 */ |
| 217 class InterfaceTypeSuggestionBuilder { | 210 class InterfaceTypeSuggestionBuilder { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 } | 293 } |
| 301 _completionTypesGenerated[identifier] = | 294 _completionTypesGenerated[identifier] = |
| 302 _COMPLETION_TYPE_FIELD_OR_METHOD_OR_GETSET; | 295 _COMPLETION_TYPE_FIELD_OR_METHOD_OR_GETSET; |
| 303 } else { | 296 } else { |
| 304 // Unexpected element type; skip it. | 297 // Unexpected element type; skip it. |
| 305 assert(false); | 298 assert(false); |
| 306 return; | 299 return; |
| 307 } | 300 } |
| 308 CompletionSuggestion suggestion = createSuggestion(element, kind: kind); | 301 CompletionSuggestion suggestion = createSuggestion(element, kind: kind); |
| 309 if (suggestion != null) { | 302 if (suggestion != null) { |
| 310 request.suggestions.add(suggestion); | 303 request.addSuggestion(suggestion); |
| 311 } | 304 } |
| 312 } | 305 } |
| 313 | 306 |
| 314 void _buildSuggestions(InterfaceType type, LibraryElement library) { | 307 void _buildSuggestions(InterfaceType type, LibraryElement library) { |
| 315 // Visit all of the types in the class hierarchy, collecting possible | 308 // Visit all of the types in the class hierarchy, collecting possible |
| 316 // completions. If multiple elements are found that complete to the same | 309 // completions. If multiple elements are found that complete to the same |
| 317 // identifier, addSuggestion will discard all but the first (with a few | 310 // identifier, addSuggestion will discard all but the first (with a few |
| 318 // exceptions to handle getter/setter pairs). | 311 // exceptions to handle getter/setter pairs). |
| 319 for (InterfaceType targetType in _getTypeOrdering(type)) { | 312 for (InterfaceType targetType in _getTypeOrdering(type)) { |
| 320 for (MethodElement method in targetType.methods) { | 313 for (MethodElement method in targetType.methods) { |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 * or `false` if [computeFull] should be called. | 568 * or `false` if [computeFull] should be called. |
| 576 */ | 569 */ |
| 577 bool computeFast(AstNode node); | 570 bool computeFast(AstNode node); |
| 578 | 571 |
| 579 /** | 572 /** |
| 580 * Return a future that computes the suggestions given a fully resolved AST. | 573 * Return a future that computes the suggestions given a fully resolved AST. |
| 581 * The future returns `true` if suggestions were added, else `false`. | 574 * The future returns `true` if suggestions were added, else `false`. |
| 582 */ | 575 */ |
| 583 Future<bool> computeFull(AstNode node); | 576 Future<bool> computeFull(AstNode node); |
| 584 } | 577 } |
| OLD | NEW |