mirror of
https://github.com/neovim/neovim.git
synced 2025-12-18 12:25:34 +00:00
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:
@@ -1,9 +1,7 @@
|
|||||||
local vim = vim
|
local vim = vim
|
||||||
local validate = vim.validate
|
local validate = vim.validate
|
||||||
local api = vim.api
|
|
||||||
local vfn = vim.fn
|
local vfn = vim.fn
|
||||||
local util = require 'vim.lsp.util'
|
local util = require 'vim.lsp.util'
|
||||||
local list_extend = vim.list_extend
|
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@@ -154,36 +152,14 @@ end
|
|||||||
--@param start_pos ({number, number}, optional) mark-indexed position.
|
--@param start_pos ({number, number}, optional) mark-indexed position.
|
||||||
---Defaults to the end of the last visual selection.
|
---Defaults to the end of the last visual selection.
|
||||||
function M.range_formatting(options, start_pos, end_pos)
|
function M.range_formatting(options, start_pos, end_pos)
|
||||||
validate {
|
validate { options = {options, 't', true} }
|
||||||
options = {options, 't', true};
|
|
||||||
start_pos = {start_pos, 't', true};
|
|
||||||
end_pos = {end_pos, 't', true};
|
|
||||||
}
|
|
||||||
local sts = vim.bo.softtabstop;
|
local sts = vim.bo.softtabstop;
|
||||||
options = vim.tbl_extend('keep', options or {}, {
|
options = vim.tbl_extend('keep', options or {}, {
|
||||||
tabSize = (sts > 0 and sts) or (sts < 0 and vim.bo.shiftwidth) or vim.bo.tabstop;
|
tabSize = (sts > 0 and sts) or (sts < 0 and vim.bo.shiftwidth) or vim.bo.tabstop;
|
||||||
insertSpaces = vim.bo.expandtab;
|
insertSpaces = vim.bo.expandtab;
|
||||||
})
|
})
|
||||||
local A = list_extend({}, start_pos or api.nvim_buf_get_mark(0, '<'))
|
local params = util.make_given_range_params(start_pos, end_pos)
|
||||||
local B = list_extend({}, end_pos or api.nvim_buf_get_mark(0, '>'))
|
params.options = options
|
||||||
-- 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;
|
|
||||||
}
|
|
||||||
return request('textDocument/rangeFormatting', params)
|
return request('textDocument/rangeFormatting', params)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -308,6 +284,21 @@ function M.code_action(context)
|
|||||||
request('textDocument/codeAction', params)
|
request('textDocument/codeAction', params)
|
||||||
end
|
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.
|
--- Executes an LSP server command.
|
||||||
---
|
---
|
||||||
--@param command A valid `ExecuteCommandParams` object
|
--@param command A valid `ExecuteCommandParams` object
|
||||||
|
|||||||
@@ -1465,11 +1465,46 @@ end
|
|||||||
function M.make_range_params()
|
function M.make_range_params()
|
||||||
local position = make_position_param()
|
local position = make_position_param()
|
||||||
return {
|
return {
|
||||||
textDocument = { uri = vim.uri_from_bufnr(0) },
|
textDocument = M.make_text_document_params(),
|
||||||
range = { start = position; ["end"] = position; }
|
range = { start = position; ["end"] = position; }
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Using the given range in the current buffer, creates an object that
|
||||||
|
--- is similar to |vim.lsp.util.make_range_params()|.
|
||||||
|
---
|
||||||
|
--@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.
|
||||||
|
--@returns { textDocument = { uri = `current_file_uri` }, range = { start =
|
||||||
|
---`start_position`, end = `end_position` } }
|
||||||
|
function M.make_given_range_params(start_pos, end_pos)
|
||||||
|
validate {
|
||||||
|
start_pos = {start_pos, 't', true};
|
||||||
|
end_pos = {end_pos, 't', true};
|
||||||
|
}
|
||||||
|
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], M.character_offset(0, A[1], A[2])}
|
||||||
|
end
|
||||||
|
if B[2] > 0 then
|
||||||
|
B = {B[1], M.character_offset(0, B[1], B[2])}
|
||||||
|
end
|
||||||
|
return {
|
||||||
|
textDocument = M.make_text_document_params(),
|
||||||
|
range = {
|
||||||
|
start = {line = A[1], character = A[2]},
|
||||||
|
['end'] = {line = B[1], character = B[2]}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
--- Creates a `TextDocumentIdentifier` object for the current buffer.
|
--- Creates a `TextDocumentIdentifier` object for the current buffer.
|
||||||
---
|
---
|
||||||
--@returns `TextDocumentIdentifier`
|
--@returns `TextDocumentIdentifier`
|
||||||
|
|||||||
Reference in New Issue
Block a user