Skip to content

Commit ccb7d76

Browse files
authored
Merge pull request #37366 from rintaro/5.5-ide-completion-rdar75620636
[5.5][CodeCompletion] Make object literals optional
2 parents 6c01131 + 8b65d0d commit ccb7d76

File tree

5 files changed

+100
-29
lines changed

5 files changed

+100
-29
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,9 @@ struct CodeCompletionResultSink {
867867
/// Whether to annotate the results with XML.
868868
bool annotateResult = false;
869869

870+
/// Whether to emit object literals if desired.
871+
bool includeObjectLiterals = true;
872+
870873
std::vector<CodeCompletionResult *> Results;
871874

872875
/// A single-element cache for module names stored in Allocator, keyed by a
@@ -938,6 +941,11 @@ class CodeCompletionContext {
938941
void setAnnotateResult(bool flag) { CurrentResults.annotateResult = flag; }
939942
bool getAnnotateResult() { return CurrentResults.annotateResult; }
940943

944+
void setIncludeObjectLiterals(bool flag) {
945+
CurrentResults.includeObjectLiterals = flag;
946+
}
947+
bool includeObjectLiterals() { return CurrentResults.includeObjectLiterals; }
948+
941949
/// Allocate a string owned by the code completion context.
942950
StringRef copyString(StringRef Str);
943951

lib/IDE/CodeCompletion.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,28 +4346,31 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
43464346
builder.addRightBracket();
43474347
});
43484348

4349-
auto floatType = context.getFloatDecl()->getDeclaredInterfaceType();
4350-
addFromProto(LK::ColorLiteral, [&](Builder &builder) {
4351-
builder.addBaseName("#colorLiteral");
4352-
builder.addLeftParen();
4353-
builder.addCallParameter(context.getIdentifier("red"), floatType);
4354-
builder.addComma();
4355-
builder.addCallParameter(context.getIdentifier("green"), floatType);
4356-
builder.addComma();
4357-
builder.addCallParameter(context.getIdentifier("blue"), floatType);
4358-
builder.addComma();
4359-
builder.addCallParameter(context.getIdentifier("alpha"), floatType);
4360-
builder.addRightParen();
4361-
});
4362-
4363-
auto stringType = context.getStringDecl()->getDeclaredInterfaceType();
4364-
addFromProto(LK::ImageLiteral, [&](Builder &builder) {
4365-
builder.addBaseName("#imageLiteral");
4366-
builder.addLeftParen();
4367-
builder.addCallParameter(context.getIdentifier("resourceName"),
4368-
stringType);
4369-
builder.addRightParen();
4370-
});
4349+
// Optionally add object literals.
4350+
if (CompletionContext->includeObjectLiterals()) {
4351+
auto floatType = context.getFloatDecl()->getDeclaredInterfaceType();
4352+
addFromProto(LK::ColorLiteral, [&](Builder &builder) {
4353+
builder.addBaseName("#colorLiteral");
4354+
builder.addLeftParen();
4355+
builder.addCallParameter(context.getIdentifier("red"), floatType);
4356+
builder.addComma();
4357+
builder.addCallParameter(context.getIdentifier("green"), floatType);
4358+
builder.addComma();
4359+
builder.addCallParameter(context.getIdentifier("blue"), floatType);
4360+
builder.addComma();
4361+
builder.addCallParameter(context.getIdentifier("alpha"), floatType);
4362+
builder.addRightParen();
4363+
});
4364+
4365+
auto stringType = context.getStringDecl()->getDeclaredInterfaceType();
4366+
addFromProto(LK::ImageLiteral, [&](Builder &builder) {
4367+
builder.addBaseName("#imageLiteral");
4368+
builder.addLeftParen();
4369+
builder.addCallParameter(context.getIdentifier("resourceName"),
4370+
stringType);
4371+
builder.addRightParen();
4372+
});
4373+
}
43714374

