Skip to content

[CodeCompletion] Escape declaration base name if needed #23372

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
41 changes: 33 additions & 8 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,32 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
llvm_unreachable("unhandled kind");
}

void addValueBaseName(CodeCompletionResultBuilder &Builder,
DeclBaseName Name) {
auto NameStr = Name.userFacingName();
bool shouldEscapeKeywords;
if (Name.isSpecial()) {
// Special names (i.e. 'init') are always displayed as its user facing
// name.
shouldEscapeKeywords = false;
} else if (ExprType) {
// After dot. User can write any keyword after '.' except for `init` and
// `self`. E.g. 'func `init`()' must be called by 'expr.`init`()'.
shouldEscapeKeywords = NameStr == "self" || NameStr == "init";
} else {
// As primary expresson. We have to escape almost every keywords except
// for 'self' and 'Self'.
shouldEscapeKeywords = NameStr != "self" && NameStr != "Self";
}

if (!shouldEscapeKeywords) {
Builder.addTextChunk(NameStr);
} else {
SmallString<16> buffer;
Builder.addTextChunk(Builder.escapeKeyword(NameStr, true, buffer));
}
}

void addLeadingDot(CodeCompletionResultBuilder &Builder) {
if (NeedOptionalUnwrap) {
Builder.setNumBytesToErase(NumBytesToEraseForOptionalUnwrap);
Expand Down Expand Up @@ -1995,7 +2021,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
VD->shouldHideFromEditor())
return;

StringRef Name = VD->getName().get();
Identifier Name = VD->getName();
assert(!Name.empty() && "name should not be empty");

CommandWordsPairs Pairs;
Expand All @@ -2005,15 +2031,15 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
getSemanticContext(VD, Reason), ExpectedTypes);
Builder.setAssociatedDecl(VD);
addLeadingDot(Builder);
Builder.addTextChunk(Name);
addValueBaseName(Builder, Name);
setClangDeclKeywords(VD, Pairs, Builder);

if (!VD->hasValidSignature())
return;

// Add a type annotation.
Type VarType = getTypeOfMember(VD);
if (VD->getName() != Ctx.Id_self && VD->isInOut()) {
if (Name != Ctx.Id_self && VD->isInOut()) {
// It is useful to show inout for function parameters.
// But for 'self' it is just noise.
VarType = InOutType::get(VarType);
Expand Down Expand Up @@ -2319,7 +2345,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
return;
foundFunction(FD);

StringRef Name = FD->getName().get();
Identifier Name = FD->getName();
assert(!Name.empty() && "name should not be empty");

Type FunctionType = getTypeOfMember(FD);
Expand Down Expand Up @@ -2350,7 +2376,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
setClangDeclKeywords(FD, Pairs, Builder);
Builder.setAssociatedDecl(FD);
addLeadingDot(Builder);
Builder.addTextChunk(Name);
addValueBaseName(Builder, Name);
if (IsDynamicLookup)
Builder.addDynamicLookupMethodCallTail();
else if (FD->getAttrs().hasAttribute<OptionalAttr>())
Expand Down Expand Up @@ -2680,8 +2706,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
Builder.setAssociatedDecl(EED);
setClangDeclKeywords(EED, Pairs, Builder);
addLeadingDot(Builder);

Builder.addTextChunk(EED->getName().str());
addValueBaseName(Builder, EED->getName());

// Enum element is of function type; (Self.type) -> Self or
// (Self.Type) -> (Args...) -> Self.
Expand Down Expand Up @@ -2759,7 +2784,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {

// Base name
addLeadingDot(Builder);
Builder.addTextChunk(AFD->getBaseName().userFacingName());
addValueBaseName(Builder, AFD->getBaseName());

// Add the argument labels.
auto ArgLabels = AFD->getFullName().getArgumentNames();
Expand Down
9 changes: 4 additions & 5 deletions lib/IDE/CodeCompletionResultBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,8 @@ class CodeCompletionResultBuilder {
addTypeAnnotation(Annotation);
}

StringRef escapeArgumentLabel(StringRef Word,
bool escapeAllKeywords,
llvm::SmallString<16> &EscapedKeyword) {
StringRef escapeKeyword(StringRef Word, bool escapeAllKeywords,
llvm::SmallString<16> &EscapedKeyword) {
bool shouldEscape = false;
if (escapeAllKeywords) {
#define KEYWORD(kw) .Case(#kw, true)
Expand Down Expand Up @@ -325,15 +324,15 @@ class CodeCompletionResultBuilder {
llvm::SmallString<16> EscapedKeyword;
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::CallParameterName,
escapeArgumentLabel(Name.str(), false, EscapedKeyword));
escapeKeyword(Name.str(), false, EscapedKeyword));
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::CallParameterColon, ": ");
} else if (!LocalName.empty()) {
// Use local (non-API) parameter name if we have nothing else.
llvm::SmallString<16> EscapedKeyword;
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::CallParameterInternalName,
escapeArgumentLabel(LocalName.str(), false, EscapedKeyword));
escapeKeyword(LocalName.str(), false, EscapedKeyword));
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::CallParameterColon, ": ");
}
Expand Down
97 changes: 97 additions & 0 deletions test/IDE/complete_escaped_keyword.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=STATIC_PRIMARY | %FileCheck %s -check-prefix=STATIC_PRIMARY
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=STATIC_SELF_NODOT | %FileCheck %s -check-prefix=STATIC_SELF_NODOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=STATIC_SELF_DOT | %FileCheck %s -check-prefix=STATIC_SELF_DOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=META_NODOT | %FileCheck %s -check-prefix=STATIC_SELF_NODOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=META_DOT | %FileCheck %s -check-prefix=STATIC_SELF_DOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INSTANCE_PRIMARY | %FileCheck %s -check-prefix=INSTANCE_PRIMARY
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INSTANCE_SELF_NODOT | %FileCheck %s -check-prefix=INSTANCE_SELF_NODOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INSTANCE_SELF_DOT | %FileCheck %s -check-prefix=INSTANCE_SELF_DOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=VALUE_NODOT | %FileCheck %s -check-prefix=INSTANCE_SELF_NODOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=VALUE_DOT | %FileCheck %s -check-prefix=INSTANCE_SELF_DOT


