Skip to content

Commit cf87ad8

Browse files
committed
[gardening] Move filter name printing alongside other completion methods
1 parent 34de805 commit cf87ad8

File tree

5 files changed

+71
-78
lines changed

5 files changed

+71
-78
lines changed

include/swift/IDE/CodeCompletionResultPrinter.h

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ void printCodeCompletionResultTypeNameAnnotated(
3737
void printCodeCompletionResultSourceText(
3838
const CodeCompletionResult &Result, llvm::raw_ostream &OS);
3939

40+
void printCodeCompletionResultFilterName(
41+
const CodeCompletionResult &Result, llvm::raw_ostream &OS);
42+
4043
} // namespace ide
4144
} // namespace swift
4245

lib/IDE/CodeCompletionResultPrinter.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,67 @@ void swift::ide::printCodeCompletionResultSourceText(const CodeCompletionResult
368368
}
369369
}
370370
}
371+
372+
void swift::ide::printCodeCompletionResultFilterName(const CodeCompletionResult &Result, llvm::raw_ostream &OS) {
373+
auto str = Result.getCompletionString();
374+
// FIXME: we need a more uniform way to handle operator completions.
375+
if (str->getChunks().size() == 1 && str->getChunks()[0].is(ChunkKind::Dot)) {
376+
OS << ".";
377+
return;
378+
} else if (str->getChunks().size() == 2 &&
379+
str->getChunks()[0].is(ChunkKind::QuestionMark) &&
380+
str->getChunks()[1].is(ChunkKind::Dot)) {
381+
OS << "?.";
382+
return;
383+
}
384+
385+
auto FirstTextChunk = str->getFirstTextChunkIndex();
386+
if (FirstTextChunk.hasValue()) {
387+
auto chunks = str->getChunks().slice(*FirstTextChunk);
388+
for (auto i = chunks.begin(), e = chunks.end(); i != e; ++i) {
389+
auto &C = *i;
390+
391+
if (C.is(ChunkKind::BraceStmtWithCursor))
392+
break; // Don't include brace-stmt in filter name.
393+
394+
if (C.is(ChunkKind::Equal)) {
395+
OS << C.getText();
396+
break;
397+
}
398+
399+
bool shouldPrint = !C.isAnnotation();
400+
switch (C.getKind()) {
401+
case ChunkKind::TypeAnnotation:
402+
case ChunkKind::CallParameterInternalName:
403+
case ChunkKind::CallParameterClosureType:
404+
case ChunkKind::CallParameterClosureExpr:
405+
case ChunkKind::CallParameterType:
406+
case ChunkKind::DeclAttrParamColon:
407+
case ChunkKind::Comma:
408+
case ChunkKind::Whitespace:
409+
case ChunkKind::Ellipsis:
410+
case ChunkKind::Ampersand:
411+
case ChunkKind::OptionalMethodCallTail:
412+
continue;
413+
case ChunkKind::CallParameterTypeBegin:
414+
case ChunkKind::TypeAnnotationBegin: {
415+
// Skip call parameter type or type annotation structure.
416+
auto nestingLevel = C.getNestingLevel();
417+
do { ++i; } while (i != e && !i->endsPreviousNestedGroup(nestingLevel));
418+
--i;
419+
continue;
420+
}
421+
case ChunkKind::CallParameterColon:
422+
// Since we don't add the type, also don't add the space after ':'.
423+
if (shouldPrint)
424+
OS << ":";
425+
continue;
426+
default:
427+
break;
428+
}
429+
430+
if (C.hasText() && shouldPrint)
431+
OS << C.getText();
432+
}
433+
}
434+
}

tools/SourceKit/lib/SwiftLang/CodeCompletion.h

