Skip to content

[SourceKit] Adjust result sorting in complete.open/update #37095

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
Apr 28, 2021
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
8 changes: 6 additions & 2 deletions lib/IDE/FuzzyStringMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ FuzzyStringMatcher::FuzzyStringMatcher(StringRef pattern_)
pattern.size() * pattern.size(); // max run length score
if (upperCharCount) // max uppercase match score
maxScore += (upperCharCount + 1) * (upperCharCount + 1);
maxScore *= 1.1 * 2.5; // exact prefix match bonus
maxScore *= 1.1 * 2.5 * 1.2; // exact prefix match bonus
}
}

Expand Down Expand Up @@ -503,8 +503,12 @@ CandidateSpecificMatcher::scoreCandidateTrial(unsigned firstPatternPos) {
}

// Exact prefix matches are the best.
if (!runs.empty() && runs[0].location == 0 && runs[0].length == patternLength)
if (!runs.empty() && runs[0].location == 0 && runs[0].length == patternLength) {
trialScore *= 2.5;
// Case sensitive exact prefix matches are the best of the best.
if (candidate.startswith(pattern))
trialScore *= 1.2;
}

// FIXME: popular/unpopular API.

Expand Down
2 changes: 1 addition & 1 deletion test/SourceKit/CodeComplete/complete_fuzzy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ struct Test4 {
#^CONTEXT_SORT_1,myVa^#
// CONTEXT_SORT_1: Results for filterText: myVa [
// CONTEXT_SORT_1-NEXT: myVarTest4
// CONTEXT_SORT_1-NEXT: myLocalVar
// CONTEXT_SORT_1-NEXT: myVar
// CONTEXT_SORT_1-NEXT: myLocalVar

// CONTEXT_SORT_2: Results for filterText: myVa [
// CONTEXT_SORT_2-NEXT: myVarTest4
Expand Down
12 changes: 6 additions & 6 deletions test/SourceKit/CodeComplete/complete_popular_api.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ struct OuterNominal {
// POPULAR_STMT_0-LABEL: Results for filterText: [
// POPULAR_STMT_0: argColor
// POPULAR_STMT_0: localColor
// POPULAR_STMT_0: good()
// POPULAR_STMT_0: fromDerivedColor
// POPULAR_STMT_0: fromSuperColor
// POPULAR_STMT_0: good()
// POPULAR_STMT_0: fromOuterNominalColor
// POPULAR_STMT_0: DDModuleColor
// POPULAR_STMT_0: CCModuleColor
// POPULAR_STMT_0: EEModuleColor
// POPULAR_STMT_0: CCModuleColor
// POPULAR_STMT_0: fromOuterNominalColor
// POPULAR_STMT_0: globalColor
// POPULAR_STMT_0: okay()
// POPULAR_STMT_0: ModuleCollaborate
Expand All @@ -94,11 +94,11 @@ struct OuterNominal {
// POPULAR_STMT_0: localColor
// POPULAR_STMT_0: fromDerivedColor
// POPULAR_STMT_0: fromSuperColor
// POPULAR_STMT_0: fromOuterNominalColor
// POPULAR_STMT_0: globalColor
// POPULAR_STMT_0: DDModuleColor
// POPULAR_STMT_0: CCModuleColor
// POPULAR_STMT_0: EEModuleColor
// POPULAR_STMT_0: CCModuleColor
// POPULAR_STMT_0: fromOuterNominalColor
// POPULAR_STMT_0: globalColor
// POPULAR_STMT_0: ModuleCollaborate
// POPULAR_STMT_0: BBModuleColor
// POPULAR_STMT_0: AAModuleColor
Expand Down
4 changes: 2 additions & 2 deletions test/SourceKit/CodeComplete/complete_sort_order.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ func test7() {
#^CASE_0,caseSensitiveCheck,CaseSensitiveCheck^#
}
// CASE_0: Results for filterText: caseSensitiveCheck [
// CASE_0: CaseSensitiveCheck
// CASE_0: caseSensitiveCheck
// CASE_0: CaseSensitiveCheck
// CASE_0: caseSensitiveCheck.
// CASE_0: ]
// CASE_0: Results for filterText: CaseSensitiveCheck [
// CASE_0: caseSensitiveCheck
// CASE_0: CaseSensitiveCheck
// CASE_0: caseSensitiveCheck
// CASE_0: CaseSensitiveCheck(
// CASE_0: ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// BEGIN TheModule.swift
public struct Machine {
public init() {}
}

public func machineFunc() {}

// BEGIN main.swift
import TheModule
public struct MyMachine {}

struct Local {
var machineHidden: Bool { true }
func hideMachine() -> Void {}

func test() {
#^COMPLETE,,M,Ma,Mac,Mach^#
}
}

// RUN: %empty-directory(%t/src)
// RUN: %{python} %utils/split_file.py -o %t/src %s

// RUN: %empty-directory(%t/Modules)
// RUN: %target-swift-frontend -emit-module %t/src/TheModule.swift -module-name TheModule -o %t/Modules/TheModule.swiftmodule

// RUN: %complete-test -tok=COMPLETE %t/src/main.swift -- -target %target-triple -I %t/Modules | %FileCheck --check-prefix=CHECK %s

// CHECK-LABEL: Results for filterText: [
// CHECK: hideMachine()
// CHECK: machineHidden
// CHECK: MyMachine
// CHECK: ]

// CHECK-LABEL: Results for filterText: M [
// CHECK: MyMachine
// CHECK: Machine
// CHECK: machineHidden
// CHECK: machineFunc()
// CHECK: ]

// CHECK-LABEL: Results for filterText: Ma [
// CHECK: Machine
// CHECK: machineHidden
// CHECK: hideMachine()
// CHECK: machineFunc()
// CHECK: MyMachine
// CHECK: ]

// CHECK-LABEL: Results for filterText: Mac [
// CHECK: Machine
// CHECK: machineHidden
// CHECK: machineFunc()
// CHECK: hideMachine()
// CHECK: MyMachine
// CHECK: ]

// CHECK-LABEL: Results for filterText: Mach [
// CHECK: Machine
// CHECK: machineHidden
// CHECK: machineFunc()
// CHECK: hideMachine()
// CHECK: MyMachine
// CHECK: ]

4 changes: 2 additions & 2 deletions tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ struct Options {
unsigned showTopNonLiteralResults = 3;

// Options for combining priorities.
unsigned semanticContextWeight = 15;
unsigned fuzzyMatchWeight = 10;
unsigned semanticContextWeight = 7;
unsigned fuzzyMatchWeight = 13;
unsigned popularityBonus = 2;
};

Expand Down
2 changes: 1 addition & 1 deletion unittests/IDE/FuzzyStringMatcherTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ TEST(FuzzyStringMatcher, NormalizeScore) {
FuzzyStringMatcher n("abc");
n.normalize = true;
EXPECT_DOUBLE_EQ(1.0, n.scoreCandidate("abc"));
EXPECT_DOUBLE_EQ(1.0, n.scoreCandidate("ABC"));
EXPECT_DOUBLE_EQ(0.83333333333333337, n.scoreCandidate("ABC"));
}

TEST(FuzzyStringMatcher, TokenizingCharacters) {
Expand Down