neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

help.lua (5698B)


      1 -- use treesitter over syntax (for highlighted code blocks)
      2 vim.treesitter.start()
      3 
      4 --- Apply current colorscheme to lists of default highlight groups
      5 ---
      6 --- Note: {patterns} is assumed to be sorted by occurrence in the file.
      7 --- @param patterns {start:string,stop:string,match:string}[]
      8 local function colorize_hl_groups(patterns)
      9  local ns = vim.api.nvim_create_namespace('nvim.vimhelp')
     10  vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
     11 
     12  local save_cursor = vim.fn.getcurpos()
     13 
     14  for _, pat in pairs(patterns) do
     15    local start_lnum = vim.fn.search(pat.start, 'c')
     16    local end_lnum = vim.fn.search(pat.stop)
     17    if start_lnum == 0 or end_lnum == 0 then
     18      break
     19    end
     20 
     21    for lnum = start_lnum, end_lnum do
     22      local word = vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, true)[1]:match(pat.match)
     23      if vim.fn.hlexists(word) ~= 0 then
     24        vim.api.nvim_buf_set_extmark(0, ns, lnum - 1, 0, { end_col = #word, hl_group = word })
     25      end
     26    end
     27  end
     28 
     29  vim.fn.setpos('.', save_cursor)
     30 end
     31 
     32 -- Add custom highlights for list in `:h highlight-groups`.
     33 local bufname = vim.fs.normalize(vim.api.nvim_buf_get_name(0))
     34 if vim.endswith(bufname, '/doc/syntax.txt') then
     35  colorize_hl_groups({
     36    { start = [[\*group-name\*]], stop = '^======', match = '^(%w+)\t' },
     37    { start = [[\*highlight-groups\*]], stop = '^======', match = '^(%w+)\t' },
     38  })
     39 elseif vim.endswith(bufname, '/doc/treesitter.txt') then
     40  colorize_hl_groups({
     41    {
     42      start = [[\*treesitter-highlight-groups\*]],
     43      stop = [[\*treesitter-highlight-spell\*]],
     44      match = '^@[%w%p]+',
     45    },
     46  })
     47 elseif vim.endswith(bufname, '/doc/diagnostic.txt') then
     48  colorize_hl_groups({
     49    { start = [[\*diagnostic-highlights\*]], stop = '^======', match = '^(%w+)' },
     50  })
     51 elseif vim.endswith(bufname, '/doc/lsp.txt') then
     52  colorize_hl_groups({
     53    { start = [[\*lsp-highlight\*]], stop = '^------', match = '^(%w+)' },
     54    { start = [[\*lsp-semantic-highlight\*]], stop = '^======', match = '^@[%w%p]+' },
     55  })
     56 end
     57 
     58 vim.keymap.set('n', 'gO', function()
     59  require('vim.treesitter._headings').show_toc()
     60 end, { buffer = 0, silent = true, desc = 'Show an Outline of the current buffer' })
     61 
     62 vim.keymap.set('n', ']]', function()
     63  require('vim.treesitter._headings').jump({ count = 1 })
     64 end, { buffer = 0, silent = false, desc = 'Jump to next section' })
     65 vim.keymap.set('n', '[[', function()
     66  require('vim.treesitter._headings').jump({ count = -1 })
     67 end, { buffer = 0, silent = false, desc = 'Jump to previous section' })
     68 
     69 local parser = assert(vim.treesitter.get_parser(0, 'vimdoc', { error = false }))
     70 
     71 local function runnables()
     72  ---@type table<integer, { lang: string, code: string }>
     73  local code_blocks = {}
     74  local query = vim.treesitter.query.parse(
     75    'vimdoc',
     76    [[
     77    (codeblock
     78      (language) @_lang
     79      .
     80      (code) @code
     81      (#any-of? @_lang "lua" "vim")
     82      (#set! @code lang @_lang))
     83  ]]
     84  )
     85 
     86  local root = parser:parse()[1]:root()
     87  for _, match, metadata in query:iter_matches(root, 0, 0, -1) do
     88    for id, nodes in pairs(match) do
     89      local name = query.captures[id]
     90      local node = nodes[1]
     91      local start, _, end_ = node:parent():range()
     92 
     93      if name == 'code' then
     94        local code = vim.treesitter.get_node_text(node, 0)
     95        local lang_node = match[metadata[id].lang][1] --[[@as TSNode]]
     96        local lang = vim.treesitter.get_node_text(lang_node, 0)
     97        for i = start + 1, end_ do
     98          code_blocks[i] = { lang = lang, code = code }
     99        end
    100      end
    101    end
    102  end
    103 
    104  vim.keymap.set('n', 'g==', function()
    105    local pos = vim.api.nvim_win_get_cursor(0)[1]
    106    local code_block = code_blocks[pos]
    107    if not code_block then
    108      vim.print('No code block found')
    109    elseif code_block.lang == 'lua' then
    110      vim.cmd.lua(code_block.code)
    111    elseif code_block.lang == 'vim' then
    112      vim.cmd(code_block.code)
    113    end
    114  end, { buffer = true })
    115 end
    116 
    117 -- Retry once if the buffer has changed during the iteration of the code
    118 -- blocks. See #36574.
    119 if not pcall(runnables) then
    120  runnables()
    121 end
    122 
    123 local url_ns = vim.api.nvim_create_namespace('nvim.help.urls')
    124 local function urls()
    125  local filepath = vim.fs.normalize(vim.api.nvim_buf_get_name(0))
    126 
    127  if vim.fs.relpath(vim.env.VIMRUNTIME, filepath) ~= nil then
    128    local base = 'https://neovim.io/doc/user/helptag/?tag='
    129    local query = vim.treesitter.query.parse(
    130      'vimdoc',
    131      [[
    132    ((optionlink) @helplink)
    133    (taglink
    134      text: (_) @helplink)
    135    (tag
    136      text: (_) @helplink)
    137    ]]
    138    )
    139 
    140    vim.api.nvim_buf_clear_namespace(0, url_ns, 0, -1)
    141    local root = parser:parse()[1]:root()
    142    for _, match, _ in query:iter_matches(root, 0, 0, -1) do
    143      for id, nodes in pairs(match) do
    144        if query.captures[id] == 'helplink' then
    145          for _, node in ipairs(nodes) do
    146            local start_line, start_col, end_line, end_col = node:range()
    147            local tag = vim.treesitter.get_node_text(node, 0)
    148            vim.api.nvim_buf_set_extmark(0, url_ns, start_line, start_col, {
    149              end_line = end_line,
    150              end_col = end_col,
    151              url = base .. vim.uri_encode(tag),
    152            })
    153          end
    154        end
    155      end
    156    end
    157  end
    158 end
    159 
    160 -- Retry once if the buffer has changed during the iteration of the code
    161 -- blocks. See #36574.
    162 if not pcall(urls) then
    163  urls()
    164 end
    165 
    166 vim.b.undo_ftplugin = (vim.b.undo_ftplugin or '')
    167  .. '\n sil! exe "nunmap <buffer> gO" | sil! exe "nunmap <buffer> g=="'
    168  .. '\n sil! exe "nunmap <buffer> ]]" | sil! exe "nunmap <buffer> [["'
    169  .. ('\n call v:lua.vim.api.nvim_buf_clear_namespace(0, %d, 0, -1)'):format(url_ns)
    170 vim.b.undo_ftplugin = vim.b.undo_ftplugin .. ' | call v:lua.vim.treesitter.stop()'