Fix fuzzy matching option check and matched position

This commit is contained in:
hrsh7th
2023-02-15 00:30:03 +09:00
parent 1b2e843a69
commit a926412e16
2 changed files with 22 additions and 17 deletions

View File

@@ -128,17 +128,12 @@ matcher.match = function(input, word, option)
end
if #matches == 0 then
matches = {
{
index = 1,
input_match_start = 1,
input_match_end = 1,
word_match_start = 1,
word_match_end = 1,
strict_ratio = 0,
fuzzy = false,
}
}
if not option.disallow_fuzzy_matching and not option.disallow_prefix_unmatching and not option.disallow_partial_fuzzy_matching then
if matcher.fuzzy(input, word, matches) then
return 1, matches
end
end
return 0, {}
end
matcher.debug(word, matches)
@@ -203,10 +198,9 @@ end
--- fuzzy
matcher.fuzzy = function(input, word, matches)
local last_match = matches[#matches]
local input_index = matches[1] and (matches[1].input_match_end + 1) or 1
-- Lately specified middle of text.
local input_index = last_match.input_match_end + 1
for i = 1, #matches - 1 do
local curr_match = matches[i]
local next_match = matches[i + 1]
@@ -227,7 +221,7 @@ matcher.fuzzy = function(input, word, matches)
local last_input_index = input_index
local matched = false
local word_offset = 0
local word_index = last_match.word_match_end + 1
local word_index = matches[1] and (matches[1].word_match_end + 1) or 1
local input_match_start = -1
local input_match_end = -1
local word_match_start = -1

View File

@@ -37,12 +37,23 @@ describe('matcher', function()
assert.is.truthy(matcher.match('Unit', 'net.UnixListener', { disallow_partial_fuzzy_matching = true }) == 0)
assert.is.truthy(matcher.match('Unit', 'net.UnixListener', { disallow_partial_fuzzy_matching = false }) >= 1)
assert.is.truthy(matcher.match('tail', 'HCDetails', {
local score, matches
score, matches = matcher.match('tail', 'HCDetails', {
disallow_fuzzy_matching = false,
disallow_partial_matching = false,
disallow_prefix_unmatching = false,
disallow_partial_fuzzy_matching = false,
}) >= 1)
})
assert.is.truthy(score >= 1)
assert.equals(matches[1].word_match_start, 5)
score, matches = matcher.match('tail', 'HCDetails', {
disallow_fuzzy_matching = false,
disallow_partial_matching = false,
disallow_prefix_unmatching = false,
disallow_partial_fuzzy_matching = true,
})
assert.is.truthy(score == 0)
end)
it('disallow_fuzzy_matching', function()