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
Next Next commit
fix(lsp): use line start/end for visual line selection
Fixes #22629
  • Loading branch information
mfussenegger committed Mar 11, 2023
commit 435ccbf1a230bb2eb6089015513122c522592ad7
12 changes: 9 additions & 3 deletions runtime/lua/vim/lsp/buf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ function M.completion(context)
end

---@private
---@param mode "v"|"V"
---@return table {start={row, col}, end={row, col}} using (1, 0) indexing
local function range_from_selection()
local function range_from_selection(mode)
-- TODO: Use `vim.region()` instead https://github.com/neovim/neovim/pull/13896

-- [bufnum, lnum, col, off]; both row and column 1-indexed
Expand All @@ -129,6 +130,11 @@ local function range_from_selection()
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]
mfussenegger marked this conversation as resolved.
Show resolved Hide resolved
end

-- A user can start visual selection at the end and move backwards
-- Normalize the range to start < end
Expand Down Expand Up @@ -200,7 +206,7 @@ function M.format(options)
local mode = api.nvim_get_mode().mode
local range = options.range
if not range and mode == 'v' or mode == 'V' then
range = range_from_selection()
range = range_from_selection(mode)
end
local method = range and 'textDocument/rangeFormatting' or 'textDocument/formatting'

Expand Down Expand Up @@ -772,7 +778,7 @@ function M.code_action(options)
local end_ = assert(options.range['end'], 'range must have a `end` property')
params = util.make_given_range_params(start, end_)
elseif mode == 'v' or mode == 'V' then
local range = range_from_selection()
local range = range_from_selection(mode)
params = util.make_given_range_params(range.start, range['end'])
else
params = util.make_range_params()
Expand Down
27 changes: 27 additions & 0 deletions test/functional/plugin/lsp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3554,6 +3554,7 @@ describe('LSP', function()
vim.cmd.normal('v')
vim.api.nvim_win_set_cursor(0, { 2, 3 })
vim.lsp.buf.format({ bufnr = bufnr, false })
vim.lsp.stop_client(client_id)
return server.messages
]])
eq("textDocument/rangeFormatting", result[3].method)
Expand All @@ -3563,6 +3564,32 @@ describe('LSP', function()
}
eq(expected_range, result[3].params.range)
end)
it('format formats range in visual line mode', function()
exec_lua(create_server_definition)
local result = exec_lua([[
local server = _create_server({ capabilities = {
documentFormattingProvider = true,
documentRangeFormattingProvider = true,
}})
local bufnr = vim.api.nvim_get_current_buf()
local client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
vim.api.nvim_win_set_buf(0, bufnr)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, {'foo', 'bar baz'})
vim.api.nvim_win_set_cursor(0, { 1, 2 })
vim.cmd.normal('V')
vim.api.nvim_win_set_cursor(0, { 2, 1 })
vim.lsp.buf.format({ bufnr = bufnr, false })
vim.lsp.stop_client(client_id)
return server.messages
]])
eq("textDocument/rangeFormatting", result[3].method)
-- 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)
end)
it('Aborts with notify if no clients support requested method', function()
exec_lua(create_server_definition)
exec_lua([[
Expand Down