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-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
|
|
|
|
|
|
|
function M.workspace_symbols(opts)
|
|
|
|
opts = opts or {}
|
2021-04-22 23:39:24 +00:00
|
|
|
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running")
|
|
|
|
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
|
|
|
}
|
|
|
|
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-05-17 03:15:15 +00:00
|
|
|
local results_lsp = vim.lsp.buf_request_sync(0, "workspace/symbol", params,
|
|
|
|
lspopts.timeout or 15000)
|
2021-04-22 23:39:24 +00:00
|
|
|
if not results_lsp or vim.tbl_isempty(results_lsp) then
|
|
|
|
print(bufnr, "symbol not found for buf")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- result_lsp
|
|
|
|
local result = {}
|
|
|
|
for i = 1, #results_lsp do
|
2021-05-21 11:39:46 +00:00
|
|
|
if results_lsp[i] ~= nil and results_lsp[i].result ~= nil and #results_lsp[i].result > 0 then
|
|
|
|
result = results_lsp[i].result
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
2021-04-22 23:39:24 +00:00
|
|
|
local items = symbols_to_items(result)
|
2021-04-23 04:21:22 +00:00
|
|
|
-- log(#items, items[1])
|
2021-04-22 23:39:24 +00:00
|
|
|
|
2021-05-17 03:15:15 +00:00
|
|
|
local ft = vim.api.nvim_buf_get_option(0, "ft")
|
|
|
|
|
2021-04-22 23:39:24 +00:00
|
|
|
if #items > 0 then
|
2021-04-23 04:21:22 +00:00
|
|
|
lspopts.items = items
|
2021-05-17 03:15:15 +00:00
|
|
|
lspopts.ft = ft
|
2021-04-22 23:39:24 +00:00
|
|
|
gui.new_list_view(lspopts)
|
2021-04-19 02:56:32 +00:00
|
|
|
else
|
|
|
|
print("symbols not found")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-20 22:07:13 +00:00
|
|
|
function M.document_symbol_handler(err, _, result, _, bufnr)
|
2021-05-29 11:08:30 +00:00
|
|
|
if err then
|
|
|
|
print(bufnr, "failed to get document symbol")
|
|
|
|
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-04-20 22:07:13 +00:00
|
|
|
print(bufnr, "symbol not found for buf")
|
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
|
|
|
|
-- local cwd = vim.fn.getcwd(0) .. "/"
|
|
|
|
-- local add = util.get_relative_path(cwd, item.filename)
|
|
|
|
-- data[i] = data[i] .. " - " .. add
|
|
|
|
-- end
|
|
|
|
-- item.text = nil
|
|
|
|
-- end
|
|
|
|
-- opts.data = data
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.workspace_symbol_handler(err, _, result, _, bufnr)
|
2021-05-29 11:08:30 +00:00
|
|
|
if err then
|
|
|
|
print(bufnr, "failed to get workspace symbol")
|
|
|
|
end
|
2021-04-20 22:07:13 +00:00
|
|
|
if not result or vim.tbl_isempty(result) then
|
|
|
|
print(bufnr, "symbol not found for buf")
|
|
|
|
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
|
|
|
|
|
|
|
local ft = vim.api.nvim_buf_get_option(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
|
|
|
|
-- local cwd = vim.fn.getcwd(0) .. "/"
|
|
|
|
-- local add = util.get_relative_path(cwd, item.filename)
|
|
|
|
-- data[i] = data[i] .. " - " .. add
|
|
|
|
-- end
|
|
|
|
-- item.text = nil
|
|
|
|
-- end
|
|
|
|
-- opts.data = data
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|