aboutsummaryrefslogtreecommitdiff
path: root/runtime/ftplugin/help.lua
blob: 479e4d8b9ff245419dafaf39057434328da00e3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
-- use treesitter over syntax (for highlighted code blocks)
vim.treesitter.start()

-- Add custom highlights for list in `:h highlight-groups`.
local bufname = vim.fs.normalize(vim.api.nvim_buf_get_name(0))
if vim.endswith(bufname, '/doc/syntax.txt') then
  require('vim.vimhelp').highlight_groups({
    { start = [[\*group-name\*]], stop = '^======', match = '^(%w+)\t' },
    { start = [[\*highlight-groups\*]], stop = '^======', match = '^(%w+)\t' },
  })
elseif vim.endswith(bufname, '/doc/treesitter.txt') then
  require('vim.vimhelp').highlight_groups({
    {
      start = [[\*treesitter-highlight-groups\*]],
      stop = [[\*treesitter-highlight-spell\*]],
      match = '^@[%w%p]+',
    },
  })
elseif vim.endswith(bufname, '/doc/diagnostic.txt') then
  require('vim.vimhelp').highlight_groups({
    { start = [[\*diagnostic-highlights\*]], stop = '^======', match = '^(%w+)' },
  })
elseif vim.endswith(bufname, '/doc/lsp.txt') then
  require('vim.vimhelp').highlight_groups({
    { start = [[\*lsp-highlight\*]], stop = '^------', match = '^(%w+)' },
    { start = [[\*lsp-semantic-highlight\*]], stop = '^======', match = '^@[%w%p]+' },
  })
end

vim.keymap.set('n', 'gO', function()
  require('vim.vimhelp').show_toc()
end, { buffer = 0, silent = true })

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

vim.api.nvim_buf_clear_namespace(0, run_message_ns, 0, -1)
for _, match, metadata in query:iter_matches(tree: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() --[[@as integer]]

    if name == 'code' then
      vim.api.nvim_buf_set_extmark(0, run_message_ns, start, 0, {
        virt_text = { { 'Run with `g==`', 'LspCodeLens' } },
      })
      local code = vim.treesitter.get_node_text(node, 0)
      local lang_node = match[metadata[id].lang][1] --[[@as TSNode]]
      local lang = vim.treesitter.get_node_text(lang_node, 0)
      for i = start + 1, end_ do
        code_blocks[i] = { lang = lang, code = code }
      end
    end
  end
end

vim.keymap.set('n', 'g==', function()
  local pos = vim.api.nvim_win_get_cursor(0)[1]
  local code_block = code_blocks[pos]
  if not code_block then
    vim.print('No code block found')
  elseif code_block.lang == 'lua' then
    vim.cmd.lua(code_block.code)
  elseif code_block.lang == 'vim' then
    vim.cmd(code_block.code)
  end
end, { buffer = true })

vim.b.undo_ftplugin = (vim.b.undo_ftplugin or '')
  .. '\n exe "nunmap <buffer> gO" | exe "nunmap <buffer> g=="'
vim.b.undo_ftplugin = vim.b.undo_ftplugin .. ' | call v:lua.vim.treesitter.stop()'