|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 |
| -#include "swift/Basic/LLVM.h" |
14 | 13 | #include "swift/IDE/CodeCompletionResultPrinter.h"
|
| 14 | +#include "swift/AST/ASTPrinter.h" |
| 15 | +#include "swift/Basic/LLVM.h" |
15 | 16 | #include "swift/IDE/CodeCompletion.h"
|
16 | 17 | #include "swift/Markup/XMLUtils.h"
|
17 | 18 | #include "llvm/Support/raw_ostream.h"
|
@@ -255,3 +256,186 @@ void swift::ide::printCodeCompletionResultTypeNameAnnotated(const CodeCompletion
|
255 | 256 | AnnotatingResultPrinter printer(OS);
|
256 | 257 | printer.printTypeName(Result);
|
257 | 258 | }
|
| 259 | + |
| 260 | +/// Provide the text for the call parameter, including constructing a typed |
| 261 | +/// editor placeholder for it. |
| 262 | +static void |
| 263 | +constructTextForCallParam(ArrayRef<CodeCompletionString::Chunk> ParamGroup, |
| 264 | + raw_ostream &OS) { |
| 265 | + assert(ParamGroup.front().is(ChunkKind::CallParameterBegin)); |
| 266 | + |
| 267 | + for (; !ParamGroup.empty(); ParamGroup = ParamGroup.slice(1)) { |
| 268 | + auto &C = ParamGroup.front(); |
| 269 | + if (C.isAnnotation()) |
| 270 | + continue; |
| 271 | + if (C.is(ChunkKind::CallParameterInternalName) || |
| 272 | + C.is(ChunkKind::CallParameterType) || |
| 273 | + C.is(ChunkKind::CallParameterTypeBegin) || |
| 274 | + C.is(ChunkKind::CallParameterClosureExpr)) { |
| 275 | + break; |
| 276 | + } |
| 277 | + if (!C.hasText()) |
| 278 | + continue; |
| 279 | + OS << C.getText(); |
| 280 | + } |
| 281 | + |
| 282 | + SmallString<32> DisplayString; |
| 283 | + SmallString<32> TypeString; |
| 284 | + SmallString<32> ExpansionTypeString; |
| 285 | + |
| 286 | + for (auto i = ParamGroup.begin(), e = ParamGroup.end(); i != e; ++i) { |
| 287 | + auto &C = *i; |
| 288 | + if (C.is(ChunkKind::CallParameterTypeBegin)) { |
| 289 | + assert(TypeString.empty()); |
| 290 | + auto nestingLevel = C.getNestingLevel(); |
| 291 | + ++i; |
| 292 | + for (; i != e; ++i) { |
| 293 | + if (i->endsPreviousNestedGroup(nestingLevel)) |
| 294 | + break; |
| 295 | + if (!i->isAnnotation() && i->hasText()) { |
| 296 | + TypeString += i->getText(); |
| 297 | + DisplayString += i->getText(); |
| 298 | + } |
| 299 | + } |
| 300 | + --i; |
| 301 | + continue; |
| 302 | + } |
| 303 | + if (C.is(ChunkKind::CallParameterClosureType)) { |
| 304 | + assert(ExpansionTypeString.empty()); |
| 305 | + ExpansionTypeString = C.getText(); |
| 306 | + continue; |
| 307 | + } |
| 308 | + if (C.is(ChunkKind::CallParameterType)) { |
| 309 | + assert(TypeString.empty()); |
| 310 | + TypeString = C.getText(); |
| 311 | + } |
| 312 | + if (C.is(ChunkKind::CallParameterClosureExpr)) { |
| 313 | + // We have a closure expression, so provide it directly instead of in |
| 314 | + // a placeholder. |
| 315 | + OS << "{"; |
| 316 | + if (!C.getText().empty()) |
| 317 | + OS << " " << C.getText(); |
| 318 | + OS << "\n" << getCodePlaceholder() << "\n}"; |
| 319 | + return; |
| 320 | + } |
| 321 | + if (C.isAnnotation() || !C.hasText()) |
| 322 | + continue; |
| 323 | + DisplayString += C.getText(); |
| 324 | + } |
| 325 | + |
| 326 | + StringRef Display = DisplayString.str(); |
| 327 | + StringRef Type = TypeString.str(); |
| 328 | + StringRef ExpansionType = ExpansionTypeString.str(); |
| 329 | + if (ExpansionType.empty()) |
| 330 | + ExpansionType = Type; |
| 331 | + |
| 332 | + OS << "<#T##" << Display; |
| 333 | + if (Display == Type && Display == ExpansionType) { |
| 334 | + // Short version, display and type are the same. |
| 335 | + } else { |
| 336 | + OS << "##" << Type; |
| 337 | + if (ExpansionType != Type) |
| 338 | + OS << "##" << ExpansionType; |
| 339 | + } |
| 340 | + OS << "#>"; |
| 341 | +} |
| 342 | + |
| 343 | +void swift::ide::printCodeCompletionResultSourceText( |
| 344 | + const CodeCompletionResult &Result, llvm::raw_ostream &OS) { |
| 345 | + auto Chunks = Result.getCompletionString()->getChunks(); |
| 346 | + for (size_t i = 0; i < Chunks.size(); ++i) { |
| 347 | + auto &C = Chunks[i]; |
| 348 | + if (C.is(ChunkKind::BraceStmtWithCursor)) { |
| 349 | + OS << " {\n" << getCodePlaceholder() << "\n}"; |
| 350 | + continue; |
| 351 | + } |
| 352 | + if (C.is(ChunkKind::CallParameterBegin)) { |
| 353 | + size_t Start = i++; |
| 354 | + for (; i < Chunks.size(); ++i) { |
| 355 | + if (Chunks[i].endsPreviousNestedGroup(C.getNestingLevel())) |
| 356 | + break; |
| 357 | + } |
| 358 | + constructTextForCallParam(Chunks.slice(Start, i - Start), OS); |
| 359 | + --i; |
| 360 | + continue; |
| 361 | + } |
| 362 | + if (C.is(ChunkKind::TypeAnnotationBegin)) { |
| 363 | + // Skip type annotation structure. |
| 364 | + auto level = C.getNestingLevel(); |
| 365 | + do { |
| 366 | + ++i; |
| 367 | + } while (i != Chunks.size() && !Chunks[i].endsPreviousNestedGroup(level)); |
| 368 | + --i; |
| 369 | + } |
| 370 | + if (!C.isAnnotation() && C.hasText()) { |
| 371 | + OS << C.getText(); |
| 372 | + } |
| 373 | + } |
| 374 | +} |
| 375 | + |
| 376 | +void swift::ide::printCodeCompletionResultFilterName( |
| 377 | + const CodeCompletionResult &Result, llvm::raw_ostream &OS) { |
| 378 | + auto str = Result.getCompletionString(); |
| 379 | + // FIXME: we need a more uniform way to handle operator completions. |
| 380 | + if (str->getChunks().size() == 1 && str->getChunks()[0].is(ChunkKind::Dot)) { |
| 381 | + OS << "."; |
| 382 | + return; |
| 383 | + } else if (str->getChunks().size() == 2 && |
| 384 | + str->getChunks()[0].is(ChunkKind::QuestionMark) && |
| 385 | + str->getChunks()[1].is(ChunkKind::Dot)) { |
| 386 | + OS << "?."; |
| 387 | + return; |
| 388 | + } |
| 389 | + |
| 390 | + auto FirstTextChunk = str->getFirstTextChunkIndex(); |
| 391 | + if (FirstTextChunk.hasValue()) { |
| 392 | + auto chunks = str->getChunks().slice(*FirstTextChunk); |
| 393 | + for (auto i = chunks.begin(), e = chunks.end(); i != e; ++i) { |
| 394 | + auto &C = *i; |
| 395 | + |
| 396 | + if (C.is(ChunkKind::BraceStmtWithCursor)) |
| 397 | + break; // Don't include brace-stmt in filter name. |
| 398 | + |
| 399 | + if (C.is(ChunkKind::Equal)) { |
| 400 | + OS << C.getText(); |
| 401 | + break; |
| 402 | + } |
| 403 | + |
| 404 | + bool shouldPrint = !C.isAnnotation(); |
| 405 | + switch (C.getKind()) { |
| 406 | + case ChunkKind::TypeAnnotation: |
| 407 | + case ChunkKind::CallParameterInternalName: |
| 408 | + case ChunkKind::CallParameterClosureType: |
| 409 | + case ChunkKind::CallParameterClosureExpr: |
| 410 | + case ChunkKind::CallParameterType: |
| 411 | + case ChunkKind::DeclAttrParamColon: |
| 412 | + case ChunkKind::Comma: |
| 413 | + case ChunkKind::Whitespace: |
| 414 | + case ChunkKind::Ellipsis: |
| 415 | + case ChunkKind::Ampersand: |
| 416 | + case ChunkKind::OptionalMethodCallTail: |
| 417 | + continue; |
| 418 | + case ChunkKind::CallParameterTypeBegin: |
| 419 | + case ChunkKind::TypeAnnotationBegin: { |
| 420 | + // Skip call parameter type or type annotation structure. |
| 421 | + auto nestingLevel = C.getNestingLevel(); |
| 422 | + do { |
| 423 | + ++i; |
| 424 | + } while (i != e && !i->endsPreviousNestedGroup(nestingLevel)); |
| 425 | + --i; |
| 426 | + continue; |
| 427 | + } |
| 428 | + case ChunkKind::CallParameterColon: |
| 429 | + // Since we don't add the type, also don't add the space after ':'. |
| 430 | + if (shouldPrint) |
| 431 | + OS << ":"; |
| 432 | + continue; |
| 433 | + default: |
| 434 | + break; |
| 435 | + } |
| 436 | + |
| 437 | + if (C.hasText() && shouldPrint) |
| 438 | + OS << C.getText(); |
| 439 | + } |
| 440 | + } |
| 441 | +} |
0 commit comments