-6
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,6 @@ class CompletionBuilder {
132132
Optional<uint8_t> moduleImportDepth;
133133
PopularityFactor popularityFactor;
134134

135-
public:
136-
static void getFilterName(CodeCompletionString *str, raw_ostream &OS);
137-
static void getDescription(SwiftResult *result, raw_ostream &OS,
138-
bool leadingPunctuation,
139-
bool annotatedDecription = false);
140-
141135
public:
142136
CompletionBuilder(CompletionSink &sink, SwiftResult &base);
143137

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp

+2-68
Original file line numberDiff line numberDiff line change
@@ -1209,72 +1209,6 @@ bool LimitedResultView::walk(CodeCompletionView::Walker &walker) const {
12091209
// CompletionBuilder
12101210
//===----------------------------------------------------------------------===//
12111211

1212-
void CompletionBuilder::getFilterName(CodeCompletionString *str,
1213-
raw_ostream &OS) {
1214-
using ChunkKind = CodeCompletionString::Chunk::ChunkKind;
1215-
1216-
// FIXME: we need a more uniform way to handle operator completions.
1217-
if (str->getChunks().size() == 1 && str->getChunks()[0].is(ChunkKind::Dot)) {
1218-
OS << ".";
1219-
return;
1220-
} else if (str->getChunks().size() == 2 &&
1221-
str->getChunks()[0].is(ChunkKind::QuestionMark) &&
1222-
str->getChunks()[1].is(ChunkKind::Dot)) {
1223-
OS << "?.";
1224-
return;
1225-
}
1226-
1227-
auto FirstTextChunk = str->getFirstTextChunkIndex();
1228-
if (FirstTextChunk.hasValue()) {
1229-
auto chunks = str->getChunks().slice(*FirstTextChunk);
1230-
for (auto i = chunks.begin(), e = chunks.end(); i != e; ++i) {
1231-
auto &C = *i;
1232-
1233-
if (C.is(ChunkKind::BraceStmtWithCursor))
1234-
break; // Don't include brace-stmt in filter name.
1235-
1236-
if (C.is(ChunkKind::Equal)) {
1237-
OS << C.getText();
1238-
break;
1239-
}
1240-
1241-
bool shouldPrint = !C.isAnnotation();
1242-
switch (C.getKind()) {
1243-
case ChunkKind::TypeAnnotation:
1244-
case ChunkKind::CallParameterInternalName:
1245-
case ChunkKind::CallParameterClosureType:
1246-
case ChunkKind::CallParameterClosureExpr:
1247-
case ChunkKind::CallParameterType:
1248-
case ChunkKind::DeclAttrParamColon:
1249-
case ChunkKind::Comma:
1250-
case ChunkKind::Whitespace:
1251-
case ChunkKind::Ellipsis:
1252-
case ChunkKind::Ampersand:
1253-
case ChunkKind::OptionalMethodCallTail:
1254-
continue;
1255-
case ChunkKind::CallParameterTypeBegin:
1256-
case ChunkKind::TypeAnnotationBegin: {
1257-
// Skip call parameter type or type annotation structure.
1258-
auto nestingLevel = C.getNestingLevel();
1259-
do { ++i; } while (i != e && !i->endsPreviousNestedGroup(nestingLevel));
1260-
--i;
1261-
continue;
1262-
}
1263-
case ChunkKind::CallParameterColon:
1264-
// Since we don't add the type, also don't add the space after ':'.
1265-
if (shouldPrint)
1266-
OS << ":";
1267-
continue;
1268-
default:
1269-
break;
1270-
}
1271-
1272-
if (C.hasText() && shouldPrint)
1273-
OS << C.getText();
1274-
}
1275-
}
1276-
}
1277-
12781212
CompletionBuilder::CompletionBuilder(CompletionSink &sink, SwiftResult &base)
12791213
: sink(sink), current(base) {
12801214
typeRelation = current.getExpectedTypeRelation();
@@ -1286,7 +1220,7 @@ CompletionBuilder::CompletionBuilder(CompletionSink &sink, SwiftResult &base)
12861220
// strings for our inner "." result.
12871221
if (current.getCompletionString()->getFirstTextChunkIndex().hasValue()) {
12881222
llvm::raw_svector_ostream OSS(originalName);
1289-
getFilterName(current.getCompletionString(), OSS);
1223+
ide::printCodeCompletionResultFilterName(current, OSS);
12901224
}
12911225
}
12921226

@@ -1336,7 +1270,7 @@ Completion *CompletionBuilder::finish() {
13361270
}
13371271

13381272
llvm::raw_svector_ostream OSS(nameStorage);
1339-
getFilterName(base.getCompletionString(), OSS);
1273+
ide::printCodeCompletionResultFilterName(base, OSS);
13401274
name = OSS.str();
13411275
}
13421276

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ struct SwiftToSourceKitCompletionAdapter {
4949
llvm::SmallString<64> name;
5050
{
5151
llvm::raw_svector_ostream OSS(name);
52-
CodeCompletion::CompletionBuilder::getFilterName(
53-
result->getCompletionString(), OSS);
52+
ide::printCodeCompletionResultFilterName(*result, OSS);
5453
}
5554

5655
llvm::SmallString<64> description;
@@ -887,8 +886,7 @@ filterInnerResults(ArrayRef<Result *> results, bool includeInner,
887886
llvm::SmallString<64> filterName;
888887
{
889888
llvm::raw_svector_ostream OSS(filterName);
890-
CodeCompletion::CompletionBuilder::getFilterName(
891-
result->getCompletionString(), OSS);
889+
ide::printCodeCompletionResultFilterName(*result, OSS);
892890
}
893891
llvm::SmallString<64> description;
894892
{

0 commit comments

Comments
 (0)