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:
Mathias Fußenegger
2022-08-08 13:02:15 +02:00
committed by GitHub
parent 2d5fce2cdb
commit a46e6afb8b
2 changed files with 25 additions and 22 deletions

View File

@@ -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 },
},
}
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
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,
},
}
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