Skip to content

Commit 34de805

Browse files
committed
[gardening] Move source text printing alongside other completion methods
1 parent 6480bfa commit 34de805

File tree

3 files changed

+118
-121
lines changed

3 files changed

+118
-121
lines changed

include/swift/IDE/CodeCompletionResultPrinter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ void printCodeCompletionResultTypeName(
3434
void printCodeCompletionResultTypeNameAnnotated(
3535
const CodeCompletionResult &Result, llvm::raw_ostream &OS);
3636

37+
void printCodeCompletionResultSourceText(
38+
const CodeCompletionResult &Result, llvm::raw_ostream &OS);
39+
3740
} // namespace ide
3841
} // namespace swift
3942

lib/IDE/CodeCompletionResultPrinter.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/Basic/LLVM.h"
14+
#include "swift/AST/ASTPrinter.h"
1415
#include "swift/IDE/CodeCompletionResultPrinter.h"
1516
#include "swift/IDE/CodeCompletion.h"
1617
#include "swift/Markup/XMLUtils.h"
@@ -255,3 +256,115 @@ void swift::ide::printCodeCompletionResultTypeNameAnnotated(const CodeCompletion
255256
AnnotatingResultPrinter printer(OS);
256257
printer.printTypeName(Result);
257258
}
259+
260+
/// Provide the text for the call parameter, including constructing a typed
261+
/// editor placeholder for it.
262+
static void constructTextForCallParam(
263+
ArrayRef<CodeCompletionString::Chunk> ParamGroup, raw_ostream &OS) {
264+
assert(ParamGroup.front().is(ChunkKind::CallParameterBegin));
265+
266+
for (; !ParamGroup.empty(); ParamGroup = ParamGroup.slice(1)) {
267+
auto &C = ParamGroup.front();
268+
if (C.isAnnotation())
269+
continue;
270+
if (C.is(ChunkKind::CallParameterInternalName) ||
271+
C.is(ChunkKind::CallParameterType) ||
272+
C.is(ChunkKind::CallParameterTypeBegin) ||
273+
C.is(ChunkKind::CallParameterClosureExpr)) {
274+
break;
275+
}
276+
if (!C.hasText())
277+
continue;
278+
OS << C.getText();
279+
}
280+
281+
SmallString<32> DisplayString;
282+
SmallString<32> TypeString;
283+
SmallString<32> ExpansionTypeString;
284+
285+
for (auto i = ParamGroup.begin(), e = ParamGroup.end(); i != e; ++i) {
286+
auto &C = *i;
287+
if (C.is(ChunkKind::CallParameterTypeBegin)) {
288+
assert(TypeString.empty());
289+
auto nestingLevel = C.getNestingLevel();
290+
++i;
291+
for (; i != e; ++i) {
292+
if (i->endsPreviousNestedGroup(nestingLevel))
293+
break;
294+
if (!i->isAnnotation() && i->hasText()) {
295+
TypeString += i->getText();
296+
DisplayString += i->getText();
297+
}
298+
}
299+
--i;
300+
continue;
301+
}
302+
if (C.is(ChunkKind::CallParameterClosureType)) {
303+
assert(ExpansionTypeString.empty());
304+
ExpansionTypeString = C.getText();
305+
continue;
306+
}
307+
if (C.is(ChunkKind::CallParameterType)) {
308+
assert(TypeString.empty());
309+
TypeString = C.getText();
310+
}
311+
if (C.is(ChunkKind::CallParameterClosureExpr)) {
312+
// We have a closure expression, so provide it directly instead of in
313+
// a placeholder.
314+
OS << "{";
315+
if (!C.getText().empty())
316+
OS << " " << C.getText();
317+
OS << "\n" << getCodePlaceholder() << "\n}";
318+
return;
319+
}
320+
if (C.isAnnotation() || !C.hasText())
321+
continue;
322+
DisplayString += C.getText();
323+
}
324+
325+
StringRef Display = DisplayString.str();
326+
StringRef Type = TypeString.str();
327+
StringRef ExpansionType = ExpansionTypeString.str();
328+
if (ExpansionType.empty())
329+
ExpansionType = Type;
330+
331+
OS << "<#T##" << Display;
332+
if (Display == Type && Display == ExpansionType) {
333+
// Short version, display and type are the same.
334+
} else {
335+
OS << "##" << Type;
336+
if (ExpansionType != Type)
337+
OS << "##" << ExpansionType;
338+
}
339+
OS << "#>";
340+
}
341+
342+
void swift::ide::printCodeCompletionResultSourceText(const CodeCompletionResult &Result, llvm::raw_ostream &OS) {
343+
auto Chunks = Result.getCompletionString()->getChunks();
344+
for (size_t i = 0; i < Chunks.size(); ++i) {
345+
auto &C = Chunks[i];
346+
if (C.is(ChunkKind::BraceStmtWithCursor)) {
347+
OS << " {\n" << getCodePlaceholder() << "\n}";
348+
continue;
349+
}
350+
if (C.is(ChunkKind::CallParameterBegin)) {
351+
size_t Start = i++;
352+
for (; i < Chunks.size(); ++i) {
353+
if (Chunks[i].endsPreviousNestedGroup(C.getNestingLevel()))
354+
break;
355+
}
356+
constructTextForCallParam(Chunks.slice(Start, i-Start), OS);
357+
--i;
358+
continue;
359+
}
360+
if (C.is(ChunkKind::TypeAnnotationBegin)) {
361+
// Skip type annotation structure.
362+
auto level = C.getNestingLevel();
363+
do { ++i; } while (i != Chunks.size() && !Chunks[i].endsPreviousNestedGroup(level));
364+
--i;
365+
}
366+
if (!C.isAnnotation() && C.hasText()) {
367+
OS << C.getText();
368+
}
369+
}
370+
}

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 2 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ struct SwiftToSourceKitCompletionAdapter {
7575
bool legacyLiteralToKeyword,
7676
bool annotatedDescription);
7777

