aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/completion.lua
diff options
context:
space:
mode:
authorMathias Fussenegger <f.mathias@zignar.net>2024-05-28 23:20:25 +0200
committerMathias Fußenegger <mfussenegger@users.noreply.github.com>2024-05-30 09:24:24 +0200
commitb2bad0ac91ddb9b33c3547b6fd4f7278794818d9 (patch)
tree0dcf74de9a8ebbd54ceec230b11698fd9c4977e5 /runtime/lua/vim/lsp/completion.lua
parent0df2c6b5d09fab392dd1a14e4b2e6a3b03203aaa (diff)
downloadrneovim-b2bad0ac91ddb9b33c3547b6fd4f7278794818d9.tar.gz
rneovim-b2bad0ac91ddb9b33c3547b6fd4f7278794818d9.tar.bz2
rneovim-b2bad0ac91ddb9b33c3547b6fd4f7278794818d9.zip
feat(lsp): support postfix snippets in completion
Diffstat (limited to 'runtime/lua/vim/lsp/completion.lua')
-rw-r--r--runtime/lua/vim/lsp/completion.lua35
1 files changed, 25 insertions, 10 deletions
diff --git a/runtime/lua/vim/lsp/completion.lua b/runtime/lua/vim/lsp/completion.lua
index f9b0563b86..39c0c5fa29 100644
--- a/runtime/lua/vim/lsp/completion.lua
+++ b/runtime/lua/vim/lsp/completion.lua
@@ -129,18 +129,33 @@ end
--- @param item lsp.CompletionItem
--- @return string
local function get_completion_word(item)
- if item.textEdit ~= nil and item.textEdit.newText ~= nil and item.textEdit.newText ~= '' then
- if item.insertTextFormat == protocol.InsertTextFormat.PlainText then
- return item.textEdit.newText
- else
- return parse_snippet(item.textEdit.newText)
- end
- elseif item.insertText ~= nil and item.insertText ~= '' then
- if item.insertTextFormat == protocol.InsertTextFormat.PlainText then
- return item.insertText
- else
+ if item.insertTextFormat == protocol.InsertTextFormat.Snippet then
+ if item.textEdit then
+ -- Use label instead of text if text has different starting characters.
+ -- label is used as abbr (=displayed), but word is used for filtering
+ -- This is required for things like postfix completion.
+ -- E.g. in lua:
+ --
+ -- local f = {}
+ -- f@|
+ -- ▲
+ -- └─ cursor
+ --
+ -- item.textEdit.newText: table.insert(f, $0)
+ -- label: insert
+ --
+ -- Typing `i` would remove the candidate because newText starts with `t`.
+ local text = item.insertText or item.textEdit.newText
+ return #text < #item.label and text or item.label
+ elseif item.insertText and item.insertText ~= '' then
return parse_snippet(item.insertText)
+ else
+ return item.label
end
+ elseif item.textEdit then
+ return item.textEdit.newText
+ elseif item.insertText and item.insertText ~= '' then
+ return item.insertText
end
return item.label
end