From 451d761f76a4fad5496160bd1ebee0628b7b4234 Mon Sep 17 00:00:00 2001 From: ray-x Date: Fri, 16 Sep 2022 21:21:20 +1000 Subject: [PATCH] features side panel for symbol reference --- README.md | 1 + doc/navigator.txt | 6 +++- lua/navigator/lspclient/mapping.lua | 23 +++++++------ lua/navigator/lspwrapper.lua | 8 ++++- lua/navigator/reference.lua | 53 ++++++++++++++++++++++++++--- 5 files changed, 73 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index e5ab7ab..81f716e 100644 --- a/README.md +++ b/README.md @@ -777,6 +777,7 @@ You can override the above highlight to fit your current colorscheme | LspRestart | reload lsp | | LspToggleFmt | toggle lsp format | | LspSymbols | document symbol in side panel | +| NRefPanel | show symbol reference in side panel | | TSymobls | treesitter symbol in side panel | | Calltree {args} | lsp call hierarchy call tree, args: -i (incomming default), -o (outgoing) | diff --git a/doc/navigator.txt b/doc/navigator.txt index 1a80271..b0cf6f3 100644 --- a/doc/navigator.txt +++ b/doc/navigator.txt @@ -660,8 +660,9 @@ COMMANDS *navigator-command | Nctags {args} | show ctags symbols, args: -g regen ctags | | LspRestart | reload lsp | | LspSymbols | document symbol in side panel | +| NRefPanel |symbol reference in side panel | | TSymobls | treesitter symbol in side panel | -| CallTree {args} | lsp call hierarchy call tree, args: -i (incomming default), -o (outgoing) | +| Calltree {args} | lsp call hierarchy call tree, args: -i (incomming default), -o (outgoing) | :LspToggleFmt *:LspToggleFmt* Toggle lsp auto format. @@ -683,6 +684,9 @@ COMMANDS *navigator-command :TSSymbols *:TSSymbols* Treesitter symbol in side panel. +:NRefPanel *:NRefPanel* + Symbol reference in side panel. + :Calltree [flags] *:Calltree* Lsp call hierarchy call tree. [flags]: diff --git a/lua/navigator/lspclient/mapping.lua b/lua/navigator/lspclient/mapping.lua index efd7bb1..773cd10 100644 --- a/lua/navigator/lspclient/mapping.lua +++ b/lua/navigator/lspclient/mapping.lua @@ -71,17 +71,6 @@ local key_maps = { { key = 'la', mode = 'n', func = require('navigator.codelens').run_action, desc = 'run code lens action' }, } -local commands = { - [[command! -nargs=* Nctags lua require("navigator.ctags").ctags()]], - "command! -nargs=0 LspLog lua require'navigator.lspclient.config'.open_lsp_log()", - "command! -nargs=0 LspRestart lua require'navigator.lspclient.config'.reload_lsp()", - "command! -nargs=0 LspToggleFmt lua require'navigator.lspclient.mapping'.toggle_lspformat()", - "command! -nargs=0 LspKeymaps lua require'navigator.lspclient.mapping'.get_keymaps_help()", - "command! -nargs=0 LspSymbols lua require'navigator.symbols'.side_panel()", - "command! -nargs=0 TSymbols lua require'navigator.treesitter'.side_panel()", - "command! -nargs=* Calltree lua require'navigator.hierarchy'.calltree()", -} - local key_maps_help = {} -- LuaFormatter on local M = {} @@ -126,6 +115,18 @@ local check_cap = function(opts) end local function set_cmds(_) + local commands = { + [[command! -nargs=* Nctags lua require("navigator.ctags").ctags()]], + "command! -nargs=0 LspLog lua require'navigator.lspclient.config'.open_lsp_log()", + "command! -nargs=0 LspRestart lua require'navigator.lspclient.config'.reload_lsp()", + "command! -nargs=0 LspToggleFmt lua require'navigator.lspclient.mapping'.toggle_lspformat()", + "command! -nargs=0 LspKeymaps lua require'navigator.lspclient.mapping'.get_keymaps_help()", + "command! -nargs=0 LspSymbols lua require'navigator.symbols'.side_panel()", + "command! -nargs=0 TSymbols lua require'navigator.treesitter'.side_panel()", + "command! -nargs=0 NRefPanel lua require'navigator.reference'.side_panel()", + "command! -nargs=* Calltree lua require'navigator.hierarchy'.calltree()", + } + for _, value in pairs(commands) do vim.cmd(value) end diff --git a/lua/navigator/lspwrapper.lua b/lua/navigator/lspwrapper.lua index 8179e62..0a3cdbb 100644 --- a/lua/navigator/lspwrapper.lua +++ b/lua/navigator/lspwrapper.lua @@ -366,7 +366,7 @@ function M.locations_to_items(locations, ctx) local proj_file = item.uri:find(cwd) or is_win or i < _NgConfigValues.treesitter_analysis_max_num local unload, def local context = '' - if TS_analysis_enabled and proj_file then + if TS_analysis_enabled and proj_file and not ctx.no_show then local ts_context = nts.ref_context local bufnr = vim.uri_to_bufnr(item.uri) @@ -420,7 +420,13 @@ function M.locations_to_items(locations, ctx) end item.filename = assert(vim.uri_to_fname(item.uri)) + local filename = item.filename:gsub(cwd .. path_sep, path_cur, 1) + + if ctx.no_show then + local shorten = require('guihua.util').shorten + filename = shorten(filename) + end item.display_filename = filename or item.filename item.call_by = context -- find_ts_func_by_range(funcs, item.range) item.rpath = util.get_relative_path(cwd, item.filename) diff --git a/lua/navigator/reference.lua b/lua/navigator/reference.lua index 66fd9ce..e899e45 100644 --- a/lua/navigator/reference.lua +++ b/lua/navigator/reference.lua @@ -11,6 +11,7 @@ local locations_to_items = lsphelper.locations_to_items local M = {} local ref_view = function(err, locations, ctx, cfg) + cfg = cfg or {} local truncate = cfg and cfg.truncate or 20 local opts = {} trace('arg1', err, ctx, locations) @@ -78,7 +79,7 @@ local ref_view = function(err, locations, ctx, cfg) local thread_items = vim.deepcopy(items) log('splits: ', #items, #second_part) - local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft') + local ft = vim.api.nvim_buf_get_option(ctx.bufnr or 0, 'ft') local wwidth = vim.api.nvim_get_option('columns') local mwidth = _NgConfigValues.width @@ -94,12 +95,22 @@ local ref_view = function(err, locations, ctx, cfg) api = 'Reference', enable_preview_edit = true, } - local listview = gui.new_list_view(opts) + local listview + if not ctx.no_show then + listview = gui.new_list_view(opts) - if listview == nil then - vim.notify('failed to create preview windows', vim.lsp.log_levels.INFO) - return + if listview == nil then + vim.notify('failed to create preview windows', vim.lsp.log_levels.INFO) + return + end end + + if ctx.no_show then + opts.side_panel = true + local data = require('navigator.render').prepare_for_render(items, opts) + return data + end + -- trace("update items", listview.ctrl.class) local nv_ref_async nv_ref_async = vim.loop.new_async(vim.schedule_wrap(function() @@ -134,6 +145,9 @@ end local ref_hdlr = function(err, locations, ctx, cfg) _NgConfigValues.closer = nil trace(err, locations, ctx, cfg) + if ctx.no_show then + return ref_view(err, locations, ctx, cfg) + end M.async_hdlr = vim.loop.new_async(vim.schedule_wrap(function() ref_view(err, locations, ctx, cfg) if M.async_hdlr:is_active() then @@ -208,7 +222,36 @@ local ref = function() end) end +local function side_panel() + local Panel = require('guihua.panel') + + local currentWord = vim.fn.expand('') + local p = Panel:new({ + scope = 'range', + header = '  ' .. currentWord .. ' ref ', + render = function(bufnr) + local ft = vim.api.nvim_buf_get_option(bufnr, 'buftype') + if ft == 'nofile' or ft == 'guihua' or ft == 'prompt' then + return + end + local ref_params = vim.lsp.util.make_position_params() + local sync_req = require('navigator.lspwrapper').call_sync + return sync_req( + 'textDocument/references', + ref_params, + { timeout = 1000, bufnr = bufnr, no_show = true }, + vim.lsp.with(function(err, locations, ctx, cfg) + cfg.side_panel = true + return ref_hdlr(err, locations, ctx, cfg) + end, { no_show = true }) + ) + end, + }) + p:open(true) +end + return { + side_panel = side_panel, reference_handler = ref_hdlr, reference = ref_req, ref_view = ref_view,