feat(lsp): include end_col, end_lnum in vim.lsp.buf.locations_to_items #29164

This commit is contained in:
Tom Praschan
2024-06-10 03:14:55 +02:00
committed by GitHub
parent 40329f32d8
commit 20f22f75ee
4 changed files with 21 additions and 4 deletions

View File

@ -1983,7 +1983,9 @@ locations_to_items({locations}, {offset_encoding})
(`table[]`) A list of objects with the following fields: (`table[]`) A list of objects with the following fields:
• {filename} (`string`) • {filename} (`string`)
• {lnum} (`integer`) 1-indexed line number • {lnum} (`integer`) 1-indexed line number
• {end_lnum} (`integer`) 1-indexed end line number
• {col} (`integer`) 1-indexed column • {col} (`integer`) 1-indexed column
• {end_col} (`integer`) 1-indexed end column
• {text} (`string`) • {text} (`string`)
• {user_data} (`lsp.Location|lsp.LocationLink`) • {user_data} (`lsp.Location|lsp.LocationLink`)

View File

@ -113,6 +113,7 @@ LSP
• Completion side effects (including snippet expansion, execution of commands • Completion side effects (including snippet expansion, execution of commands
and application of additional text edits) is now built-in. and application of additional text edits) is now built-in.
• |vim.lsp.util.locations_to_items()| sets `end_col` and `end_lnum` fields.
LUA LUA

View File

@ -1722,7 +1722,9 @@ end)
---@inlinedoc ---@inlinedoc
---@field filename string ---@field filename string
---@field lnum integer 1-indexed line number ---@field lnum integer 1-indexed line number
---@field end_lnum integer 1-indexed end line number
---@field col integer 1-indexed column ---@field col integer 1-indexed column
---@field end_col integer 1-indexed end column
---@field text string ---@field text string
---@field user_data lsp.Location|lsp.LocationLink ---@field user_data lsp.Location|lsp.LocationLink
@ -1749,7 +1751,7 @@ function M.locations_to_items(locations, offset_encoding)
end end
local items = {} local items = {}
---@type table<string, {start: lsp.Position, location: lsp.Location|lsp.LocationLink}[]> ---@type table<string, {start: lsp.Position, end: lsp.Position, location: lsp.Location|lsp.LocationLink}[]>
local grouped = setmetatable({}, { local grouped = setmetatable({}, {
__index = function(t, k) __index = function(t, k)
local v = {} local v = {}
@ -1761,7 +1763,7 @@ function M.locations_to_items(locations, offset_encoding)
-- locations may be Location or LocationLink -- locations may be Location or LocationLink
local uri = d.uri or d.targetUri local uri = d.uri or d.targetUri
local range = d.range or d.targetSelectionRange local range = d.range or d.targetSelectionRange
table.insert(grouped[uri], { start = range.start, location = d }) table.insert(grouped[uri], { start = range.start, ['end'] = range['end'], location = d })
end end
---@type string[] ---@type string[]
@ -1776,6 +1778,9 @@ function M.locations_to_items(locations, offset_encoding)
local line_numbers = {} local line_numbers = {}
for _, temp in ipairs(rows) do for _, temp in ipairs(rows) do
table.insert(line_numbers, temp.start.line) table.insert(line_numbers, temp.start.line)
if temp.start.line ~= temp['end'].line then
table.insert(line_numbers, temp['end'].line)
end
end end
-- get all the lines for this uri -- get all the lines for this uri
@ -1783,13 +1788,18 @@ function M.locations_to_items(locations, offset_encoding)
for _, temp in ipairs(rows) do for _, temp in ipairs(rows) do
local pos = temp.start local pos = temp.start
local end_pos = temp['end']
local row = pos.line local row = pos.line
local end_row = end_pos.line
local line = lines[row] or '' local line = lines[row] or ''
local col = M._str_byteindex_enc(line, pos.character, offset_encoding) local col = M._str_byteindex_enc(line, pos.character, offset_encoding)
local end_col = M._str_byteindex_enc(lines[end_row] or '', end_pos.character, offset_encoding)
table.insert(items, { table.insert(items, {
filename = filename, filename = filename,
lnum = row + 1, lnum = row + 1,
end_lnum = end_row + 1,
col = col + 1, col = col + 1,
end_col = end_col + 1,
text = line, text = line,
user_data = temp.location, user_data = temp.location,
}) })

View File

@ -2690,13 +2690,15 @@ describe('LSP', function()
{ {
filename = '/fake/uri', filename = '/fake/uri',
lnum = 1, lnum = 1,
end_lnum = 2,
col = 3, col = 3,
end_col = 4,
text = 'testing', text = 'testing',
user_data = { user_data = {
uri = 'file:///fake/uri', uri = 'file:///fake/uri',
range = { range = {
start = { line = 0, character = 2 }, start = { line = 0, character = 2 },
['end'] = { line = 0, character = 3 }, ['end'] = { line = 1, character = 3 },
}, },
}, },
}, },
@ -2710,7 +2712,7 @@ describe('LSP', function()
uri = 'file:///fake/uri', uri = 'file:///fake/uri',
range = { range = {
start = { line = 0, character = 2 }, start = { line = 0, character = 2 },
['end'] = { line = 0, character = 3 }, ['end'] = { line = 1, character = 3 },
} }
}, },
} }
@ -2723,7 +2725,9 @@ describe('LSP', function()
{ {
filename = '/fake/uri', filename = '/fake/uri',
lnum = 1, lnum = 1,
end_lnum = 1,
col = 3, col = 3,
end_col = 4,
text = 'testing', text = 'testing',
user_data = { user_data = {
targetUri = 'file:///fake/uri', targetUri = 'file:///fake/uri',