Skip to content

Commit

Permalink
(mini.completion) Respect filterText from LSP response.
Browse files Browse the repository at this point in the history
Details:
- Resolves
  #306 (comment).
  • Loading branch information
echasnovski committed Feb 4, 2024
1 parent 21b3205 commit 9adc2ee
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lua/mini/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ MiniCompletion.config = {
process_items = function(items, base)
local res = vim.tbl_filter(function(item)
-- Keep items which match the base and are not snippets
return vim.startswith(H.get_completion_word(item), base) and item.kind ~= 15
local text = item.filterText or H.get_completion_word(item)
return vim.startswith(text, base) and item.kind ~= 15
end, items)

table.sort(res, function(a, b) return (a.sortText or a.label) < (b.sortText or b.label) end)
Expand Down
19 changes: 14 additions & 5 deletions tests/dir-completion/mock-months-lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,22 @@ local construct_additionTextEdits = function(id, name)
end

local construct_textEdit = function(name)
local pos = _G.mock_textEdit_pos
if pos == nil then return end
if _G.mock_textEdit == nil then return end
local new_text, pos = _G.mock_textEdit.new_text, _G.mock_textEdit.pos
return {
newText = '.' .. name,
newText = new_text(name),
range = {
start = { line = pos[1] - 1, character = pos[2] - 1 },
['end'] = { line = pos[1] - 1, character = pos[2] },
},
}
end

local construct_filterText = function(name)
if _G.mock_filterText == nil then return end
return _G.mock_filterText(name)
end

Months.requests = {
['textDocument/completion'] = function(params)
local items = {}
Expand All @@ -65,8 +70,12 @@ Months.requests = {
if vim.tbl_contains({ 'September', 'November' }, item.name) then
res.additionalTextEdits = construct_additionTextEdits('completion', item.name)
end
-- Mock `textEdit` as in `tsserver` when called after `.`
if vim.tbl_contains({ 'April', 'August' }, item.name) then res.textEdit = construct_textEdit(item.name) end

if vim.tbl_contains({ 'April', 'August' }, item.name) then
res.textEdit = construct_textEdit(item.name)
res.filterText = construct_filterText(item.name)
end

table.insert(items, res)
end

Expand Down
30 changes: 28 additions & 2 deletions tests/test_completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,41 @@ end
T['Manual completion']['prefers completion range from LSP response'] = function()
set_lines({})
type_keys('i', 'months.')
child.lua('_G.mock_textEdit_pos = vim.api.nvim_win_get_cursor(0)')
-- Mock `textEdit` as in `tsserver` when called after `.`
child.lua([[_G.mock_textEdit = {
pos = vim.api.nvim_win_get_cursor(0),
new_text = function(name) return '.' .. name end,
} ]])
type_keys('<C-space>')

eq(get_completion(), { '.April', '.August' })
eq(get_completion('abbr'), { 'April', 'August' })
eq(get_completion('word'), { '.April', '.August' })
type_keys('<C-n>', '<C-y>')
eq(get_lines(), { 'months.April' })
eq(get_cursor(), { 1, 12 })
end

T['Manual completion']['respects `filterText` from LSP response'] = function()
set_lines({})
type_keys('i', 'months.')
-- Mock `textEdit` and `filterText` as in `tsserver` when called after `.`
-- (see https://github.com/echasnovski/mini.nvim/issues/306#issuecomment-1602245446)
child.lua([[
_G.mock_textEdit = {
pos = vim.api.nvim_win_get_cursor(0),
new_text = function(name) return '[' .. name .. ']' end,
}
_G.mock_filterText = function(name) return '.' .. name end
]])
type_keys('<C-space>')

eq(get_completion('abbr'), { 'April', 'August' })
eq(get_completion('word'), { '[April]', '[August]' })
type_keys('<C-n>', '<C-y>')
eq(get_lines(), { 'months[April]' })
eq(get_cursor(), { 1, 13 })
end

T['Manual completion']['respects `vim.{g,b}.minicompletion_disable`'] = new_set({
parametrize = { { 'g' }, { 'b' } },
}, {
Expand Down

0 comments on commit 9adc2ee

Please sign in to comment.