Skip to content

[code-completion] Add filter rules for description in addition filter-name #10301

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
Jun 16, 2017
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
10 changes: 10 additions & 0 deletions test/SourceKit/CodeComplete/Inputs/filter-rules/hideDesc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
key.kind: source.codecompletion.description,
key.hide: 1,
key.names: [
"over(Int)",
"[Int]"
]
}
]
16 changes: 16 additions & 0 deletions test/SourceKit/CodeComplete/Inputs/filter-rules/showDesc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
key.kind: source.codecompletion.identifier,
key.hide: 1,
key.names: [
"[]"
]
},
{
key.kind: source.codecompletion.description,
key.hide: 0,
key.names: [
"[Float]"
]
}
]
47 changes: 47 additions & 0 deletions test/SourceKit/CodeComplete/complete_filter_rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,50 @@ func testHideOp10() {
struct local {}
local#^HIDE_OP_10^#
}

// RUN: %complete-test -filter-rules=%S/Inputs/filter-rules/hideDesc.json -tok=HIDE_DESC_1 %s -- -F %S/../Inputs/libIDE-mock-sdk | %FileCheck %s -check-prefix=HIDE_DESC_1
func testHideDesc1() {
struct Local {
func over(_: Int) {}
func over(_: Float) {}
}

Local().#^HIDE_DESC_1^#
}
// HIDE_DESC_1-NOT: over
// HIDE_DESC_1: over(Float)
// HIDE_DESC_1-NOT: over

// RUN: %complete-test -filter-rules=%S/Inputs/filter-rules/hideDesc.json -tok=HIDE_DESC_2 %s -- -F %S/../Inputs/libIDE-mock-sdk | %FileCheck %s -check-prefix=HIDE_DESC_2
// RUN: %complete-test -filter-rules=%S/Inputs/filter-rules/hideDesc.json -tok=HIDE_DESC_3 %s -- -F %S/../Inputs/libIDE-mock-sdk | %FileCheck %s -check-prefix=HIDE_DESC_2
func testHideDesc2() {
struct Local {
subscript(_: Int) -> Int { return 0 }
subscript(_: Float) -> Float { return 0.0 }
}

Local()#^HIDE_DESC_2^#

let local = Local()
#^HIDE_DESC_3,local^#
}
// HIDE_DESC_2-NOT: [Int]
// HIDE_DESC_2: [Float]
// HIDE_DESC_2-NOT: [Int]

// RUN: %complete-test -filter-rules=%S/Inputs/filter-rules/showDesc.json -tok=SHOW_DESC_2 %s -- -F %S/../Inputs/libIDE-mock-sdk | %FileCheck %s -check-prefix=SHOW_DESC_2
// RUN: %complete-test -filter-rules=%S/Inputs/filter-rules/showDesc.json -tok=SHOW_DESC_3 %s -- -F %S/../Inputs/libIDE-mock-sdk | %FileCheck %s -check-prefix=SHOW_DESC_2
func testHideDesc2() {
struct Local {
subscript(_: Int) -> Int { return 0 }
subscript(_: Float) -> Float { return 0.0 }
}

Local()#^SHOW_DESC_2^#

let local = Local()
#^SHOW_DESC_3,local^#
}
// SHOW_DESC_2-NOT: [Int]
// SHOW_DESC_2: [Float]
// SHOW_DESC_2-NOT: [Int]
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 @@ -153,6 +153,7 @@ struct FilterRule {
Literal,
CustomCompletion,
Identifier,
Description,
};
Kind kind;
bool hide;
Expand Down
10 changes: 5 additions & 5 deletions tools/SourceKit/lib/SwiftLang/CodeCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ struct FilterRules {
// FIXME: hide individual custom completions

llvm::StringMap<bool> hideModule;
llvm::StringMap<bool> hideByName;
llvm::StringMap<bool> hideByFilterName;
llvm::StringMap<bool> hideByDescription;

bool hideCompletion(Completion *completion) const;
bool hideCompletion(SwiftResult *completion,
StringRef name,
void *customKind = nullptr) const;
bool hideName(StringRef name) const;
bool hideCompletion(SwiftResult *completion, StringRef name,
StringRef description, void *customKind = nullptr) const;
bool hideFilterName(StringRef name) const;
};

} // end namespace CodeCompletion
Expand Down
28 changes: 19 additions & 9 deletions tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,22 +447,32 @@ static bool isHighPriorityKeyword(CodeCompletionKeywordKind kind) {
}
}

