Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit 6354de7

Browse files
committed
feat: fuzzy match
1 parent 7ebf6e0 commit 6354de7

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

lua/completion/util.lua

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,28 @@ local function get_completion_word(item)
2525
return item.label
2626
end
2727

28-
local function remove_unmatch_completion_items(items, prefix)
29-
return vim.tbl_filter(function(item)
30-
local word = get_completion_word(item)
31-
return vim.startswith(word, prefix)
32-
end, items)
33-
end
34-
3528
function M.sort_completion_items(items)
3629
table.sort(items, function(a, b)
3730
if a.priority ~= b.priority and a.priority ~= nil and b.priority ~= nil then
3831
return a.priority > b.priority
39-
elseif vim.g.completion_sorting == 'alphabet' then
40-
return a.word < b.word
4132
elseif a.score ~= b.score and a.score ~= nil and b.score ~= nil then
4233
return a.score < b.score
34+
elseif vim.g.completion_sorting == 'alphabet' then
35+
return a.word < b.word
4336
else
4437
return string.len(a.word) < string.len(b.word)
4538
end
4639
end)
4740
end
4841

49-
function M.text_document_completion_list_to_complete_items(result, prefix)
42+
function M.text_document_completion_list_to_complete_items(result, prefix, score_func)
5043
local items = vim.lsp.util.extract_completion_items(result)
5144
if vim.tbl_isempty(items) then
5245
return {}
5346
end
5447

5548
local customize_label = vim.g.completion_customize_lsp_label
56-
items = remove_unmatch_completion_items(items, prefix)
49+
-- items = remove_unmatch_completion_items(items, prefix)
5750
-- sort_completion_items(items)
5851

5952
local matches = {}
@@ -82,18 +75,39 @@ function M.text_document_completion_list_to_complete_items(result, prefix)
8275
}
8376
local kind = protocol.CompletionItemKind[completion_item.kind]
8477
local priority = vim.g.completion_items_priority[kind] or 1
85-
table.insert(matches, {
86-
word = word,
87-
abbr = completion_item.label,
88-
kind = customize_label[kind] or kind or '',
89-
menu = completion_item.detail or '',
90-
info = info,
91-
priority = priority,
92-
icase = 1,
93-
user_data = user_data,
94-
dup = 1,
95-
empty = 1,
96-
})
78+
if vim.g.completion_fuzzy_match == 1 then
79+
local score = score_func(prefix, word)
80+
if score <= 1 then
81+
table.insert(matches, {
82+
word = word,
83+
abbr = completion_item.label,
84+
kind = customize_label[kind] or kind or '',
85+
menu = completion_item.detail or '',
86+
info = info,
87+
priority = priority,
88+
score = score,
89+
icase = 1,
90+
user_data = user_data,
91+
dup = 1,
92+
empty = 1,
93+
})
94+
end
95+
else
96+
if vim.startswith(word, prefix) then
97+
table.insert(matches, {
98+
word = word,
99+
abbr = completion_item.label,
100+
kind = customize_label[kind] or kind or '',
101+
menu = completion_item.detail or '',
102+
info = info,
103+
priority = priority,
104+
icase = 1,
105+
user_data = user_data,
106+
dup = 1,
107+
empty = 1,
108+
})
109+
end
110+
end
97111
end
98112
end
99113

lua/source/lsp.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ M.triggerFunction = function(prefix, _, bufnr, _)
2727
return
2828
end
2929
if api.nvim_get_mode()['mode'] == 'i' or api.nvim_get_mode()['mode'] == 'ic' then
30-
local matches = util.text_document_completion_list_to_complete_items(result, prefix)
30+
local matches = util.text_document_completion_list_to_complete_items(result, prefix, util.fuzzy_score)
3131
M.items = matches
3232
end
3333
M.callback = true

plugin/completion.vim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ if ! exists('g:completion_sorting')
6969
let g:completion_sorting = 'alphabet'
7070
endif
7171

72-
if ! exists('g:completion_sorted_alphabetically')
73-
let g:completion_sorted_alphabetically = v:false
74-
endif
75-
7672
if ! exists('g:completion_max_items')
7773
let g:completion_max_items = v:null
7874
endif
7975

76+
if ! exists('g:completion_fuzzy_match')
77+
let g:completion_fuzzy_match = 1
78+
endif
79+
8080
if ! exists('g:completion_chain_complete_list')
8181
let g:completion_chain_complete_list = {
8282
\ 'default' : {

0 commit comments

Comments
 (0)