Skip to content

[CodeCompletion] Always print argument ':' in filterName #72887

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 1 commit into from
Apr 16, 2024
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
74 changes: 36 additions & 38 deletions lib/IDE/CodeCompletionResultBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,60 +234,58 @@ void CodeCompletionResultBuilder::setAssociatedDecl(const Decl *D) {
void CodeCompletionResultBuilder::addCallArgument(
Identifier Name, Identifier LocalName, Type Ty, Type ContextTy,
bool IsVarArg, bool IsInOut, bool IsIUO, bool IsAutoClosure,
bool UseUnderscoreLabel, bool IsLabeledTrailingClosure, bool HasDefault) {
bool IsLabeledTrailingClosure, bool IsForOperator, bool HasDefault) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UseUnderscoreLabel was meant to indicate "is labeled trailing closure" when it's introduced 😅

++CurrentNestingLevel;
using ChunkKind = CodeCompletionString::Chunk::ChunkKind;

addSimpleChunk(ChunkKind::CallArgumentBegin);

if (shouldAnnotateResults()) {
if (!Name.empty() || !LocalName.empty()) {
llvm::SmallString<16> EscapedKeyword;

if (!Name.empty()) {
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::CallArgumentName,
escapeKeyword(Name.str(), false, EscapedKeyword));

if (!LocalName.empty() && Name != LocalName) {
addChunkWithTextNoCopy(ChunkKind::Text, " ");
getLastChunk().setIsAnnotation();
addChunkWithText(
ChunkKind::CallArgumentInternalName,
escapeKeyword(LocalName.str(), false, EscapedKeyword));
getLastChunk().setIsAnnotation();
}
} else {
assert(!LocalName.empty());
addChunkWithTextNoCopy(ChunkKind::CallArgumentName, "_");
getLastChunk().setIsAnnotation();
llvm::SmallString<16> EscapedKeyword;
if (!Name.empty()) {
addChunkWithText(ChunkKind::CallArgumentName,
escapeKeyword(Name.str(), false, EscapedKeyword));
if (!LocalName.empty() && Name != LocalName) {
addChunkWithTextNoCopy(ChunkKind::Text, " ");
getLastChunk().setIsAnnotation();
addChunkWithText(ChunkKind::CallArgumentInternalName,
escapeKeyword(LocalName.str(), false, EscapedKeyword));
getLastChunk().setIsAnnotation();
}
addChunkWithTextNoCopy(ChunkKind::CallArgumentColon, ": ");
} else if (!LocalName.empty()) {
addChunkWithTextNoCopy(ChunkKind::CallArgumentName, "_");
getLastChunk().setIsAnnotation();
addChunkWithTextNoCopy(ChunkKind::Text, " ");
getLastChunk().setIsAnnotation();
addChunkWithText(ChunkKind::CallArgumentInternalName,
escapeKeyword(LocalName.str(), false, EscapedKeyword));
addChunkWithTextNoCopy(ChunkKind::CallArgumentColon, ": ");
} else if (!IsForOperator) {
addChunkWithTextNoCopy(ChunkKind::CallArgumentName, "_");
if (!IsLabeledTrailingClosure)
getLastChunk().setIsAnnotation();
addChunkWithTextNoCopy(ChunkKind::CallArgumentColon, ": ");
if (!IsLabeledTrailingClosure)
getLastChunk().setIsAnnotation();
}
} else {
llvm::SmallString<16> stash;
ChunkKind nameKind;
StringRef nameStr;
if (!Name.empty()) {
llvm::SmallString<16> EscapedKeyword;
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::CallArgumentName,
escapeKeyword(Name.str(), false, EscapedKeyword));
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::CallArgumentColon, ": ");
} else if (UseUnderscoreLabel) {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::CallArgumentName, "_");
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::CallArgumentColon, ": ");
nameKind = ChunkKind::CallArgumentName;
nameStr = escapeKeyword(Name.str(), false, stash);
} else if (IsLabeledTrailingClosure) {
nameKind = ChunkKind::CallArgumentName;
nameStr = "_";
} else if (!LocalName.empty()) {
// Use local (non-API) parameter name if we have nothing else.
llvm::SmallString<16> EscapedKeyword;
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::CallArgumentInternalName,
escapeKeyword(LocalName.str(), false, EscapedKeyword));
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::CallArgumentColon, ": ");
nameKind = ChunkKind::CallArgumentInternalName;
nameStr = escapeKeyword(LocalName.str(), false, stash);
}
if (!nameStr.empty()) {
addChunkWithText(nameKind, nameStr);
addChunkWithTextNoCopy(ChunkKind::CallArgumentColon, ": ");
}
}

