1
1
local appearance = require " nvim-tree.appearance"
2
2
3
+ -- others with name and links less than this arbitrary value are short
4
+ local SHORT_LEN = 50
5
+
3
6
local M = {}
4
7
5
8
--- @class HighlightDisplay for : NvimTreeHiTest
@@ -8,7 +11,7 @@ local M = {}
8
11
--- @field def string : hi concrete definition after following any links
9
12
local HighlightDisplay = {}
10
13
11
- --- @param group string nvim-tree highlight group
14
+ --- @param group string nvim-tree highlight group name
12
15
--- @return HighlightDisplay
13
16
function HighlightDisplay :new (group )
14
17
local o = {}
@@ -39,39 +42,92 @@ function HighlightDisplay:new(group)
39
42
return o
40
43
end
41
44
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
42
50
function HighlightDisplay :render (bufnr , fmt , l )
43
51
local text = string.format (fmt , self .group , self .links , self .def )
44
52
45
53
vim .api .nvim_buf_set_lines (bufnr , l , - 1 , true , { text })
46
54
vim .api .nvim_buf_add_highlight (bufnr , - 1 , self .group , l , 0 , # self .group )
55
+
56
+ return l + 1
47
57
end
48
58
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 )
53
66
local max_group_len = 0
54
67
local max_links_len = 0
55
68
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
60
71
max_group_len = math.max (max_group_len , # display .group )
61
72
max_links_len = math.max (max_links_len , # display .links )
62
73
end
63
74
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
66
78
67
79
-- render and highlight
68
- local l = 0
69
80
local fmt = string.format (" %%-%d.%ds %%-%d.%ds %%s" , max_group_len , max_group_len , max_links_len , max_links_len )
70
81
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
73
121
end
74
122
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
+
75
131
-- finalise and focus the buffer
76
132
vim .api .nvim_buf_set_option (bufnr , " modifiable" , false )
77
133
vim .cmd .buffer (bufnr )
0 commit comments