Skip to content

Commit 6581eba

Browse files
committed
[CodeCompletion] Improve ordering of prefix matches vs popular names
In the experimental code-completion path. rdar://problem/24843463
1 parent 77dd9b2 commit 6581eba

File tree

6 files changed

+55
-10
lines changed

6 files changed

+55
-10
lines changed

test/SourceKit/CodeComplete/complete_fuzzy.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ struct Test4 {
105105

106106
// CONTEXT_SORT_2: Results for filterText: myVa [
107107
// CONTEXT_SORT_2-NEXT: myVarTest4
108-
// CONTEXT_SORT_2-NEXT: myLocalVar
109108
// CONTEXT_SORT_2-NEXT: myVar
109+
// CONTEXT_SORT_2-NEXT: myLocalVar
110110

111111
// CONTEXT_SORT_3: Results for filterText: myVa [
112112
// CONTEXT_SORT_3-NEXT: myVar

test/SourceKit/CodeComplete/complete_popular_api.swift

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ struct OuterNominal {
8181
// POPULAR_STMT_0: okay()
8282
// POPULAR_STMT_0: DDModuleColor
8383
// POPULAR_STMT_0: CCModuleColor
84-
// POPULAR_STMT_0: EEModuleColor
85-
// bad() ends up here because it's an unpopular global but that's still better
86-
// than a random "other module" result like ModuleCollaborate.
84+
// bad() ends up here because it's an unpopular global but that's still
85+
// generally better than "other module" results.
8786
// POPULAR_STMT_0: bad()
87+
// POPULAR_STMT_0: EEModuleColor
8888
// POPULAR_STMT_0: ModuleCollaborate
8989
// POPULAR_STMT_0: ]
9090
// POPULAR_STMT_0-LABEL: Results for filterText: col [
@@ -104,3 +104,41 @@ struct OuterNominal {
104104
}
105105
}
106106
}
107+
108+
struct Outer {
109+
struct ABTabularMonkey {}
110+
struct ABTextMockery {}
111+
struct ABTradeableEquity {}
112+
struct ABVocalContour {}
113+
struct ABBobtail {}
114+
struct ABFont {}
115+
}
116+
117+
// RUN: %complete-test -hide-none -fuzz -group=none -popular="%s.popular" -unpopular="%s.unpopular" -tok=POPULAR_VS_PREFIX_1 %s -- -I %S/Inputs | FileCheck %s -check-prefix=POPULAR_VS_PREFIX_1
118+
func testPopularityVsPrefixMatch1() {
119+
let x: Outer.#^POPULAR_VS_PREFIX_1,,AB,ABT^#
120+
}
121+
// POPULAR_VS_PREFIX_1-LABEL: Results for filterText: [
122+
// POPULAR_VS_PREFIX_1-NEXT: ABVocalContour
123+
// POPULAR_VS_PREFIX_1-NEXT: ABBobtail
124+
// POPULAR_VS_PREFIX_1-NEXT: ABFont
125+
// POPULAR_VS_PREFIX_1-NEXT: ABTabularMonkey
126+
// POPULAR_VS_PREFIX_1-NEXT: ABTextMockery
127+
// POPULAR_VS_PREFIX_1-NEXT: ABTradeableEquity
128+
// POPULAR_VS_PREFIX_1: ]
129+
// POPULAR_VS_PREFIX_1-LABEL: Results for filterText: AB [
130+
// POPULAR_VS_PREFIX_1-NEXT: ABVocalContour
131+
// POPULAR_VS_PREFIX_1-NEXT: ABBobtail
132+
// POPULAR_VS_PREFIX_1-NEXT: ABFont
133+
// POPULAR_VS_PREFIX_1-NEXT: ABTextMockery
134+
// POPULAR_VS_PREFIX_1-NEXT: ABTabularMonkey
135+
// POPULAR_VS_PREFIX_1-NEXT: ABTradeableEquity
136+
// POPULAR_VS_PREFIX_1-NEXT: ]
137+
// POPULAR_VS_PREFIX_1-LABEL: Results for filterText: ABT [
138+
// POPULAR_VS_PREFIX_1-NEXT: ABTextMockery
139+
// POPULAR_VS_PREFIX_1-NEXT: ABTabularMonkey
140+
// POPULAR_VS_PREFIX_1-NEXT: ABTradeableEquity
141+
// POPULAR_VS_PREFIX_1-NEXT: ABVocalContour
142+
// POPULAR_VS_PREFIX_1-NEXT: ABBobtail
143+
// POPULAR_VS_PREFIX_1-NEXT: ABFont
144+
// POPULAR_VS_PREFIX_1-NEXT: ]

