Skip to content

Commit b278fc2

Browse files
authored
feat(#2415): add :NvimTreeHiTest (#2664)
* feat(#2415): add :NvimTreeHiTest * feat(#2415): split out appearance diagnostics
1 parent 8cbb1db commit b278fc2

File tree

6 files changed

+326
-201
lines changed

6 files changed

+326
-201
lines changed

doc/nvim-tree-lua.txt

+33-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CONTENTS *nvim-tree*
1010
2.1 Quickstart: Setup |nvim-tree-quickstart-setup|
1111
2.2 Quickstart: Help |nvim-tree-quickstart-help|
1212
2.3 Quickstart: Custom Mappings |nvim-tree-quickstart-custom-mappings|
13+
2.4 Quickstart: Highlight |nvim-tree-quickstart-highlight|
1314
3. Commands |nvim-tree-commands|
1415
4. Setup |nvim-tree-setup|
1516
5. Opts |nvim-tree-opts|
@@ -43,6 +44,7 @@ CONTENTS *nvim-tree*
4344
6.7 API Marks |nvim-tree-api.marks|
4445
6.8 API Config |nvim-tree-api.config|
4546
6.9 API Commands |nvim-tree-api.commands|
47+
6.10 API Diagnostics |nvim-tree-api.diagnostics|
4648
7. Mappings |nvim-tree-mappings|
4749
7.1 Mappings: Default |nvim-tree-mappings-default|
4850
8. Highlight |nvim-tree-highlight|
@@ -230,6 +232,16 @@ via |nvim-tree.on_attach| e.g. >
230232
---
231233
}
232234
<
235+
==============================================================================
236+
2.4 QUICKSTART: HIGHLIGHT *nvim-tree-quickstart-highlight*
237+
238+
Run |:NvimTreeHiTest| to show all the highlights that nvim-tree uses.
239+
240+
They can be customised before or after setup is called and will be immediately
241+
applied at runtime.
242+
243+
See |nvim-tree-highlight| for details.
244+
233245
==============================================================================
234246
3. COMMANDS *nvim-tree-commands*
235247

@@ -324,6 +336,14 @@ via |nvim-tree.on_attach| e.g. >
324336

325337
Calls: `api.tree.collapse_all(true)`
326338

339+
*:NvimTreeHiTest*
340+
341+
Show nvim-tree highlight groups similar to `:so $VIMRUNTIME/syntax/hitest.vim`
342+
343+
See |nvim-tree-api.diagnostics.hi_test()|
344+
345+
Calls: `api.diagnostics.hi_test()`
346+
327347
==============================================================================
328348
4. SETUP *nvim-tree-setup*
329349

@@ -2077,7 +2097,7 @@ config.mappings.get_keymap_default()
20772097
(table) as per |nvim_buf_get_keymap()|
20782098

20792099
==============================================================================
2080-
6.8 API COMMANDS *nvim-tree-api.commands*
2100+
6.9 API COMMANDS *nvim-tree-api.commands*
20812101

20822102
commands.get() *nvim-tree-api.commands.get()*
20832103
Retrieve all commands, see |nvim-tree-commands|
@@ -2088,6 +2108,15 @@ commands.get() *nvim-tree-api.commands.get()*
20882108
{command} (function)
20892109
{opts} (table)
20902110

2111+
==============================================================================
2112+
6.10 DIAGNOSTICS *nvim-tree-api.diagnostics*
2113+
2114+
diagnostics.hi_test() *nvim-tree-api.diagnostics.hi_test()*
2115+
Open a new buffer displaying all nvim-tree highlight groups, their link
2116+
chain and concrete definition.
2117+
2118+
Similar to `:so $VIMRUNTIME/syntax/hitest.vim` as per |:highlight|
2119+
20912120
==============================================================================
20922121
7. MAPPINGS *nvim-tree-mappings*
20932122

@@ -2253,7 +2282,9 @@ Example |:highlight| >
22532282
It is recommended to enable 'termguicolors' for the more pleasant 24-bit
22542283
colours.
22552284

2256-
To view the active highlight groups run `:so $VIMRUNTIME/syntax/hitest.vim`
2285+
To view the nvim-tree highlight groups run |:NvimTreeHiTest|
2286+
2287+
To view all active highlight groups run `:so $VIMRUNTIME/syntax/hitest.vim`
22572288
as per |:highlight|
22582289

22592290
The `*HL` groups are additive as per |nvim-tree-opts-renderer| precedence.

lua/nvim-tree/api.lua

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local lib = require "nvim-tree.lib"
22
local view = require "nvim-tree.view"
33
local utils = require "nvim-tree.utils"
44
local actions = require "nvim-tree.actions"
5+
local appearance_diagnostics = require "nvim-tree.appearance.diagnostics"
56
local events = require "nvim-tree.events"
67
local help = require "nvim-tree.help"
78
local live_filter = require "nvim-tree.live-filter"
@@ -39,6 +40,7 @@ local Api = {
3940
mappings = {},
4041
},
4142
commands = {},
43+
diagnostics = {},
4244
}
4345

