diff --git a/README.md b/README.md index 6e19b81..4f6a35f 100644 --- a/README.md +++ b/README.md @@ -346,18 +346,22 @@ require'navigator'.setup({ hover = { enable = true, - keymap = { - [''] = { - go = function() - local w = vim.fn.expand('') - vim.cmd('GoDoc ' .. w) - end, - default = function() - local w = vim.fn.expand('') - vim.lsp.buf.workspace_symbol(w) - end, - }, - }, + -- fallback when hover failed + -- e.g. if filetype is go, try godoc + go = function() + local w = vim.fn.expand('') + vim.cmd('GoDoc ' .. w) + end, + -- if python, do python doc + python = function() + -- run pydoc, behaviours defined in lua/navigator.lua + end, + default = function() + -- fallback apply to all file types not been specified above + -- local w = vim.fn.expand('') + -- vim.lsp.buf.workspace_symbol(w) + end, + }, diagnostic_scrollbar_sign = {'▃', '▆', '█'}, -- experimental: diagnostic status in scroll bar area; set to false to disable the diagnostic sign, -- for other style, set to {'╍', 'ﮆ'} or {'-', '='} diff --git a/lua/navigator.lua b/lua/navigator.lua index a1541e1..12b2af8 100644 --- a/lua/navigator.lua +++ b/lua/navigator.lua @@ -98,43 +98,42 @@ _NgConfigValues = { workspace = { enable = true }, hover = { enable = true, - keymaps = { - [''] = { - go = function() - local w = vim.fn.expand('') - w = w:gsub('*', '') - vim.cmd('GoDoc ' .. w) - end, - python = function() - local w = vim.fn.expand('') - local setup = { - 'pydoc', - w, + -- fallback if hover failed + -- go = function() + -- local w = vim.fn.expand('') + -- w = w:gsub('*', '') + -- vim.cmd('GoDoc ' .. w) + -- end, + python = function() + local w = vim.fn.expand('') + local setup = { + 'pydoc', + w, + } + return vim.fn.jobstart(setup, { + on_stdout = function(_, data, _) + if not data or (#data == 1 and vim.fn.empty(data[1]) == 1) then + return + end + local close_events = { 'CursorMoved', 'CursorMovedI', 'BufHidden', 'InsertCharPre' } + local config = { + close_events = close_events, + focusable = true, + border = 'single', + width = 80, + zindex = 100, } - return vim.fn.jobstart(setup, { - on_stdout = function(_, data, _) - if not data or (#data == 1 and vim.fn.empty(data[1]) == 1) then - return - end - local close_events = { 'CursorMoved', 'CursorMovedI', 'BufHidden', 'InsertCharPre' } - local config = { - close_events = close_events, - focusable = true, - border = 'single', - width = 80, - zindex = 100, - } - vim.lsp.util.open_floating_preview(data, 'python', config) - end, - }) + vim.lsp.util.open_floating_preview(data, 'python', config) end, - default = function() - local w = vim.fn.expand('') - print('default ' .. w) - vim.lsp.buf.workspace_symbol(w) - end, - }, - }, + }) + end, + default = function() + -- local w = vim.fn.expand('') + -- print('default ' .. w) + -- vim.lsp.buf.workspace_symbol(w) + end, + -- }, + -- }, }, -- bind hover action to keymap; there are other options e.g. noice, lspsaga provides lsp hover format_on_save = true, -- {true|false} set to false to disasble lsp code format on save (if you are using prettier/efm/formater etc) -- table: {enable = {'lua', 'go'}, disable = {'javascript', 'typescript'}} to enable/disable specific language @@ -246,6 +245,9 @@ M.deprecated = function(cfg) vim.lsp.get_clients = vim.lsp.get_active_clients vim.islist = vim.tbl_islist end + if cfg.lsp and cfg.lsp.hover and cfg.lsp.hover.keymaps then + warn('lsp.hover.keymaps is deprecated, refer to README for more details') + end end local extend_config = function(opts) diff --git a/lua/navigator/hover.lua b/lua/navigator/hover.lua index ede42cf..6ea4aa2 100644 --- a/lua/navigator/hover.lua +++ b/lua/navigator/hover.lua @@ -8,18 +8,32 @@ local M = {} function M.handler(err, result, ctx, config) config = config or {} config.focus_id = ctx.method - if err then - return vim.notify('no hover info ' .. err) - end if api.nvim_get_current_buf() ~= ctx.bufnr then -- Ignore result since buffer changed. This happens for slow language servers. return end - if not (result and result.contents) then + local failed = false + if err then + vim.notify('no hover info ' .. err) + failed = true + end + if not result or not result.contents then if config.silent ~= true then vim.notify('No hover information available') end - return + failed = true + end + local bufnr = ctx.bufnr + -- get filetype for bufnr + local ft = api.nvim_buf_get_option(bufnr, 'filetype') + if failed then + if _NgConfigValues.lsp.hover.ft then + local fallback_fn = _NgConfigValues.hover.ft or '' + if type(fallback_fn) == 'function' then + fallback_fn(ctx, config) + end + end + return -- return early as no valid hover info lets fallback to other sources end local format = 'markdown' local contents ---@type string[]