Skip to content

[SourceKit] Add completion kind field to completion response #25913

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
16 changes: 16 additions & 0 deletions test/SourceKit/CodeComplete/complete_unresolvedmember.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
enum Foo {
case east, west
case other(String)
init(i: Int) {}
init(s: String) {}
static var instance: Foo { .east }
static func create() -> Foo { .west }
}

func test() -> Foo {
return .
}

// RUN: %sourcekitd-test -req=complete -pos=11:11 %s -- %s > %t.response
// RUN: diff -u %s.response %t.response

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
key.results: [
{
key.kind: source.lang.swift.decl.function.method.class,
key.name: "create()",
key.sourcetext: "create()",
key.description: "create()",
key.typename: "Foo",
key.context: source.codecompletion.context.thisclass,
key.num_bytes_to_erase: 0,
key.associated_usrs: "s:25complete_unresolvedmember3FooO6createACyFZ",
key.modulename: "complete_unresolvedmember"
},
{
key.kind: source.lang.swift.decl.enumelement,
key.name: "east",
key.sourcetext: "east",
key.description: "east",
key.typename: "Foo",
key.context: source.codecompletion.context.exprspecific,
key.num_bytes_to_erase: 0,
key.associated_usrs: "s:25complete_unresolvedmember3FooO4eastyA2CmF",
key.modulename: "complete_unresolvedmember"
},
{
key.kind: source.lang.swift.decl.function.constructor,
key.name: "init(i:)",
key.sourcetext: "init(i: <#T##Int#>)",
key.description: "init(i: Int)",
key.typename: "Foo",
key.context: source.codecompletion.context.thisclass,
key.num_bytes_to_erase: 0,
key.associated_usrs: "s:25complete_unresolvedmember3FooO1iACSi_tcfc",
key.modulename: "complete_unresolvedmember"
},
{
key.kind: source.lang.swift.decl.function.constructor,
key.name: "init(s:)",
key.sourcetext: "init(s: <#T##String#>)",
key.description: "init(s: String)",
key.typename: "Foo",
key.context: source.codecompletion.context.thisclass,
key.num_bytes_to_erase: 0,
key.associated_usrs: "s:25complete_unresolvedmember3FooO1sACSS_tcfc",
key.modulename: "complete_unresolvedmember"
},
{
key.kind: source.lang.swift.decl.var.class,
key.name: "instance",
key.sourcetext: "instance",
key.description: "instance",
key.typename: "Foo",
key.context: source.codecompletion.context.thisclass,
key.num_bytes_to_erase: 0,
key.associated_usrs: "s:25complete_unresolvedmember3FooO8instanceACvpZ",
key.modulename: "complete_unresolvedmember"
},
{
key.kind: source.lang.swift.decl.enumelement,
key.name: "other()",
key.sourcetext: "other(<#T##String#>)",
key.description: "other(String)",
key.typename: "Foo",
key.context: source.codecompletion.context.exprspecific,
key.num_bytes_to_erase: 0,
key.associated_usrs: "s:25complete_unresolvedmember3FooO5otheryACSScACmF",
key.modulename: "complete_unresolvedmember"
},
{
key.kind: source.lang.swift.decl.enumelement,
key.name: "west",
key.sourcetext: "west",
key.description: "west",
key.typename: "Foo",
key.context: source.codecompletion.context.exprspecific,
key.num_bytes_to_erase: 0,
key.associated_usrs: "s:25complete_unresolvedmember3FooO4westyA2CmF",
key.modulename: "complete_unresolvedmember"
}
],
key.kind: source.lang.swift.completion.unresolvedmember
}
1 change: 1 addition & 0 deletions tools/SourceKit/include/SourceKit/Core/LangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class CodeCompletionConsumer {

virtual void failed(StringRef ErrDescription) = 0;

virtual void setCompletionKind(UIdent kind) {};
virtual bool handleResult(const CodeCompletionInfo &Info) = 0;
};

Expand Down
18 changes: 18 additions & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ struct SwiftCodeCompletionConsumer
};
} // anonymous namespace

/// Returns completion context kind \c UIdent to report to the client.
/// For now, only returns "unresolved member" kind because others are unstable.
/// Returns invalid \c UIents other cases.
static UIdent getUIDForCodeCompletionKindToReport(CompletionKind kind) {
switch (kind) {
case CompletionKind::UnresolvedMember:
return UIdent("source.lang.swift.completion.unresolvedmember");
default:
return UIdent();
}
}

static bool swiftCodeCompleteImpl(SwiftLangSupport &Lang,
llvm::MemoryBuffer *UnresolvedInputFile,
unsigned Offset,
Expand Down Expand Up @@ -205,6 +217,12 @@ void SwiftLangSupport::codeComplete(llvm::MemoryBuffer *UnresolvedInputFile,
SwiftCodeCompletionConsumer SwiftConsumer([&](
MutableArrayRef<CodeCompletionResult *> Results,
SwiftCompletionInfo &info) {

auto kind = getUIDForCodeCompletionKindToReport(
info.completionContext->CodeCompletionKind);
if (kind.isValid())
SKConsumer.setCompletionKind(kind);

bool hasRequiredType = info.completionContext->typeContextKind == TypeContextKind::Required;
CodeCompletionContext::sortCompletionResults(Results);
// FIXME: this adhoc filtering should be configurable like it is in the
Expand Down
6 changes: 6 additions & 0 deletions tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,7 @@ class SKCodeCompletionConsumer : public CodeCompletionConsumer {

void failed(StringRef ErrDescription) override;

void setCompletionKind(UIdent kind) override;
bool handleResult(const CodeCompletionInfo &Info) override;
};
} // end anonymous namespace
Expand All @@ -1853,6 +1854,11 @@ void SKCodeCompletionConsumer::failed(StringRef ErrDescription) {
ErrorDescription = ErrDescription;
}

void SKCodeCompletionConsumer::setCompletionKind(UIdent kind) {
assert(kind.isValid());
RespBuilder.getDictionary().set(KeyKind, kind);
}

bool SKCodeCompletionConsumer::handleResult(const CodeCompletionInfo &R) {
Optional<StringRef> ModuleNameOpt;
if (!R.ModuleName.empty())
Expand Down