Skip to content

Commit 9adc2ee

Browse files
committed
(mini.completion) Respect filterText from LSP response.
Details: - Resolves #306 (comment).
1 parent 21b3205 commit 9adc2ee

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

lua/mini/completion.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ MiniCompletion.config = {
243243
process_items = function(items, base)
244244
local res = vim.tbl_filter(function(item)
245245
-- Keep items which match the base and are not snippets
246-
return vim.startswith(H.get_completion_word(item), base) and item.kind ~= 15
246+
local text = item.filterText or H.get_completion_word(item)
247+
return vim.startswith(text, base) and item.kind ~= 15
247248
end, items)
248249

249250
table.sort(res, function(a, b) return (a.sortText or a.label) < (b.sortText or b.label) end)

tests/dir-completion/mock-months-lsp.lua

+14-5
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,22 @@ local construct_additionTextEdits = function(id, name)
4545
end
4646

4747
local construct_textEdit = function(name)
48-
local pos = _G.mock_textEdit_pos
49-
if pos == nil then return end
48+
if _G.mock_textEdit == nil then return end
49+
local new_text, pos = _G.mock_textEdit.new_text, _G.mock_textEdit.pos
5050
return {
51-
newText = '.' .. name,
51+
newText = new_text(name),
5252
range = {
5353
start = { line = pos[1] - 1, character = pos[2] - 1 },
5454
['end'] = { line = pos[1] - 1, character = pos[2] },
5555
},
5656
}
5757
end
5858

59+
local construct_filterText = function(name)
60+
if _G.mock_filterText == nil then return end
61+
return _G.mock_filterText(name)
62+
end
63+
5964
Months.requests = {
6065
['textDocument/completion'] = function(params)
6166
local items = {}
@@ -65,8 +70,12 @@ Months.requests = {
6570
if vim.tbl_contains({ 'September', 'November' }, item.name) then
6671
res.additionalTextEdits = construct_additionTextEdits('completion', item.name)
6772
end
68-
-- Mock `textEdit` as in `tsserver` when called after `.`
69-
if vim.tbl_contains({ 'April', 'August' }, item.name) then res.textEdit = construct_textEdit(item.name) end
73+
74+
if vim.tbl_contains({ 'April', 'August' }, item.name) then
75+
res.textEdit = construct_textEdit(item.name)
76+
res.filterText = construct_filterText(item.name)
77+
end
78+
7079
table.insert(items, res)
7180
end
7281

tests/test_completion.lua

+28-2
Original file line numberDiff line numberDiff line change
@@ -471,15 +471,41 @@ end
471471
T['Manual completion']['prefers completion range from LSP response'] = function()
472472
set_lines({})
473473
type_keys('i', 'months.')
474-
child.lua('_G.mock_textEdit_pos = vim.api.nvim_win_get_cursor(0)')
474+
-- Mock `textEdit` as in `tsserver` when called after `.`
475+
child.lua([[_G.mock_textEdit = {
476+
pos = vim.api.nvim_win_get_cursor(0),
477+
new_text = function(name) return '.' .. name end,
478+
} ]])
475479
type_keys('<C-space>')
476480

477-
eq(get_completion(), { '.April', '.August' })
481+
eq(get_completion('abbr'), { 'April', 'August' })
482+
eq(get_completion('word'), { '.April', '.August' })
478483
type_keys('<C-n>', '<C-y>')
479484
eq(get_lines(), { 'months.April' })
480485
eq(get_cursor(), { 1, 12 })
481486
end
482487

488+
T['Manual completion']['respects `filterText` from LSP response'] = function()
489+
set_lines({})
490+
type_keys('i', 'months.')
491+
-- Mock `textEdit` and `filterText` as in `tsserver` when called after `.`
492+
-- (see https://github.com/echasnovski/mini.nvim/issues/306#issuecomment-1602245446)
493+
child.lua([[
494+
_G.mock_textEdit = {
495+
pos = vim.api.nvim_win_get_cursor(0),
496+
new_text = function(name) return '[' .. name .. ']' end,
497+
}
498+
_G.mock_filterText = function(name) return '.' .. name end
499+
]])
500+
type_keys('<C-space>')
501+
502+
eq(get_completion('abbr'), { 'April', 'August' })
503+
eq(get_completion('word'), { '[April]', '[August]' })
504+
type_keys('<C-n>', '<C-y>')
505+
eq(get_lines(), { 'months[April]' })
506+
eq(get_cursor(), { 1, 13 })
507+
end
508+
483509
T['Manual completion']['respects `vim.{g,b}.minicompletion_disable`'] = new_set({
484510
parametrize = { { 'g' }, { 'b' } },
485511
}, {

0 commit comments

Comments
 (0)