Skip to content

Commit cb877fd

Browse files
authored
Merge pull request #10301 from benlangmuir/cc-rule-desc
[code-completion] Add filter rules for description in addition filter-name
2 parents 0e4449c + 61a1519 commit cb877fd

File tree

8 files changed

+132
-24
lines changed

8 files changed

+132
-24
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
key.kind: source.codecompletion.description,
4+
key.hide: 1,
5+
key.names: [
6+
"over(Int)",
7+
"[Int]"
8+
]
9+
}
10+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
key.kind: source.codecompletion.identifier,
4+
key.hide: 1,
5+
key.names: [
6+
"[]"
7+
]
8+
},
9+
{
10+
key.kind: source.codecompletion.description,
11+
key.hide: 0,
12+
key.names: [
13+
"[Float]"
14+
]
15+
}
16+
]

test/SourceKit/CodeComplete/complete_filter_rules.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,50 @@ func testHideOp10() {
183183
struct local {}
184184
local#^HIDE_OP_10^#
185185
}
186+
187+
// 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
188+
func testHideDesc1() {
189+
struct Local {
190+
func over(_: Int) {}
191+
func over(_: Float) {}
192+
}
193+
194+
Local().#^HIDE_DESC_1^#
195+
}
196+
// HIDE_DESC_1-NOT: over
197+
// HIDE_DESC_1: over(Float)
198+
// HIDE_DESC_1-NOT: over
199+
200+
// 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
201+
// 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
202+
func testHideDesc2() {
203+
struct Local {
204+
subscript(_: Int) -> Int { return 0 }
205+
subscript(_: Float) -> Float { return 0.0 }
206+
}
207+
208+
Local()#^HIDE_DESC_2^#
209+
210+
let local = Local()
211+
#^HIDE_DESC_3,local^#
212+
}
213+
// HIDE_DESC_2-NOT: [Int]
214+
// HIDE_DESC_2: [Float]
215+
// HIDE_DESC_2-NOT: [Int]
216+
217+
// 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
218+
// 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
219+
func testHideDesc2() {
220+
struct Local {
221+
subscript(_: Int) -> Int { return 0 }
222+
subscript(_: Float) -> Float { return 0.0 }
223+
}
224+
225+
Local()#^SHOW_DESC_2^#
226+
227+
let local = Local()
228+
#^SHOW_DESC_3,local^#
229+
}
230+
// SHOW_DESC_2-NOT: [Int]
231+
// SHOW_DESC_2: [Float]
232+
// SHOW_DESC_2-NOT: [Int]

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ struct FilterRule {
153153
Literal,
154154
CustomCompletion,
155155
Identifier,
156+
Description,
156157
};
157158
Kind kind;
158159
bool hide;

tools/SourceKit/lib/SwiftLang/CodeCompletion.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ struct FilterRules {
231231
// FIXME: hide individual custom completions
232232

233233
llvm::StringMap<bool> hideModule;
234-
llvm::StringMap<bool> hideByName;
234+
llvm::StringMap<bool> hideByFilterName;
235+
llvm::StringMap<bool> hideByDescription;
235236

236237
bool hideCompletion(Completion *completion) const;
237-
bool hideCompletion(SwiftResult *completion,
238-
StringRef name,
239-
void *customKind = nullptr) const;
240-
bool hideName(StringRef name) const;
238+
bool hideCompletion(SwiftResult *completion, StringRef name,
239+
StringRef description, void *customKind = nullptr) const;
240+
bool hideFilterName(StringRef name) const;
241241
};
242242

243243
} // end namespace CodeCompletion

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -447,22 +447,32 @@ static bool isHighPriorityKeyword(CodeCompletionKeywordKind kind) {
447447
}
448448
}
449449

