navigator.lua/lua/navigator/symbols.lua

181 lines
5.6 KiB
Lua
Raw Normal View History

2021-04-19 02:56:32 +00:00
local gui = require "navigator.gui"
local M = {}
local log = require "navigator.util".log
2021-04-20 22:25:31 +00:00
local verbose = require "navigator.util".debug
2021-04-19 02:56:32 +00:00
local lsphelper = require "navigator.lspwrapper"
local locations_to_items = lsphelper.locations_to_items
2021-04-20 22:07:13 +00:00
local clone = require "guihua.util".clone
local symbol_kind = require "navigator.lspclient.lspkind".symbol_kind
2021-04-19 02:56:32 +00:00
function M.document_symbols(opts)
opts = opts or {}
local params = vim.lsp.util.make_position_params()
params.context = {includeDeclaration = true}
params.query = ""
2021-04-20 22:07:13 +00:00
local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params, opts.timeout or 5000)
2021-04-19 02:56:32 +00:00
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
local cmd = table.concat(lines, "\n")
if #lines > 0 then
gui.new_list_view({data = lines})
else
print("symbols not found")
end
end
function M.workspace_symbols(opts)
opts = opts or {}
local params = vim.lsp.util.make_position_params()
params.context = {includeDeclaration = true}
params.query = ""
2021-04-20 22:07:13 +00:00
local results_lsp = vim.lsp.buf_request_sync(0, "workspace/symbol", params, opts.timeout or 15000)
2021-04-19 02:56:32 +00:00
log(results_lsp)
local locations = {}
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-20 22:07:13 +00:00
function M.document_symbol_handler(err, _, result, _, bufnr)
if err then
print(bufnr, "failed to get document symbol")
end
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 {})
-- log(locations)
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
item.detail = result[i].detail or ''
if item.detail == '()' then item.detail = 'func' end
item.lnum = result[i].range.start.line + 1
item.text = "[" .. kind .. "]" .. item.detail .. " " .. item.name
item.filename = fname
table.insert(locations, item)
if result[i].children ~= nil then
for _, c in pairs (result[i].children) do
local child = {}
child.kind = c.kind
child.name = c.name
child.range = c.range
local ckind = symbol_kind(child.kind)
child.selectionRange = c.selectionRange
child.fname = fname
child.uri = uri
child.lnum = c.range.start.line + 1
child.detail = c.detail or ''
child.text = "  [" .. ckind .. "] " .. child.detail .. " " .. child.name
table.insert(locations, child)
end
end
end
2021-04-20 22:25:31 +00:00
-- verbose(locations)
2021-04-20 22:07:13 +00:00
-- local items = locations_to_items(locations)
gui.new_list_view({items = locations, prompt = true, rawdata = true, api = '華 '})
-- 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)
if err then
print(bufnr, "failed to get workspace symbol")
end
if not result or vim.tbl_isempty(result) then
print(bufnr, "symbol not found for buf")
return
end
log(result)
local locations = {}
2021-04-19 02:56:32 +00:00
for i = 1, #result do
2021-04-20 22:07:13 +00:00
local item = result[i].location or {}
2021-04-19 02:56:32 +00:00
item.kind = result[i].kind
item.containerName = result[i].containerName
item.name = result[i].name
item.text = result[i].name
if #item.containerName > 0 then
2021-04-20 22:07:13 +00:00
item.text = item.text:gsub(item.containerName, "", 1)
2021-04-19 02:56:32 +00:00
end
table.insert(locations, item)
end
local items = locations_to_items(locations)
2021-04-20 22:07:13 +00:00
gui.new_list_view({items = items, prompt = 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