Skip to content

Commit 6b9fd75

Browse files
committed
fix(#2658): add all highlight groups to :NvimTreeHiTest
1 parent 308f2fc commit 6b9fd75

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

lua/nvim-tree/appearance/diagnostics.lua

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local M = {}
88
---@field def string :hi concrete definition after following any links
99
local HighlightDisplay = {}
1010

11-
---@param group string nvim-tree highlight group
11+
---@param group string nvim-tree highlight group name
1212
---@return HighlightDisplay
1313
function HighlightDisplay:new(group)
1414
local o = {}
@@ -39,38 +39,83 @@ function HighlightDisplay:new(group)
3939
return o
4040
end
4141

42+
---Render one group.
43+
---@param bufnr number to render in
44+
---@param fmt string format string for group, links, def
45+
---@param l number line number to render at
46+
---@return number l next line number
4247
function HighlightDisplay:render(bufnr, fmt, l)
4348
local text = string.format(fmt, self.group, self.links, self.def)
4449

4550
vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { text })
4651
vim.api.nvim_buf_add_highlight(bufnr, -1, self.group, l, 0, #self.group)
52+
53+
return l + 1
4754
end
4855

49-
---Run a test similar to :so $VIMRUNTIME/syntax/hitest.vim
50-
---Display all nvim-tree highlight groups, their link chain and actual definition
51-
function M.hi_test()
52-
local displays = {}
56+
---Render many groups.
57+
---@param header string before with underline line
58+
---@param groups string[] highlight group names
59+
---@param bufnr number to render in
60+
---@param l number line number to start at
61+
---@return number l next line number
62+
local function render_groups(header, groups, bufnr, l)
5363
local max_group_len = 0
5464
local max_links_len = 0
5565

56-
-- build all highlight groups, name only
57-
for _, highlight_group in ipairs(appearance.HIGHLIGHT_GROUPS) do
58-
local display = HighlightDisplay:new(highlight_group.group)
66+
---@type HighlightDisplay[]
67+
local displays = {}
68+
69+
-- build all highlight groups, using name only
70+
for _, group in ipairs(groups) do
71+
local display = HighlightDisplay:new(group)
5972
table.insert(displays, display)
6073
max_group_len = math.max(max_group_len, #display.group)
6174
max_links_len = math.max(max_links_len, #display.links)
6275
end
6376

64-
-- create a buffer
65-
local bufnr = vim.api.nvim_create_buf(false, true)
77+
-- header
78+
vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { header, (header:gsub(".", "-")) })
79+
l = l + 2
6680

6781
-- render and highlight
68-
local l = 0
6982
local fmt = string.format("%%-%d.%ds %%-%d.%ds %%s", max_group_len, max_group_len, max_links_len, max_links_len)
7083
for _, display in ipairs(displays) do
71-
display:render(bufnr, fmt, l)
72-
l = l + 1
84+
l = display:render(bufnr, fmt, l)
85+
end
86+
87+
return l
88+
end
89+
90+
---Run a test similar to :so $VIMRUNTIME/syntax/hitest.vim
91+
---Display all nvim-tree and neovim highlight groups, their link chain and actual definition
92+
function M.hi_test()
93+
-- create a buffer
94+
local bufnr = vim.api.nvim_create_buf(false, true)
95+
96+
local l = 0
97+
98+
-- nvim-tree groups, ordered
99+
local groups = {}
100+
for _, highlight_group in ipairs(appearance.HIGHLIGHT_GROUPS) do
101+
table.insert(groups, highlight_group.group)
102+
end
103+
l = render_groups("nvim-tree", groups, bufnr, l)
104+
105+
vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { "" })
106+
l = l + 1
107+
108+
-- built in groups, ordered opaquely by nvim
109+
groups = {}
110+
local ok, out = pcall(vim.api.nvim_cmd, { cmd = "highlight" }, { output = true })
111+
if ok then
112+
for group in string.gmatch(out, "(%w*)%s+xxx") do
113+
if group:find("NvimTree", 1, true) ~= 1 then
114+
table.insert(groups, group)
115+
end
116+
end
73117
end
118+
l = render_groups("neovim", groups, bufnr, l)
74119

75120
-- finalise and focus the buffer
76121
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)

lua/nvim-tree/appearance/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
local M = {}
22

3+
---@class HighlightGroup
4+
---@field group string
5+
---@field link string|nil
6+
---@field def string|nil
7+
8+
---@type HighlightGroup[]
39
-- All highlight groups: linked or directly defined.
410
-- Please add new groups to help and preserve order.
511
-- Please avoid directly defined groups to preserve accessibility for TUI.

0 commit comments

Comments
 (0)