test/SourceKit/CodeComplete/complete_popular_api.swift.popular

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ xhappy
33
EEModuleColor
44
DDModuleColor
55
CCModuleColor
6+
ABVocalContour
7+
ABBobtail
8+
ABFont

tools/SourceKit/lib/Support/FuzzyStringMatcher.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ FuzzyStringMatcher::FuzzyStringMatcher(StringRef pattern_)
4646
pattern.size() * pattern.size(); // max run length score
4747
if (upperCharCount) // max uppercase match score
4848
maxScore += (upperCharCount + 1) * (upperCharCount + 1);
49-
maxScore *= 1.1; // exact match bonus
49+
maxScore *= 1.1 * 2.5; // exact prefix match bonus
5050
}
5151
}
5252

@@ -501,6 +501,10 @@ CandidateSpecificMatcher::scoreCandidateTrial(unsigned firstPatternPos) {
501501
trialScore *= 1.1;
502502
}
503503

504+
// Exact prefix matches are the best.
505+
if (!runs.empty() && runs[0].location == 0 && runs[0].length == patternLength)
506+
trialScore *= 2.5;
507+
504508
// FIXME: popular/unpopular API.
505509

506510
// We really don't like matches that don't start at a token.

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct Options {
4747
// the same as the worst possible match N/10 "contexts" ahead of it.
4848
unsigned semanticContextWeight = 10 * Completion::numSemanticContexts;
4949
unsigned fuzzyMatchWeight = 9;
50-
unsigned popularityBonus = 9;
50+
unsigned popularityBonus = 5;
5151
};
5252

5353
struct SwiftCompletionInfo {

unittests/SourceKit/Support/FuzzyStringMatcherTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ TEST(FuzzyStringMatcher, NormalizeSingleCharacterScore) {
276276
TEST(FuzzyStringMatcher, NormalizeScore) {
277277
FuzzyStringMatcher m("AB");
278278
m.normalize = true;
279-
EXPECT_EQ(1.0, m.scoreCandidate("AB"));
280-
EXPECT_EQ(0.0, m.scoreCandidate("BB"));
279+
EXPECT_DOUBLE_EQ(1.0, m.scoreCandidate("AB"));
280+
EXPECT_DOUBLE_EQ(0.0, m.scoreCandidate("BB"));
281281
EXPECT_GT(1.0, m.scoreCandidate("ab"));
282282
EXPECT_LT(0.0, m.scoreCandidate("ab"));
283283
EXPECT_GT(1.0, m.scoreCandidate("ABB"));
@@ -291,8 +291,8 @@ TEST(FuzzyStringMatcher, NormalizeScore) {
291291

292292
FuzzyStringMatcher n("abc");
293293
n.normalize = true;
294-
EXPECT_EQ(1.0, n.scoreCandidate("abc"));
295-
EXPECT_EQ(1.0, n.scoreCandidate("ABC"));
294+
EXPECT_DOUBLE_EQ(1.0, n.scoreCandidate("abc"));
295+
EXPECT_DOUBLE_EQ(1.0, n.scoreCandidate("ABC"));
296296
}
297297

298298
TEST(FuzzyStringMatcher, TokenizingCharacters) {

0 commit comments

Comments
 (0)