bool FilterRules::hideName(StringRef name) const {
auto I = hideByName.find(name);
if (I != hideByName.end())
return I->getValue();
bool FilterRules::hideFilterName(StringRef name) const {
auto I = hideByFilterName.find(name);
if (I != hideByFilterName.end())
return I->getValue();
return hideAll;
}

bool FilterRules::hideCompletion(Completion *completion) const {
return hideCompletion(completion, completion->getName(), completion->getCustomKind());
return hideCompletion(completion, completion->getName(),
completion->getDescription(),
completion->getCustomKind());
}

bool FilterRules::hideCompletion(SwiftResult *completion, StringRef name, void *customKind) const {
bool FilterRules::hideCompletion(SwiftResult *completion, StringRef filterName,
StringRef description,
void *customKind) const {

if (!description.empty()) {
auto I = hideByDescription.find(description);
if (I != hideByDescription.end())
return I->getValue();
}

if (!name.empty()) {
auto I = hideByName.find(name);
if (I != hideByName.end())
if (!filterName.empty()) {
auto I = hideByFilterName.find(filterName);
if (I != hideByFilterName.end())
return I->getValue();
}

Expand Down
31 changes: 21 additions & 10 deletions tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,12 @@ static void translateFilterRules(ArrayRef<FilterRule> rawFilterRules,
// Note: name is null-terminated.
if (canonicalizeFilterName(name.data(), canonName))
continue;
filterRules.hideByName[canonName] = rule.hide;
filterRules.hideByFilterName[canonName] = rule.hide;
}
break;
case FilterRule::Description:
for (auto name : rule.names) {
filterRules.hideByDescription[name] = rule.hide;
}
break;
case FilterRule::Module:
Expand Down Expand Up @@ -955,13 +960,19 @@ filterInnerResults(ArrayRef<Result *> results, bool includeInner,
if (!includeInnerOperators && result->isOperator())
continue;

llvm::SmallString<64> name;
llvm::SmallString<64> filterName;
{
llvm::raw_svector_ostream OSS(name);
llvm::raw_svector_ostream OSS(filterName);
CodeCompletion::CompletionBuilder::getFilterName(
result->getCompletionString(), OSS);
}
if (rules.hideCompletion(result, name))
llvm::SmallString<64> description;
{
llvm::raw_svector_ostream OSS(description);
CodeCompletion::CompletionBuilder::getDescription(
result, OSS, /*leadingPunctuation=*/false);
}
if (rules.hideCompletion(result, filterName, description))
continue;

bool inner = checkInnerResult(result, hasDot, hasQDot, hasInit);
Expand Down Expand Up @@ -1040,11 +1051,11 @@ static void transformAndForwardResults(
options.addInnerOperators, hasDot, hasQDot, hasInit,
rules);
if (options.addInnerOperators) {
if (hasInit && !rules.hideName("("))
if (hasInit && !rules.hideFilterName("("))
innerResults.insert(innerResults.begin(), buildParen());
if (hasDot && !rules.hideName("."))
if (hasDot && !rules.hideFilterName("."))
innerResults.insert(innerResults.begin(), buildDot());
if (hasQDot && !rules.hideName("?."))
if (hasQDot && !rules.hideFilterName("?."))
innerResults.insert(innerResults.begin(), buildQDot());
}

Expand Down Expand Up @@ -1095,11 +1106,11 @@ static void transformAndForwardResults(
}

if (options.addInnerOperators) {
if (hasInit && !rules.hideName("("))
if (hasInit && !rules.hideFilterName("("))
innerResults.insert(innerResults.begin(), buildParen());
if (hasDot && !rules.hideName("."))
if (hasDot && !rules.hideFilterName("."))
innerResults.insert(innerResults.begin(), buildDot());
if (hasQDot && !rules.hideName("?."))
if (hasQDot && !rules.hideFilterName("?."))
innerResults.insert(innerResults.begin(), buildQDot());
}

Expand Down
13 changes: 13 additions & 0 deletions tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ static LazySKDUID KindKeyword("source.codecompletion.keyword");
static LazySKDUID KindLiteral("source.codecompletion.literal");
static LazySKDUID KindCustom("source.codecompletion.custom");
static LazySKDUID KindIdentifier("source.codecompletion.identifier");
static LazySKDUID KindDescription("source.codecompletion.description");

static UIdent DiagKindNote("source.diagnostic.severity.note");
static UIdent DiagKindWarning("source.diagnostic.severity.warning");
Expand Down Expand Up @@ -1719,6 +1720,8 @@ static sourcekitd_response_t codeCompleteOpen(StringRef Name,
rule.kind = FilterRule::CustomCompletion;
} else if (kind == KindIdentifier) {
rule.kind = FilterRule::Identifier;
} else if (kind == KindDescription) {
rule.kind = FilterRule::Description;
} else {
// Warning: unknown
}
Expand Down Expand Up @@ -1746,6 +1749,16 @@ static sourcekitd_response_t codeCompleteOpen(StringRef Name,
rule.names.assign(names.begin(), names.end());
break;
}
case FilterRule::Description: {
SmallVector<const char *, 8> names;
if (dict.getStringArray(KeyNames, names, false)) {
failed = true;
CCC.failed("filter rule missing required key 'key.names'");
return true;
}
rule.names.assign(names.begin(), names.end());
break;
}
case FilterRule::Keyword:
case FilterRule::Literal:
case FilterRule::CustomCompletion: {
Expand Down