fix(help): wrong tag url in third-party help docs #36115

fix(help): only set url for nvim-owned tags

Problem:
1. gx on |nonexistingtag| opens
https://neovim.io/doc/user/helptag.html?nonexistingtag.
2. b:undo_ftplugin doesn't remove url extmarks.

Solution:
1. Check if the tag is defined in a help file in $VIMRUNTIME.
2. Solution: clear namespace for buffer in b:undo_ftplugin.
This commit is contained in:
Yochem van Rosmalen
2025-10-13 00:08:18 +02:00
committed by GitHub
parent c6113da5a9
commit 62d3a2110d
2 changed files with 27 additions and 6 deletions

View File

@@ -115,8 +115,8 @@ do
end, { buffer = true })
end
local url_ns = vim.api.nvim_create_namespace('nvim.help.urls')
do
local ns = vim.api.nvim_create_namespace('nvim.help.urls')
local base = 'https://neovim.io/doc/user/helptag.html?tag='
local query = vim.treesitter.query.parse(
'vimdoc',
@@ -129,17 +129,28 @@ do
]]
)
local function is_nvim_tag(tag)
local tagsfile = vim.fs.joinpath(vim.env.VIMRUNTIME, 'doc', 'tags')
local candidates = vim.fn.taglist('^' .. tag .. '$', tagsfile)
if #candidates == 0 then
return false
end
return vim.fs.relpath(vim.env.VIMRUNTIME, candidates[1].filename) ~= nil
end
for _, match, _ in query:iter_matches(root, 0, 0, -1) do
for id, nodes in pairs(match) do
if query.captures[id] == 'helplink' then
for _, node in ipairs(nodes) do
local start_line, start_col, end_line, end_col = node:range()
local tag = vim.treesitter.get_node_text(node, 0)
vim.api.nvim_buf_set_extmark(0, ns, start_line, start_col, {
end_line = end_line,
end_col = end_col,
url = base .. vim.uri_encode(tag),
})
if is_nvim_tag(tag) then
vim.api.nvim_buf_set_extmark(0, url_ns, start_line, start_col, {
end_line = end_line,
end_col = end_col,
url = base .. vim.uri_encode(tag),
})
end
end
end
end
@@ -149,4 +160,5 @@ end
vim.b.undo_ftplugin = (vim.b.undo_ftplugin or '')
.. '\n sil! exe "nunmap <buffer> gO" | sil! exe "nunmap <buffer> g=="'
.. '\n sil! exe "nunmap <buffer> ]]" | sil! exe "nunmap <buffer> [["'
.. ('\n call v:lua.vim.api.nvim_buf_clear_namespace(0, %d, 0, -1)'):format(url_ns)
vim.b.undo_ftplugin = vim.b.undo_ftplugin .. ' | call v:lua.vim.treesitter.stop()'

View File

@@ -202,6 +202,15 @@ describe('vim.ui', function()
local tagname =
n.api.nvim_buf_get_text(0, tag[2], tag[3], tag[4].end_row, tag[4].end_col, {})[1]
eq(vim.uri_encode(tagname), param)
-- non-nvim tags are ignored
local buf = n.api.nvim_create_buf(false, false)
n.api.nvim_buf_set_lines(buf, 0, 0, false, {
'|nonexisting|',
})
n.api.nvim_set_option_value('filetype', 'help', { buf = buf, scope = 'local' })
local tags = n.api.nvim_buf_get_extmarks(buf, link_ns, 0, -1, {})
eq(#tags, 0)
end)
end)
end)