OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 6 import 'package:analysis_server/src/protocol_server.dart' |
| 7 hide Element, ElementKind; |
| 8 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; |
| 9 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/file_system/file_system.dart'; |
| 11 import 'package:analyzer_plugin/src/utilities/completion/suggestion_builder.dart
'; |
| 12 |
| 13 /** |
| 14 * Common mixin for sharing behavior. |
| 15 */ |
| 16 abstract class ElementSuggestionBuilder { |
| 17 // Copied from analysis_server/lib/src/services/completion/dart/suggestion_bui
lder.dart |
| 18 /** |
| 19 * A collection of completion suggestions. |
| 20 */ |
| 21 final List<CompletionSuggestion> suggestions = <CompletionSuggestion>[]; |
| 22 |
| 23 /** |
| 24 * A set of existing completions used to prevent duplicate suggestions. |
| 25 */ |
| 26 final Set<String> _completions = new Set<String>(); |
| 27 |
| 28 /** |
| 29 * A map of element names to suggestions for synthetic getters and setters. |
| 30 */ |
| 31 final Map<String, CompletionSuggestion> _syntheticMap = |
| 32 <String, CompletionSuggestion>{}; |
| 33 |
| 34 /** |
| 35 * Return the library in which the completion is requested. |
| 36 */ |
| 37 LibraryElement get containingLibrary; |
| 38 |
| 39 /** |
| 40 * Return the kind of suggestions that should be built. |
| 41 */ |
| 42 CompletionSuggestionKind get kind; |
| 43 |
| 44 /** |
| 45 * Return the resource provider used to access the file system. |
| 46 */ |
| 47 ResourceProvider get resourceProvider; |
| 48 |
| 49 /** |
| 50 * Add a suggestion based upon the given element. |
| 51 */ |
| 52 void addSuggestion(Element element, |
| 53 {String prefix, int relevance: DART_RELEVANCE_DEFAULT}) { |
| 54 if (element.isPrivate) { |
| 55 if (element.library != containingLibrary) { |
| 56 return; |
| 57 } |
| 58 } |
| 59 String completion = element.displayName; |
| 60 if (prefix != null && prefix.length > 0) { |
| 61 if (completion == null || completion.length <= 0) { |
| 62 completion = prefix; |
| 63 } else { |
| 64 completion = '$prefix.$completion'; |
| 65 } |
| 66 } |
| 67 if (completion == null || completion.length <= 0) { |
| 68 return; |
| 69 } |
| 70 SuggestionBuilderImpl builder = new SuggestionBuilderImpl(resourceProvider); |
| 71 CompletionSuggestion suggestion = builder.forElement(element, |
| 72 completion: completion, kind: kind, relevance: relevance); |
| 73 if (suggestion != null) { |
| 74 if (element.isSynthetic && element is PropertyAccessorElement) { |
| 75 String cacheKey; |
| 76 if (element.isGetter) { |
| 77 cacheKey = element.name; |
| 78 } |
| 79 if (element.isSetter) { |
| 80 cacheKey = element.name; |
| 81 cacheKey = cacheKey.substring(0, cacheKey.length - 1); |
| 82 } |
| 83 if (cacheKey != null) { |
| 84 CompletionSuggestion existingSuggestion = _syntheticMap[cacheKey]; |
| 85 |
| 86 // Pair getter/setter by updating the existing suggestion |
| 87 if (existingSuggestion != null) { |
| 88 CompletionSuggestion getter = |
| 89 element.isGetter ? suggestion : existingSuggestion; |
| 90 protocol.ElementKind elemKind = |
| 91 element.enclosingElement is ClassElement |
| 92 ? protocol.ElementKind.FIELD |
| 93 : protocol.ElementKind.TOP_LEVEL_VARIABLE; |
| 94 existingSuggestion.element = new protocol.Element( |
| 95 elemKind, |
| 96 existingSuggestion.element.name, |
| 97 existingSuggestion.element.flags, |
| 98 location: getter.element.location, |
| 99 typeParameters: getter.element.typeParameters, |
| 100 parameters: null, |
| 101 returnType: getter.returnType); |
| 102 return; |
| 103 } |
| 104 |
| 105 // Cache lone getter/setter so that it can be paired |
| 106 _syntheticMap[cacheKey] = suggestion; |
| 107 } |
| 108 } |
| 109 if (_completions.add(suggestion.completion)) { |
| 110 suggestions.add(suggestion); |
| 111 } |
| 112 } |
| 113 } |
| 114 } |
OLD | NEW |