You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some hover providers (like rust-analyzer) allow clients to have certain actions embedded in the hover, which would need providers to be able to modify the buffer and window (set keymaps, change window options) etc. So, I think allowing providers the ability to do that is a good idea.
Proposed Change
Allow providers to define a on_render function which is called when the hover is rendered.
Example Implementation
diff --git a/lua/hover/actions.lua b/lua/hover/actions.lua
index 65d8cc5..321c269 100644
--- a/lua/hover/actions.lua+++ b/lua/hover/actions.lua@@ -66,11 +66,13 @@ local function focus_or_close_hover()
end
local function show_hover(provider_id, config, result, opts)
- local _, winnr = util.open_floating_preview(result.lines, result.filetype, opts)+ local bufnr, winnr = util.open_floating_preview(result.lines, result.filetype, opts)
if config.title then
add_title(winnr, provider_id)
end
++ return bufnr, winnr
end
-- Must be called in async context
@@ -89,7 +91,10 @@ local function run_provider(provider)
local result = provider.execute()
if result then
async.scheduler()
- show_hover(provider.id, config, result, opts)+ local bufnr, winnr = show_hover(provider.id, config, result, opts)+ if provider.on_render then+ provider.on_render(bufnr, winnr)+ end
return true
end
Example provider implementation (rust-tools hover actions)
---@diagnosticdisable:missing-parameter,param-type-mismatchlocalM= {}
M._state= { commands=nil }
localfunctionexecute_rust_analyzer_command(action, ctx)
localfn=vim.lsp.commands[action.command]
iffnthenfn(action, ctx)
endend-- run the command under the cursor, if the thing under the cursor is not the-- command then do nothinglocalfunctionrun_command(ctx)
localwinnr=vim.api.nvim_get_current_win()
localline=vim.api.nvim_win_get_cursor(winnr)[1]
ifline>#M._state.commandsthenreturnendlocalaction=M._state.commands[line]
vim.api.nvim_win_close(winnr, true)
execute_rust_analyzer_command(action, ctx)
endlocalfunctionparse_commands()
localprompt= {}
fori, valueinipairs(M._state.commands) doifvalue.command=="rust-analyzer.gotoLocation" thentable.insert(
prompt,
string.format("%d. Go to %s (%s)", i, value.title, value.tooltip)
)
elseifvalue.command=="rust-analyzer.showReferences" thentable.insert(prompt, string.format("%d. %s", i, "Go to " ..value.title))
elsetable.insert(prompt, string.format("%d. %s", i, value.title))
endendreturnpromptendrequire("hover").register({
name="Rust Hover Actions",
enabled=function()
returntrueend,
execute=function(done)
localutil=require("vim.lsp.util")
localparams=util.make_position_params()
vim.lsp.buf_request(
0,
"textDocument/hover",
params,
function(_, result, ctx)
ifnotresultornotresult.contentsthendone()
returnendM._state.commands=nillocallines=util.convert_input_to_markdown_lines(result.contents)
ifresult.actionsthenM._state.commands=result.actions[1].commandslocalprompt=parse_commands()
locall= {}
for_, valueinipairs(prompt) dotable.insert(l, value)
endlines=vim.list_extend(l, lines)
endlines=util.trim_empty_lines(lines)
M._state.ctx=ctxifvim.tbl_isempty(lines) thendone()
returnenddone({ lines=lines, filetype="markdown" })
end
)
end,
on_render=function(bufnr, winnr)
ifM._state.commands==nilthenreturnend-- makes more sense in a dropdown-ish uivim.api.nvim_win_set_option(winnr, "cursorline", true)
-- run the command under the cursorvim.keymap.set("n", "<CR>", function()
run_command(M._state.ctx)
end, { buffer=bufnr, noremap=true, silent=true })
end,
})
returnM
The text was updated successfully, but these errors were encountered:
I had a use case for this where the hover output was colored using ANSI sequences and I wanted to render those correctly.
Here is how I did it, might be helpful:
When you call the done callback pass a custom filetype that ideally is not used anywhere else:
done { lines=job:result(), filetype='glow' }
Internally, the syntax will be set to this filetype, so using the following snippet you can run code once it is opened:
vim.api.nvim_create_autocmd('Syntax', {
pattern="glow",
callback=function(ctx)
vim.schedule(function()
vim.api.nvim_buf_set_option(ctx.buf, 'modifiable', true)
-- Do your stuff here.vim.api.nvim_buf_set_option(ctx.buf, 'modifiable', false)
end)
end,
})
Full working example of my use case (GitHub repos rendered with charmbracelet/glow, converted using m00qek/baleia.nvim):
Some hover providers (like rust-analyzer) allow clients to have certain actions embedded in the hover, which would need providers to be able to modify the buffer and window (set keymaps, change window options) etc. So, I think allowing providers the ability to do that is a good idea.
Proposed Change
Allow providers to define a
on_render
function which is called when the hover is rendered.Example Implementation
Example provider implementation (rust-tools hover actions)
The text was updated successfully, but these errors were encountered: