Skip to content

Commit 75ff948

Browse files
committed
[CodeCompletion] Boost exact case-sensitive prefix match
When there are symbols 'Label' and 'label', if a user type 'Lab', 'Label' should be prioritized. rdar://77164709
1 parent 2327f10 commit 75ff948

File tree

7 files changed

+83
-14
lines changed

7 files changed

+83
-14
lines changed

Diff for: lib/IDE/FuzzyStringMatcher.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ FuzzyStringMatcher::FuzzyStringMatcher(StringRef pattern_)
4747
pattern.size() * pattern.size(); // max run length score
4848
if (upperCharCount) // max uppercase match score
4949
maxScore += (upperCharCount + 1) * (upperCharCount + 1);
50-
maxScore *= 1.1 * 2.5; // exact prefix match bonus
50+
maxScore *= 1.1 * 2.5 * 1.2; // exact prefix match bonus
5151
}
5252
}
5353

@@ -503,8 +503,12 @@ CandidateSpecificMatcher::scoreCandidateTrial(unsigned firstPatternPos) {
503503
}
504504

505505
// Exact prefix matches are the best.
506-
if (!runs.empty() && runs[0].location == 0 && runs[0].length == patternLength)
506+
if (!runs.empty() && runs[0].location == 0 && runs[0].length == patternLength) {
507507
trialScore *= 2.5;
508+
// Case sensitive exact prefix matches are the best of the best.
509+
if (candidate.startswith(pattern))
510+
trialScore *= 1.2;
511+
}
508512

509513
// FIXME: popular/unpopular API.
510514

