2021-04-19 02:56:32 +00:00
|
|
|
local gui = require "navigator.gui"
|
|
|
|
local M = {}
|
2021-05-17 03:15:15 +00:00
|
|
|
local log = require"navigator.util".log
|
2021-09-05 22:34:26 +00:00
|
|
|
local mk_handler = require"navigator.util".mk_handler
|
2021-04-19 02:56:32 +00:00
|
|
|
local lsphelper = require "navigator.lspwrapper"
|
|
|
|
local locations_to_items = lsphelper.locations_to_items
|
2021-05-17 03:15:15 +00:00
|
|
|
local clone = require"guihua.util".clone
|
|
|
|
local symbol_kind = require"navigator.lspclient.lspkind".symbol_kind
|
2021-04-22 23:39:24 +00:00
|
|
|
local symbols_to_items = lsphelper.symbols_to_items
|
|
|
|
|
2021-04-24 02:38:47 +00:00
|
|
|
-- function M.document_symbols(opts)
|
|
|
|
-- assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running")
|
|
|
|
-- opts = opts or {}
|
|
|
|
-- local params = vim.lsp.util.make_position_params()
|
|
|
|
-- params.context = {includeDeclaration = true}
|
|
|
|
-- params.query = ""
|
|
|
|
-- local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params, opts.timeout or 3000)
|
|
|
|
-- local locations = {}
|
|
|
|
-- log(results_lsp)
|
|
|
|
-- for _, server_results in pairs(results_lsp) do
|
|
|
|
-- if server_results.result then
|
|
|
|
-- vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result) or {})
|
|
|
|
-- end
|
|
|
|
-- end
|
|
|
|
-- local lines = {}
|
|
|
|
--
|
|
|
|
-- for _, loc in ipairs(locations) do
|
|
|
|
-- table.insert(lines, string.format("%s:%s:%s", loc.filename, loc.lnum, loc.text))
|
|
|
|
-- end
|
|
|
|
-- if #lines > 0 then
|
|
|
|
-- gui.new_list_view({data = lines})
|
|
|
|
-- else
|
|
|
|
-- print("symbols not found")
|
|
|
|
-- end
|
|
|
|
-- end
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2021-10-13 09:27:46 +00:00
|
|
|
function M.workspace_symbols(query)
|
2021-04-19 02:56:32 +00:00
|
|
|
opts = opts or {}
|
2021-04-22 23:39:24 +00:00
|
|
|
local lspopts = {
|
|
|
|
loc = "top_center",
|
|
|
|
prompt = true,
|
2021-04-23 04:21:22 +00:00
|
|
|
-- rawdata = true,
|
|
|
|
api = " "
|
2021-04-22 23:39:24 +00:00
|
|
|
}
|
2021-10-13 09:27:46 +00:00
|
|
|
|
|
|
|
query = query or pcall(vim.fn.input, "Query: ")
|
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
|
|
vim.list_extend(lspopts, opts)
|
|
|
|
local params = {query=query}
|
|
|
|
vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, _bufnr)
|
|
|
|
if client.resolved_capabilities.workspace_symbol then
|
|
|
|
client.request("workspace/symbol", params, M.workspace_symbol_handler, _bufnr)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.document_symbols(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local lspopts = {
|
|
|
|
loc = "top_center",
|
|
|
|
prompt = true,
|
|
|
|
-- rawdata = true,
|
|
|
|
api = " "
|
|
|
|
}
|
|
|
|
|
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
2021-04-22 23:39:24 +00:00
|
|
|
vim.list_extend(lspopts, opts)
|
2021-04-19 02:56:32 +00:00
|
|
|
local params = vim.lsp.util.make_position_params()
|
|
|
|
params.context = {includeDeclaration = true}
|
2021-04-22 23:39:24 +00:00
|
|
|
params.query = opts.prompt or ""
|
2021-10-13 09:27:46 +00:00
|
|
|
vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, _bufnr)
|
|
|
|
if client.resolved_capabilities.document_symbol then
|
|
|
|
client.request("textDocument/documentSymbol", params, M.document_symbol_handler, _bufnr)
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
2021-10-13 09:27:46 +00:00
|
|
|
end)
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
2021-10-13 09:27:46 +00:00
|
|
|
|
2021-09-05 22:34:26 +00:00
|
|
|
M.document_symbol_handler = mk_handler(function(err, result, ctx)
|
2021-05-29 11:08:30 +00:00
|
|
|
if err then
|
2021-09-05 22:34:26 +00:00
|
|
|
print("failed to get document symbol", ctx)
|
2021-05-29 11:08:30 +00:00
|
|
|
end
|
2021-04-20 22:07:13 +00:00
|
|
|
|
2021-04-19 02:56:32 +00:00
|
|
|
if not result or vim.tbl_isempty(result) then
|
2021-09-05 22:34:26 +00:00
|
|
|
print("symbol not found for buf", ctx)
|
2021-04-19 02:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
-- log(result)
|
|
|
|
local locations = {}
|
2021-04-20 22:07:13 +00:00
|
|
|
local fname = vim.fn.expand("%:p:f")
|
|
|
|
local uri = vim.uri_from_fname(fname)
|
|
|
|
-- vim.list_extend(locations, vim.lsp.util.symbols_to_items(result) or {})
|
2021-04-22 23:39:24 +00:00
|
|
|
log(result[1])
|
2021-04-20 22:07:13 +00:00
|
|
|
for i = 1, #result do
|
|
|
|
local item = {}
|
|
|
|
item.kind = result[i].kind
|
|
|
|
local kind = symbol_kind(item.kind)
|
|
|
|
item.name = result[i].name
|
|
|
|
item.range = result[i].range
|
|
|
|
item.uri = uri
|
|
|
|
item.selectionRange = result[i].selectionRange
|
2021-04-22 23:39:24 +00:00
|
|
|
item.detail = result[i].detail or ""
|
2021-05-29 11:08:30 +00:00
|
|
|
if item.detail == "()" then
|
|
|
|
item.detail = "func"
|
|
|
|
end
|
2021-04-20 22:07:13 +00:00
|
|
|
|
|
|
|
item.lnum = result[i].range.start.line + 1
|
2021-05-29 11:08:30 +00:00
|
|
|
item.text = "[" .. kind .. "]" .. item.name .. " " .. item.detail
|
2021-04-20 22:07:13 +00:00
|
|
|
|
|
|
|
item.filename = fname
|
|
|
|
|
|
|
|
table.insert(locations, item)
|
|
|
|
if result[i].children ~= nil then
|
2021-04-22 23:39:24 +00:00
|
|
|
for _, c in pairs(result[i].children) do
|
2021-04-20 22:07:13 +00:00
|
|
|
local child = {}
|
|
|
|
child.kind = c.kind
|
|
|
|
child.name = c.name
|
|
|
|
child.range = c.range
|
|
|
|
local ckind = symbol_kind(child.kind)
|
|
|
|
child.selectionRange = c.selectionRange
|
2021-06-02 15:25:56 +00:00
|
|
|
child.filename = fname
|
2021-04-20 22:07:13 +00:00
|
|
|
child.uri = uri
|
|
|
|
child.lnum = c.range.start.line + 1
|
2021-04-22 23:39:24 +00:00
|
|
|
child.detail = c.detail or ""
|
2021-05-29 11:08:30 +00:00
|
|
|
child.text = " [" .. ckind .. "] " .. child.name .. " " .. child.detail
|
2021-04-20 22:07:13 +00:00
|
|
|
table.insert(locations, child)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-05-17 03:15:15 +00:00
|
|
|
|
|
|
|
local ft = vim.api.nvim_buf_get_option(bufnr, "ft")
|
2021-05-21 11:39:46 +00:00
|
|
|
-- trace(locations)
|
2021-04-20 22:07:13 +00:00
|
|
|
-- local items = locations_to_items(locations)
|
2021-05-21 11:39:46 +00:00
|
|
|
gui.new_list_view({items = locations, prompt = true, rawdata = true, ft = ft, api = " "})
|
2021-04-20 22:07:13 +00:00
|
|
|
|
|
|
|
-- if locations == nil or vim.tbl_isempty(locations) then
|
|
|
|
-- print "References not found"
|
|
|
|
-- return
|
|
|
|
-- end
|
|
|
|
-- local items = locations_to_items(locations)
|
|
|
|
-- gui.new_list_view({items = items})
|
|
|
|
-- local filename = vim.api.nvim_buf_get_name(bufnr)
|
|
|
|
-- local items = vim.lsp.util.symbols_to_items(result, bufnr)
|
|
|
|
-- local data = {}
|
|
|
|
-- for i, item in pairs(action.items) do
|
|
|
|
-- data[i] = item.text
|
|
|
|
-- if filename ~= item.filename then
|
2021-09-09 02:59:58 +00:00
|
|
|
-- local cwd = vim.loop.cwd() .. "/"
|
2021-04-20 22:07:13 +00:00
|
|
|
-- local add = util.get_relative_path(cwd, item.filename)
|
|
|
|
-- data[i] = data[i] .. " - " .. add
|
|
|
|
-- end
|
|
|
|
-- item.text = nil
|
|
|
|
-- end
|
|
|
|
-- opts.data = data
|
2021-09-05 22:34:26 +00:00
|
|
|
end)
|
2021-04-20 22:07:13 +00:00
|
|
|
|
2021-09-05 22:34:26 +00:00
|
|
|
M.workspace_symbol_handler = mk_handler(function(err, result, ctx, cfg)
|
2021-05-29 11:08:30 +00:00
|
|
|
if err then
|
2021-09-05 22:34:26 +00:00
|
|
|
print("failed to get workspace symbol", ctx)
|
2021-05-29 11:08:30 +00:00
|
|
|
end
|
2021-04-20 22:07:13 +00:00
|
|
|
if not result or vim.tbl_isempty(result) then
|
2021-09-05 22:34:26 +00:00
|
|
|
print("symbol not found for buf", ctx)
|
2021-04-20 22:07:13 +00:00
|
|
|
return
|
|
|
|
end
|
2021-04-22 23:39:24 +00:00
|
|
|
log(result[1])
|
|
|
|
local items = symbols_to_items(result)
|
|
|
|
log(items[1])
|
|
|
|
-- local locations = {}
|
|
|
|
-- for i = 1, #result do
|
|
|
|
-- local item = result[i].location or {}
|
|
|
|
-- item.kind = result[i].kind
|
|
|
|
-- item.containerName = result[i].containerName or ""
|
|
|
|
-- item.name = result[i].name
|
|
|
|
-- item.text = result[i].name
|
|
|
|
-- if #item.containerName > 0 then
|
|
|
|
-- item.text = item.text:gsub(item.containerName, "", 1)
|
|
|
|
-- end
|
|
|
|
-- table.insert(locations, item)
|
|
|
|
-- end
|
|
|
|
-- local items = locations_to_items(locations)
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2021-09-05 22:34:26 +00:00
|
|
|
local ft = vim.api.nvim_buf_get_option(ctx.bufnr, "ft")
|
2021-05-21 11:39:46 +00:00
|
|
|
gui.new_list_view({items = items, prompt = true, ft = ft, rowdata = true, api = " "})
|
2021-04-19 02:56:32 +00:00
|
|
|
|
|
|
|
-- if locations == nil or vim.tbl_isempty(locations) then
|
|
|
|
-- print "References not found"
|
|
|
|
-- return
|
|
|
|
-- end
|
|
|
|
-- local items = locations_to_items(locations)
|
|
|
|
-- gui.new_list_view({items = items})
|
|
|
|
-- local filename = vim.api.nvim_buf_get_name(bufnr)
|
|
|
|
-- local items = vim.lsp.util.symbols_to_items(result, bufnr)
|
|
|
|
-- local data = {}
|
|
|
|
-- for i, item in pairs(action.items) do
|
|
|
|
-- data[i] = item.text
|
|
|
|
-- if filename ~= item.filename then
|
2021-09-09 02:59:58 +00:00
|
|
|
-- local cwd = vim.loop.cwd() .. "/"
|
2021-04-19 02:56:32 +00:00
|
|
|
-- local add = util.get_relative_path(cwd, item.filename)
|
|
|
|
-- data[i] = data[i] .. " - " .. add
|
|
|
|
-- end
|
|
|
|
-- item.text = nil
|
|
|
|
-- end
|
|
|
|
-- opts.data = data
|
2021-09-05 22:34:26 +00:00
|
|
|
end)
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2021-10-13 09:27:46 +00:00
|
|
|
|
2021-04-19 02:56:32 +00:00
|
|
|
return M
|