mirror of
https://github.com/neovim/neovim.git
synced 2025-10-26 12:27:24 +00:00
fix(lsp): set end_col in formatexpr (#19676)
The last line was excluded from formatting via formatexpr because the character in the params was set to 0 instead of the end of line.
This commit is contained in:
committed by
GitHub
parent
2d5fce2cdb
commit
a46e6afb8b
@@ -1982,29 +1982,32 @@ function lsp.formatexpr(opts)
|
||||
return 1
|
||||
end
|
||||
|
||||
local start_line = vim.v.lnum
|
||||
local end_line = start_line + vim.v.count - 1
|
||||
local start_lnum = vim.v.lnum
|
||||
local end_lnum = start_lnum + vim.v.count - 1
|
||||
|
||||
if start_line > 0 and end_line > 0 then
|
||||
local params = {
|
||||
textDocument = util.make_text_document_params(),
|
||||
range = {
|
||||
start = { line = start_line - 1, character = 0 },
|
||||
['end'] = { line = end_line - 1, character = 0 },
|
||||
if start_lnum <= 0 or end_lnum <= 0 then
|
||||
return 0
|
||||
end
|
||||
local bufnr = api.nvim_get_current_buf()
|
||||
for _, client in pairs(lsp.get_active_clients({ bufnr = bufnr })) do
|
||||
if client.supports_method('textDocument/rangeFormatting') then
|
||||
local params = util.make_formatting_params()
|
||||
local end_line = vim.fn.getline(end_lnum)
|
||||
local end_col = util._str_utfindex_enc(end_line, nil, client.offset_encoding)
|
||||
params.range = {
|
||||
start = {
|
||||
line = start_lnum - 1,
|
||||
character = 0,
|
||||
},
|
||||
['end'] = {
|
||||
line = end_lnum - 1,
|
||||
character = end_col,
|
||||
},
|
||||
}
|
||||
params.options = util.make_formatting_params().options
|
||||
local client_results =
|
||||
vim.lsp.buf_request_sync(0, 'textDocument/rangeFormatting', params, timeout_ms)
|
||||
|
||||
-- Apply the text edits from one and only one of the clients.
|
||||
for client_id, response in pairs(client_results) do
|
||||
local response =
|
||||
client.request_sync('textDocument/rangeFormatting', params, timeout_ms, bufnr)
|
||||
if response.result then
|
||||
vim.lsp.util.apply_text_edits(
|
||||
response.result,
|
||||
0,
|
||||
vim.lsp.get_client_by_id(client_id).offset_encoding
|
||||
)
|
||||
vim.lsp.util.apply_text_edits(response.result, 0, client.offset_encoding)
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
@@ -113,7 +113,7 @@ end
|
||||
--- Convert byte index to `encoding` index.
|
||||
--- Convenience wrapper around vim.str_utfindex
|
||||
---@param line string line to be indexed
|
||||
---@param index number byte index (utf-8), or `nil` for length
|
||||
---@param index number|nil byte index (utf-8), or `nil` for length
|
||||
---@param encoding string utf-8|utf-16|utf-32|nil defaults to utf-16
|
||||
---@return number `encoding` index of `index` in `line`
|
||||
function M._str_utfindex_enc(line, index, encoding)
|
||||
|
||||
Reference in New Issue
Block a user