Skip to content

[5.5][CodeCompletion] Make object literals optional #37366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/swift/IDE/CodeCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,9 @@ struct CodeCompletionResultSink {
/// Whether to annotate the results with XML.
bool annotateResult = false;

/// Whether to emit object literals if desired.
bool includeObjectLiterals = true;

std::vector<CodeCompletionResult *> Results;

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

void setIncludeObjectLiterals(bool flag) {
CurrentResults.includeObjectLiterals = flag;
}
bool includeObjectLiterals() { return CurrentResults.includeObjectLiterals; }

/// Allocate a string owned by the code completion context.
StringRef copyString(StringRef Str);

Expand Down
47 changes: 25 additions & 22 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4346,28 +4346,31 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
builder.addRightBracket();
});

auto floatType = context.getFloatDecl()->getDeclaredInterfaceType();
addFromProto(LK::ColorLiteral, [&](Builder &builder) {
builder.addBaseName("#colorLiteral");
builder.addLeftParen();
builder.addCallParameter(context.getIdentifier("red"), floatType);
builder.addComma();
builder.addCallParameter(context.getIdentifier("green"), floatType);
builder.addComma();
builder.addCallParameter(context.getIdentifier("blue"), floatType);
builder.addComma();
builder.addCallParameter(context.getIdentifier("alpha"), floatType);
builder.addRightParen();
});

auto stringType = context.getStringDecl()->getDeclaredInterfaceType();
addFromProto(LK::ImageLiteral, [&](Builder &builder) {
builder.addBaseName("#imageLiteral");
builder.addLeftParen();
builder.addCallParameter(context.getIdentifier("resourceName"),
stringType);
builder.addRightParen();
});
// Optionally add object literals.
if (CompletionContext->includeObjectLiterals()) {
auto floatType = context.getFloatDecl()->getDeclaredInterfaceType();
addFromProto(LK::ColorLiteral, [&](Builder &builder) {
builder.addBaseName("#colorLiteral");
builder.addLeftParen();
builder.addCallParameter(context.getIdentifier("red"), floatType);
builder.addComma();
builder.addCallParameter(context.getIdentifier("green"), floatType);
builder.addComma();
builder.addCallParameter(context.getIdentifier("blue"), floatType);
builder.addComma();
builder.addCallParameter(context.getIdentifier("alpha"), floatType);
builder.addRightParen();
});

auto stringType = context.getStringDecl()->getDeclaredInterfaceType();
addFromProto(LK::ImageLiteral, [&](Builder &builder) {
builder.addBaseName("#imageLiteral");
builder.addLeftParen();
builder.addCallParameter(context.getIdentifier("resourceName"),
stringType);
builder.addRightParen();
});
}

