fix(lsp): support v:count in selection_range() #34551

Co-authored-by: Yi Ming <ofseed@foxmail.com>
This commit is contained in:
Riley Bruins
2025-06-17 14:10:57 -07:00
committed by GitHub
parent b3c78f4b3c
commit 17c18efbe5
5 changed files with 140 additions and 20 deletions

View File

@ -1839,11 +1839,11 @@ rename({new_name}, {opts}) *vim.lsp.buf.rename()*
selection_range({direction}) *vim.lsp.buf.selection_range()*
Perform an incremental selection at the cursor position based on ranges
given by the LSP. The `direction` parameter specifies whether the
selection should head inward or outward.
given by the LSP. The `direction` parameter specifies the number of times
to expand the selection. Negative values will shrink the selection.
Parameters: ~
• {direction} (`'inner'|'outer'`)
• {direction} (`integer`)
signature_help({config}) *vim.lsp.buf.signature_help()*
Displays signature information about the symbol under the cursor in a

View File

@ -24,7 +24,8 @@ EXPERIMENTS
LSP
todo
|vim.lsp.buf.selection_range()| now accepts an integer which specifies its
direction, rather than a string `'outer'` or `'inner'`.
OPTIONS

View File

@ -210,12 +210,12 @@ do
end, { desc = 'vim.lsp.buf.implementation()' })
vim.keymap.set('x', 'an', function()
vim.lsp.buf.selection_range('outer')
end, { desc = "vim.lsp.buf.selection_range('outer')" })
vim.lsp.buf.selection_range(vim.v.count1)
end, { desc = 'vim.lsp.buf.selection_range(vim.v.count1)' })
vim.keymap.set('x', 'in', function()
vim.lsp.buf.selection_range('inner')
end, { desc = "vim.lsp.buf.selection_range('inner')" })
vim.lsp.buf.selection_range(-vim.v.count1)
end, { desc = 'vim.lsp.buf.selection_range(-vim.v.count1)' })
vim.keymap.set('n', 'gO', function()
vim.lsp.buf.document_symbol()

View File

@ -1413,20 +1413,16 @@ local function is_empty(range)
end
--- Perform an incremental selection at the cursor position based on ranges given by the LSP. The
--- `direction` parameter specifies whether the selection should head inward or outward.
--- `direction` parameter specifies the number of times to expand the selection. Negative values
--- will shrink the selection.
---
--- @param direction 'inner' | 'outer'
--- @param direction integer
function M.selection_range(direction)
validate('direction', direction, function(v)
return v == 'inner' or v == 'outer'
end)
validate('direction', direction, 'number')
if selection_ranges then
local offset = direction == 'outer' and 1 or -1
local new_index = selection_ranges.index + offset
if new_index <= #selection_ranges.ranges and new_index >= 1 then
selection_ranges.index = new_index
end
local new_index = selection_ranges.index + direction
selection_ranges.index = math.min(#selection_ranges.ranges, math.max(1, new_index))
select_range(selection_ranges.ranges[selection_ranges.index])
return
@ -1498,8 +1494,9 @@ function M.selection_range(direction)
})
if #ranges > 0 then
selection_ranges = { index = 1, ranges = ranges }
select_range(ranges[1])
local index = math.min(#ranges, math.max(1, direction))
selection_ranges = { index = index, ranges = ranges }
select_range(ranges[index])
end
end
)