enum MyEnum {
case `class`(struct: String)
case `let`(`var`: String)

init(`init`: String) {}
static func `public`(private: String) -> Int {}

func `init`(deinit: String) -> Int {}
func `if`(else: String) -> Int {}

var `self`: Int { return 0 }

static func testStatic(meta: MyEnum.Type) {
let _ = #^STATIC_PRIMARY^#
// STATIC_PRIMARY: Begin completion
// STATIC_PRIMARY-DAG: Decl[LocalVar]/Local: self[#MyEnum.Type#]; name=self
// STATIC_PRIMARY-DAG: Decl[EnumElement]/CurrNominal: `class`({#struct: String#})[#MyEnum#]; name=`class`(struct: String)
// STATIC_PRIMARY-DAG: Decl[EnumElement]/CurrNominal: `let`({#`var`: String#})[#MyEnum#]; name=`let`(`var`: String)
// STATIC_PRIMARY-DAG: Decl[StaticMethod]/CurrNominal: `public`({#private: String#})[#Int#]; name=`public`(private: String)
// STATIC_PRIMARY-DAG: Decl[InstanceMethod]/CurrNominal: `init`({#(self): MyEnum#})[#(deinit: String) -> Int#]; name=`init`(self: MyEnum)
// STATIC_PRIMARY-DAG: Decl[InstanceMethod]/CurrNominal: `if`({#(self): MyEnum#})[#(else: String) -> Int#]; name=`if`(self: MyEnum)
// STATIC_PRIMARY: End completion

let _ = self#^STATIC_SELF_NODOT^#
// STATIC_SELF_NODOT: Begin completions
// STATIC_SELF_NODOT-DAG: Keyword[self]/CurrNominal: .self[#MyEnum.Type#]; name=self
// STATIC_SELF_NODOT-DAG: Decl[EnumElement]/CurrNominal: .class({#struct: String#})[#MyEnum#]; name=class(struct: String)
// STATIC_SELF_NODOT-DAG: Decl[EnumElement]/CurrNominal: .let({#`var`: String#})[#MyEnum#]; name=let(`var`: String)
// STATIC_SELF_NODOT-DAG: Decl[Constructor]/CurrNominal: .init({#init: String#})[#MyEnum#]; name=init(init: String)
// STATIC_SELF_NODOT-DAG: Decl[StaticMethod]/CurrNominal: .public({#private: String#})[#Int#]; name=public(private: String)
// STATIC_SELF_NODOT-DAG: Decl[InstanceMethod]/CurrNominal: .`init`({#(self): MyEnum#})[#(deinit: String) -> Int#]; name=`init`(self: MyEnum)
// STATIC_SELF_NODOT-DAG: Decl[InstanceMethod]/CurrNominal: .if({#(self): MyEnum#})[#(else: String) -> Int#]; name=if(self: MyEnum)
// STATIC_SELF_NODOT: End completion

let _ = self.#^STATIC_SELF_DOT^#
// STATIC_SELF_DOT: Begin completions
// STATIC_SELF_DOT-DAG: Keyword[self]/CurrNominal: self[#MyEnum.Type#]; name=self
// STATIC_SELF_DOT-DAG: Decl[EnumElement]/CurrNominal: class({#struct: String#})[#MyEnum#]; name=class(struct: String)
// STATIC_SELF_DOT-DAG: Decl[EnumElement]/CurrNominal: let({#`var`: String#})[#MyEnum#]; name=let(`var`: String)
// STATIC_SELF_DOT-DAG: Decl[Constructor]/CurrNominal: init({#init: String#})[#MyEnum#]; name=init(init: String)
// STATIC_SELF_DOT-DAG: Decl[StaticMethod]/CurrNominal: public({#private: String#})[#Int#]; name=public(private: String)
// STATIC_SELF_DOT-DAG: Decl[InstanceMethod]/CurrNominal: `init`({#(self): MyEnum#})[#(deinit: String) -> Int#]; name=`init`(self: MyEnum)
// STATIC_SELF_DOT-DAG: Decl[InstanceMethod]/CurrNominal: if({#(self): MyEnum#})[#(else: String) -> Int#]; name=if(self: MyEnum)
// STATIC_SELF_DOT: End completion

let _ = meta#^META_NODOT^#
// SAME AS 'STATIC_SELF_NODOT'.

let _ = meta.#^META_DOT^#
// SAME AS 'STATIC_SELF_DOT'.
}

func testInstance(val: MyEnum) {
let _ = #^INSTANCE_PRIMARY^#
// INSTANCE_PRIMARY: Begin completion
// INSTANCE_PRIMARY-DAG: Decl[LocalVar]/Local: self[#MyEnum#]; name=self
// INSTANCE_PRIMARY-DAG: Decl[InstanceVar]/CurrNominal: self[#Int#]; name=self
// FIXME: ^ This is shadowed. We should hide this.
// INSTANCE_PRIMARY-DAG: Decl[InstanceMethod]/CurrNominal: `init`({#deinit: String#})[#Int#]; name=`init`(deinit: String)
// INSTANCE_PRIMARY-DAG: Decl[InstanceMethod]/CurrNominal: `if`({#else: String#})[#Int#]; name=`if`(else: String)
// INSTANCE_PRIMARY: End completion

let _ = self#^INSTANCE_SELF_NODOT^#
// INSTANCE_SELF_NODOT: Begin completions
// INSTANCE_SELF_NODOT-DAG: Decl[InstanceMethod]/CurrNominal: .`init`({#deinit: String#})[#Int#]; name=`init`(deinit: String)
// INSTANCE_SELF_NODOT-DAG: Decl[InstanceMethod]/CurrNominal: .if({#else: String#})[#Int#]; name=if(else: String)
// INSTANCE_SELF_NODOT-DAG: Decl[InstanceVar]/CurrNominal: .`self`[#Int#]; name=`self`
// INSTANCE_SELF_NODOT-DAG: Keyword[self]/CurrNominal: .self[#MyEnum#]; name=self

// INSTANCE_SELF_NODOT: End completions

let _ = self.#^INSTANCE_SELF_DOT^#
// INSTANCE_SELF_DOT: Begin completions
// INSTANCE_SELF_DOT-DAG: Decl[InstanceMethod]/CurrNominal: `init`({#deinit: String#})[#Int#]; name=`init`(deinit: String)
// INSTANCE_SELF_DOT-DAG: Decl[InstanceMethod]/CurrNominal: if({#else: String#})[#Int#]; name=if(else: String)
// INSTANCE_SELF_DOT-DAG: Decl[InstanceVar]/CurrNominal: `self`[#Int#]; name=`self`
// INSTANCE_SELF_DOT-DAG: Keyword[self]/CurrNominal: self[#MyEnum#]; name=self
// INSTANCE_SELF_DOT: End completions

let _ = val#^VALUE_NODOT^#
// SAME AS 'INSTANCE_SELF_NODOT'.
let _ = val.#^VALUE_DOT^#
// SAME AS 'INSTANCE_SELF_DOT'.
}
}