@@ -8,7 +8,7 @@ local M = {}
8
8
--- @field def string : hi concrete definition after following any links
9
9
local HighlightDisplay = {}
10
10
11
- --- @param group string nvim-tree highlight group
11
+ --- @param group string nvim-tree highlight group name
12
12
--- @return HighlightDisplay
13
13
function HighlightDisplay :new (group )
14
14
local o = {}
@@ -39,38 +39,83 @@ function HighlightDisplay:new(group)
39
39
return o
40
40
end
41
41
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
42
47
function HighlightDisplay :render (bufnr , fmt , l )
43
48
local text = string.format (fmt , self .group , self .links , self .def )
44
49
45
50
vim .api .nvim_buf_set_lines (bufnr , l , - 1 , true , { text })
46
51
vim .api .nvim_buf_add_highlight (bufnr , - 1 , self .group , l , 0 , # self .group )
52
+
53
+ return l + 1
47
54
end
48
55
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 )
53
63
local max_group_len = 0
54
64
local max_links_len = 0
55
65
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 )
59
72
table.insert (displays , display )
60
73
max_group_len = math.max (max_group_len , # display .group )
61
74
max_links_len = math.max (max_links_len , # display .links )
62
75
end
63
76
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
66
80
67
81
-- render and highlight
68
- local l = 0
69
82
local fmt = string.format (" %%-%d.%ds %%-%d.%ds %%s" , max_group_len , max_group_len , max_links_len , max_links_len )
70
83
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
73
117
end
118
+ l = render_groups (" neovim" , groups , bufnr , l )
74
119
75
120
-- finalise and focus the buffer
76
121
vim .api .nvim_buf_set_option (bufnr , " modifiable" , false )
0 commit comments