Hyperlinks to Rules in Neovim #21192
-
|
In the vscode extension the diagnostics show a hyperlink directly to the mentioned rule in the documentation. I may have missed it but is this also in the neovim plugin? I couldn't figure it out when looking. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hi @adrianveen Ruff's editor functionality is based on the LSP. Specifically, Ruff uses the |
Beta Was this translation helpful? Give feedback.
-
|
I am an absolute lua noob so this is without any warranty to be good code, but I am using something like this to access and open the links: vim.api.nvim_create_user_command('RuffLineLinks', function()
local line = vim.fn.line '.' - 1
local diags = vim.diagnostic.get(0, { lnum = line })
local items = {}
for _, d in ipairs(diags) do
local lsp = d.user_data and d.user_data.lsp
local cd = lsp and lsp.codeDescription
if cd and cd.href then
table.insert(items, {
label = string.format('%s — %s', d.code or '?', d.message),
url = cd.href,
})
end
end
if #items == 0 then
vim.notify('No Ruff diagnostic links on this line', vim.log.levels.INFO)
return
end
vim.ui.select(items, {
prompt = 'Ruff rule documentation',
format_item = function(item)
return item.label
end,
}, function(choice)
if choice then
vim.ui.open(choice.url)
end
end)
end, { desc = 'Open Ruff diagnostic links (code-action style)' })
vim.keymap.set('n', '<leader>cr', '<cmd>RuffLineLinks<CR>', { desc = '[C]ode [R]uff Links' })
|
Beta Was this translation helpful? Give feedback.
Hi @adrianveen
Ruff's editor functionality is based on the LSP. Specifically, Ruff uses the
CodeDescriptionfield to expose the URL to the client. However, this also requires that the client, in this case neovim, supports that part of the LSP specification which I don't know if it does.