aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/callbacks.lua
diff options
context:
space:
mode:
authorJesse <github@jessebakker.com>2020-05-16 01:18:59 +0200
committerGitHub <noreply@github.com>2020-05-16 01:18:59 +0200
commitf559e5249e3aa155687b335272da8f0c73255ee4 (patch)
tree1f2ebdabff710220c717011c57d386fae2cdfd17 /runtime/lua/vim/lsp/callbacks.lua
parentc37d9fa3da3cea303915f29e75665f50c4c13b41 (diff)
downloadrneovim-f559e5249e3aa155687b335272da8f0c73255ee4.tar.gz
rneovim-f559e5249e3aa155687b335272da8f0c73255ee4.tar.bz2
rneovim-f559e5249e3aa155687b335272da8f0c73255ee4.zip
LSP: Add textDocument/codeAction support (#11607)
* Add textDocument/codeAction * Add callback for workspace/executeCommand * Escape newlines in codeAction titles * Return empty list in get_line_diagnostics if no buffer diagnostics * Add stub documentation * Validate context parameter in code_action * Add support for edit in CodeAction responses * Group diagnostics by line in vim.lsp.util.get_line_diagnostics() * Advertise code action literal support
Diffstat (limited to 'runtime/lua/vim/lsp/callbacks.lua')
-rw-r--r--runtime/lua/vim/lsp/callbacks.lua40
1 files changed, 40 insertions, 0 deletions
diff --git a/runtime/lua/vim/lsp/callbacks.lua b/runtime/lua/vim/lsp/callbacks.lua
index 09ca4b61e4..17f88a5181 100644
--- a/runtime/lua/vim/lsp/callbacks.lua
+++ b/runtime/lua/vim/lsp/callbacks.lua
@@ -3,6 +3,7 @@ local protocol = require 'vim.lsp.protocol'
local util = require 'vim.lsp.util'
local vim = vim
local api = vim.api
+local buf = require 'vim.lsp.buf'
local M = {}
@@ -11,6 +12,45 @@ local function err_message(...)
api.nvim_command("redraw")
end
+M['workspace/executeCommand'] = function(err, _)
+ if err then
+ error("Could not execute code action: "..err.message)
+ end
+end
+
+M['textDocument/codeAction'] = function(_, _, actions)
+ if vim.tbl_isempty(actions) then
+ print("No code actions available")
+ return
+ end
+
+ local option_strings = {"Code Actions:"}
+ for i, action in ipairs(actions) do
+ local title = action.title:gsub('\r\n', '\\r\\n')
+ title = title:gsub('\n', '\\n')
+ table.insert(option_strings, string.format("%d. %s", i, title))
+ end
+
+ local choice = vim.fn.inputlist(option_strings)
+ if choice < 1 or choice > #actions then
+ return
+ end
+ local action_chosen = actions[choice]
+ -- textDocument/codeAction can return either Command[] or CodeAction[].
+ -- If it is a CodeAction, it can have either an edit, a command or both.
+ -- Edits should be executed first
+ if action_chosen.edit or type(action_chosen.command) == "table" then
+ if action_chosen.edit then
+ util.apply_workspace_edit(action_chosen.edit)
+ end
+ if type(action_chosen.command) == "table" then
+ buf.execute_command(action_chosen.command)
+ end
+ else
+ buf.execute_command(action_chosen)
+ end
+end
+
M['workspace/applyEdit'] = function(_, _, workspace_edit)
if not workspace_edit then return end
-- TODO(ashkan) Do something more with label?