vimhelp.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. -- Extra functionality for displaying Vim help.
  2. local M = {}
  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. function M.highlight_groups(patterns)
  8. local ns = vim.api.nvim_create_namespace('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. --- Show a table of contents for the help buffer in a loclist
  27. function M.show_toc()
  28. local bufnr = vim.api.nvim_get_current_buf()
  29. local parser = assert(vim.treesitter.get_parser(bufnr, 'vimdoc', { error = false }))
  30. local query = vim.treesitter.query.parse(
  31. parser:lang(),
  32. [[
  33. (h1 (heading) @h1)
  34. (h2 (heading) @h2)
  35. (h3 (heading) @h3)
  36. (column_heading (heading) @h4)
  37. ]]
  38. )
  39. local root = parser:parse()[1]:root()
  40. local headings = {}
  41. for id, node, _, _ in query:iter_captures(root, bufnr) do
  42. local text = vim.treesitter.get_node_text(node, bufnr)
  43. local capture = query.captures[id]
  44. local row, col = node:start()
  45. -- only column_headings at col 1 are headings, otherwise it's code examples
  46. local is_code = (capture == 'h4' and col > 0)
  47. -- ignore tabular material
  48. local is_table = (capture == 'h4' and (text:find('\t') or text:find(' ')))
  49. -- ignore tag-only headings
  50. local is_tag = node:child_count() == 1 and node:child(0):type() == 'tag'
  51. if not (is_code or is_table or is_tag) then
  52. table.insert(headings, {
  53. bufnr = bufnr,
  54. lnum = row + 1,
  55. text = (capture == 'h3' or capture == 'h4') and '  ' .. text or text,
  56. })
  57. end
  58. end
  59. vim.fn.setloclist(0, headings, ' ')
  60. vim.fn.setloclist(0, {}, 'a', { title = 'Help TOC' })
  61. vim.cmd.lopen()
  62. end
  63. return M