Skip to content

Commit 3c9c520

Browse files
committed
[CodeCompletion] Add image and color literals to results
Surface these results in the codecomplete code path (they're already there in the codecomplete.open code path) so we can use them from IDEs. For now, just use ad-hoc filtering to show them when the type matches (or there is no expected type). Ideally we would make this filtering configurable like we do in the codecomplete.open code path. rdar://problem/25836544
1 parent c62ed91 commit 3c9c520

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

test/SourceKit/CodeComplete/complete_literals.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
()
2+
let var1 = 1
3+
let var2: Int = 1
24
// XFAIL: broken_std_regex
3-
// RUN: %sourcekitd-test -req=complete -pos=1:2 %s -- %s | FileCheck %s -check-prefix=KEYWORDS
4-
// RUN: %sourcekitd-test -req=complete.open -pos=1:2 %s -- %s | FileCheck %s -check-prefix=LITERALS
5+
// RUN: %sourcekitd-test -req=complete -pos=1:2 %s -- %s | FileCheck %s -check-prefix=COMPLETE_1
6+
// RUN: %sourcekitd-test -req=complete -pos=2:12 %s -- %s | FileCheck %s -check-prefix=COMPLETE_1
7+
// RUN: %sourcekitd-test -req=complete -pos=3:17 %s -- %s | FileCheck %s -check-prefix=COMPLETE_2
8+
9+
// COMPLETE_1-NOT: source.lang.swift.literal
10+
// COMPLETE_1: source.lang.swift.literal.color
11+
// COMPLETE_1: source.lang.swift.literal.image
12+
// COMPLETE_1-NOT: source.lang.swift.literal
13+
// COMPLETE_1: key.name: "nil"
14+
// COMPLETE_1-NOT: source.lang.swift.literal
515

6-
// KEYWORDS-NOT: source.lang.swift.literal
7-
// KEYWORDS: key.name: "nil"
8-
// KEYWORDS-NOT: source.lang.swift.literal
16+
// COMPLETE_2-NOT: source.lang.swift.literal
17+
// COMPLETE_2: key.name: "nil"
18+
// COMPLETE_2-NOT: source.lang.swift.literal
19+
20+
// RUN: %sourcekitd-test -req=complete.open -pos=1:2 %s -- %s | FileCheck %s -check-prefix=LITERALS
921
// LITERALS: key.kind: source.lang.swift.literal.string
1022
// LITERALS: key.sourcetext: "\"<#{{.*}}#>\""
1123
// LITERALS: key.kind: source.lang.swift.literal.boolean

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ struct SwiftToSourceKitCompletionAdapter {
6161

6262
Completion extended(*result, name, description);
6363
return handleResult(consumer, &extended, /*leadingPunctuation=*/false,
64-
/*includeLiterals=*/false);
64+
/*legacyLiteralToKeyword=*/true);
6565
}
6666

6767
static bool handleResult(SourceKit::CodeCompletionConsumer &consumer,
6868
Completion *result, bool leadingPunctuation,
69-
bool includeLiterals);
69+
bool legacyLiteralToKeyword);
7070

7171
static void getResultSourceText(const CodeCompletionString *CCStr,
7272
raw_ostream &OS);
@@ -219,9 +219,29 @@ void SwiftLangSupport::codeComplete(llvm::MemoryBuffer *UnresolvedInputFile,
219219
SourceKit::CodeCompletionConsumer &SKConsumer,
220220
ArrayRef<const char *> Args) {
221221
SwiftCodeCompletionConsumer SwiftConsumer([&](
222-
MutableArrayRef<CodeCompletionResult *> Results, SwiftCompletionInfo &) {
222+
MutableArrayRef<CodeCompletionResult *> Results,
223+
SwiftCompletionInfo &info) {
224+
bool hasExpectedType = info.completionContext->HasExpectedTypeRelation;
223225
CodeCompletionContext::sortCompletionResults(Results);
226+
// FIXME: this adhoc filtering should be configurable like it is in the
227+
// codeCompleteOpen path.
224228
for (auto *Result : Results) {
229+
if (Result->getKind() == CodeCompletionResult::Literal) {
230+
switch (Result->getLiteralKind()) {
231+
case CodeCompletionLiteralKind::NilLiteral:
232+
case CodeCompletionLiteralKind::BooleanLiteral:
233+
break;
234+
case CodeCompletionLiteralKind::ImageLiteral:
235+
case CodeCompletionLiteralKind::ColorLiteral:
236+
if (hasExpectedType &&
237+
Result->getExpectedTypeRelation() <
238+
CodeCompletionResult::Convertible)
239+
continue;
240+
break;
241+
default:
242+
continue;
243+
}
244+
}
225245
if (!SwiftToSourceKitCompletionAdapter::handleResult(SKConsumer, Result))
226246
break;
227247
}
@@ -410,7 +430,7 @@ static UIdent KeywordFuncUID("source.lang.swift.keyword.func");
410430

411431
bool SwiftToSourceKitCompletionAdapter::handleResult(
412432
SourceKit::CodeCompletionConsumer &Consumer, Completion *Result,
413-
bool leadingPunctuation, bool includeLiterals) {
433+
bool leadingPunctuation, bool legacyLiteralToKeyword) {
414434

415435
static UIdent KeywordUID("source.lang.swift.keyword");
416436
static UIdent PatternUID("source.lang.swift.pattern");
@@ -428,14 +448,14 @@ bool SwiftToSourceKitCompletionAdapter::handleResult(
428448
Info.Kind = SwiftLangSupport::getUIDForCodeCompletionDeclKind(
429449
Result->getAssociatedDeclKind());
430450
} else if (Result->getKind() == CodeCompletionResult::Literal) {
431-
if (includeLiterals) {
432-
Info.Kind = getUIDForCodeCompletionLiteralKind(Result->getLiteralKind());
433-
} else if (Result->getLiteralKind() ==
434-
CodeCompletionLiteralKind::BooleanLiteral ||
435-
Result->getLiteralKind() ==
436-
CodeCompletionLiteralKind::NilLiteral) {
437-
// If we're not including literals, fallback to keywords as appropriate.
451+
auto literalKind = Result->getLiteralKind();
452+
if (legacyLiteralToKeyword &&
453+
(literalKind == CodeCompletionLiteralKind::BooleanLiteral ||
454+
literalKind == CodeCompletionLiteralKind::NilLiteral)) {
455+
// Fallback to keywords as appropriate.
438456
Info.Kind = KeywordUID;
457+
} else {
458+
Info.Kind = getUIDForCodeCompletionLiteralKind(literalKind);
439459
}
440460
}
441461

@@ -761,7 +781,7 @@ class SwiftGroupedCodeCompletionConsumer : public CodeCompletionView::Walker {
761781
bool handleResult(Completion *result) override {
762782
return SwiftToSourceKitCompletionAdapter::handleResult(
763783
consumer, result, /*leadingPunctuation=*/true,
764-
/*includeLiterals=*/true);
784+
/*legacyLiteralToKeyword=*/false);
765785
}
766786
void startGroup(StringRef name) override {
767787
static UIdent GroupUID("source.lang.swift.codecomplete.group");

0 commit comments

Comments
 (0)