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

Code before:
      match = '^@[%w%p]+',
    },
  })
elseif vim.endswith(bufname, '/doc/diagnostic.txt') then
  colorize_hl_groups({
    { start = [[\*diagnostic-highlights\*]], stop = '^======', match = '^(%w+)' },
  })
elseif vim.endswith(bufname, '/doc/lsp.txt') then
  colorize_hl_groups({
    { start = [[\*lsp-highlight\*]], stop = '^------', match = '^(%w+)' },
    { start = [[\*lsp-semantic-highlight\*]], stop = '^======', match = '^@[%w%p]+' },
  })
end

vim.keymap.set('n', 'gO', function()
  require('vim.treesitter._headings').show_toc()
end, { buffer = 0, silent = true, desc = 'Show an Outline of the current buffer' })

vim.keymap.set('n', ']]', function()
  require('vim.treesitter._headings').jump({ count = 1 })
end, { buffer = 0, silent = false, desc = 'Jump to next section' })
vim.keymap.set('n', '[[', function()
  require('vim.treesitter._headings').jump({ count = -1 })
end, { buffer = 0, silent = false, desc = 'Jump to previous section' })

local parser = assert(vim.treesitter.get_parser(0, 'vimdoc', { error = false }))
local root = parser:parse()[1]:root()

-- Add "runnables" for Lua/Vimscript code examples.
---@type table<integer, { lang: string, code: string }>
do
  local code_blocks = {}
  local query = vim.treesitter.query.parse(
    'vimdoc',
    [[
    (codeblock
      (language) @_lang
      .
      (code) @code
      (#any-of? @_lang "lua" "vim")
      (#set! @code lang @_lang))
  ]]
  )

  for _, match, metadata in query:iter_matches(root, 0, 0, -1) do
    for id, nodes in pairs(match) do
      local name = query.captures[id]
      local node = nodes[1]
      local start, _, end_ = node:parent():range()

      if name == 'code' then

Review comment (suggestion):
I've kept the `parser` and `root` definition outside the `do...end` blocks so the code for "runnables" and for the gx implementation share those. Can also be repeated if that's cleaner.

Code after:
      match = '^@[%w%p]+',
    },
  })
elseif vim.endswith(bufname, '/doc/diagnostic.txt') then
  colorize_hl_groups({
    { start = [[\*diagnostic-highlights\*]], stop = '^======', match = '^(%w+)' },
  })
elseif vim.endswith(bufname, '/doc/lsp.txt') then
  colorize_hl_groups({
    { start = [[\*lsp-highlight\*]], stop = '^------', match = '^(%w+)' },
    { start = [[\*lsp-semantic-highlight\*]], stop = '^======', match = '^@[%w%p]+' },
  })
end

vim.keymap.set('n', 'gO', function()
  require('vim.treesitter._headings').show_toc()
end, { buffer = 0, silent = true, desc = 'Show an Outline of the current buffer' })

vim.keymap.set('n', ']]', function()
  require('vim.treesitter._headings').jump({ count = 1 })
end, { buffer = 0, silent = false, desc = 'Jump to next section' })
vim.keymap.set('n', '[[', function()
  require('vim.treesitter._headings').jump({ count = -1 })
end, { buffer = 0, silent = false, desc = 'Jump to previous section' })

local parser = assert(vim.treesitter.get_parser(0, 'vimdoc', { error = false }))
local root = parser:parse()[1]:root()

-- Add "runnables" for Lua/Vimscript code examples.
do
  ---@type table<integer, { lang: string, code: string }>
  local code_blocks = {}
  local query = vim.treesitter.query.parse(
    'vimdoc',
    [[
    (codeblock
      (language) @_lang
      .
      (code) @code
      (#any-of? @_lang "lua" "vim")
      (#set! @code lang @_lang))
  ]]
  )

  for _, match, metadata in query:iter_matches(root, 0, 0, -1) do
    for id, nodes in pairs(match) do
      local name = query.captures[id]
      local node = nodes[1]
      local start, _, end_ = node:parent():range()

      if name == 'code' then