Expand Down
12 changes: 7 additions & 5 deletions lib/IDE/CodeCompletionResultBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,16 @@ class CodeCompletionResultBuilder {

void addCallArgument(Identifier Name, Identifier LocalName, Type Ty,
Type ContextTy, bool IsVarArg, bool IsInOut, bool IsIUO,
bool IsAutoClosure, bool UseUnderscoreLabel,
bool IsLabeledTrailingClosure, bool HasDefault);
bool IsAutoClosure, bool IsLabeledTrailingClosure,
bool IsForOperator, bool HasDefault);

void addCallArgument(Identifier Name, Type Ty, Type ContextTy = Type()) {
void addCallArgument(Identifier Name, Type Ty, Type ContextTy = Type(),
bool IsForOperator = false) {
addCallArgument(Name, Identifier(), Ty, ContextTy,
/*IsVarArg=*/false, /*IsInOut=*/false, /*IsIUO=*/false,
/*IsAutoClosure=*/false, /*UseUnderscoreLabel=*/false,
/*IsLabeledTrailingClosure=*/false, /*HasDefault=*/false);
/*IsAutoClosure=*/false,
/*IsLabeledTrailingClosure=*/false, IsForOperator,
/*HasDefault=*/false);
}

void addGenericParameter(StringRef Name) {
Expand Down
3 changes: 1 addition & 2 deletions lib/IDE/CodeCompletionResultPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,7 @@ static void printCodeCompletionResultFilterName(
case ChunkKind::CallArgumentColon:
case ChunkKind::ParameterDeclColon:
// Since we don't add the type, also don't add the space after ':'.
if (shouldPrint)
OS << ":";
OS << ":";
++i;
continue;
case ChunkKind::Text:
Expand Down
15 changes: 9 additions & 6 deletions lib/IDE/CompletionLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,8 +1073,8 @@ bool CompletionLookup::addCallArgumentPatterns(
Builder.addCallArgument(argName, bodyName,
eraseArchetypes(paramTy, genericSig), contextTy,
isVariadic, isInOut, isIUO, isAutoclosure,
/*UseUnderscoreLabel=*/false,
/*IsLabeledTrailingClosure=*/false, hasDefault);
/*IsLabeledTrailingClosure=*/false,
/*IsForOperator=*/false, hasDefault);

modifiedBuilder = true;
needComma = true;
Expand Down Expand Up @@ -2560,7 +2560,8 @@ void CompletionLookup::addAssignmentOperator(Type RHSType) {
Type contextTy;
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
contextTy = typeContext->getDeclaredTypeInContext();
builder.addCallArgument(Identifier(), RHSType, contextTy);
builder.addCallArgument(Identifier(), RHSType, contextTy,
/*IsForOperator=*/true);
}

void CompletionLookup::addInfixOperatorCompletion(OperatorDecl *op,
Expand All @@ -2584,7 +2585,8 @@ void CompletionLookup::addInfixOperatorCompletion(OperatorDecl *op,
Type contextTy;
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
contextTy = typeContext->getDeclaredTypeInContext();
builder.addCallArgument(Identifier(), RHSType, contextTy);
builder.addCallArgument(Identifier(), RHSType, contextTy,
/*IsForOperator=*/true);
}
if (resultType)
addTypeAnnotation(builder, resultType);
Expand Down Expand Up @@ -2913,8 +2915,9 @@ void CompletionLookup::addCallArgumentCompletionResults(
Builder.addCallArgument(Arg->getLabel(), Identifier(), Arg->getPlainType(),
ContextType, Arg->isVariadic(), Arg->isInOut(),
/*IsIUO=*/false, Arg->isAutoClosure(),
/*UseUnderscoreLabel=*/true,
isLabeledTrailingClosure, /*HasDefault=*/false);
isLabeledTrailingClosure,
/*IsForOperator=*/false,
/*HasDefault=*/false);
Builder.addFlair(CodeCompletionFlairBit::ArgumentLabels);
auto Ty = Arg->getPlainType();
if (Arg->isInOut()) {
Expand Down
13 changes: 10 additions & 3 deletions test/IDE/complete_annotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

struct MyStruct {
init(x: Int) {}
init<T, U>(_: T.Type, _: U.Type, value: Int = 1) {}
var propNormal: Int { fatalError() }
var propFunction: () -> MyStruct { fatalError() }
func labelNameParamName(label param: (inout Int) throws -> MyStruct) rethrows {}
func labelName(label: (@autoclosure () -> Int) -> Int) {}
func sameName(label label: inout Int) {}
func paramName(_ param: Int) {}
func emptyName<T>(_: T.Type) {}
subscript(param: Int) -> Int { 1 }
subscript(label param: Int) -> Int { 1 }

Expand Down Expand Up @@ -54,19 +56,20 @@ func testType(value: #^GLOBAL_TYPE^#) {}
func testMember(value: MyStruct) {
value.#^EXPR_MEMBER^#
}
// EXPR_MEMBER: Begin completions, 7 items
// EXPR_MEMBER: Begin completions, 8 items
// EXPR_MEMBER-DAG: Keyword[self]/CurrNominal: <keyword>self</keyword>; typename=<typeid.user>MyStruct</typeid.user>;
// EXPR_MEMBER-DAG: Decl[InstanceVar]/CurrNominal: <name>propNormal</name>; typename=<typeid.sys>Int</typeid.sys>;
// EXPR_MEMBER-DAG: Decl[InstanceVar]/CurrNominal: <name>propFunction</name>; typename=() -&gt; <typeid.user>MyStruct</typeid.user>;
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelNameParamName</name>(<callarg><callarg.label>label</callarg.label> <callarg.param>param</callarg.param>: <callarg.type>(<keyword>inout</keyword> <typeid.sys>Int</typeid.sys>) <keyword>throws</keyword> -&gt; <typeid.user>MyStruct</typeid.user></callarg.type></callarg>) <keyword>rethrows</keyword>; typename=<typeid.sys>Void</typeid.sys>;
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelName</name>(<callarg><callarg.label>label</callarg.label>: <callarg.type>(<attribute>@autoclosure</attribute> () -&gt; <typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Int</typeid.sys></callarg.type></callarg>); typename=<typeid.sys>Void</typeid.sys>;
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>sameName</name>(<callarg><callarg.label>label</callarg.label>: &amp;<callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>); typename=<typeid.sys>Void</typeid.sys>;
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>paramName</name>(<callarg><callarg.label>_</callarg.label> <callarg.param>param</callarg.param>: <callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>); typename=<typeid.sys>Void</typeid.sys>;
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>emptyName</name>(<callarg><callarg.label>_</callarg.label>: <callarg.type><typeid.user>T</typeid.user>.Type</callarg.type></callarg>); typename=<typeid.sys>Void</typeid.sys>; name=emptyName(:); sourcetext=emptyName(<#T##T.Type#>)

func testPostfix(value: MyStruct) {
value #^EXPR_POSTFIX^#
}
// EXPR_POSTFIX: Begin completions, 10 items
// EXPR_POSTFIX: Begin completions, 11 items
// EXPR_POSTFIX-DAG: Decl[InstanceVar]/CurrNominal: <name>propNormal</name>; typename=<typeid.sys>Int</typeid.sys>;
// EXPR_POSTFIX-DAG: Decl[InstanceVar]/CurrNominal: <name>propFunction</name>; typename=() -&gt; <typeid.user>MyStruct</typeid.user>;
// EXPR_POSTFIX-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelNameParamName</name>(<callarg><callarg.label>label</callarg.label> <callarg.param>param</callarg.param>: <callarg.type>(<keyword>inout</keyword> <typeid.sys>Int</typeid.sys>) <keyword>throws</keyword> -&gt; <typeid.user>MyStruct</typeid.user></callarg.type></callarg>) <keyword>rethrows</keyword>; typename=<typeid.sys>Void</typeid.sys>;
Expand All @@ -77,17 +80,21 @@ func testPostfix(value: MyStruct) {
// EXPR_POSTFIX-DAG: Decl[Subscript]/CurrNominal: [<callarg><callarg.label>label</callarg.label> <callarg.param>param</callarg.param>: <callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>]; typename=<typeid.sys>Int</typeid.sys>;
// EXPR_POSTFIX-DAG: Keyword[self]/CurrNominal: <keyword>self</keyword>; typename=<typeid.user>MyStruct</typeid.user>;
// EXPR_POSTFIX-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]/IsSystem: <name>+</name>; typename=<typeid.user>MyStruct</typeid.user>;
// EXPR_POSTFIX-DAG: Decl[InstanceMethod]/CurrNominal: <name>emptyName</name>(<callarg><callarg.label>_</callarg.label>: <callarg.type><typeid.user>T</typeid.user>.Type</callarg.type></callarg>); typename=<typeid.sys>Void</typeid.sys>; name=emptyName(:); sourcetext=.emptyName(<#T##T.Type#>)

func testImplicitMember() -> MyStruct {
return .#^EXPR_IMPLICITMEMBER^#
}
// EXPR_IMPLICITMEMBER: Begin completions, 7 items
// EXPR_IMPLICITMEMBER: Begin completions, 10 items
// EXPR_IMPLICITMEMBER-DAG: Decl[Constructor]/CurrNominal/TypeRelation[Convertible]: <name>init</name>(<callarg><callarg.label>x</callarg.label>: <callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>); typename=<typeid.user>MyStruct</typeid.user>;
// EXPR_IMPLICITMEMBER-DAG: Decl[Constructor]/CurrNominal/TypeRelation[Convertible]: <name>init</name>(<callarg><callarg.label>_</callarg.label>: <callarg.type><typeid.user>T</typeid.user>.Type</callarg.type></callarg>, <callarg><callarg.label>_</callarg.label>: <callarg.type><typeid.user>U</typeid.user>.Type</callarg.type></callarg>); typename=<typeid.user>MyStruct</typeid.user>; name=init(::); sourcetext=init(<#T##T.Type#>, <#T##U.Type#>)
// EXPR_IMPLICITMEMBER-DAG: Decl[Constructor]/CurrNominal/TypeRelation[Convertible]: <name>init</name>(<callarg><callarg.label>_</callarg.label>: <callarg.type><typeid.user>T</typeid.user>.Type</callarg.type></callarg>, <callarg><callarg.label>_</callarg.label>: <callarg.type><typeid.user>U</typeid.user>.Type</callarg.type></callarg>, <callarg><callarg.label>value</callarg.label>: <callarg.type><typeid.sys>Int</typeid.sys></callarg.type><callarg.default/></callarg>); typename=<typeid.user>MyStruct</typeid.user>; name=init(::value:); sourcetext=init(<#T##T.Type#>, <#T##U.Type#>, value: <#T##Int#>)
// EXPR_IMPLICITMEMBER-DAG: Decl[StaticVar]/CurrNominal/Flair[ExprSpecific]/TypeRelation[Convertible]: <name>instance</name>; typename=<typeid.user>MyStruct</typeid.user>;
// EXPR_IMPLICITMEMBER-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Invalid]: <name>labelNameParamName</name>(<callarg><callarg.label>_</callarg.label> <callarg.param>self</callarg.param>: <callarg.type><typeid.user>MyStruct</typeid.user></callarg.type></callarg>); typename=(label: (<keyword>inout</keyword> <typeid.sys>Int</typeid.sys>) <keyword>throws</keyword> -&gt; <typeid.user>MyStruct</typeid.user>) -&gt; <typeid.sys>Void</typeid.sys>;
// EXPR_IMPLICITMEMBER-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Invalid]: <name>labelName</name>(<callarg><callarg.label>_</callarg.label> <callarg.param>self</callarg.param>: <callarg.type><typeid.user>MyStruct</typeid.user></callarg.type></callarg>); typename=(label: (<attribute>@autoclosure</attribute> () -&gt; <typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Void</typeid.sys>;
// EXPR_IMPLICITMEMBER-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Invalid]: <name>sameName</name>(<callarg><callarg.label>_</callarg.label> <callarg.param>self</callarg.param>: <callarg.type><typeid.user>MyStruct</typeid.user></callarg.type></callarg>); typename=(label: <keyword>inout</keyword> <typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Void</typeid.sys>;
// EXPR_IMPLICITMEMBER-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Invalid]: <name>paramName</name>(<callarg><callarg.label>_</callarg.label> <callarg.param>self</callarg.param>: <callarg.type><typeid.user>MyStruct</typeid.user></callarg.type></callarg>); typename=(<typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Void</typeid.sys>;
// EXPR_IMPLICITMEMBER-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Invalid]: <name>emptyName</name>(<callarg><callarg.label>_</callarg.label> <callarg.param>self</callarg.param>: <callarg.type><typeid.user>MyStruct</typeid.user></callarg.type></callarg>); typename=(<typeid.user>T</typeid.user>.Type) -&gt; <typeid.sys>Void</typeid.sys>; name=emptyName(:); sourcetext=emptyName(<#T##self: MyStruct##MyStruct#>)
// EXPR_IMPLICITMEMBER-DAG: Decl[StaticMethod]/CurrNominal/Flair[ExprSpecific]/TypeRelation[Convertible]: <name>create</name>(<callarg><callarg.label>x</callarg.label>: <callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>); typename=<typeid.user>MyStruct</typeid.user>;

func testArgument() -> MyStruct {
Expand Down