help.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. -- use treesitter over syntax (for highlighted code blocks)
  2. vim.treesitter.start()
  3. --- Apply current colorscheme to lists of default highlight groups
  4. ---
  5. --- Note: {patterns} is assumed to be sorted by occurrence in the file.
  6. --- @param patterns {start:string,stop:string,match:string}[]
  7. local function colorize_hl_groups(patterns)
  8. local ns = vim.api.nvim_create_namespace('nvim.vimhelp')
  9. vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
  10. local save_cursor = vim.fn.getcurpos()
  11. for _, pat in pairs(patterns) do
  12. local start_lnum = vim.fn.search(pat.start, 'c')
  13. local end_lnum = vim.fn.search(pat.stop)
  14. if start_lnum == 0 or end_lnum == 0 then
  15. break
  16. end
  17. for lnum = start_lnum, end_lnum do
  18. local word = vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, true)[1]:match(pat.match)
  19. if vim.fn.hlexists(word) ~= 0 then
  20. vim.api.nvim_buf_set_extmark(0, ns, lnum - 1, 0, { end_col = #word, hl_group = word })
  21. end
  22. end
  23. end
  24. vim.fn.setpos('.', save_cursor)
  25. end
  26. -- Add custom highlights for list in `:h highlight-groups`.
  27. local bufname = vim.fs.normalize(vim.api.nvim_buf_get_name(0))
  28. if vim.endswith(bufname, '/doc/syntax.txt') then
  29. colorize_hl_groups({
  30. { start = [[\*group-name\*]], stop = '^======', match = '^(%w+)\t' },
  31. { start = [[\*highlight-groups\*]], stop = '^======', match = '^(%w+)\t' },
  32. })
  33. elseif vim.endswith(bufname, '/doc/treesitter.txt') then
  34. colorize_hl_groups({
  35. {
  36. start = [[\*treesitter-highlight-groups\*]],
  37. stop = [[\*treesitter-highlight-spell\*]],
  38. match = '^@[%w%p]+',
  39. },
  40. })
  41. elseif vim.endswith(bufname, '/doc/diagnostic.txt') then
  42. colorize_hl_groups({
  43. { start = [[\*diagnostic-highlights\*]], stop = '^======', match = '^(%w+)' },
  44. })
  45. elseif vim.endswith(bufname, '/doc/lsp.txt') then
  46. colorize_hl_groups({
  47. { start = [[\*lsp-highlight\*]], stop = '^------', match = '^(%w+)' },
  48. { start = [[\*lsp-semantic-highlight\*]], stop = '^======', match = '^@[%w%p]+' },
  49. })
  50. end
  51. vim.keymap.set('n', 'gO', function()
  52. require('vim.treesitter._headings').show_toc()
  53. end, { buffer = 0, silent = true, desc = 'Show an Outline of the current buffer' })
  54. vim.keymap.set('n', ']]', function()
  55. require('vim.treesitter._headings').jump({ count = 1 })
  56. end, { buffer = 0, silent = false, desc = 'Jump to next section' })
  57. vim.keymap.set('n', '[[', function()
  58. require('vim.treesitter._headings').jump({ count = -1 })
  59. end, { buffer = 0, silent = false, desc = 'Jump to previous section' })
  60. -- Add "runnables" for Lua/Vimscript code examples.
  61. ---@type table<integer, { lang: string, code: string }>
  62. local code_blocks = {}
  63. local parser = assert(vim.treesitter.get_parser(0, 'vimdoc', { error = false }))
  64. local query = vim.treesitter.query.parse(
  65. 'vimdoc',
  66. [[
  67. (codeblock
  68. (language) @_lang
  69. .
  70. (code) @code
  71. (#any-of? @_lang "lua" "vim")
  72. (#set! @code lang @_lang))
  73. ]]
  74. )
  75. local root = parser:parse()[1]:root()
  76. for _, match, metadata in query:iter_matches(root, 0, 0, -1) do
  77. for id, nodes in pairs(match) do
  78. local name = query.captures[id]
  79. local node = nodes[1]
  80. local start, _, end_ = node:parent():range()
  81. if name == 'code' then
  82. local code = vim.treesitter.get_node_text(node, 0)
  83. local lang_node = match[metadata[id].lang][1] --[[@as TSNode]]
  84. local lang = vim.treesitter.get_node_text(lang_node, 0)
  85. for i = start + 1, end_ do
  86. code_blocks[i] = { lang = lang, code = code }
  87. end
  88. end
  89. end
  90. end
  91. vim.keymap.set('n', 'g==', function()
  92. local pos = vim.api.nvim_win_get_cursor(0)[1]
  93. local code_block = code_blocks[pos]
  94. if not code_block then
  95. vim.print('No code block found')
  96. elseif code_block.lang == 'lua' then
  97. vim.cmd.lua(code_block.code)
  98. elseif code_block.lang == 'vim' then
  99. vim.cmd(code_block.code)
  100. end
  101. end, { buffer = true })
  102. vim.b.undo_ftplugin = (vim.b.undo_ftplugin or '')
  103. .. '\n sil! exe "nunmap <buffer> gO" | sil! exe "nunmap <buffer> g=="'
  104. .. '\n sil! exe "nunmap <buffer> ]]" | sil! exe "nunmap <buffer> [["'
  105. vim.b.undo_ftplugin = vim.b.undo_ftplugin .. ' | call v:lua.vim.treesitter.stop()'