Repository: neovim/neovim
File: runtime/ftplugin/help.lua (Lua)

Code before:
    (taglink
      text: (_) @helplink)
    (tag
      text: (_) @helplink)
  ]]
  )

  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

  local helpbuf = vim.api.nvim_get_current_buf()
  vim.api.nvim_set_decoration_provider(url_ns, {
    on_win = function (_, _, buf, topline, botline)
      if buf ~= helpbuf then
        return
      end
      for _, match, _ in query:iter_matches(root, 0, topline, botline) do
        for id, nodes in pairs(match) do
          if query.captures[id] == 'helplink' then
            for _, node in ipairs(nodes) do
              local sl, sc, el, ec = node:range()
              if #vim.api.nvim_buf_get_extmarks(buf, url_ns, { sl, sc }, { el, ec }, {}) == 0 then
                local tag = vim.treesitter.get_node_text(node, 0)
                if is_nvim_tag(tag) then
                  vim.api.nvim_buf_set_extmark(0, url_ns, sl, sc, {
                    end_line = el,
                    end_col = ec,
                    url = base .. vim.uri_encode(tag),
                  })
                end
              end
            end
          end
        end
      end
    end
  })
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> [["'
vim.b.undo_ftplugin = vim.b.undo_ftplugin .. ' | call v:lua.vim.treesitter.stop()'


Review comment (performance):
Is it performance-wise better to check if the extmark has been set (this is what runtime/lua/vim/treesitter/highlighter.lua does) or to just clear them first?

Code after:
    (taglink
      text: (_) @helplink)
    (tag
      text: (_) @helplink)
  ]]
  )

  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)
          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
  end
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()'