// Add tuple completion (item, item).
{
Expand Down
58 changes: 58 additions & 0 deletions test/SourceKit/CodeComplete/complete_object_literals.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
func test(color: String) {

}

// RUN: %sourcekitd-test \
// RUN: -req=complete -pos=2:1 %s -- %s == \
// RUN: -req=complete -pos=2:1 -req-opts=includeobjectliterals=1 %s -- %s == \
// RUN: -req=complete -pos=2:1 -req-opts=includeobjectliterals=0 %s -- %s == \
// RUN: -req=complete -pos=2:1 %s -- %s \
// RUN: | tee %t.out | %FileCheck --check-prefix=CHECK1 %s

// CHECK1-LABEL: key.results: [
// CHECK1: {
// CHECK1: key.kind: source.lang.swift.literal.color,
// CHECK1: key.name: "#colorLiteral(red:green:blue:alpha:)",
// CHECK1: key.sourcetext: "#colorLiteral(red: <#T##Float#>, green: <#T##Float#>, blue: <#T##Float#>, alpha: <#T##Float#>)",
// CHECK1: key.description: "#colorLiteral(red: Float, green: Float, blue: Float, alpha: Float)",
// CHECK1: },
// CHECK1: {
// CHECK1: key.kind: source.lang.swift.literal.image,
// CHECK1: key.name: "#imageLiteral(resourceName:)",
// CHECK1: key.sourcetext: "#imageLiteral(resourceName: <#T##String#>)",
// CHECK1: key.description: "#imageLiteral(resourceName: String)",
// CHECK1: },

// CHECK1-LABEL: key.results: [
// CHECK1: {
// CHECK1: key.kind: source.lang.swift.literal.color,
// CHECK1: key.name: "#colorLiteral(red:green:blue:alpha:)",
// CHECK1: key.sourcetext: "#colorLiteral(red: <#T##Float#>, green: <#T##Float#>, blue: <#T##Float#>, alpha: <#T##Float#>)",
// CHECK1: key.description: "#colorLiteral(red: Float, green: Float, blue: Float, alpha: Float)",
// CHECK1: },
// CHECK1: {
// CHECK1: key.kind: source.lang.swift.literal.image,
// CHECK1: key.name: "#imageLiteral(resourceName:)",
// CHECK1: key.sourcetext: "#imageLiteral(resourceName: <#T##String#>)",
// CHECK1: key.description: "#imageLiteral(resourceName: String)",
// CHECK1: },

// CHECK1-LABEL: key.results: [
// CHECK1-NOT: source.lang.swift.literal.color
// CHECK1-NOT: colorLiteral
// CHECK1-NOT: source.lang.swift.literal.image,
// CHECK1-NOT: imageLiteral

// CHECK1-LABEL: key.results: [
// CHECK1: {
// CHECK1: key.kind: source.lang.swift.literal.color,
// CHECK1: key.name: "#colorLiteral(red:green:blue:alpha:)",
// CHECK1: key.sourcetext: "#colorLiteral(red: <#T##Float#>, green: <#T##Float#>, blue: <#T##Float#>, alpha: <#T##Float#>)",
// CHECK1: key.description: "#colorLiteral(red: Float, green: Float, blue: Float, alpha: Float)",
// CHECK1: },
// CHECK1: {
// CHECK1: key.kind: source.lang.swift.literal.image,
// CHECK1: key.name: "#imageLiteral(resourceName:)",
// CHECK1: key.sourcetext: "#imageLiteral(resourceName: <#T##String#>)",
// CHECK1: key.description: "#imageLiteral(resourceName: String)",
// CHECK1: },
1 change: 1 addition & 0 deletions tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct Options {
bool hideByNameStyle = true;
bool fuzzyMatching = true;
bool annotatedDescription = false;
bool includeObjectLiterals = true;
unsigned minFuzzyLength = 2;
unsigned showTopNonLiteralResults = 3;

Expand Down
15 changes: 8 additions & 7 deletions tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static bool swiftCodeCompleteImpl(
unsigned Offset, SwiftCodeCompletionConsumer &SwiftConsumer,
ArrayRef<const char *> Args,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
bool annotateDescription, std::string &Error) {
const CodeCompletion::Options &opts, std::string &Error) {
return Lang.performCompletionLikeOperation(
UnresolvedInputFile, Offset, Args, FileSystem, Error,
[&](CompilerInstance &CI, bool reusingASTContext) {
Expand All @@ -130,7 +130,8 @@ static bool swiftCodeCompleteImpl(
auto swiftCache = Lang.getCodeCompletionCache(); // Pin the cache.
ide::CodeCompletionContext CompletionContext(swiftCache->getCache());
CompletionContext.ReusingASTContext = reusingASTContext;
CompletionContext.setAnnotateResult(annotateDescription);
CompletionContext.setAnnotateResult(opts.annotatedDescription);
CompletionContext.setIncludeObjectLiterals(opts.includeObjectLiterals);
std::unique_ptr<CodeCompletionCallbacksFactory> callbacksFactory(
ide::makeCodeCompletionCallbacksFactory(CompletionContext,
SwiftConsumer));
Expand Down Expand Up @@ -214,8 +215,7 @@ void SwiftLangSupport::codeComplete(

std::string Error;
if (!swiftCodeCompleteImpl(*this, UnresolvedInputFile, Offset, SwiftConsumer,
Args, fileSystem,
CCOpts.annotatedDescription, Error)) {
Args, fileSystem, CCOpts, Error)) {
SKConsumer.failed(Error);
}
}
Expand Down Expand Up @@ -728,6 +728,7 @@ static void translateCodeCompletionOptions(OptionsDictionary &from,
static UIdent KeyFuzzyWeight("key.codecomplete.sort.fuzzyweight");
static UIdent KeyPopularityBonus("key.codecomplete.sort.popularitybonus");
static UIdent KeyAnnotatedDescription("key.codecomplete.annotateddescription");
static UIdent KeyIncludeObjectLiterals("key.codecomplete.includeobjectliterals");

from.valueForOption(KeySortByName, to.sortByName);
from.valueForOption(KeyUseImportDepth, to.useImportDepth);
Expand All @@ -753,6 +754,7 @@ static void translateCodeCompletionOptions(OptionsDictionary &from,
from.valueForOption(KeyHideByName, to.hideByNameStyle);
from.valueForOption(KeyTopNonLiteral, to.showTopNonLiteralResults);
from.valueForOption(KeyAnnotatedDescription, to.annotatedDescription);
from.valueForOption(KeyIncludeObjectLiterals, to.includeObjectLiterals);
}

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

// Invoke completion.
if (!swiftCodeCompleteImpl(*this, inputBuf, offset, swiftConsumer,
extendedArgs, fileSystem,
CCOpts.annotatedDescription, error)) {
extendedArgs, fileSystem, CCOpts, error)) {
consumer.failed(error);
return;
}
Expand Down