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:
• {filename} (`string`)
• {lnum} (`integer`) 1-indexed line number
• {end_lnum} (`integer`) 1-indexed end line number
• {col} (`integer`) 1-indexed column
• {end_col} (`integer`) 1-indexed end column
• {text} (`string`)
• {user_data} (`lsp.Location|lsp.LocationLink`)

View File

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

View File

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

View File

@ -2690,13 +2690,15 @@ describe('LSP', function()
{
filename = '/fake/uri',
lnum = 1,
end_lnum = 2,
col = 3,
end_col = 4,
text = 'testing',
user_data = {
uri = 'file:///fake/uri',
range = {
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',
range = {
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',
lnum = 1,
end_lnum = 1,
col = 3,
end_col = 4,
text = 'testing',
user_data = {
targetUri = 'file:///fake/uri',