mirror of
https://github.com/neovim/neovim.git
synced 2025-12-17 20:05:38 +00:00
Add support for textDocument/references.
Add set_qflist and set_loclist. - Also add locations_to_items, which calculates byte offsets for character positions in files and avoids unnecessary operations.
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
local vim = vim
|
||||||
local validate = vim.validate
|
local validate = vim.validate
|
||||||
local api = vim.api
|
local api = vim.api
|
||||||
local vfn = vim.fn
|
local vfn = vim.fn
|
||||||
@@ -330,5 +331,19 @@ function M.rename(new_name)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.references(context)
|
||||||
|
validate { context = { context, 't', true } }
|
||||||
|
local params = util.make_position_params()
|
||||||
|
params.context = context or {
|
||||||
|
includeDeclaration = true;
|
||||||
|
}
|
||||||
|
params[vim.type_idx] = vim.types.dictionary
|
||||||
|
request('textDocument/references', params, function(_, _, result)
|
||||||
|
if not result then return end
|
||||||
|
util.set_qflist(result)
|
||||||
|
vim.api.nvim_command("copen")
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
-- vim:sw=2 ts=2 et
|
-- vim:sw=2 ts=2 et
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ M['textDocument/publishDiagnostics'] = function(_, _, result)
|
|||||||
util.buf_diagnostics_save_positions(bufnr, result.diagnostics)
|
util.buf_diagnostics_save_positions(bufnr, result.diagnostics)
|
||||||
util.buf_diagnostics_underline(bufnr, result.diagnostics)
|
util.buf_diagnostics_underline(bufnr, result.diagnostics)
|
||||||
util.buf_diagnostics_virtual_text(bufnr, result.diagnostics)
|
util.buf_diagnostics_virtual_text(bufnr, result.diagnostics)
|
||||||
-- util.buf_loclist(bufnr, result.diagnostics)
|
-- util.set_loclist(result.diagnostics)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function log_message(_, _, result, client_id)
|
local function log_message(_, _, result, client_id)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
local protocol = require 'vim.lsp.protocol'
|
local protocol = require 'vim.lsp.protocol'
|
||||||
|
local vim = vim
|
||||||
local validate = vim.validate
|
local validate = vim.validate
|
||||||
local api = vim.api
|
local api = vim.api
|
||||||
|
|
||||||
@@ -573,30 +574,71 @@ do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.buf_loclist(bufnr, locations)
|
local position_sort = sort_by_key(function(v)
|
||||||
local targetwin
|
return {v.line, v.character}
|
||||||
for _, winnr in ipairs(api.nvim_list_wins()) do
|
end)
|
||||||
local winbuf = api.nvim_win_get_buf(winnr)
|
|
||||||
if winbuf == bufnr then
|
-- Returns the items with the byte position calculated correctly and in sorted
|
||||||
targetwin = winnr
|
-- order.
|
||||||
break
|
function M.locations_to_items(locations)
|
||||||
|
local items = {}
|
||||||
|
local grouped = setmetatable({}, {
|
||||||
|
__index = function(t, k)
|
||||||
|
local v = {}
|
||||||
|
rawset(t, k, v)
|
||||||
|
return v
|
||||||
|
end;
|
||||||
|
})
|
||||||
|
for _, d in ipairs(locations) do
|
||||||
|
local start = d.range.start
|
||||||
|
local fname = assert(vim.uri_to_fname(d.uri))
|
||||||
|
table.insert(grouped[fname], start)
|
||||||
|
end
|
||||||
|
local keys = vim.tbl_keys(grouped)
|
||||||
|
table.sort(keys)
|
||||||
|
-- TODO(ashkan) I wish we could do this lazily.
|
||||||
|
for _, fname in ipairs(keys) do
|
||||||
|
local rows = grouped[fname]
|
||||||
|
table.sort(rows, position_sort)
|
||||||
|
local i = 0
|
||||||
|
for line in io.lines(fname) do
|
||||||
|
for _, pos in ipairs(rows) do
|
||||||
|
local row = pos.line
|
||||||
|
if i == row then
|
||||||
|
local col
|
||||||
|
if pos.character > #line then
|
||||||
|
col = #line
|
||||||
|
else
|
||||||
|
col = vim.str_byteindex(line, pos.character)
|
||||||
|
end
|
||||||
|
table.insert(items, {
|
||||||
|
filename = fname,
|
||||||
|
lnum = row + 1,
|
||||||
|
col = col + 1;
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
i = i + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not targetwin then return end
|
return items
|
||||||
|
end
|
||||||
|
|
||||||
local items = {}
|
-- locations is Location[]
|
||||||
local path = api.nvim_buf_get_name(bufnr)
|
-- Only sets for the current window.
|
||||||
for _, d in ipairs(locations) do
|
function M.set_loclist(locations)
|
||||||
-- TODO: URL parsing here?
|
vim.fn.setloclist(0, {}, ' ', {
|
||||||
local start = d.range.start
|
title = 'Language Server';
|
||||||
table.insert(items, {
|
items = M.locations_to_items(locations);
|
||||||
filename = path,
|
})
|
||||||
lnum = start.line + 1,
|
end
|
||||||
col = start.character + 1,
|
|
||||||
text = d.message,
|
-- locations is Location[]
|
||||||
})
|
function M.set_qflist(locations)
|
||||||
end
|
vim.fn.setqflist({}, ' ', {
|
||||||
vim.fn.setloclist(targetwin, items, ' ', 'Language Server')
|
title = 'Language Server';
|
||||||
|
items = M.locations_to_items(locations);
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Remove empty lines from the beginning and end.
|
-- Remove empty lines from the beginning and end.
|
||||||
|
|||||||
Reference in New Issue
Block a user