From 62d3a2110df4999b39e25026ad85628551b5e80c Mon Sep 17 00:00:00 2001 From: Yochem van Rosmalen Date: Mon, 13 Oct 2025 00:08:18 +0200 Subject: [PATCH] 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. --- runtime/ftplugin/help.lua | 24 ++++++++++++++++++------ test/functional/lua/ui_spec.lua | 9 +++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/runtime/ftplugin/help.lua b/runtime/ftplugin/help.lua index 34a477ad62..0379bd0c97 100644 --- a/runtime/ftplugin/help.lua +++ b/runtime/ftplugin/help.lua @@ -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 gO" | sil! exe "nunmap g=="' .. '\n sil! exe "nunmap ]]" | sil! exe "nunmap [["' + .. ('\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()' diff --git a/test/functional/lua/ui_spec.lua b/test/functional/lua/ui_spec.lua index 572df3ee26..5fe4257a3d 100644 --- a/test/functional/lua/ui_spec.lua +++ b/test/functional/lua/ui_spec.lua @@ -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)