2021-12-12 23:42:53 +00:00
|
|
|
local gui = require('navigator.gui')
|
2021-04-19 02:56:32 +00:00
|
|
|
local M = {}
|
2021-12-12 23:42:53 +00:00
|
|
|
local log = require('navigator.util').log
|
2021-12-24 06:30:56 +00:00
|
|
|
local trace = require('navigator.util').trace
|
2021-12-12 23:42:53 +00:00
|
|
|
local mk_handler = require('navigator.util').mk_handler
|
|
|
|
local lsphelper = require('navigator.lspwrapper')
|
2021-04-19 02:56:32 +00:00
|
|
|
local locations_to_items = lsphelper.locations_to_items
|
2021-12-12 23:42:53 +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-10-13 09:27:46 +00:00
|
|
|
function M.workspace_symbols(query)
|
2021-12-12 23:42:53 +00:00
|
|
|
query = query or pcall(vim.fn.input, 'Query: ')
|
2021-10-13 09:27:46 +00:00
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
2021-12-29 04:50:41 +00:00
|
|
|
local params = { query = query }
|
2021-10-13 09:27:46 +00:00
|
|
|
vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, _bufnr)
|
|
|
|
if client.resolved_capabilities.workspace_symbol then
|
2021-12-12 23:42:53 +00:00
|
|
|
client.request('workspace/symbol', params, M.workspace_symbol_handler, _bufnr)
|
2021-10-13 09:27:46 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.document_symbols(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local lspopts = {
|
2021-12-12 23:42:53 +00:00
|
|
|
loc = 'top_center',
|
2021-10-13 09:27:46 +00:00
|
|
|
prompt = true,
|
|
|
|
-- rawdata = true,
|
2021-12-29 04:50:41 +00:00
|
|
|
api = ' ',
|
2021-10-13 09:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2021-12-29 04:50:41 +00:00
|
|
|
params.context = { includeDeclaration = true }
|
2021-12-12 23:42:53 +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
|
2021-12-12 23:42:53 +00:00
|
|
|
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-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-12-29 04:50:41 +00:00
|
|
|
vim.notify('failed to get document symbol' .. vim.inspect(ctx), vim.lsp.log_levels.WARN)
|
2021-05-29 11:08:30 +00:00
|
|
|
end
|
2021-12-12 23:42:53 +00:00
|
|
|
local bufnr = ctx.bufnr or 0
|
2022-01-06 04:37:37 +00:00
|
|
|
local query = ' '
|
|
|
|
if ctx.params and ctx.params.query then
|
|
|
|
query = query .. ctx.params.query .. ' '
|
|
|
|
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
|
2022-01-06 04:37:37 +00:00
|
|
|
vim.notify('symbol' .. query .. 'not found for buf' .. vim.inspect(ctx), vim.lsp.log_levels.WARN)
|
2021-04-19 02:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local locations = {}
|
2021-12-12 23:42:53 +00:00
|
|
|
local fname = vim.fn.expand('%:p:f')
|
2021-04-20 22:07:13 +00:00
|
|
|
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
|
2021-11-05 23:21:57 +00:00
|
|
|
item.range = result[i].range or result[i].location.range
|
2021-04-20 22:07:13 +00:00
|
|
|
item.uri = uri
|
|
|
|
item.selectionRange = result[i].selectionRange
|
2021-12-12 23:42:53 +00:00
|
|
|
item.detail = result[i].detail or ''
|
|
|
|
if item.detail == '()' then
|
|
|
|
item.detail = 'func'
|
2021-05-29 11:08:30 +00:00
|
|
|
end
|
2021-04-20 22:07:13 +00:00
|
|
|
|
|
|
|
item.lnum = result[i].range.start.line + 1
|
2021-12-12 23:42:53 +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-12-12 23:42:53 +00:00
|
|
|
child.detail = c.detail or ''
|
2021-12-24 06:30:56 +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
|
|
|
|
2021-12-12 23:42:53 +00:00
|
|
|
local ft = vim.api.nvim_buf_get_option(bufnr, 'ft')
|
2021-12-29 04:50:41 +00:00
|
|
|
gui.new_list_view({ items = locations, prompt = true, rawdata = true, ft = ft, api = ' ' })
|
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-12-24 06:30:56 +00:00
|
|
|
trace(err, result, ctx, cfg)
|
2021-05-29 11:08:30 +00:00
|
|
|
if err then
|
2021-12-29 04:50:41 +00:00
|
|
|
vim.notify('failed to get workspace symbol' .. vim.inspect(ctx), vim.lsp.log_levels.WARN)
|
2021-05-29 11:08:30 +00:00
|
|
|
end
|
2022-01-06 11:06:40 +00:00
|
|
|
local query = ' '
|
2022-01-06 04:37:37 +00:00
|
|
|
if ctx.params and ctx.params.query then
|
|
|
|
query = query .. ctx.params.query .. ' '
|
|
|
|
end
|
2021-04-20 22:07:13 +00:00
|
|
|
if not result or vim.tbl_isempty(result) then
|
2022-01-06 04:37:37 +00:00
|
|
|
log('symbol not found', ctx)
|
|
|
|
vim.notify('symbol' .. query .. 'not found for buf ' .. tostring(ctx.bufnr), vim.lsp.log_levels.WARN)
|
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])
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2021-12-12 23:42:53 +00:00
|
|
|
local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft')
|
2021-12-29 04:50:41 +00:00
|
|
|
gui.new_list_view({ items = items, prompt = true, ft = ft, rowdata = true, api = ' ' })
|
2021-09-05 22:34:26 +00:00
|
|
|
end)
|
2021-04-19 02:56:32 +00:00
|
|
|
|
|
|
|
return M
|