Skip to content

Commit 6e2d53a

Browse files
committed
refactor(parser_markdown): Improved parsing of highlight group names.
1 parent 3be2f9f commit 6e2d53a

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

lua/helpview/parsers/vimdoc.lua

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,21 @@ end
357357
vimdoc.tag = function (buffer, _, text, range)
358358
local after = vim.api.nvim_buf_get_text(buffer, range.row_start, range.col_end, range.row_start, -1, {})[1] or "";
359359

360+
if string.match(text[1], "%*hl%-.+%*") then
361+
local next_line = vim.api.nvim_buf_get_lines(buffer, range.row_start + 1, range.row_start + 2, false)[1];
362+
local spaces, hl = string.match(next_line or "", "^(%s*)([a-zA-Z0-9_.@-]+)")
363+
364+
if hl then
365+
vimdoc.internal_hl(buffer, _, { hl }, {
366+
row_start = range.row_start + 1,
367+
row_end = range.row_start + 1,
368+
369+
col_start = #spaces,
370+
col_end = #spaces + vim.fn.strcharlen(hl);
371+
});
372+
end
373+
end
374+
360375
vimdoc.insert({
361376
class = "vimdoc_tag",
362377

@@ -386,16 +401,30 @@ vimdoc.taglink = function (buffer, _, text, range)
386401
});
387402
end
388403

404+
--- Word processor for `default` help files.
405+
---@param text string[]
406+
---@param range helpview.parsed.range
407+
vimdoc.internal_hl = function (buffer, _, text, range)
408+
local after = vim.api.nvim_buf_get_text(buffer, range.row_start, range.col_end, range.row_start, -1, {})[1] or "";
409+
410+
vimdoc.insert({
411+
class = "vimdoc_hl",
412+
413+
group_name = text[1],
414+
after = after:match("^ +"),
415+
416+
text = text,
417+
range = range
418+
});
419+
end
420+
389421
--- Word processor.
390422
---@param text string[]
391423
---@param range helpview.parsed.range
392424
vimdoc.hl = function (buffer, _, text, range)
393425
if not vim.g.__helpview_hl_group_map then
394-
-- Do not show highlight groups if we don't
395-
-- have the highlight group map.
396426
return;
397427
elseif not text[1] or not vim.g.__helpview_hl_group_map[text[1]] then
398-
-- Highlight group doesn't exist.
399428
return;
400429
end
401430

@@ -479,6 +508,16 @@ vimdoc.parse = function (buffer, TSTree, from, to)
479508

480509
if not capture_name:match("^vimdoc%.") then
481510
goto continue
511+
elseif capture_name == "vimdoc.hl" then
512+
local runtime = vim.pesc(
513+
vim.fn.expand("$VIMRUNTIME")
514+
);
515+
local bufname = vim.api.nvim_buf_get_name(buffer);
516+
517+
if string.match(bufname, "^" .. runtime) then
518+
-- Use `vimdoc.default_hl()` for the builtin help files.
519+
goto continue;
520+
end
482521
end
483522

484523
---@type string?

plugin/helpview.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ health.notify("trace", {
1616
message = "Start"
1717
});
1818

19-
--- Initiate the highlight groups.
20-
require("helpview.highlights").setup();
21-
vim.g.__helpview_hl_group_map = vim.api.nvim_get_hl(0, {});
22-
2319
health.notify("trace", {
2420
level = 5,
2521
message = "Created highlight groups"
2622
});
2723

2824
--- Update highlight groups on colorscheme changes.
29-
vim.api.nvim_create_autocmd({ "ColorScheme" }, {
25+
vim.api.nvim_create_autocmd({ "VimEnter", "ColorScheme" }, {
3026
group = helpview.au,
3127
callback = function ()
3228
---+

0 commit comments

Comments
 (0)