|
20 | 20 | #include "swift/Basic/OptionSet.h"
|
21 | 21 | #include "swift/Basic/StringExtras.h"
|
22 | 22 | #include "swift/Frontend/Frontend.h"
|
| 23 | +#include "swift/IDE/CodeCompletionString.h" |
23 | 24 | #include "llvm/ADT/ArrayRef.h"
|
24 | 25 | #include "llvm/ADT/StringMap.h"
|
25 | 26 | #include "llvm/ADT/StringRef.h"
|
@@ -73,347 +74,6 @@ ArrayRef<T> copyArray(llvm::BumpPtrAllocator &Allocator,
|
73 | 74 | return llvm::makeArrayRef(Buffer, Arr.size());
|
74 | 75 | }
|
75 | 76 |
|
76 |
| -namespace detail { |
77 |
| -class CodeCompletionStringChunk { |
78 |
| - friend class swift::ide::CodeCompletionResultBuilder; |
79 |
| - |
80 |
| -public: |
81 |
| - enum class ChunkKind { |
82 |
| - /// "open", "public", "internal", "fileprivate", or "private". |
83 |
| - AccessControlKeyword, |
84 |
| - |
85 |
| - /// such as @"available". |
86 |
| - DeclAttrKeyword, |
87 |
| - |
88 |
| - /// such as "unavailable" etc. for @available. |
89 |
| - DeclAttrParamKeyword, |
90 |
| - |
91 |
| - /// The "override" keyword. |
92 |
| - OverrideKeyword, |
93 |
| - |
94 |
| - /// The "throws", "rethrows" and "async" keyword. |
95 |
| - EffectsSpecifierKeyword, |
96 |
| - |
97 |
| - /// The keyword part of a declaration before the name, like "func". |
98 |
| - DeclIntroducer, |
99 |
| - |
100 |
| - /// Other generic keyword. |
101 |
| - Keyword, |
102 |
| - |
103 |
| - /// Other generic attributes. |
104 |
| - Attribute, |
105 |
| - |
106 |
| - /// Normal text chunk. |
107 |
| - Text, |
108 |
| - |
109 |
| - /// Base name of the result. |
110 |
| - BaseName, |
111 |
| - |
112 |
| - /// The first chunk of an optional substring that continues until |
113 |
| - /// \c NestingLevel decreases. |
114 |
| - OptionalBegin, |
115 |
| - |
116 |
| - /// Punctuation. |
117 |
| - LeftParen, |
118 |
| - RightParen, |
119 |
| - LeftBracket, |
120 |
| - RightBracket, |
121 |
| - LeftAngle, |
122 |
| - RightAngle, |
123 |
| - Dot, |
124 |
| - Ellipsis, |
125 |
| - Comma, |
126 |
| - ExclamationMark, |
127 |
| - QuestionMark, |
128 |
| - Ampersand, |
129 |
| - Equal, |
130 |
| - Whitespace, |
131 |
| - |
132 |
| - /// The first chunk of a whole generic parameter clause. |
133 |
| - /// E.g '<T, C: Collection>' |
134 |
| - GenericParameterClauseBegin, |
135 |
| - |
136 |
| - /// The first chunk of a generic quirement clause. |
137 |
| - /// E.g. 'where T: Collection, T.Element == Int' |
138 |
| - GenericRequirementClauseBegin, |
139 |
| - |
140 |
| - /// The first chunk of a substring that describes the parameter for a |
141 |
| - /// generic type. |
142 |
| - GenericParameterBegin, |
143 |
| - /// Generic type parameter name. |
144 |
| - GenericParameterName, |
145 |
| - |
146 |
| - /// The first chunk of a substring that describes the argument for a |
147 |
| - /// function call. |
148 |
| - CallArgumentBegin, |
149 |
| - |
150 |
| - /// Function call argument label. |
151 |
| - CallArgumentName, |
152 |
| - |
153 |
| - /// Function parameter internal / local name for an call argument. If the |
154 |
| - /// parameter has no formal API name, it can still have a local name which |
155 |
| - /// can be useful for display purposes. |
156 |
| - /// |
157 |
| - /// This chunk should not be inserted into the editor buffer. |
158 |
| - CallArgumentInternalName, |
159 |
| - |
160 |
| - /// A colon between argument name and value. Should be inserted in the |
161 |
| - /// editor buffer if the preceding CallArgumentName was inserted. |
162 |
| - CallArgumentColon, |
163 |
| - |
164 |
| - /// A colon between parameter name and value. Used in decl attribute. |
165 |
| - DeclAttrParamColon, |
166 |
| - |
167 |
| - /// Required argument type. |
168 |
| - CallArgumentType, |
169 |
| - |
170 |
| - /// Argument type tag for annotated results. |
171 |
| - CallArgumentTypeBegin, |
172 |
| - |
173 |
| - /// Added if the argument has a default value. |
174 |
| - CallArgumentDefaultBegin, |
175 |
| - |
176 |
| - /// System type name. |
177 |
| - TypeIdSystem, |
178 |
| - |
179 |
| - /// Non-system type name. |
180 |
| - TypeIdUser, |
181 |
| - |
182 |
| - /// Desugared closure argument type. This can be used to get the |
183 |
| - /// closure type if CallArgumentType is a TypeAliasType. |
184 |
| - CallArgumentClosureType, |
185 |
| - |
186 |
| - /// An expanded closure expression for the value of an argument, including |
187 |
| - /// the left and right braces and possible signature. The preferred |
188 |
| - /// position to put the cursor after the completion result is inserted |
189 |
| - /// into the editor buffer is between the braces. |
190 |
| - CallArgumentClosureExpr, |
191 |
| - |
192 |
| - /// A placeholder for \c ! or \c ? in a call to a method found by dynamic |
193 |
| - /// lookup. |
194 |
| - /// |
195 |
| - /// The default spelling is \c !, but clients may render it as \c ? if |
196 |
| - /// desired. |
197 |
| - DynamicLookupMethodCallTail, |
198 |
| - |
199 |
| - /// A placeholder for \c ! or \c ? in a call to an optional method. |
200 |
| - /// |
201 |
| - /// The default spelling is \c !, but clients may render it as \c ? if |
202 |
| - /// desired. |
203 |
| - OptionalMethodCallTail, |
204 |
| - |
205 |
| - /// The first chunk of a substring that describes the single parameter |
206 |
| - /// declaration for a parameter clause. |
207 |
| - ParameterDeclBegin, |
208 |
| - |
209 |
| - ParameterDeclExternalName, |
210 |
| - |
211 |
| - ParameterDeclLocalName, |
212 |
| - |
213 |
| - ParameterDeclColon, |
214 |
| - |
215 |
| - ParameterDeclTypeBegin, |
216 |
| - |
217 |
| - /// Default argument clause for parameter declarations. |
218 |
| - DefaultArgumentClauseBegin, |
219 |
| - |
220 |
| - /// First chunk for effect specifiers. i.e. 'async' and 'throws'. |
221 |
| - EffectsSpecifierClauseBegin, |
222 |
| - |
223 |
| - /// First chunk for result type clause i.e. ' -> ResultTy' or ': ResultTy'. |
224 |
| - DeclResultTypeClauseBegin, |
225 |
| - |
226 |
| - /// First chunk for attribute and modifier list i.e. 'override public' |
227 |
| - AttributeAndModifierListBegin, |
228 |
| - |
229 |
| - /// Specifies the type of the whole entity that is returned in this code |
230 |
| - /// completion result. For example, for variable references it is the |
231 |
| - /// variable type, for function calls it is the return type. |
232 |
| - /// |
233 |
| - /// This chunk should not be inserted into the editor buffer. |
234 |
| - TypeAnnotation, |
235 |
| - |
236 |
| - /// Structured group version of 'TypeAnnotation'. |
237 |
| - /// This grouped chunks should not be inserted into the editor buffer. |
238 |
| - TypeAnnotationBegin, |
239 |
| - |
240 |
| - /// A brace statement -- left brace and right brace. The preferred |
241 |
| - /// position to put the cursor after the completion result is inserted |
242 |
| - /// into the editor buffer is between the braces. |
243 |
| - /// |
244 |
| - /// The spelling is always "{}", but clients may choose to insert newline |
245 |
| - /// and indentation in between. |
246 |
| - BraceStmtWithCursor, |
247 |
| - }; |
248 |
| - |
249 |
| - static bool chunkStartsNestedGroup(ChunkKind Kind) { |
250 |
| - return Kind == ChunkKind::CallArgumentBegin || |
251 |
| - Kind == ChunkKind::GenericParameterBegin || |
252 |
| - Kind == ChunkKind::OptionalBegin || |
253 |
| - Kind == ChunkKind::CallArgumentTypeBegin || |
254 |
| - Kind == ChunkKind::CallArgumentDefaultBegin || |
255 |
| - Kind == ChunkKind::TypeAnnotationBegin || |
256 |
| - Kind == ChunkKind::ParameterDeclBegin || |
257 |
| - Kind == ChunkKind::ParameterDeclTypeBegin || |
258 |
| - Kind == ChunkKind::DefaultArgumentClauseBegin || |
259 |
| - Kind == ChunkKind::GenericParameterClauseBegin || |
260 |
| - Kind == ChunkKind::EffectsSpecifierClauseBegin || |
261 |
| - Kind == ChunkKind::DeclResultTypeClauseBegin || |
262 |
| - Kind == ChunkKind::GenericRequirementClauseBegin || |
263 |
| - Kind == ChunkKind::AttributeAndModifierListBegin; |
264 |
| - } |
265 |
| - |
266 |
| - static bool chunkHasText(ChunkKind Kind) { |
267 |
| - return Kind == ChunkKind::AccessControlKeyword || |
268 |
| - Kind == ChunkKind::OverrideKeyword || |
269 |
| - Kind == ChunkKind::EffectsSpecifierKeyword || |
270 |
| - Kind == ChunkKind::DeclAttrKeyword || |
271 |
| - Kind == ChunkKind::DeclIntroducer || |
272 |
| - Kind == ChunkKind::Keyword || |
273 |
| - Kind == ChunkKind::Attribute || |
274 |
| - Kind == ChunkKind::BaseName || |
275 |
| - Kind == ChunkKind::Text || |
276 |
| - Kind == ChunkKind::LeftParen || |
277 |
| - Kind == ChunkKind::RightParen || |
278 |
| - Kind == ChunkKind::LeftBracket || |
279 |
| - Kind == ChunkKind::RightBracket || |
280 |
| - Kind == ChunkKind::LeftAngle || |
281 |
| - Kind == ChunkKind::RightAngle || |
282 |
| - Kind == ChunkKind::Dot || |
283 |
| - Kind == ChunkKind::Ellipsis || |
284 |
| - Kind == ChunkKind::Comma || |
285 |
| - Kind == ChunkKind::ExclamationMark || |
286 |
| - Kind == ChunkKind::QuestionMark || |
287 |
| - Kind == ChunkKind::Ampersand || |
288 |
| - Kind == ChunkKind::Equal || |
289 |
| - Kind == ChunkKind::Whitespace || |
290 |
| - Kind == ChunkKind::CallArgumentName || |
291 |
| - Kind == ChunkKind::CallArgumentInternalName || |
292 |
| - Kind == ChunkKind::CallArgumentColon || |
293 |
| - Kind == ChunkKind::CallArgumentType || |
294 |
| - Kind == ChunkKind::CallArgumentClosureType || |
295 |
| - Kind == ChunkKind::CallArgumentClosureExpr || |
296 |
| - Kind == ChunkKind::ParameterDeclExternalName || |
297 |
| - Kind == ChunkKind::ParameterDeclLocalName || |
298 |
| - Kind == ChunkKind::ParameterDeclColon || |
299 |
| - Kind == ChunkKind::DeclAttrParamColon || |
300 |
| - Kind == ChunkKind::DeclAttrParamKeyword || |
301 |
| - Kind == ChunkKind::GenericParameterName || |
302 |
| - Kind == ChunkKind::DynamicLookupMethodCallTail || |
303 |
| - Kind == ChunkKind::OptionalMethodCallTail || |
304 |
| - Kind == ChunkKind::TypeAnnotation || |
305 |
| - Kind == ChunkKind::BraceStmtWithCursor || |
306 |
| - Kind == ChunkKind::TypeIdSystem || |
307 |
| - Kind == ChunkKind::TypeIdUser; |
308 |
| - } |
309 |
| - |
310 |
| -private: |
311 |
| - unsigned Kind : 8; |
312 |
| - unsigned NestingLevel : 8; |
313 |
| - |
314 |
| - /// If true, then this chunk is an annotation that is included only |
315 |
| - /// for exposition and may not be inserted in the editor buffer. |
316 |
| - unsigned IsAnnotation : 1; |
317 |
| - |
318 |
| - StringRef Text; |
319 |
| - |
320 |
| - CodeCompletionStringChunk(ChunkKind Kind, unsigned NestingLevel, StringRef Text, |
321 |
| - bool isAnnotation) |
322 |
| - : Kind(unsigned(Kind)), NestingLevel(NestingLevel), |
323 |
| - IsAnnotation(isAnnotation), Text(Text) { |
324 |
| - assert(chunkHasText(Kind)); |
325 |
| - } |
326 |
| - |
327 |
| - CodeCompletionStringChunk(ChunkKind Kind, unsigned NestingLevel, |
328 |
| - bool isAnnotation) |
329 |
| - : Kind(unsigned(Kind)), NestingLevel(NestingLevel), |
330 |
| - IsAnnotation(isAnnotation) { |
331 |
| - assert(!chunkHasText(Kind)); |
332 |
| - } |
333 |
| - |
334 |
| - void setIsAnnotation() { |
335 |
| - IsAnnotation = 1; |
336 |
| - } |
337 |
| - |
338 |
| -public: |
339 |
| - ChunkKind getKind() const { |
340 |
| - return ChunkKind(Kind); |
341 |
| - } |
342 |
| - |
343 |
| - bool is(ChunkKind K) const { return getKind() == K; } |
344 |
| - |
345 |
| - unsigned getNestingLevel() const { |
346 |
| - return NestingLevel; |
347 |
| - } |
348 |
| - |
349 |
| - bool isAnnotation() const { |
350 |
| - return IsAnnotation; |
351 |
| - } |
352 |
| - |
353 |
| - bool hasText() const { return chunkHasText(getKind()); } |
354 |
| - |
355 |
| - StringRef getText() const { |
356 |
| - assert(hasText()); |
357 |
| - return Text; |
358 |
| - } |
359 |
| - |
360 |
| - bool endsPreviousNestedGroup(unsigned GroupNestingLevel) const { |
361 |
| - return NestingLevel < GroupNestingLevel || |
362 |
| - (NestingLevel == GroupNestingLevel && chunkStartsNestedGroup(getKind())); |
363 |
| - } |
364 |
| - |
365 |
| - static CodeCompletionStringChunk createWithText(ChunkKind Kind, |
366 |
| - unsigned NestingLevel, |
367 |
| - StringRef Text, |
368 |
| - bool isAnnotation = false) { |
369 |
| - return CodeCompletionStringChunk(Kind, NestingLevel, Text, isAnnotation); |
370 |
| - } |
371 |
| - |
372 |
| - static CodeCompletionStringChunk createSimple(ChunkKind Kind, |
373 |
| - unsigned NestingLevel, |
374 |
| - bool isAnnotation = false) { |
375 |
| - return CodeCompletionStringChunk(Kind, NestingLevel, isAnnotation); |
376 |
| - } |
377 |
| -}; |
378 |
| - |
379 |
| -} // end namespace detail |
380 |
| - |
381 |
| -/// A structured representation of a code completion string. |
382 |
| -class alignas(detail::CodeCompletionStringChunk) CodeCompletionString final : |
383 |
| - private llvm::TrailingObjects<CodeCompletionString, |
384 |
| - detail::CodeCompletionStringChunk> { |
385 |
| - friend class CodeCompletionResultBuilder; |
386 |
| - friend TrailingObjects; |
387 |
| - |
388 |
| -public: |
389 |
| - using Chunk = detail::CodeCompletionStringChunk; |
390 |
| - |
391 |
| -private: |
392 |
| - unsigned NumChunks : 16; |
393 |
| - |
394 |
| - CodeCompletionString(ArrayRef<Chunk> Chunks); |
395 |
| - |
396 |
| -public: |
397 |
| - /// Creates a \c CodeCompletionString from a list of \c Chunks. |
398 |
| - /// |
399 |
| - /// \note The caller must ensure any text inside \c Chunks will outlive this |
400 |
| - /// object, typically by storing them inside a \c CodeCompletionResultSink. |
401 |
| - static CodeCompletionString *create(llvm::BumpPtrAllocator &Allocator, |
402 |
| - ArrayRef<Chunk> Chunks); |
403 |
| - |
404 |
| - ArrayRef<Chunk> getChunks() const { |
405 |
| - return {getTrailingObjects<Chunk>(), NumChunks}; |
406 |
| - } |
407 |
| - |
408 |
| - StringRef getFirstTextChunk(bool includeLeadingPunctuation = false) const; |
409 |
| - Optional<unsigned> |
410 |
| - getFirstTextChunkIndex(bool includeLeadingPunctuation = false) const; |
411 |
| - |
412 |
| - /// Print a debug representation of the code completion string to \p OS. |
413 |
| - void print(raw_ostream &OS) const; |
414 |
| - SWIFT_DEBUG_DUMP; |
415 |
| -}; |
416 |
| - |
417 | 77 | /// Describes the origin of the code completion result.
|
418 | 78 | ///
|
419 | 79 | /// This enum is ordered from the contexts that are "nearest" to the code
|
|
0 commit comments