lsp: Add vim.lsp.buf.range_code_action() (#12962)

Allows to execute code_action for a given range.
:'<,'>lua vim.lsp.buf.range_code_action()
This commit is contained in:
notomo
2020-09-25 04:53:08 +09:00
committed by GitHub
parent f1fbb9e9ca
commit 4a996bc431
2 changed files with 54 additions and 28 deletions

View File

@@ -1,9 +1,7 @@
local vim = vim
local validate = vim.validate
local api = vim.api
local vfn = vim.fn
local util = require 'vim.lsp.util'
local list_extend = vim.list_extend
local M = {}
@@ -154,36 +152,14 @@ end
--@param start_pos ({number, number}, optional) mark-indexed position.
---Defaults to the end of the last visual selection.
function M.range_formatting(options, start_pos, end_pos)
validate {
options = {options, 't', true};
start_pos = {start_pos, 't', true};
end_pos = {end_pos, 't', true};
}
validate { options = {options, 't', true} }
local sts = vim.bo.softtabstop;
options = vim.tbl_extend('keep', options or {}, {
tabSize = (sts > 0 and sts) or (sts < 0 and vim.bo.shiftwidth) or vim.bo.tabstop;
insertSpaces = vim.bo.expandtab;
})
local A = list_extend({}, start_pos or api.nvim_buf_get_mark(0, '<'))
local B = list_extend({}, end_pos or api.nvim_buf_get_mark(0, '>'))
-- convert to 0-index
A[1] = A[1] - 1
B[1] = B[1] - 1
-- account for encoding.
if A[2] > 0 then
A = {A[1], util.character_offset(0, A[1], A[2])}
end
if B[2] > 0 then
B = {B[1], util.character_offset(0, B[1], B[2])}
end
local params = {
textDocument = { uri = vim.uri_from_bufnr(0) };
range = {
start = { line = A[1]; character = A[2]; };
["end"] = { line = B[1]; character = B[2]; };
};
options = options;
}
local params = util.make_given_range_params(start_pos, end_pos)
params.options = options
return request('textDocument/rangeFormatting', params)
end
@@ -308,6 +284,21 @@ function M.code_action(context)
request('textDocument/codeAction', params)
end
--- Performs |vim.lsp.buf.code_action()| for a given range.
---
--@param context: (table, optional) Valid `CodeActionContext` object
--@param start_pos ({number, number}, optional) mark-indexed position.
---Defaults to the start of the last visual selection.
--@param end_pos ({number, number}, optional) mark-indexed position.
---Defaults to the end of the last visual selection.
function M.range_code_action(context, start_pos, end_pos)
validate { context = { context, 't', true } }
context = context or { diagnostics = util.get_line_diagnostics() }
local params = util.make_given_range_params(start_pos, end_pos)
params.context = context
request('textDocument/codeAction', params)
end
--- Executes an LSP server command.
---
--@param command A valid `ExecuteCommandParams` object