Skip to content

Commit c77af69

Browse files
committed
[CodeCompletion] Switch filter-rules and popular api to follow SE-0021 naming
We want inputs for function names to follow SE-0021 with the exception that a function with no arguments is spelled `foo()` instead of `foo`, because we have no type to disambiguate with and it's not ambiguous with a call in this context. Internally, we use a filter name without underscores because we don't want to introduce spurious matches to the `_` character which might be part of identifiers. For now, continue to accept the old names to ease the transition. rdar://problem/24350800
1 parent b4ce34a commit c77af69

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

test/SourceKit/CodeComplete/Inputs/filter-rules/hideNames.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
key.hide: 1,
55
key.names: [
66
"hideThis1()",
7-
"hideThis2(:namedParam2:)",
8-
"hideThis3()",
7+
"hideThis2(_:namedParam2:)",
8+
"hideThis3(namedParam1:_:)",
99
]
1010
},
1111
{

test/SourceKit/CodeComplete/complete_filter_rules.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Foo
66
struct TestHideName {
77
func hideThis1() {}
88
func hideThis2(_ x: Int, namedParam2: Int) {}
9-
func hideThis3() {}
9+
func hideThis3(namedParam1: Int, _ unnamedParam: Int) {}
1010
func hideThis4(namedParam1 x: Int, namedParam2: Int) {}
1111
func dontHideThisByName() {}
1212

test/SourceKit/CodeComplete/complete_popular_api.swift.popular

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
good(:p2:p3:p4:p5:)
1+
good(_:p2:p3:p4:p5:)
22
good()
33
xhappy
44
EEModuleColor

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ struct FilterRule {
157157
};
158158
Kind kind;
159159
bool hide;
160-
std::vector<StringRef> names;
160+
std::vector<StringRef> names; ///< Must be null-terminated.
161161
std::vector<UIdent> uids;
162162
};
163163

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "SourceKit/Support/Logging.h"
1717
#include "SourceKit/Support/UIdent.h"
1818

19+
#include "swift/Basic/Fallthrough.h"
1920
#include "swift/Frontend/Frontend.h"
2021
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
2122
#include "swift/IDE/CodeCompletionCache.h"
@@ -819,6 +820,40 @@ static void translateCodeCompletionOptions(OptionsDictionary &from,
819820
from.valueForOption(KeyTopNonLiteral, to.showTopNonLiteralResults);
820821
}
821822

823+
/// Canonicalize a name that is in the format of a reference to a function into
824+
/// the name format used internally for filtering.
825+
///
826+
/// Returns true if the name is invalid.
827+
static bool canonicalizeFilterName(const char *origName,
828+
SmallVectorImpl<char> &Result) {
829+
assert(origName);
830+
const char *p = origName;
831+
char curr = '\0';
832+
char prev;
833+
834+
// FIXME: disallow unnamed parameters without underscores `foo(::)`.
835+
while (true) {
836+
prev = curr;
837+
curr = *p++;
838+
switch (curr) {
839+
case '\0':
840+
return false; // Done.
841+
case '_':
842+
// Remove the _ underscore for an unnamed parameter.
843+
if (prev == ':' || prev == '(') {
844+
char next = *p;
845+
if (next == ':' || next == ')')
846+
continue;
847+
}
848+
SWIFT_FALLTHROUGH;
849+
default:
850+
Result.push_back(curr);
851+
continue;
852+
}
853+
}
854+
llvm_unreachable("exit is on null byte");
855+
}
856+
822857
static void translateFilterRules(ArrayRef<FilterRule> rawFilterRules,
823858
CodeCompletion::FilterRules &filterRules) {
824859
for (auto &rule : rawFilterRules) {
@@ -828,7 +863,11 @@ static void translateFilterRules(ArrayRef<FilterRule> rawFilterRules,
828863
break;
829864
case FilterRule::Identifier:
830865
for (auto name : rule.names) {
831-
filterRules.hideByName[name] = rule.hide;
866+
SmallString<128> canonName;
867+
// Note: name is null-terminated.
868+
if (canonicalizeFilterName(name.data(), canonName))
869+
continue;
870+
filterRules.hideByName[canonName] = rule.hide;
832871
}
833872
break;
834873
case FilterRule::Module:
@@ -1201,10 +1240,16 @@ void SwiftLangSupport::codeCompleteSetPopularAPI(
12011240
ThreadSafeRefCntPtr<SwiftPopularAPI> newPopularAPI(new SwiftPopularAPI);
12021241
auto &nameToFactor = newPopularAPI->nameToFactor;
12031242
for (unsigned i = 0, n = popularAPI.size(); i < n; ++i) {
1204-
nameToFactor[popularAPI[i]] = Factor(double(n - i) / n);
1243+
SmallString<64> name;
1244+
if (canonicalizeFilterName(popularAPI[i], name))
1245+
continue;
1246+
nameToFactor[name] = Factor(double(n - i) / n);
12051247
}
12061248
for (unsigned i = 0, n = unpopularAPI.size(); i < n; ++i) {
1207-
nameToFactor[unpopularAPI[i]] = Factor(-double(n - i) / n);
1249+
SmallString<64> name;
1250+
if (canonicalizeFilterName(unpopularAPI[i], name))
1251+
continue;
1252+
nameToFactor[name] = Factor(-double(n - i) / n);
12081253
}
12091254

12101255
PopularAPI = newPopularAPI; // replace the old popular API.

0 commit comments

Comments
 (0)