From 77b235b2c9f9a86863ebc570ec99214d0eed5ffa Mon Sep 17 00:00:00 2001 From: ray-x Date: Fri, 10 Feb 2023 09:10:39 +1100 Subject: [PATCH] using async call in sidepanel --- README.md | 50 +++++++++++++++++++++++++++++++++++++ lua/navigator/sidepanel.lua | 28 +++++++++++++++------ lua/navigator/symbols.lua | 2 +- 3 files changed, 71 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 13a860f..3a78f6e 100644 --- a/README.md +++ b/README.md @@ -755,6 +755,9 @@ Treesitter outline and Diagnostics image image +The side panel is vim buffer. You can toggle folds with za/zo/zc + + Calltree (Expandable LSP call hierarchy) image @@ -914,6 +917,53 @@ end [known issues I am working on](https://github.com/ray-x/navigator.lua/issues/1) + +## API and extensions +The plugin built on top of guihua, you can extend the plugin based on your requirements. +e.g. A side pannel of lsp symbols and lsp diagnostics: + + +```lua +local function treesitter_and_diag_panel() + local Panel = require('guihua.panel') + + local diag = require('navigator.diagnostics') + 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 +``` + ## Todo - The project is in the early phase, bugs expected, PRs and suggestions are welcome diff --git a/lua/navigator/sidepanel.lua b/lua/navigator/sidepanel.lua index 5249203..87e41cb 100644 --- a/lua/navigator/sidepanel.lua +++ b/lua/navigator/sidepanel.lua @@ -1,4 +1,3 @@ -local gui = require('navigator.gui') local diagnostic = vim.diagnostic or vim.lsp.diagnostic local util = require('navigator.util') local log = util.log @@ -57,18 +56,31 @@ function M.lsp_and_diag_panel() if ft == 'nofile' or ft == 'guihua' or ft == 'prompt' then return end + local lsp local p = Panel:new({ header = 'symbols', render = function(bufnr) bufnr = bufnr or api.nvim_get_current_buf() 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 lsp_call = require('navigator.lspwrapper').call_sync + if not Panel:is_open() or vim.fn.empty(lsp) == 1 then + lsp = lsp_call( + 'textDocument/documentSymbol', + params, + { timeout = 2000, bufnr = bufnr, no_show = true }, + vim.lsp.with(require('navigator.symbols').document_symbol_handler, { no_show = true }) + ) + else + lsp_call = require('navigator.lspwrapper').call_async + local f = function(err, result, ctx) + -- log(result, ctx) + ctx = ctx or {} + ctx.no_show = true + lsp = require('navigator.symbols').document_symbol_handler(err, result, ctx) + return lsp + end + lsp_call('textDocument/documentSymbol', params, f, bufnr) + end return lsp end, }) diff --git a/lua/navigator/symbols.lua b/lua/navigator/symbols.lua index ec04e31..c210eb3 100644 --- a/lua/navigator/symbols.lua +++ b/lua/navigator/symbols.lua @@ -26,7 +26,7 @@ function M.document_symbols(opts) api = ' ', } - local bufnr = vim.api.nvim_get_current_buf() + local bufnr = opts.bufnr or vim.api.nvim_get_current_buf() vim.list_extend(lspopts, opts) local params = vim.lsp.util.make_position_params() params.context = { includeDeclaration = true }