This repository was archived by the owner on Oct 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathmatching.lua
More file actions
82 lines (70 loc) · 1.81 KB
/
matching.lua
File metadata and controls
82 lines (70 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
local vim = vim
local util = require 'completion.util'
local opt = require 'completion.option'
local M = {}
local function setup_case(prefix, word)
local ignore_case = opt.get_option('matching_ignore_case')
if ignore_case and opt.get_option('matching_smart_case') and prefix:match('[A-Z]') then
ignore_case = false
end
if ignore_case then
return string.lower(prefix), string.lower(word)
end
return prefix, word
end
local function fuzzy_match(prefix, word)
prefix, word = setup_case(prefix, word)
local score = util.fuzzy_score(prefix, word)
if score < 1 then
return true, score
else
return false
end
end
local function substring_match(prefix, word)
prefix, word = setup_case(prefix, word)
if string.find(word, prefix, 1, true) then
return true
else
return false
end
end
local function exact_match(prefix, word)
prefix, word = setup_case(prefix, word)
if vim.startswith(word, prefix) then
return true
else
return false
end
end
local function all_match()
return true
end
local matching_strategy = {
fuzzy = fuzzy_match,
substring = substring_match,
exact = exact_match,
all = all_match,
}
M.matching = function(complete_items, prefix, item)
local matcher_list = opt.get_option('matching_strategy_list')
local matching_priority = 2
for _, method in ipairs(matcher_list) do
local is_match, score = matching_strategy[method](prefix, item.word)
if is_match then
if item.abbr == nil then
item.abbr = item.word
end
item.score = score
if item.priority ~= nil then
item.priority = item.priority + 10*matching_priority
else
item.priority = 10*matching_priority
end
util.addCompletionItems(complete_items, item)
break
end
matching_priority = matching_priority - 1
end
end
return M