11local appearance = require " nvim-tree.appearance"
22
3+ -- others with name and links less than this arbitrary value are short
4+ local SHORT_LEN = 50
5+
36local M = {}
47
58--- @class HighlightDisplay for : NvimTreeHiTest
@@ -8,7 +11,7 @@ local M = {}
811--- @field def string : hi concrete definition after following any links
912local HighlightDisplay = {}
1013
11- --- @param group string nvim-tree highlight group
14+ --- @param group string nvim-tree highlight group name
1215--- @return HighlightDisplay
1316function HighlightDisplay :new (group )
1417 local o = {}
@@ -39,39 +42,92 @@ function HighlightDisplay:new(group)
3942 return o
4043end
4144
45+ --- Render one group.
46+ --- @param bufnr number to render in
47+ --- @param fmt string format string for group , links , def
48+ --- @param l number line number to render at
49+ --- @return number l next line number
4250function HighlightDisplay :render (bufnr , fmt , l )
4351 local text = string.format (fmt , self .group , self .links , self .def )
4452
4553 vim .api .nvim_buf_set_lines (bufnr , l , - 1 , true , { text })
4654 vim .api .nvim_buf_add_highlight (bufnr , - 1 , self .group , l , 0 , # self .group )
55+
56+ return l + 1
4757end
4858
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 = {}
59+ --- Render many groups.
60+ --- @param header string before with underline line
61+ --- @param displays HighlightDisplay[] highlight group
62+ --- @param bufnr number to render in
63+ --- @param l number line number to start at
64+ --- @return number l next line number
65+ local function render_displays (header , displays , bufnr , l )
5366 local max_group_len = 0
5467 local max_links_len = 0
5568
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 )
59- table.insert (displays , display )
69+ -- build all highlight groups, using name only
70+ for _ , display in ipairs (displays ) do
6071 max_group_len = math.max (max_group_len , # display .group )
6172 max_links_len = math.max (max_links_len , # display .links )
6273 end
6374
64- -- create a buffer
65- local bufnr = vim .api .nvim_create_buf (false , true )
75+ -- header
76+ vim .api .nvim_buf_set_lines (bufnr , l , - 1 , true , { header , (header :gsub (" ." , " -" )) })
77+ l = l + 2
6678
6779 -- render and highlight
68- local l = 0
6980 local fmt = string.format (" %%-%d.%ds %%-%d.%ds %%s" , max_group_len , max_group_len , max_links_len , max_links_len )
7081 for _ , display in ipairs (displays ) do
71- display :render (bufnr , fmt , l )
72- l = l + 1
82+ l = display :render (bufnr , fmt , l )
83+ end
84+
85+ return l
86+ end
87+
88+ --- Run a test similar to :so $VIMRUNTIME/syntax/hitest.vim
89+ --- Display all nvim-tree and neovim highlight groups, their link chain and actual definition
90+ function M .hi_test ()
91+ -- create a buffer
92+ local bufnr = vim .api .nvim_create_buf (false , true )
93+
94+ local l = 0
95+
96+ -- nvim-tree groups, ordered
97+ local displays = {}
98+ for _ , highlight_group in ipairs (appearance .HIGHLIGHT_GROUPS ) do
99+ local display = HighlightDisplay :new (highlight_group .group )
100+ table.insert (displays , display )
101+ end
102+ l = render_displays (" nvim-tree" , displays , bufnr , l )
103+
104+ vim .api .nvim_buf_set_lines (bufnr , l , - 1 , true , { " " })
105+ l = l + 1
106+
107+ -- built in groups, ordered opaquely by nvim
108+ local displays_short , displays_long = {}, {}
109+ local ok , out = pcall (vim .api .nvim_cmd , { cmd = " highlight" }, { output = true })
110+ if ok then
111+ for group in string.gmatch (out , " (%w*)%s+xxx" ) do
112+ if group :find (" NvimTree" , 1 , true ) ~= 1 then
113+ local display = HighlightDisplay :new (group )
114+ if # display .group + # display .links > SHORT_LEN then
115+ table.insert (displays_long , display )
116+ else
117+ table.insert (displays_short , display )
118+ end
119+ end
120+ end
73121 end
74122
123+ -- short ones first
124+ l = render_displays (" other, short" , displays_short , bufnr , l )
125+ vim .api .nvim_buf_set_lines (bufnr , l , - 1 , true , { " " })
126+ l = l + 1
127+
128+ -- long
129+ render_displays (" other, long" , displays_long , bufnr , l )
130+
75131 -- finalise and focus the buffer
76132 vim .api .nvim_buf_set_option (bufnr , " modifiable" , false )
77133 vim .cmd .buffer (bufnr )
0 commit comments