From 66d84151e94052f710b1dfb0d1fce2faaca6dced Mon Sep 17 00:00:00 2001 From: ray-x Date: Thu, 9 Feb 2023 21:33:15 +1100 Subject: [PATCH] panel LSP symbols + diagnostic --- README.md | 2 +- lua/navigator/diagnostics.lua | 78 ++++++++--------------- lua/navigator/lspclient/mapping.lua | 2 + lua/navigator/sidepanel.lua | 96 +++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 59383c8..d43d4a2 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ variable is: also enables you to handle workspace with mixed types of codes (e.g. Go + javascript + yml). A better default setup is included for LSP clients. -- Out of box experience. 10 lines of minimum vimrc can turn your neovim into a full-featured LSP & Treesitter powered IDE +- Out of box experience. 10 lines of minimum init.lua can turn your neovim into a full-featured LSP & Treesitter powered IDE - UI with floating windows, navigator provides a visual way to manage and navigate through symbols, diagnostic errors, reference etc. It covers all features(handler) provided by LSP from commonly used search reference, to less commonly used search for interface diff --git a/lua/navigator/diagnostics.lua b/lua/navigator/diagnostics.lua index 24875d9..287fc5f 100644 --- a/lua/navigator/diagnostics.lua +++ b/lua/navigator/diagnostics.lua @@ -1,5 +1,4 @@ local gui = require('navigator.gui') -local diagnostic_list = {} local diagnostic = vim.diagnostic or vim.lsp.diagnostic -- local hide = diagnostic.hide or diagnostic.clear local util = require('navigator.util') @@ -14,9 +13,10 @@ local api = vim.api _NG_VT_DIAG_NS = api.nvim_create_namespace('navigator_lua_diag') if not util.nvim_0_6_1() then - util.warn('Navigator 0.4+ only support nvim-0.6+, please use Navigator 0.3.x or a newer version of neovim') + util.warn( + 'Navigator 0.4+ only support nvim-0.6+, please use Navigator 0.3.x or a newer version of neovim' + ) end -diagnostic_list[vim.bo.filetype] = {} local diag_map = {} if vim.diagnostic then @@ -38,6 +38,10 @@ local function get_count(bufnr, level) end end +local M = {} +M.diagnostic_list = {} +M.diagnostic_list[vim.bo.filetype] = {} + local function error_marker(result, ctx, config) if _NgConfigValues.lsp.diagnostic_scrollbar_sign == nil @@ -130,7 +134,8 @@ local function error_marker(result, ctx, config) if pos[#pos] == bar then bar = _NgConfigValues.lsp.diagnostic_scrollbar_sign[3] end - pos[#pos] = { line = p, sign = bar, severity = math.min(diag.severity, pos[#pos].severity) } + pos[#pos] = + { line = p, sign = bar, severity = math.min(diag.severity, pos[#pos].severity) } else table.insert(pos, { line = p, @@ -194,8 +199,8 @@ local diag_hdlr = function(err, result, ctx, config) end local cwd = vim.loop.cwd() local ft = vim.bo.filetype - if diagnostic_list[ft] == nil then - diagnostic_list[vim.bo.filetype] = {} + if M.diagnostic_list[ft] == nil then + M.diagnostic_list[vim.bo.filetype] = {} end local client_id = ctx.client_id @@ -287,11 +292,11 @@ local diag_hdlr = function(err, result, ctx, config) table.insert(item_list, item) end -- local old_items = vim.fn.getqflist() - if diagnostic_list[ft][uri] == nil then - diagnostic_list[ft][uri] = {} + if M.diagnostic_list[ft][uri] == nil then + M.diagnostic_list[ft][uri] = {} end - diagnostic_list[ft][uri][tostring(client_id)] = item_list - trace(uri, ft, diagnostic_list) + M.diagnostic_list[ft][uri][tostring(client_id)] = item_list + trace(uri, ft, M.diagnostic_list) if not result.uri then result.uri = uri end @@ -310,7 +315,6 @@ end -- return debounce(100, diag_hdlr) -- end -local M = {} function M.setup() if diagnostic_cfg ~= nil and diagnostic_cfg.float ~= nil then return @@ -334,7 +338,9 @@ function M.setup() }, } diagnostic_cfg.virtual_text = _NgConfigValues.lsp.diagnostic.virtual_text - if type(_NgConfigValues.lsp.diagnostic.virtual_text) == 'table' and _NgConfigValues.icons.icons then + if + type(_NgConfigValues.lsp.diagnostic.virtual_text) == 'table' and _NgConfigValues.icons.icons + then diagnostic_cfg.virtual_text.prefix = _NgConfigValues.icons.diagnostic_virtual_text end -- vim.lsp.handlers["textDocument/publishDiagnostics"] @@ -380,8 +386,8 @@ M.toggle_diagnostics = function() end M.show_buf_diagnostics = function() - if diagnostic_list[vim.bo.filetype] ~= nil then - local results = diagnostic_list[vim.bo.filetype] + if M.diagnostic_list[vim.bo.filetype] ~= nil then + local results = M.diagnostic_list[vim.bo.filetype] local display_items = {} for _, client_items in pairs(results) do for _, items in pairs(client_items) do @@ -394,7 +400,9 @@ M.show_buf_diagnostics = function() if #display_items > 0 then local listview = gui.new_list_view({ items = display_items, - api = _NgConfigValues.icons.diagnostic_file .. _NgConfigValues.icons.diagnostic_head .. ' Diagnostic ', + api = _NgConfigValues.icons.diagnostic_file + .. _NgConfigValues.icons.diagnostic_head + .. ' Diagnostic ', enable_preview_edit = true, }) if listview == nil then @@ -415,7 +423,7 @@ M.setloclist = function(bufnr) if diag_cnt == 0 then log('great, no errors!') - -- vim.fn.getloclist(0, {filewinid=0}) + -- vim.fn.getloclist(0, {filewinid=0}) return vim.cmd('lclose') end @@ -508,44 +516,6 @@ function M.show_diagnostics(pos) diagnostic.open_float(bufnr, opt) end -function M.treesitter_and_diag_panel() - local Panel = require('guihua.panel') - - local ft = vim.bo.filetype - local results = diagnostic_list[ft] - log(diagnostic_list, ft) - - local bufnr = api.nvim_get_current_buf() - local p = Panel:new({ - header = 'treesitter', - render = function(b) - log('render for ', bufnr, b) - return require('navigator.treesitter').all_ts_nodes(b) - end, - }) - p:add_section({ - header = 'diagnostic', - render = function(buf) - log(buf, diagnostic) - if diagnostic_list[ft] ~= nil then - local display_items = {} - for _, client_items in pairs(results) do - for _, items in pairs(client_items) do - for _, it in pairs(items) do - log(it) - table.insert(display_items, it) - end - end - end - return display_items - else - return {} - end - end, - }) - p:open(true) -end - function M.config(cfg) M.setup() cfg = cfg or {} diff --git a/lua/navigator/lspclient/mapping.lua b/lua/navigator/lspclient/mapping.lua index 67c629f..6558b81 100644 --- a/lua/navigator/lspclient/mapping.lua +++ b/lua/navigator/lspclient/mapping.lua @@ -128,6 +128,8 @@ local function set_cmds(_) "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()", + "command! -nargs=* TsDiag lua require'navigator.sidepanel'.treesitter_and_diag_panel()", + "command! -nargs=* LspDiag lua require'navigator.sidepanel'.lsp_and_diag_panel()", } for _, value in pairs(commands) do diff --git a/lua/navigator/sidepanel.lua b/lua/navigator/sidepanel.lua index e69de29..795b1c6 100644 --- a/lua/navigator/sidepanel.lua +++ b/lua/navigator/sidepanel.lua @@ -0,0 +1,96 @@ +local gui = require('navigator.gui') +local diagnostic = vim.diagnostic or vim.lsp.diagnostic +local diag = require('navigator.diagnostics') +local util = require('navigator.util') +local log = util.log +local api = vim.api + +local M = {} +function M.treesitter_and_diag_panel() + local Panel = require('guihua.panel') + + local ft = vim.bo.filetype + local results = diag.diagnostic_list[ft] + log(diag.diagnostic_list, ft) + + local bufnr = api.nvim_get_current_buf() + local p = Panel:new({ + header = 'treesitter', + render = function(b) + log('render for ', bufnr, b) + return require('navigator.treesitter').all_ts_nodes(b) + end, + }) + p:add_section({ + header = 'diagnostic', + render = function(buf) + log(buf, diagnostic) + if diag.diagnostic_list[ft] ~= nil then + local display_items = {} + for _, client_items in pairs(results) do + for _, items in pairs(client_items) do + for _, it in pairs(items) do + log(it) + table.insert(display_items, it) + end + end + end + return display_items + else + return {} + end + end, + }) + p:open(true) +end + +function M.lsp_and_diag_panel() + local Panel = require('guihua.panel') + + local ft = vim.bo.filetype + local results = diag.diagnostic_list[ft] + log(diag.diagnostic_list, ft) + + bufnr = bufnr or api.nvim_get_current_buf() + ft = vim.api.nvim_buf_get_option(bufnr, 'buftype') or vim.bo.filetype + if ft == 'nofile' or ft == 'guihua' or ft == 'prompt' then + return + end + local params = vim.lsp.util.make_range_params() + local sync_req = require('navigator.lspwrapper').call_sync + local lsp = sync_req( + 'textDocument/documentSymbol', + params, + { timeout = 2000, bufnr = bufnr, no_show = true }, + vim.lsp.with(require('navigator.symbols').document_symbol_handler, { no_show = true }) + ) + local p = Panel:new({ + header = 'symboles', + render = function(bufnr) + return lsp + end, + }) + p:add_section({ + header = 'diagnostic', + render = function(buf) + log(buf, diagnostic) + if diag.diagnostic_list[ft] ~= nil then + local display_items = {} + for _, client_items in pairs(results) do + for _, items in pairs(client_items) do + for _, it in pairs(items) do + log(it) + table.insert(display_items, it) + end + end + end + return display_items + else + return {} + end + end, + }) + p:open(true) +end + +return M