Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lsp): use line start/end for visual line selection #22632

Merged
merged 3 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! fix(lsp): use line start/end for visual line selection
  • Loading branch information
mfussenegger committed Mar 12, 2023
commit 2f4487fb87b9d041018987db95628051ebb8f0f9
10 changes: 5 additions & 5 deletions runtime/lua/vim/lsp/buf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ local function range_from_selection(mode)
local start_col = start[3]
local end_row = end_[2]
local end_col = end_[3]
if mode == 'V' then
start_col = 1
local lines = api.nvim_buf_get_lines(0, end_row - 1, end_row, true)
end_col = #lines[1]
end

-- A user can start visual selection at the end and move backwards
-- Normalize the range to start < end
Expand All @@ -144,6 +139,11 @@ local function range_from_selection(mode)
start_row, end_row = end_row, start_row
start_col, end_col = end_col, start_col
end
if mode == 'V' then
start_col = 1
local lines = api.nvim_buf_get_lines(0, end_row - 1, end_row, true)
end_col = #lines[1]
end
return {
['start'] = { start_row, start_col - 1 },
['end'] = { end_row, end_col - 1 },
Expand Down
22 changes: 21 additions & 1 deletion test/functional/plugin/lsp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3579,16 +3579,36 @@ describe('LSP', function()
vim.cmd.normal('V')
vim.api.nvim_win_set_cursor(0, { 2, 1 })
vim.lsp.buf.format({ bufnr = bufnr, false })

-- Format again with visual lines going from bottom to top
-- Must result in same formatting
vim.cmd.normal("<ESC>")
vim.api.nvim_win_set_cursor(0, { 2, 1 })
vim.cmd.normal('V')
vim.api.nvim_win_set_cursor(0, { 1, 2 })
vim.lsp.buf.format({ bufnr = bufnr, false })

vim.lsp.stop_client(client_id)
return server.messages
]])
eq("textDocument/rangeFormatting", result[3].method)
local expected_methods = {
"initialize",
"initialized",
"textDocument/rangeFormatting",
"$/cancelRequest",
"textDocument/rangeFormatting",
"$/cancelRequest",
"shutdown",
"exit",
}
eq(expected_methods, vim.tbl_map(function(x) return x.method end, result))
-- uses first column of start line and last column of end line
local expected_range = {
start = { line = 0, character = 0 },
['end'] = { line = 1, character = 7 },
}
eq(expected_range, result[3].params.range)
eq(expected_range, result[5].params.range)
end)
it('Aborts with notify if no clients support requested method', function()
exec_lua(create_server_definition)
Expand Down