feat: Add code actions support

Uses the default handler, mapped to 'a'
This commit is contained in:
simrat39
2021-04-24 14:51:28 -07:00
parent 51e1170eec
commit aaae362ae8
2 changed files with 35 additions and 0 deletions

View File

@@ -143,6 +143,10 @@ local function setup_keymaps(bufnr)
vim.api.nvim_buf_set_keymap(bufnr, "n", "r",
":lua require('symbols-outline.rename').rename()<Cr>",
{})
-- code actions
vim.api.nvim_buf_set_keymap(bufnr, "n", "a",
":lua require('symbols-outline.code_action').show_code_actions()<Cr>",
{})
-- close outline when escape is pressed
vim.api.nvim_buf_set_keymap(bufnr, "n", "<Esc>", ":bw!<Cr>",{})
end

View File

@@ -0,0 +1,31 @@
local vim = vim
local main = require('symbols-outline')
local M = {}
local function get_action_params(node, winnr)
local bufnr = vim.api.nvim_win_get_buf(winnr)
local fn = "file://" .. vim.api.nvim_buf_get_name(bufnr)
local pos = {line = node.line, character = node.character}
local diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr, node.line)
return {
textDocument = {uri = fn},
range = {start = pos, ["end"] = pos},
context = {diagnostics = diagnostics},
bufnr = bufnr
}
end
function M.show_code_actions()
local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1]
local node = main.state.flattened_outline_items[current_line]
local params = get_action_params(node, main.state.code_win)
vim.lsp.buf_request(params.bufnr, "textDocument/codeAction", params,
vim.lsp.handlers["textDocument/codeAction"])
end
return M