4446
--- Do nothing when setup not called.
@@ -245,6 +247,8 @@ Api.config.mappings.get_keymap = wrap(keymap.get_keymap)
245247
Api.config.mappings.get_keymap_default = wrap(keymap.get_keymap_default)
246248
Api.config.mappings.default_on_attach = keymap.default_on_attach
247249

250+
Api.diagnostics.hi_test = wrap(appearance_diagnostics.hi_test)
251+
248252
Api.commands.get = wrap(function()
249253
return require("nvim-tree.commands").get()
250254
end)

lua/nvim-tree/appearance.lua

-199
This file was deleted.
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
local appearance = require "nvim-tree.appearance"
2+
3+
local M = {}
4+
5+
---@class HighlightDisplay for :NvimTreeHiTest
6+
---@field group string nvim-tree highlight group name
7+
---@field links string link chain to a concretely defined group
8+
---@field def string :hi concrete definition after following any links
9+
local HighlightDisplay = {}
10+
11+
---@param group string nvim-tree highlight group
12+
---@return HighlightDisplay
13+
function HighlightDisplay:new(group)
14+
local o = {}
15+
setmetatable(o, self)
16+
self.__index = self
17+
18+
o.group = group
19+
local concrete = o.group
20+
21+
-- maybe follow links
22+
local links = {}
23+
local link = vim.api.nvim_get_hl(0, { name = o.group }).link
24+
while link do
25+
table.insert(links, link)
26+
concrete = link
27+
link = vim.api.nvim_get_hl(0, { name = link }).link
28+
end
29+
o.links = table.concat(links, " ")
30+
31+
-- concrete definition
32+
local ok, res = pcall(vim.api.nvim_cmd, { cmd = "highlight", args = { concrete } }, { output = true })
33+
if ok and type(res) == "string" then
34+
o.def = res:gsub(".*xxx *", "")
35+
else
36+
o.def = ""
37+
end
38+
39+
return o
40+
end
41+
42+
function HighlightDisplay:render(bufnr, fmt, l)
43+
local text = string.format(fmt, self.group, self.links, self.def)
44+
45+
vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { text })
46+
vim.api.nvim_buf_add_highlight(bufnr, -1, self.group, l, 0, #self.group)
47+
end
48+
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 = {}
53+
local max_group_len = 0
54+
local max_links_len = 0
55+
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)
60+
max_group_len = math.max(max_group_len, #display.group)
61+
max_links_len = math.max(max_links_len, #display.links)
62+
end
63+
64+
-- create a buffer
65+
local bufnr = vim.api.nvim_create_buf(false, true)
66+
67+
-- render and highlight
68+
local l = 0
69+
local fmt = string.format("%%-%d.%ds %%-%d.%ds %%s", max_group_len, max_group_len, max_links_len, max_links_len)
70+
for _, display in ipairs(displays) do
71+
display:render(bufnr, fmt, l)
72+
l = l + 1
73+
end
74+
75+
-- finalise and focus the buffer
76+
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
77+
vim.cmd.buffer(bufnr)
78+
end
79+
80+
return M

0 commit comments

Comments
 (0)