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; | |
6 | |
7 import 'package:analysis_services/constants.dart'; | |
8 import 'package:analysis_services/json.dart'; | |
9 import 'package:analyzer/src/generated/element.dart'; | |
10 | |
11 /** | |
12 * An enumeration of the relevance of a completion suggestion. | |
13 */ | |
14 class CompletionRelevance { | |
15 static const CompletionRelevance LOW = const CompletionRelevance('LOW'); | |
16 static const CompletionRelevance DEFAULT = | |
17 const CompletionRelevance('DEFAULT'); | |
18 static const CompletionRelevance HIGH = const CompletionRelevance('HIGH'); | |
19 | |
20 final String name; | |
21 | |
22 const CompletionRelevance(this.name); | |
23 | |
24 static CompletionRelevance value(String name) { | |
25 if (LOW.name == name) return LOW; | |
26 if (DEFAULT.name == name) return DEFAULT; | |
27 if (HIGH.name == name) return HIGH; | |
28 throw new ArgumentError('Unknown CompletionRelevance: $name'); | |
29 } | |
30 | |
31 toString() => 'CompletionRelevance.$name'; | |
32 } | |
33 | |
34 /** | |
35 * A single completion suggestion. | |
36 */ | |
37 class CompletionSuggestion implements HasToJson { | |
38 | |
39 /** | |
40 * The kind of element being suggested. | |
41 */ | |
42 final CompletionSuggestionKind kind; | |
43 | |
44 /** | |
45 * The relevance of this completion suggestion. | |
46 */ | |
47 final CompletionRelevance relevance; | |
48 | |
49 /** | |
50 * The identifier to be inserted if the suggestion is selected. | |
51 * If the suggestion is for a method or function, the client might want to | |
52 * additionally insert a template for the parameters. | |
53 * The information required in order to do so is contained in other fields. | |
54 */ | |
55 final String completion; | |
56 | |
57 /** | |
58 * The offset, relative to the beginning of the completion, of where | |
59 * the selection should be placed after insertion. | |
60 */ | |
61 final int selectionOffset; | |
62 | |
63 /** | |
64 * The number of characters that should be selected after insertion. | |
65 */ | |
66 final int selectionLength; | |
67 | |
68 /** | |
69 * `true` if the suggested element is deprecated. | |
70 */ | |
71 final bool isDeprecated; | |
72 | |
73 /** | |
74 * True if the element is not known to be valid for the target. | |
75 * This happens if the type of the target is dynamic. | |
76 */ | |
77 final bool isPotential; | |
78 | |
79 // optional fields | |
80 | |
81 // final String docSummary; | |
82 // final String docComplete; | |
83 // final String declaringType; | |
84 // final String returnType; | |
85 // final List<String> parameterNames; | |
86 // final List<String> parameterTypes; | |
87 // final int requiredParameterCount; | |
88 // final int positionalParameterCount; | |
89 // final String parameterName; | |
90 // final String parameterType; | |
91 | |
92 CompletionSuggestion(this.kind, this.relevance, this.completion, | |
93 this.selectionOffset, this.selectionLength, this.isDeprecated, | |
94 this.isPotential); | |
95 | |
96 factory CompletionSuggestion.fromJson(Map<String, Object> json) { | |
97 return new CompletionSuggestion( | |
98 CompletionSuggestionKind.valueOf(json[KIND]), | |
99 CompletionRelevance.value(json[RELEVANCE]), | |
100 json[COMPLETION], | |
101 json[SELECTION_OFFSET], | |
102 json[SELECTION_LENGTH], | |
103 json[IS_DEPRECATED], | |
104 json[IS_POTENTIAL]); | |
105 } | |
106 | |
107 @override | |
108 Map<String, Object> toJson() { | |
109 return { | |
110 KIND: kind.name, | |
111 RELEVANCE: relevance.name, | |
112 COMPLETION: completion, | |
113 SELECTION_OFFSET: selectionOffset, | |
114 SELECTION_LENGTH: selectionLength, | |
115 IS_DEPRECATED: isDeprecated, | |
116 IS_POTENTIAL: isPotential | |
117 }; | |
118 } | |
119 } | |
120 | |
121 /** | |
122 * An enumeration of the kinds of elements that can be included | |
123 * in a completion suggestion. | |
124 */ | |
125 class CompletionSuggestionKind { | |
126 static const CompletionSuggestionKind ARGUMENT_LIST = | |
127 const CompletionSuggestionKind('ARGUMENT_LIST'); | |
128 static const CompletionSuggestionKind CLASS = | |
129 const CompletionSuggestionKind('CLASS'); | |
130 static const CompletionSuggestionKind CLASS_ALIAS = | |
131 const CompletionSuggestionKind('CLASS_ALIAS'); | |
132 static const CompletionSuggestionKind CONSTRUCTOR = | |
133 const CompletionSuggestionKind('CONSTRUCTOR'); | |
134 static const CompletionSuggestionKind FIELD = | |
135 const CompletionSuggestionKind('FIELD'); | |
136 static const CompletionSuggestionKind FUNCTION = | |
137 const CompletionSuggestionKind('FUNCTION'); | |
138 static const CompletionSuggestionKind FUNCTION_TYPE_ALIAS = | |
139 const CompletionSuggestionKind('FUNCTION_TYPE_ALIAS'); | |
140 static const CompletionSuggestionKind GETTER = | |
141 const CompletionSuggestionKind('GETTER'); | |
142 static const CompletionSuggestionKind IMPORT = | |
143 const CompletionSuggestionKind('IMPORT'); | |
144 static const CompletionSuggestionKind KEYWORD = | |
145 const CompletionSuggestionKind('KEYWORD'); | |
146 static const CompletionSuggestionKind LIBRARY_PREFIX = | |
147 const CompletionSuggestionKind('LIBRARY_PREFIX'); | |
148 static const CompletionSuggestionKind LOCAL_VARIABLE = | |
149 const CompletionSuggestionKind('LOCAL_VARIABLE'); | |
150 static const CompletionSuggestionKind METHOD = | |
151 const CompletionSuggestionKind('METHOD'); | |
152 static const CompletionSuggestionKind METHOD_NAME = | |
153 const CompletionSuggestionKind('METHOD_NAME'); | |
154 static const CompletionSuggestionKind NAMED_ARGUMENT = | |
155 const CompletionSuggestionKind('NAMED_ARGUMENT'); | |
156 static const CompletionSuggestionKind OPTIONAL_ARGUMENT = | |
157 const CompletionSuggestionKind('OPTIONAL_ARGUMENT'); | |
158 static const CompletionSuggestionKind PARAMETER = | |
159 const CompletionSuggestionKind('PARAMETER'); | |
160 static const CompletionSuggestionKind SETTER = | |
161 const CompletionSuggestionKind('SETTER'); | |
162 static const CompletionSuggestionKind TOP_LEVEL_VARIABLE = | |
163 const CompletionSuggestionKind('TOP_LEVEL_VARIABLE'); | |
164 static const CompletionSuggestionKind TYPE_PARAMETER = | |
165 const CompletionSuggestionKind('TYPE_PARAMETER'); | |
166 | |
167 final String name; | |
168 | |
169 const CompletionSuggestionKind(this.name); | |
170 | |
171 @override | |
172 String toString() => name; | |
173 | |
174 static CompletionSuggestionKind fromElementKind(ElementKind kind) { | |
175 // ElementKind.ANGULAR_FORMATTER, | |
176 // ElementKind.ANGULAR_COMPONENT, | |
177 // ElementKind.ANGULAR_CONTROLLER, | |
178 // ElementKind.ANGULAR_DIRECTIVE, | |
179 // ElementKind.ANGULAR_PROPERTY, | |
180 // ElementKind.ANGULAR_SCOPE_PROPERTY, | |
181 // ElementKind.ANGULAR_SELECTOR, | |
182 // ElementKind.ANGULAR_VIEW, | |
183 if (kind == ElementKind.CLASS) return CLASS; | |
184 // ElementKind.COMPILATION_UNIT, | |
185 if (kind == ElementKind.CONSTRUCTOR) return CONSTRUCTOR; | |
186 // ElementKind.DYNAMIC, | |
187 // ElementKind.EMBEDDED_HTML_SCRIPT, | |
188 // ElementKind.ERROR, | |
189 // ElementKind.EXPORT, | |
190 // ElementKind.EXTERNAL_HTML_SCRIPT, | |
191 if (kind == ElementKind.FIELD) return FIELD; | |
192 if (kind == ElementKind.FUNCTION) return FUNCTION; | |
193 if (kind == ElementKind.FUNCTION_TYPE_ALIAS) return FUNCTION_TYPE_ALIAS; | |
194 if (kind == ElementKind.GETTER) return GETTER; | |
195 // ElementKind.HTML, | |
196 if (kind == ElementKind.IMPORT) return IMPORT; | |
197 // ElementKind.LABEL, | |
198 // ElementKind.LIBRARY, | |
199 if (kind == ElementKind.LOCAL_VARIABLE) return LOCAL_VARIABLE; | |
200 if (kind == ElementKind.METHOD) return METHOD; | |
201 // ElementKind.NAME, | |
202 if (kind == ElementKind.PARAMETER) return PARAMETER; | |
203 // ElementKind.POLYMER_ATTRIBUTE, | |
204 // ElementKind.POLYMER_TAG_DART, | |
205 // ElementKind.POLYMER_TAG_HTML, | |
206 // ElementKind.PREFIX, | |
207 if (kind == ElementKind.SETTER) return SETTER; | |
208 if (kind == ElementKind.TOP_LEVEL_VARIABLE) return TOP_LEVEL_VARIABLE; | |
209 // ElementKind.TYPE_PARAMETER, | |
210 // ElementKind.UNIVERSE | |
211 throw new ArgumentError('Unknown CompletionSuggestionKind for: $kind'); | |
212 } | |
213 | |
214 static CompletionSuggestionKind valueOf(String name) { | |
215 if (ARGUMENT_LIST.name == name) return ARGUMENT_LIST; | |
216 if (CLASS.name == name) return CLASS; | |
217 if (CLASS_ALIAS.name == name) return CLASS_ALIAS; | |
218 if (CONSTRUCTOR.name == name) return CONSTRUCTOR; | |
219 if (FIELD.name == name) return FIELD; | |
220 if (FUNCTION.name == name) return FUNCTION; | |
221 if (FUNCTION_TYPE_ALIAS.name == name) return FUNCTION_TYPE_ALIAS; | |
222 if (GETTER.name == name) return GETTER; | |
223 if (IMPORT.name == name) return IMPORT; | |
224 if (KEYWORD.name == name) return KEYWORD; | |
225 if (LIBRARY_PREFIX.name == name) return LIBRARY_PREFIX; | |
226 if (LOCAL_VARIABLE.name == name) return LOCAL_VARIABLE; | |
227 if (METHOD.name == name) return METHOD; | |
228 if (METHOD_NAME.name == name) return METHOD_NAME; | |
229 if (NAMED_ARGUMENT.name == name) return NAMED_ARGUMENT; | |
230 if (OPTIONAL_ARGUMENT.name == name) return OPTIONAL_ARGUMENT; | |
231 if (PARAMETER.name == name) return PARAMETER; | |
232 if (SETTER.name == name) return SETTER; | |
233 if (TOP_LEVEL_VARIABLE.name == name) return TOP_LEVEL_VARIABLE; | |
234 if (TYPE_PARAMETER.name == name) return TYPE_PARAMETER; | |
235 throw new ArgumentError('Unknown CompletionSuggestionKind: $name'); | |
236 } | |
237 } | |
OLD | NEW |