43724375
// Add tuple completion (item, item).
43734376
{
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
func test(color: String) {
2+
3+
}
4+
5+
// RUN: %sourcekitd-test \
6+
// RUN: -req=complete -pos=2:1 %s -- %s == \
7+
// RUN: -req=complete -pos=2:1 -req-opts=includeobjectliterals=1 %s -- %s == \
8+
// RUN: -req=complete -pos=2:1 -req-opts=includeobjectliterals=0 %s -- %s == \
9+
// RUN: -req=complete -pos=2:1 %s -- %s \
10+
// RUN: | tee %t.out | %FileCheck --check-prefix=CHECK1 %s
11+
12+
// CHECK1-LABEL: key.results: [
13+
// CHECK1: {
14+
// CHECK1: key.kind: source.lang.swift.literal.color,
15+
// CHECK1: key.name: "#colorLiteral(red:green:blue:alpha:)",
16+
// CHECK1: key.sourcetext: "#colorLiteral(red: <#T##Float#>, green: <#T##Float#>, blue: <#T##Float#>, alpha: <#T##Float#>)",
17+
// CHECK1: key.description: "#colorLiteral(red: Float, green: Float, blue: Float, alpha: Float)",
18+
// CHECK1: },
19+
// CHECK1: {
20+
// CHECK1: key.kind: source.lang.swift.literal.image,
21+
// CHECK1: key.name: "#imageLiteral(resourceName:)",
22+
// CHECK1: key.sourcetext: "#imageLiteral(resourceName: <#T##String#>)",
23+
// CHECK1: key.description: "#imageLiteral(resourceName: String)",
24+
// CHECK1: },
25+
26+
// CHECK1-LABEL: key.results: [
27+
// CHECK1: {
28+
// CHECK1: key.kind: source.lang.swift.literal.color,
29+
// CHECK1: key.name: "#colorLiteral(red:green:blue:alpha:)",
30+
// CHECK1: key.sourcetext: "#colorLiteral(red: <#T##Float#>, green: <#T##Float#>, blue: <#T##Float#>, alpha: <#T##Float#>)",
31+
// CHECK1: key.description: "#colorLiteral(red: Float, green: Float, blue: Float, alpha: Float)",
32+
// CHECK1: },
33+
// CHECK1: {
34+
// CHECK1: key.kind: source.lang.swift.literal.image,
35+
// CHECK1: key.name: "#imageLiteral(resourceName:)",
36+
// CHECK1: key.sourcetext: "#imageLiteral(resourceName: <#T##String#>)",
37+
// CHECK1: key.description: "#imageLiteral(resourceName: String)",
38+
// CHECK1: },
39+
40+
// CHECK1-LABEL: key.results: [
41+
// CHECK1-NOT: source.lang.swift.literal.color
42+
// CHECK1-NOT: colorLiteral
43+
// CHECK1-NOT: source.lang.swift.literal.image,
44+
// CHECK1-NOT: imageLiteral
45+
46+
// CHECK1-LABEL: key.results: [
47+
// CHECK1: {
48+
// CHECK1: key.kind: source.lang.swift.literal.color,
49+
// CHECK1: key.name: "#colorLiteral(red:green:blue:alpha:)",
50+
// CHECK1: key.sourcetext: "#colorLiteral(red: <#T##Float#>, green: <#T##Float#>, blue: <#T##Float#>, alpha: <#T##Float#>)",
51+
// CHECK1: key.description: "#colorLiteral(red: Float, green: Float, blue: Float, alpha: Float)",
52+
// CHECK1: },
53+
// CHECK1: {
54+
// CHECK1: key.kind: source.lang.swift.literal.image,
55+
// CHECK1: key.name: "#imageLiteral(resourceName:)",
56+
// CHECK1: key.sourcetext: "#imageLiteral(resourceName: <#T##String#>)",
57+
// CHECK1: key.description: "#imageLiteral(resourceName: String)",
58+
// CHECK1: },

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct Options {
4343
bool hideByNameStyle = true;
4444
bool fuzzyMatching = true;
4545
bool annotatedDescription = false;
46+
bool includeObjectLiterals = true;
4647
unsigned minFuzzyLength = 2;
4748
unsigned showTopNonLiteralResults = 3;
4849

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static bool swiftCodeCompleteImpl(
121121
unsigned Offset, SwiftCodeCompletionConsumer &SwiftConsumer,
122122
ArrayRef<const char *> Args,
123123
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
124-
bool annotateDescription, std::string &Error) {
124+
const CodeCompletion::Options &opts, std::string &Error) {
125125
return Lang.performCompletionLikeOperation(
126126
UnresolvedInputFile, Offset, Args, FileSystem, Error,
127127
[&](CompilerInstance &CI, bool reusingASTContext) {
@@ -130,7 +130,8 @@ static bool swiftCodeCompleteImpl(
130130
auto swiftCache = Lang.getCodeCompletionCache(); // Pin the cache.
131131
ide::CodeCompletionContext CompletionContext(swiftCache->getCache());
132132
CompletionContext.ReusingASTContext = reusingASTContext;
133-
CompletionContext.setAnnotateResult(annotateDescription);
133+
CompletionContext.setAnnotateResult(opts.annotatedDescription);
134+
CompletionContext.setIncludeObjectLiterals(opts.includeObjectLiterals);
134135
std::unique_ptr<CodeCompletionCallbacksFactory> callbacksFactory(
135136
ide::makeCodeCompletionCallbacksFactory(CompletionContext,
136137
SwiftConsumer));
@@ -214,8 +215,7 @@ void SwiftLangSupport::codeComplete(
214215

215216
std::string Error;
216217
if (!swiftCodeCompleteImpl(*this, UnresolvedInputFile, Offset, SwiftConsumer,
217-
Args, fileSystem,
218-
CCOpts.annotatedDescription, Error)) {
218+
Args, fileSystem, CCOpts, Error)) {
219219
SKConsumer.failed(Error);
220220
}
221221
}
@@ -728,6 +728,7 @@ static void translateCodeCompletionOptions(OptionsDictionary &from,
728728
static UIdent KeyFuzzyWeight("key.codecomplete.sort.fuzzyweight");
729729
static UIdent KeyPopularityBonus("key.codecomplete.sort.popularitybonus");
730730
static UIdent KeyAnnotatedDescription("key.codecomplete.annotateddescription");
731+
static UIdent KeyIncludeObjectLiterals("key.codecomplete.includeobjectliterals");
731732

732733
from.valueForOption(KeySortByName, to.sortByName);
733734
from.valueForOption(KeyUseImportDepth, to.useImportDepth);
@@ -753,6 +754,7 @@ static void translateCodeCompletionOptions(OptionsDictionary &from,
753754
from.valueForOption(KeyHideByName, to.hideByNameStyle);
754755
from.valueForOption(KeyTopNonLiteral, to.showTopNonLiteralResults);
755756
from.valueForOption(KeyAnnotatedDescription, to.annotatedDescription);
757+
from.valueForOption(KeyIncludeObjectLiterals, to.includeObjectLiterals);
756758
}
757759

758760
/// Canonicalize a name that is in the format of a reference to a function into
@@ -1023,7 +1025,7 @@ static void transformAndForwardResults(
10231025
std::string error;
10241026
if (!swiftCodeCompleteImpl(lang, buffer.get(), str.size(), swiftConsumer,
10251027
cargs, session->getFileSystem(),
1026-
options.annotatedDescription, error)) {
1028+
options, error)) {
10271029
consumer.failed(error);
10281030
return;
10291031
}
@@ -1123,8 +1125,7 @@ void SwiftLangSupport::codeCompleteOpen(
11231125

11241126
// Invoke completion.
11251127
if (!swiftCodeCompleteImpl(*this, inputBuf, offset, swiftConsumer,
1126-
extendedArgs, fileSystem,
1127-
CCOpts.annotatedDescription, error)) {
1128+
extendedArgs, fileSystem, CCOpts, error)) {
11281129
consumer.failed(error);
11291130
return;
11301131
}

0 commit comments

Comments
 (0)