Skip to content

Commit 2096cf1

Browse files
authored
feat: prefer continuous matches (#1888)
* feat: bump frizbee to 0.4.3 * feat: adjust lua scoring to match frizbee * fix: lua fuzzy match assertions
1 parent ed5472b commit 2096cf1

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ crate-type = ["cdylib"]
99

1010
[dependencies]
1111
regex = "1.11.1"
12-
frizbee = "0.4.1"
12+
frizbee = "0.4.3"
1313
serde = { version = "1.0.216", features = ["derive"] }
1414
heed = "0.21.0"
1515
mlua = { version = "0.10.2", features = ["module", "luajit"] }

lua/blink/cmp/fuzzy/lua/match.lua

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
local MATCH_SCORE = 7
2-
local GAP_PENALTY = -1
1+
local MATCH_SCORE = 12
2+
local GAP_OPEN_PENALTY = -5
3+
local GAP_EXTEND_PENALTY = -1
34

45
-- bonus for matching the first character of the haystack
5-
local PREFIX_BONUS = 6
6+
local PREFIX_BONUS = 12
67
-- bonus for matching character after a delimiter in the haystack (e.g. space, comma, underscore, slash, etc)
78
local DELIMITER_BONUS = 4
89
-- bonus for haystack == needle
910
local EXACT_MATCH_BONUS = 4
1011
-- bonus for matching the case (upper or lower) of the haystack
11-
local MATCHING_CASE_BONUS = 1
12+
local MATCHING_CASE_BONUS = 4
1213

1314
local DELIMITERS = {
1415
[string.byte(' ', 1)] = true,
@@ -43,7 +44,10 @@ local function match(needle, haystack)
4344
score = score + MATCH_SCORE
4445

4546
-- gap penalty
46-
if needle_idx ~= 1 then score = score + GAP_PENALTY * (haystack_idx - haystack_start_idx) end
47+
if needle_idx ~= 1 then
48+
local gap_length = haystack_idx - haystack_start_idx
49+
if gap_length > 0 then score = score + GAP_OPEN_PENALTY + GAP_EXTEND_PENALTY * (gap_length - 1) end
50+
end
4751

4852
-- bonuses
4953
if needle_char == haystack_char then score = score + MATCHING_CASE_BONUS end
@@ -69,10 +73,10 @@ local function match(needle, haystack)
6973
return score, exact
7074
end
7175

72-
assert(match('fbb', 'barbazfoobarbaz') == 20, 'fbb should match barbazfoobarbaz with score 18')
73-
assert(match('foo', '_foobar') == 28, 'foo should match foobar with score 29')
74-
assert(match('Foo', 'foobar') == 29, 'foo should match foobar with score 29')
75-
assert(match('foo', 'foobar') == 30, 'foo should match foobar with score 30')
76+
assert(match('fbb', 'barbazfoobarbaz') == 36, 'fbb should match barbazfoobarbaz with score 36')
77+
assert(match('foo', '_foobar') == 52, 'foo should match foobar with score 52')
78+
assert(match('Foo', 'foobar') == 56, 'foo should match foobar with score 56')
79+
assert(match('foo', 'foobar') == 60, 'foo should match foobar with score 60')
7680
assert(match('foo', 'fobar') == nil, 'foo should not match fobar')
7781

7882
return match

0 commit comments

Comments
 (0)