Skip to content

Commit

Permalink
allow hover to fallback, e.g. use pydoc when hover failed
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-x committed Jun 4, 2024
1 parent 877a457 commit 3be5241
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 52 deletions.
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,18 +346,22 @@ require'navigator'.setup({

hover = {
enable = true,
keymap = {
['<C-k>'] = {
go = function()
local w = vim.fn.expand('<cWORD>')
vim.cmd('GoDoc ' .. w)
end,
default = function()
local w = vim.fn.expand('<cWORD>')
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('<cWORD>')
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('<cWORD>')
-- 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 {'-', '='}
Expand Down
72 changes: 37 additions & 35 deletions lua/navigator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,43 +98,42 @@ _NgConfigValues = {
workspace = { enable = true },
hover = {
enable = true,
keymaps = {
['<C-k>'] = {
go = function()
local w = vim.fn.expand('<cWORD>')
w = w:gsub('*', '')
vim.cmd('GoDoc ' .. w)
end,
python = function()
local w = vim.fn.expand('<cWORD>')
local setup = {
'pydoc',
w,
-- fallback if hover failed
-- go = function()
-- local w = vim.fn.expand('<cWORD>')
-- w = w:gsub('*', '')
-- vim.cmd('GoDoc ' .. w)
-- end,
python = function()
local w = vim.fn.expand('<cWORD>')
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('<cword>')
print('default ' .. w)
vim.lsp.buf.workspace_symbol(w)
end,
},
},
})
end,
default = function()
-- local w = vim.fn.expand('<cword>')
-- 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
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 19 additions & 5 deletions lua/navigator/hover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down

0 comments on commit 3be5241

Please sign in to comment.