help.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 table of contents for 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. local run_message_ns = vim.api.nvim_create_namespace('nvim.vimdoc.run_message')
  77. vim.api.nvim_buf_clear_namespace(0, run_message_ns, 0, -1)
  78. for _, match, metadata in query:iter_matches(root, 0, 0, -1) do
  79. for id, nodes in pairs(match) do
  80. local name = query.captures[id]
  81. local node = nodes[1]
  82. local start, _, end_ = node:parent():range()
  83. if name == 'code' then
  84. vim.api.nvim_buf_set_extmark(0, run_message_ns, start, 0, {
  85. virt_text = { { 'Run with `g==`', 'LspCodeLens' } },
  86. })
  87. local code = vim.treesitter.get_node_text(node, 0)
  88. local lang_node = match[metadata[id].lang][1] --[[@as TSNode]]
  89. local lang = vim.treesitter.get_node_text(lang_node, 0)
  90. for i = start + 1, end_ do
  91. code_blocks[i] = { lang = lang, code = code }
  92. end
  93. end
  94. end
  95. end
  96. vim.keymap.set('n', 'g==', function()
  97. local pos = vim.api.nvim_win_get_cursor(0)[1]
  98. local code_block = code_blocks[pos]
  99. if not code_block then
  100. vim.print('No code block found')
  101. elseif code_block.lang == 'lua' then
  102. vim.cmd.lua(code_block.code)
  103. elseif code_block.lang == 'vim' then
  104. vim.cmd(code_block.code)
  105. end
  106. end, { buffer = true })
  107. vim.b.undo_ftplugin = (vim.b.undo_ftplugin or '')
  108. .. '\n sil! exe "nunmap <buffer> gO" | sil! exe "nunmap <buffer> g=="'
  109. .. '\n sil! exe "nunmap <buffer> ]]" | sil! exe "nunmap <buffer> [["'
  110. vim.b.undo_ftplugin = vim.b.undo_ftplugin .. ' | call v:lua.vim.treesitter.stop()'