lsp: Use nvim_buf_get_lines in locations_to_items and add more tests (#12357)
* LSP: Add tests & use nvim_buf_get_lines in locations_to_items This is to add support for cases where the server returns a URI in the locations that does not have a file scheme but needs to be loaded via a BufReadCmd event. * LSP: Don't iterate through all lines in locations_to_items * fixup! LSP: Don't iterate through all lines in locations_to_items * fixup! fixup! LSP: Don't iterate through all lines in locations_to_items * fixup! fixup! fixup! LSP: Don't iterate through all lines in locations_to_items
This commit is contained in:
parent
1920ba4b55
commit
554b21261e
|
@ -1089,40 +1089,31 @@ function M.locations_to_items(locations)
|
||||||
for _, d in ipairs(locations) do
|
for _, d in ipairs(locations) do
|
||||||
-- 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 fname = assert(vim.uri_to_fname(uri))
|
|
||||||
local range = d.range or d.targetSelectionRange
|
local range = d.range or d.targetSelectionRange
|
||||||
table.insert(grouped[fname], {start = range.start})
|
table.insert(grouped[uri], {start = range.start})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local keys = vim.tbl_keys(grouped)
|
local keys = vim.tbl_keys(grouped)
|
||||||
table.sort(keys)
|
table.sort(keys)
|
||||||
-- TODO(ashkan) I wish we could do this lazily.
|
-- TODO(ashkan) I wish we could do this lazily.
|
||||||
for _, fname in ipairs(keys) do
|
for _, uri in ipairs(keys) do
|
||||||
local rows = grouped[fname]
|
local rows = grouped[uri]
|
||||||
|
|
||||||
table.sort(rows, position_sort)
|
table.sort(rows, position_sort)
|
||||||
local i = 0
|
local bufnr = vim.uri_to_bufnr(uri)
|
||||||
for line in io.lines(fname) do
|
vim.fn.bufload(bufnr)
|
||||||
for _, temp in ipairs(rows) do
|
local filename = vim.uri_to_fname(uri)
|
||||||
local pos = temp.start
|
for _, temp in ipairs(rows) do
|
||||||
local row = pos.line
|
local pos = temp.start
|
||||||
if i == row then
|
local row = pos.line
|
||||||
local col
|
local line = (api.nvim_buf_get_lines(bufnr, row, row + 1, false) or {""})[1]
|
||||||
if pos.character > #line then
|
local col = M.character_offset(bufnr, row, pos.character)
|
||||||
col = #line
|
table.insert(items, {
|
||||||
else
|
filename = filename,
|
||||||
col = vim.str_byteindex(line, pos.character)
|
lnum = row + 1,
|
||||||
end
|
col = col + 1;
|
||||||
table.insert(items, {
|
text = line;
|
||||||
filename = fname,
|
})
|
||||||
lnum = row + 1,
|
|
||||||
col = col + 1;
|
|
||||||
text = line;
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
i = i + 1
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return items
|
return items
|
||||||
|
|
|
@ -1070,6 +1070,64 @@ describe('LSP', function()
|
||||||
]])
|
]])
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
describe('lsp.util.locations_to_items', function()
|
||||||
|
it('Convert Location[] to items', function()
|
||||||
|
local expected = {
|
||||||
|
{
|
||||||
|
filename = 'fake/uri',
|
||||||
|
lnum = 1,
|
||||||
|
col = 3,
|
||||||
|
text = 'testing'
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local actual = exec_lua [[
|
||||||
|
local bufnr = vim.uri_to_bufnr("file://fake/uri")
|
||||||
|
local lines = {"testing", "123"}
|
||||||
|
vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines)
|
||||||
|
local locations = {
|
||||||
|
{
|
||||||
|
uri = 'file://fake/uri',
|
||||||
|
range = {
|
||||||
|
start = { line = 0, character = 2 },
|
||||||
|
['end'] = { line = 0, character = 3 },
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return vim.lsp.util.locations_to_items(locations)
|
||||||
|
]]
|
||||||
|
eq(expected, actual)
|
||||||
|
end)
|
||||||
|
it('Convert LocationLink[] to items', function()
|
||||||
|
local expected = {
|
||||||
|
{
|
||||||
|
filename = 'fake/uri',
|
||||||
|
lnum = 1,
|
||||||
|
col = 3,
|
||||||
|
text = 'testing'
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local actual = exec_lua [[
|
||||||
|
local bufnr = vim.uri_to_bufnr("file://fake/uri")
|
||||||
|
local lines = {"testing", "123"}
|
||||||
|
vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines)
|
||||||
|
local locations = {
|
||||||
|
{
|
||||||
|
targetUri = vim.uri_from_bufnr(bufnr),
|
||||||
|
targetRange = {
|
||||||
|
start = { line = 0, character = 2 },
|
||||||
|
['end'] = { line = 0, character = 3 },
|
||||||
|
},
|
||||||
|
targetSelectionRange = {
|
||||||
|
start = { line = 0, character = 2 },
|
||||||
|
['end'] = { line = 0, character = 3 },
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return vim.lsp.util.locations_to_items(locations)
|
||||||
|
]]
|
||||||
|
eq(expected, actual)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
describe('lsp.util.symbols_to_items', function()
|
describe('lsp.util.symbols_to_items', function()
|
||||||
describe('convert DocumentSymbol[] to items', function()
|
describe('convert DocumentSymbol[] to items', function()
|
||||||
it('DocumentSymbol has children', function()
|
it('DocumentSymbol has children', function()
|
||||||
|
|
Loading…
Reference in New Issue