Diff for: test/SourceKit/CodeComplete/complete_fuzzy.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ struct Test4 {
100100
#^CONTEXT_SORT_1,myVa^#
101101
// CONTEXT_SORT_1: Results for filterText: myVa [
102102
// CONTEXT_SORT_1-NEXT: myVarTest4
103-
// CONTEXT_SORT_1-NEXT: myLocalVar
104103
// CONTEXT_SORT_1-NEXT: myVar
104+
// CONTEXT_SORT_1-NEXT: myLocalVar
105105

106106
// CONTEXT_SORT_2: Results for filterText: myVa [
107107
// CONTEXT_SORT_2-NEXT: myVarTest4

Diff for: test/SourceKit/CodeComplete/complete_popular_api.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ struct OuterNominal {
7777
// POPULAR_STMT_0-LABEL: Results for filterText: [
7878
// POPULAR_STMT_0: argColor
7979
// POPULAR_STMT_0: localColor
80+
// POPULAR_STMT_0: good()
8081
// POPULAR_STMT_0: fromDerivedColor
8182
// POPULAR_STMT_0: fromSuperColor
82-
// POPULAR_STMT_0: good()
83-
// POPULAR_STMT_0: fromOuterNominalColor
8483
// POPULAR_STMT_0: DDModuleColor
85-
// POPULAR_STMT_0: CCModuleColor
8684
// POPULAR_STMT_0: EEModuleColor
85+
// POPULAR_STMT_0: CCModuleColor
86+
// POPULAR_STMT_0: fromOuterNominalColor
8787
// POPULAR_STMT_0: globalColor
8888
// POPULAR_STMT_0: okay()
8989
// POPULAR_STMT_0: ModuleCollaborate
@@ -94,11 +94,11 @@ struct OuterNominal {
9494
// POPULAR_STMT_0: localColor
9595
// POPULAR_STMT_0: fromDerivedColor
9696
// POPULAR_STMT_0: fromSuperColor
97-
// POPULAR_STMT_0: fromOuterNominalColor
98-
// POPULAR_STMT_0: globalColor
9997
// POPULAR_STMT_0: DDModuleColor
100-
// POPULAR_STMT_0: CCModuleColor
10198
// POPULAR_STMT_0: EEModuleColor
99+
// POPULAR_STMT_0: CCModuleColor
100+
// POPULAR_STMT_0: fromOuterNominalColor
101+
// POPULAR_STMT_0: globalColor
102102
// POPULAR_STMT_0: ModuleCollaborate
103103
// POPULAR_STMT_0: BBModuleColor
104104
// POPULAR_STMT_0: AAModuleColor

Diff for: test/SourceKit/CodeComplete/complete_sort_order.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ func test7() {
211211
#^CASE_0,caseSensitiveCheck,CaseSensitiveCheck^#
212212
}
213213
// CASE_0: Results for filterText: caseSensitiveCheck [
214-
// CASE_0: CaseSensitiveCheck
215214
// CASE_0: caseSensitiveCheck
215+
// CASE_0: CaseSensitiveCheck
216216
// CASE_0: caseSensitiveCheck.
217217
// CASE_0: ]
218218
// CASE_0: Results for filterText: CaseSensitiveCheck [
219-
// CASE_0: caseSensitiveCheck
220219
// CASE_0: CaseSensitiveCheck
220+
// CASE_0: caseSensitiveCheck
221221
// CASE_0: CaseSensitiveCheck(
222222
// CASE_0: ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// BEGIN TheModule.swift
2+
public struct Machine {
3+
public init() {}
4+
}
5+
6+
public func machineFunc() {}
7+
8+
// BEGIN main.swift
9+
import TheModule
10+
public struct MyMachine {}
11+
12+
struct Local {
13+
var machineHidden: Bool { true }
14+
func hideMachine() -> Void {}
15+
16+
func test() {
17+
#^COMPLETE,,M,Ma,Mac,Mach^#
18+
}
19+
}
20+
21+
// RUN: %empty-directory(%t/src)
22+
// RUN: %{python} %utils/split_file.py -o %t/src %s
23+
24+
// RUN: %empty-directory(%t/Modules)
25+
// RUN: %target-swift-frontend -emit-module %t/src/TheModule.swift -module-name TheModule -o %t/Modules/TheModule.swiftmodule
26+
27+
// RUN: %complete-test -tok=COMPLETE %t/src/main.swift -- -target %target-triple -I %t/Modules | %FileCheck --check-prefix=CHECK %s
28+
29+
// CHECK-LABEL: Results for filterText: [
30+
// CHECK: hideMachine()
31+
// CHECK: machineHidden
32+
// CHECK: MyMachine
33+
// CHECK: ]
34+
35+
// CHECK-LABEL: Results for filterText: M [
36+
// CHECK: MyMachine
37+
// CHECK: Machine
38+
// CHECK: machineHidden
39+
// CHECK: machineFunc()
40+
// CHECK: ]
41+
42+
// CHECK-LABEL: Results for filterText: Ma [
43+
// CHECK: Machine
44+
// CHECK: machineHidden
45+
// CHECK: hideMachine()
46+
// CHECK: machineFunc()
47+
// CHECK: MyMachine
48+
// CHECK: ]
49+
50+
// CHECK-LABEL: Results for filterText: Mac [
51+
// CHECK: Machine
52+
// CHECK: machineHidden
53+
// CHECK: machineFunc()
54+
// CHECK: hideMachine()
55+
// CHECK: MyMachine
56+
// CHECK: ]
57+
58+
// CHECK-LABEL: Results for filterText: Mach [
59+
// CHECK: Machine
60+
// CHECK: machineHidden
61+
// CHECK: machineFunc()
62+
// CHECK: hideMachine()
63+
// CHECK: MyMachine
64+
// CHECK: ]
65+

Diff for: tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ struct Options {
4747
unsigned showTopNonLiteralResults = 3;
4848

4949
// Options for combining priorities.
50-
unsigned semanticContextWeight = 15;
51-
unsigned fuzzyMatchWeight = 10;
50+
unsigned semanticContextWeight = 7;
51+
unsigned fuzzyMatchWeight = 13;
5252
unsigned popularityBonus = 2;
5353
};
5454

Diff for: unittests/IDE/FuzzyStringMatcherTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ TEST(FuzzyStringMatcher, NormalizeScore) {
292292
FuzzyStringMatcher n("abc");
293293
n.normalize = true;
294294
EXPECT_DOUBLE_EQ(1.0, n.scoreCandidate("abc"));
295-
EXPECT_DOUBLE_EQ(1.0, n.scoreCandidate("ABC"));
295+
EXPECT_DOUBLE_EQ(0.83333333333333337, n.scoreCandidate("ABC"));
296296
}
297297

298298
TEST(FuzzyStringMatcher, TokenizingCharacters) {

0 commit comments

Comments
 (0)