Skip to content

Commit d1c7eaa

Browse files
committed
feat(#2415): split out appearance diagnostics
1 parent 8e8ce64 commit d1c7eaa

File tree

5 files changed

+94
-89
lines changed

5 files changed

+94
-89
lines changed

doc/nvim-tree-lua.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ CONTENTS *nvim-tree*
4444
6.7 API Marks |nvim-tree-api.marks|
4545
6.8 API Config |nvim-tree-api.config|
4646
6.9 API Commands |nvim-tree-api.commands|
47-
6.10 API Appearance |nvim-tree-api.appearance|
47+
6.10 API Diagnostics |nvim-tree-api.diagnostics|
4848
7. Mappings |nvim-tree-mappings|
4949
7.1 Mappings: Default |nvim-tree-mappings-default|
5050
8. Highlight |nvim-tree-highlight|
@@ -340,9 +340,9 @@ See |nvim-tree-highlight| for details.
340340

341341
Show nvim-tree highlight groups similar to `:so $VIMRUNTIME/syntax/hitest.vim`
342342

343-
See |nvim-tree-api.appearance.hi_test()|
343+
See |nvim-tree-api.diagnostics.hi_test()|
344344

345-
Calls: `api.appearance.hi_test()`
345+
Calls: `api.diagnostics.hi_test()`
346346

347347
==============================================================================
348348
4. SETUP *nvim-tree-setup*
@@ -2109,9 +2109,9 @@ commands.get() *nvim-tree-api.commands.get()*
21092109
{opts} (table)
21102110

21112111
==============================================================================
2112-
6.10 APPEARANCE *nvim-tree-api.appearance*
2112+
6.10 DIAGNOSTICS *nvim-tree-api.diagnostics*
21132113

2114-
appearance.hi_test() *nvim-tree-api.appearance.hi_test()*
2114+
diagnostics.hi_test() *nvim-tree-api.diagnostics.hi_test()*
21152115
Open a new buffer displaying all nvim-tree highlight groups, their link
21162116
chain and concrete definition.
21172117

lua/nvim-tree/api.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +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 = require "nvim-tree.appearance"
5+
local appearance_diagnostics = require "nvim-tree.appearance.diagnostics"
66
local events = require "nvim-tree.events"
77
local help = require "nvim-tree.help"
88
local live_filter = require "nvim-tree.live-filter"
@@ -39,8 +39,8 @@ local Api = {
3939
config = {
4040
mappings = {},
4141
},
42-
appearance = {},
4342
commands = {},
43+
diagnostics = {},
4444
}
4545

4646
--- Do nothing when setup not called.
@@ -247,7 +247,7 @@ Api.config.mappings.get_keymap = wrap(keymap.get_keymap)
247247
Api.config.mappings.get_keymap_default = wrap(keymap.get_keymap_default)
248248
Api.config.mappings.default_on_attach = keymap.default_on_attach
249249

250-
Api.appearance.hi_test = wrap(appearance.hi_test)
250+
Api.diagnostics.hi_test = wrap(appearance_diagnostics.hi_test)
251251

252252
Api.commands.get = wrap(function()
253253
return require("nvim-tree.commands").get()
Lines changed: 80 additions & 0 deletions
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

lua/nvim-tree/appearance.lua renamed to lua/nvim-tree/appearance/init.lua

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local M = {}
33
-- All highlight groups: linked or directly defined.
44
-- Please add new groups to help and preserve order.
55
-- Please avoid directly defined groups to preserve accessibility for TUI.
6-
local HIGHLIGHT_GROUPS = {
6+
M.HIGHLIGHT_GROUPS = {
77

88
-- Standard
99
{ group = "NvimTreeNormal", link = "Normal" },
@@ -120,7 +120,7 @@ local HIGHLIGHT_GROUPS = {
120120
}
121121

122122
-- nvim-tree highlight groups to legacy
123-
local LEGACY_LINKS = {
123+
M.LEGACY_LINKS = {
124124
NvimTreeModifiedIcon = "NvimTreeModifiedFile",
125125

126126
NvimTreeOpenedHL = "NvimTreeOpenedFile",
@@ -167,91 +167,16 @@ local LEGACY_LINKS = {
167167
NvimTreeDiagnosticHintFolderHL = "NvimTreeLspDiagnosticsHintFolderText",
168168
}
169169

170-
---@class HighlightDisplay for :NvimTreeHiTest
171-
---@field group string nvim-tree highlight group name
172-
---@field links string link chain to a concretely defined group
173-
---@field def string :hi concrete definition after following any links
174-
local HighlightDisplay = {}
175-
176-
---@param group string nvim-tree highlight group
177-
---@return HighlightDisplay
178-
function HighlightDisplay:new(group)
179-
local o = {}
180-
setmetatable(o, self)
181-
self.__index = self
182-
183-
o.group = group
184-
local concrete = o.group
185-
186-
-- maybe follow links
187-
local links = {}
188-
local link = vim.api.nvim_get_hl(0, { name = o.group }).link
189-
while link do
190-
table.insert(links, link)
191-
concrete = link
192-
link = vim.api.nvim_get_hl(0, { name = link }).link
193-
end
194-
o.links = table.concat(links, " ")
195-
196-
-- concrete definition
197-
local ok, res = pcall(vim.api.nvim_cmd, { cmd = "highlight", args = { concrete } }, { output = true })
198-
if ok and type(res) == "string" then
199-
o.def = res:gsub(".*xxx *", "")
200-
else
201-
o.def = ""
202-
end
203-
204-
return o
205-
end
206-
207-
function HighlightDisplay:render(bufnr, fmt, l)
208-
local text = string.format(fmt, self.group, self.links, self.def)
209-
210-
vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { text })
211-
vim.api.nvim_buf_add_highlight(bufnr, -1, self.group, l, 0, #self.group)
212-
end
213-
214-
---Run a test similar to :so $VIMRUNTIME/syntax/hitest.vim
215-
---Display all nvim-tree highlight groups, their link chain and actual definition
216-
function M.hi_test()
217-
local displays = {}
218-
local max_group_len = 0
219-
local max_links_len = 0
220-
221-
-- build all highlight groups, name only
222-
for _, highlight_group in ipairs(HIGHLIGHT_GROUPS) do
223-
local display = HighlightDisplay:new(highlight_group.group)
224-
table.insert(displays, display)
225-
max_group_len = math.max(max_group_len, #display.group)
226-
max_links_len = math.max(max_links_len, #display.links)
227-
end
228-
229-
-- create a buffer
230-
local bufnr = vim.api.nvim_create_buf(false, true)
231-
232-
-- render and highlight
233-
local l = 0
234-
local fmt = string.format("%%-%d.%ds %%-%d.%ds %%s", max_group_len, max_group_len, max_links_len, max_links_len)
235-
for _, display in ipairs(displays) do
236-
display:render(bufnr, fmt, l)
237-
l = l + 1
238-
end
239-
240-
-- finalise and focus the buffer
241-
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
242-
vim.cmd.buffer(bufnr)
243-
end
244-
245170
function M.setup()
246171
-- non-linked
247-
for _, g in ipairs(HIGHLIGHT_GROUPS) do
172+
for _, g in ipairs(M.HIGHLIGHT_GROUPS) do
248173
if g.def then
249174
vim.api.nvim_command("hi def " .. g.group .. " " .. g.def)
250175
end
251176
end
252177

253178
-- hard link override when legacy only is present
254-
for from, to in pairs(LEGACY_LINKS) do
179+
for from, to in pairs(M.LEGACY_LINKS) do
255180
local hl_from
256181
local hl_to
257182
if vim.fn.has "nvim-0.9" == 1 then
@@ -267,7 +192,7 @@ function M.setup()
267192
end
268193

269194
-- default links
270-
for _, g in ipairs(HIGHLIGHT_GROUPS) do
195+
for _, g in ipairs(M.HIGHLIGHT_GROUPS) do
271196
if g.link then
272197
vim.api.nvim_command("hi def link " .. g.group .. " " .. g.link)
273198
end

lua/nvim-tree/commands.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ local CMDS = {
139139
opts = {
140140
desc = "nvim-tree: highlight test",
141141
},
142-
command = api.appearance.hi_test,
142+
command = api.diagnostics.hi_test,
143143
},
144144
}
145145

0 commit comments

Comments
 (0)