summaryrefslogtreecommitdiff
path: root/lua/telescope/builtin/lsp.lua
diff options
context:
space:
mode:
authorMatsu <huhta.matias@gmail.com>2021-09-15 19:31:51 +0300
committerGitHub <noreply@github.com>2021-09-15 18:31:51 +0200
commitaaffa84ebb0d411eafe2347e2ee0424af1199219 (patch)
tree885a1e84446dc9cf66935846f3db8ae7ea813663 /lua/telescope/builtin/lsp.lua
parente7aa63db8fea4983658d03b5000d59f75259638b (diff)
feat: Remove version field if zero from codeaction calls (#738)
should fix code actions for jdtls Co-authored-by: TJ DeVries <devries.timothyj@gmail.com>
Diffstat (limited to 'lua/telescope/builtin/lsp.lua')
-rw-r--r--lua/telescope/builtin/lsp.lua77
1 files changed, 65 insertions, 12 deletions
diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua
index e5b183e..59cd1f9 100644
--- a/lua/telescope/builtin/lsp.lua
+++ b/lua/telescope/builtin/lsp.lua
@@ -217,6 +217,68 @@ lsp.code_actions = function(opts)
}
end
+ -- If the text document version is 0, set it to nil instead so that Neovim
+ -- won't refuse to update a buffer that it believes is newer than edits.
+ -- See: https://github.com/eclipse/eclipse.jdt.ls/issues/1695
+ -- Source:
+ -- https://github.com/neovim/nvim-lspconfig/blob/486f72a25ea2ee7f81648fdfd8999a155049e466/lua/lspconfig/jdtls.lua#L62
+ local function fix_zero_version(workspace_edit)
+ if workspace_edit and workspace_edit.documentChanges then
+ for _, change in pairs(workspace_edit.documentChanges) do
+ local text_document = change.textDocument
+ if text_document and text_document.version and text_document.version == 0 then
+ text_document.version = nil
+ end
+ end
+ end
+ return workspace_edit
+ end
+
+ --[[
+ -- actions is (Command | CodeAction)[] | null
+ -- CodeAction
+ -- title: String
+ -- kind?: CodeActionKind
+ -- diagnostics?: Diagnostic[]
+ -- isPreferred?: boolean
+ -- edit?: WorkspaceEdit
+ -- command?: Command
+ --
+ -- Command
+ -- title: String
+ -- command: String
+ -- arguments?: any[]
+ --]]
+ local transform_action = opts.transform_action
+ or function(action)
+ -- Remove 0 -version from LSP codeaction request payload.
+ -- Is only run on lsp codeactions which contain a comand or a arguments field
+ -- Fixed Java/jdtls compatibility with Telescope
+ -- See fix_zero_version commentary for more information
+ if action.command or action.arguments then
+ if action.command.command then
+ action.edit = fix_zero_version(action.command.arguments[1])
+ else
+ action.edit = fix_zero_version(action.arguments[1])
+ end
+ end
+ return action
+ end
+
+ local execute_action = opts.execute_action
+ or function(action)
+ if action.edit or type(action.command) == "table" then
+ if action.edit then
+ vim.lsp.util.apply_workspace_edit(action.edit)
+ end
+ if type(action.command) == "table" then
+ vim.lsp.buf.execute_command(action.command)
+ end
+ else
+ vim.lsp.buf.execute_command(action)
+ end
+ end
+
pickers.new(opts, {
prompt_title = "LSP Code Actions",
finder = finders.new_table {
@@ -237,18 +299,9 @@ lsp.code_actions = function(opts)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
- local val = selection.value
-
- if val.edit or type(val.command) == "table" then
- if val.edit then
- vim.lsp.util.apply_workspace_edit(val.edit)
- end
- if type(val.command) == "table" then
- vim.lsp.buf.execute_command(val.command)
- end
- else
- vim.lsp.buf.execute_command(val)
- end
+ local action = selection.value
+
+ execute_action(transform_action(action))
end)
return true