diff options
author | Maria José Solano <majosolano99@gmail.com> | 2024-02-26 11:42:51 -0800 |
---|---|---|
committer | Christian Clason <c.clason@uni-graz.at> | 2024-02-27 16:50:51 +0100 |
commit | 63f9c2da9aab52fa698fcbfdbc58ffd41794d28a (patch) | |
tree | 37742567d493c54245e6e0b1004f5d18b4319945 /runtime/lua | |
parent | 3d96e3f9f25389e979bb7f2417ec2135f79fbfbb (diff) | |
download | rneovim-63f9c2da9aab52fa698fcbfdbc58ffd41794d28a.tar.gz rneovim-63f9c2da9aab52fa698fcbfdbc58ffd41794d28a.tar.bz2 rneovim-63f9c2da9aab52fa698fcbfdbc58ffd41794d28a.zip |
feat(lsp): support completion itemDefaults
Diffstat (limited to 'runtime/lua')
-rw-r--r-- | runtime/lua/vim/lsp/_completion.lua | 40 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/protocol.lua | 8 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/util.lua | 4 |
3 files changed, 51 insertions, 1 deletions
diff --git a/runtime/lua/vim/lsp/_completion.lua b/runtime/lua/vim/lsp/_completion.lua index 3a38c1b5e1..a169f96565 100644 --- a/runtime/lua/vim/lsp/_completion.lua +++ b/runtime/lua/vim/lsp/_completion.lua @@ -6,6 +6,14 @@ local ms = protocol.Methods --- @alias vim.lsp.CompletionResult lsp.CompletionList | lsp.CompletionItem[] +-- TODO(mariasolos): Remove this declaration once we figure out a better way to handle +-- literal/anonymous types (see https://github.com/neovim/neovim/pull/27542/files#r1495259331). +--- @class lsp.ItemDefaults +--- @field editRange lsp.Range | { insert: lsp.Range, replace: lsp.Range } | nil +--- @field insertTextFormat lsp.InsertTextFormat? +--- @field insertTextMode lsp.InsertTextMode? +--- @field data any + ---@param input string unparsed snippet ---@return string parsed snippet local function parse_snippet(input) @@ -39,13 +47,43 @@ local function get_completion_word(item) return item.label end +--- Applies the given defaults to the completion item, modifying it in place. +--- +--- @param item lsp.CompletionItem +--- @param defaults lsp.ItemDefaults? +local function apply_defaults(item, defaults) + if not defaults then + return + end + + item.insertTextFormat = item.insertTextFormat or defaults.insertTextFormat + item.insertTextMode = item.insertTextMode or defaults.insertTextMode + item.data = item.data or defaults.data + if defaults.editRange then + local textEdit = item.textEdit or {} + item.textEdit = textEdit + textEdit.newText = textEdit.newText or item.textEditText or item.insertText + if defaults.editRange.start then + textEdit.range = textEdit.range or defaults.editRange + elseif defaults.editRange.insert then + textEdit.insert = defaults.editRange.insert + textEdit.replace = defaults.editRange.replace + end + end +end + ---@param result vim.lsp.CompletionResult ---@return lsp.CompletionItem[] local function get_items(result) if result.items then + for _, item in ipairs(result.items) do + ---@diagnostic disable-next-line: param-type-mismatch + apply_defaults(item, result.itemDefaults) + end return result.items + else + return result end - return result end --- Turns the result of a `textDocument/completion` request into vim-compatible diff --git a/runtime/lua/vim/lsp/protocol.lua b/runtime/lua/vim/lsp/protocol.lua index 82e8c4a7de..fb41311961 100644 --- a/runtime/lua/vim/lsp/protocol.lua +++ b/runtime/lua/vim/lsp/protocol.lua @@ -764,6 +764,14 @@ function protocol.make_client_capabilities() return res end)(), }, + completionList = { + itemDefaults = { + 'editRange', + 'insertTextFormat', + 'insertTextMode', + 'data', + }, + }, -- TODO(tjdevries): Implement this contextSupport = false, diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index e371cb0e15..3973e606f8 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -551,6 +551,10 @@ end --- Can be used to extract the completion items from a --- `textDocument/completion` request, which may return one of --- `CompletionItem[]`, `CompletionList` or null. +--- +--- Note that this method doesn't apply `itemDefaults` to `CompletionList`s, and hence the returned +--- results might be incorrect. +--- ---@deprecated ---@param result table The result of a `textDocument/completion` request ---@return lsp.CompletionItem[] List of completion items |