450-
bool FilterRules::hideName(StringRef name) const {
451-
auto I = hideByName.find(name);
452-
if (I != hideByName.end())
453-
return I->getValue();
450+
bool FilterRules::hideFilterName(StringRef name) const {
451+
auto I = hideByFilterName.find(name);
452+
if (I != hideByFilterName.end())
453+
return I->getValue();
454454
return hideAll;
455455
}
456456

457457
bool FilterRules::hideCompletion(Completion *completion) const {
458-
return hideCompletion(completion, completion->getName(), completion->getCustomKind());
458+
return hideCompletion(completion, completion->getName(),
459+
completion->getDescription(),
460+
completion->getCustomKind());
459461
}
460462

461-
bool FilterRules::hideCompletion(SwiftResult *completion, StringRef name, void *customKind) const {
463+
bool FilterRules::hideCompletion(SwiftResult *completion, StringRef filterName,
464+
StringRef description,
465+
void *customKind) const {
466+
467+
if (!description.empty()) {
468+
auto I = hideByDescription.find(description);
469+
if (I != hideByDescription.end())
470+
return I->getValue();
471+
}
462472

463-
if (!name.empty()) {
464-
auto I = hideByName.find(name);
465-
if (I != hideByName.end())
473+
if (!filterName.empty()) {
474+
auto I = hideByFilterName.find(filterName);
475+
if (I != hideByFilterName.end())
466476
return I->getValue();
467477
}
468478

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,12 @@ static void translateFilterRules(ArrayRef<FilterRule> rawFilterRules,
887887
// Note: name is null-terminated.
888888
if (canonicalizeFilterName(name.data(), canonName))
889889
continue;
890-
filterRules.hideByName[canonName] = rule.hide;
890+
filterRules.hideByFilterName[canonName] = rule.hide;
891+
}
892+
break;
893+
case FilterRule::Description:
894+
for (auto name : rule.names) {
895+
filterRules.hideByDescription[name] = rule.hide;
891896
}
892897
break;
893898
case FilterRule::Module:
@@ -955,13 +960,19 @@ filterInnerResults(ArrayRef<Result *> results, bool includeInner,
955960
if (!includeInnerOperators && result->isOperator())
956961
continue;
957962

958-
llvm::SmallString<64> name;
963+
llvm::SmallString<64> filterName;
959964
{
960-
llvm::raw_svector_ostream OSS(name);
965+
llvm::raw_svector_ostream OSS(filterName);
961966
CodeCompletion::CompletionBuilder::getFilterName(
962967
result->getCompletionString(), OSS);
963968
}
964-
if (rules.hideCompletion(result, name))
969+
llvm::SmallString<64> description;
970+
{
971+
llvm::raw_svector_ostream OSS(description);
972+
CodeCompletion::CompletionBuilder::getDescription(
973+
result, OSS, /*leadingPunctuation=*/false);
974+
}
975+
if (rules.hideCompletion(result, filterName, description))
965976
continue;
966977

967978
bool inner = checkInnerResult(result, hasDot, hasQDot, hasInit);
@@ -1040,11 +1051,11 @@ static void transformAndForwardResults(
10401051
options.addInnerOperators, hasDot, hasQDot, hasInit,
10411052
rules);
10421053
if (options.addInnerOperators) {
1043-
if (hasInit && !rules.hideName("("))
1054+
if (hasInit && !rules.hideFilterName("("))
10441055
innerResults.insert(innerResults.begin(), buildParen());
1045-
if (hasDot && !rules.hideName("."))
1056+
if (hasDot && !rules.hideFilterName("."))
10461057
innerResults.insert(innerResults.begin(), buildDot());
1047-
if (hasQDot && !rules.hideName("?."))
1058+
if (hasQDot && !rules.hideFilterName("?."))
10481059
innerResults.insert(innerResults.begin(), buildQDot());
10491060
}
10501061

@@ -1095,11 +1106,11 @@ static void transformAndForwardResults(
10951106
}
10961107

10971108
if (options.addInnerOperators) {
1098-
if (hasInit && !rules.hideName("("))
1109+
if (hasInit && !rules.hideFilterName("("))
10991110
innerResults.insert(innerResults.begin(), buildParen());
1100-
if (hasDot && !rules.hideName("."))
1111+
if (hasDot && !rules.hideFilterName("."))
11011112
innerResults.insert(innerResults.begin(), buildDot());
1102-
if (hasQDot && !rules.hideName("?."))
1113+
if (hasQDot && !rules.hideFilterName("?."))
11031114
innerResults.insert(innerResults.begin(), buildQDot());
11041115
}
11051116

tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ static LazySKDUID KindKeyword("source.codecompletion.keyword");
123123
static LazySKDUID KindLiteral("source.codecompletion.literal");
124124
static LazySKDUID KindCustom("source.codecompletion.custom");
125125
static LazySKDUID KindIdentifier("source.codecompletion.identifier");
126+
static LazySKDUID KindDescription("source.codecompletion.description");
126127

127128
static UIdent DiagKindNote("source.diagnostic.severity.note");
128129
static UIdent DiagKindWarning("source.diagnostic.severity.warning");
@@ -1719,6 +1720,8 @@ static sourcekitd_response_t codeCompleteOpen(StringRef Name,
17191720
rule.kind = FilterRule::CustomCompletion;
17201721
} else if (kind == KindIdentifier) {
17211722
rule.kind = FilterRule::Identifier;
1723+
} else if (kind == KindDescription) {
1724+
rule.kind = FilterRule::Description;
17221725
} else {
17231726
// Warning: unknown
17241727
}
@@ -1746,6 +1749,16 @@ static sourcekitd_response_t codeCompleteOpen(StringRef Name,
17461749
rule.names.assign(names.begin(), names.end());
17471750
break;
17481751
}
1752+
case FilterRule::Description: {
1753+
SmallVector<const char *, 8> names;
1754+
if (dict.getStringArray(KeyNames, names, false)) {
1755+
failed = true;
1756+
CCC.failed("filter rule missing required key 'key.names'");
1757+
return true;
1758+
}
1759+
rule.names.assign(names.begin(), names.end());
1760+
break;
1761+
}
17491762
case FilterRule::Keyword:
17501763
case FilterRule::Literal:
17511764
case FilterRule::CustomCompletion: {

0 commit comments

Comments
 (0)