Open simrat39 opened 2 years ago
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):
local hover = require('hover')
local Job = require 'plenary.job'
local baleia = require('baleia').setup {}
local repo_pattern = '[^%s]+/[^%s]+'
vim.api.nvim_create_autocmd(
'Syntax', {
pattern = 'glow',
callback = function(ctx)
vim.schedule(
function()
vim.api.nvim_buf_set_option(ctx.buf, 'modifiable', true)
baleia.once(ctx.buf)
vim.api.nvim_buf_set_option(ctx.buf, 'modifiable', false)
end
)
end,
}
)
local function enabled()
return vim.fn.expand('<cfile>'):match(repo_pattern) ~= nil
end
local function execute(done)
local repo = vim.fn.expand('<cfile>'):match(repo_pattern)
Job:new(
{
command = 'glow',
args = { 'github.com/' .. repo, '-s', 'dark' },
on_exit = function(job)
done { lines = job:result(), filetype = 'glow' }
end,
}
):start()
end
hover.register({
name = "GitHub repos",
priority = 1050,
enabled = enabled,
execute = execute,
})
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)