navigator.lua/lua/navigator/symbols.lua

126 lines
4.1 KiB
Lua
Raw Normal View History

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 lsphelper = require('navigator.lspwrapper')
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
function M.workspace_symbols(query)
2021-12-12 23:42:53 +00:00
query = query or pcall(vim.fn.input, 'Query: ')
local bufnr = vim.api.nvim_get_current_buf()
2021-12-29 04:50:41 +00:00
local params = { query = query }
2022-06-01 15:41:26 +00:00
vim.lsp.for_each_buffer_client(bufnr, function(client, _, _bufnr)
-- if client.resolved_capabilities.workspace_symbol then
if client.server_capabilities.workspaceSymbolProvider then
2021-12-12 23:42:53 +00:00
client.request('workspace/symbol', params, M.workspace_symbol_handler, _bufnr)
end
end)
end
function M.document_symbols(opts)
opts = opts or {}
local lspopts = {
2021-12-12 23:42:53 +00:00
loc = 'top_center',
prompt = true,
-- rawdata = true,
2021-12-29 04:50:41 +00:00
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()
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 ''
2022-06-01 15:41:26 +00:00
vim.lsp.for_each_buffer_client(bufnr, function(client, _, _bufnr)
-- if client.resolved_capabilities.document_symbol then
if client.server_capabilities.documentSymbolProvider then
2021-12-12 23:42:53 +00:00
client.request('textDocument/documentSymbol', params, M.document_symbol_handler, _bufnr)
end
end)
2021-04-19 02:56:32 +00:00
end
M.document_symbol_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
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
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
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-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 = '' })
end
2021-04-20 22:07:13 +00:00
M.workspace_symbol_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 = ' '
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
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-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 = '' })
end
2021-04-19 02:56:32 +00:00
return M