78-
static void getResultSourceText(const CodeCompletionString *CCStr,
79-
raw_ostream &OS);
80-
8178
static void getResultAssociatedUSRs(ArrayRef<StringRef> AssocUSRs,
8279
raw_ostream &OS);
8380
};
@@ -466,7 +463,7 @@ bool SwiftToSourceKitCompletionAdapter::handleResult(
466463
unsigned TextBegin = SS.size();
467464
{
468465
llvm::raw_svector_ostream ccOS(SS);
469-
getResultSourceText(Result->getCompletionString(), ccOS);
466+
ide::printCodeCompletionResultSourceText(*Result, ccOS);
470467
}
471468
unsigned TextEnd = SS.size();
472469

@@ -603,121 +600,6 @@ getCodeCompletionKeywordKindForUID(UIdent uid) {
603600
return CodeCompletionKeywordKind::None;
604601
}
605602

606-
using ChunkKind = CodeCompletionString::Chunk::ChunkKind;
607-
608-
/// Provide the text for the call parameter, including constructing a typed
609-
/// editor placeholder for it.
610-
static void constructTextForCallParam(
611-
ArrayRef<CodeCompletionString::Chunk> ParamGroup, raw_ostream &OS) {
612-
assert(ParamGroup.front().is(ChunkKind::CallParameterBegin));
613-
614-
for (; !ParamGroup.empty(); ParamGroup = ParamGroup.slice(1)) {
615-
auto &C = ParamGroup.front();
616-
if (C.isAnnotation())
617-
continue;
618-
if (C.is(ChunkKind::CallParameterInternalName) ||
619-
C.is(ChunkKind::CallParameterType) ||
620-
C.is(ChunkKind::CallParameterTypeBegin) ||
621-
C.is(ChunkKind::CallParameterClosureExpr)) {
622-
break;
623-
}
624-
if (!C.hasText())
625-
continue;
626-
OS << C.getText();
627-
}
628-
629-
SmallString<32> DisplayString;
630-
SmallString<32> TypeString;
631-
SmallString<32> ExpansionTypeString;
632-
633-
for (auto i = ParamGroup.begin(), e = ParamGroup.end(); i != e; ++i) {
634-
auto &C = *i;
635-
if (C.is(ChunkKind::CallParameterTypeBegin)) {
636-
assert(TypeString.empty());
637-
auto nestingLevel = C.getNestingLevel();
638-
++i;
639-
for (; i != e; ++i) {
640-
if (i->endsPreviousNestedGroup(nestingLevel))
641-
break;
642-
if (!i->isAnnotation() && i->hasText()) {
643-
TypeString += i->getText();
644-
DisplayString += i->getText();
645-
}
646-
}
647-
--i;
648-
continue;
649-
}
650-
if (C.is(ChunkKind::CallParameterClosureType)) {
651-
assert(ExpansionTypeString.empty());
652-
ExpansionTypeString = C.getText();
653-
continue;
654-
}
655-
if (C.is(ChunkKind::CallParameterType)) {
656-
assert(TypeString.empty());
657-
TypeString = C.getText();
658-
}
659-
if (C.is(ChunkKind::CallParameterClosureExpr)) {
660-
// We have a closure expression, so provide it directly instead of in
661-
// a placeholder.
662-
OS << "{";
663-
if (!C.getText().empty())
664-
OS << " " << C.getText();
665-
OS << "\n" << getCodePlaceholder() << "\n}";
666-
return;
667-
}
668-
if (C.isAnnotation() || !C.hasText())
669-
continue;
670-
DisplayString += C.getText();
671-
}
672-
673-
StringRef Display = DisplayString.str();
674-
StringRef Type = TypeString.str();
675-
StringRef ExpansionType = ExpansionTypeString.str();
676-
if (ExpansionType.empty())
677-
ExpansionType = Type;
678-
679-
OS << "<#T##" << Display;
680-
if (Display == Type && Display == ExpansionType) {
681-
// Short version, display and type are the same.
682-
} else {
683-
OS << "##" << Type;
684-
if (ExpansionType != Type)
685-
OS << "##" << ExpansionType;
686-
}
687-
OS << "#>";
688-
}
689-
690-
void SwiftToSourceKitCompletionAdapter::getResultSourceText(
691-
const CodeCompletionString *CCStr, raw_ostream &OS) {
692-
auto Chunks = CCStr->getChunks();
693-
for (size_t i = 0; i < Chunks.size(); ++i) {
694-
auto &C = Chunks[i];
695-
if (C.is(ChunkKind::BraceStmtWithCursor)) {
696-
OS << " {\n" << getCodePlaceholder() << "\n}";
697-
continue;
698-
}
699-
if (C.is(ChunkKind::CallParameterBegin)) {
700-
size_t Start = i++;
701-
for (; i < Chunks.size(); ++i) {
702-
if (Chunks[i].endsPreviousNestedGroup(C.getNestingLevel()))
703-
break;
704-
}
705-
constructTextForCallParam(Chunks.slice(Start, i-Start), OS);
706-
--i;
707-
continue;
708-
}
709-
if (C.is(ChunkKind::TypeAnnotationBegin)) {
710-
// Skip type annotation structure.
711-
auto level = C.getNestingLevel();
712-
do { ++i; } while (i != Chunks.size() && !Chunks[i].endsPreviousNestedGroup(level));
713-
--i;
714-
}
715-
if (!C.isAnnotation() && C.hasText()) {
716-
OS << C.getText();
717-
}
718-
}
719-
}
720-
721603
void SwiftToSourceKitCompletionAdapter::getResultAssociatedUSRs(
722604
ArrayRef<StringRef> AssocUSRs, raw_ostream &OS) {
723605
bool First = true;
@@ -1140,8 +1022,7 @@ static void transformAndForwardResults(
11401022
std::string str = inputBuf->getBuffer().slice(0, offset).str();
11411023
{
11421024
llvm::raw_string_ostream OSS(str);
1143-
SwiftToSourceKitCompletionAdapter::getResultSourceText(
1144-
exactMatch->getCompletionString(), OSS);
1025+
ide::printCodeCompletionResultSourceText(*exactMatch, OSS);
11451026
}
11461027

11471028
auto buffer =

0 commit comments

Comments
 (0)