Skip to content

Commit 0aca092

Browse files
authored
fix(#2658): change SpellCap groups to reduce confusion: ExecFile->Question, ImageFile->Question, SpecialFile->Title, Symlink->Underlined; add all other highlight groups to :NvimTreeHiTest (#2732)
* fix(#2658): add all highlight groups to :NvimTreeHiTest * fix(#2658): add all highlight groups to :NvimTreeHiTest * fix(#2658): change SpellCap groups: ExecFile->Question, ImageFile->Question, SpecialFile->Title, Symlink->Underlined
1 parent 308f2fc commit 0aca092

File tree

2 files changed

+80
-18
lines changed

2 files changed

+80
-18
lines changed

Diff for: lua/nvim-tree/appearance/diagnostics.lua

+70-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
local 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+
36
local M = {}
47

58
---@class HighlightDisplay for :NvimTreeHiTest
@@ -8,7 +11,7 @@ local M = {}
811
---@field def string :hi concrete definition after following any links
912
local HighlightDisplay = {}
1013

11-
---@param group string nvim-tree highlight group
14+
---@param group string nvim-tree highlight group name
1215
---@return HighlightDisplay
1316
function HighlightDisplay:new(group)
1417
local o = {}
@@ -39,39 +42,92 @@ function HighlightDisplay:new(group)
3942
return o
4043
end
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
4250
function 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
4757
end
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)

Diff for: lua/nvim-tree/appearance/init.lua

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
local M = {}
22

3+
---@class HighlightGroup
4+
---@field group string
5+
---@field link string|nil
6+
---@field def string|nil
7+
8+
---@type HighlightGroup[]
39
-- All highlight groups: linked or directly defined.
410
-- Please add new groups to help and preserve order.
511
-- Please avoid directly defined groups to preserve accessibility for TUI.
@@ -24,10 +30,10 @@ M.HIGHLIGHT_GROUPS = {
2430
{ group = "NvimTreeStatusLineNC", link = "StatusLineNC" },
2531

2632
-- File Text
27-
{ group = "NvimTreeExecFile", link = "SpellCap" },
28-
{ group = "NvimTreeImageFile", link = "SpellCap" },
29-
{ group = "NvimTreeSpecialFile", link = "SpellCap" },
30-
{ group = "NvimTreeSymlink", link = "SpellCap" },
33+
{ group = "NvimTreeExecFile", link = "Question" },
34+
{ group = "NvimTreeImageFile", link = "Question" },
35+
{ group = "NvimTreeSpecialFile", link = "Title" },
36+
{ group = "NvimTreeSymlink", link = "Underlined" },
3137

3238
-- Folder Text
3339
{ group = "NvimTreeRootFolder", link = "Title" },

0 commit comments

Comments
 (0)