Skip to content

fix(#2658): change SpellCap groups to reduce confusion: ExecFile->Question, ImageFile->Question, SpecialFile->Title, Symlink->Underlined; add all other highlight groups to :NvimTreeHiTest #2732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 70 additions & 14 deletions lua/nvim-tree/appearance/diagnostics.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local appearance = require "nvim-tree.appearance"

-- others with name and links less than this arbitrary value are short
local SHORT_LEN = 50

local M = {}

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

---@param group string nvim-tree highlight group
---@param group string nvim-tree highlight group name
---@return HighlightDisplay
function HighlightDisplay:new(group)
local o = {}
Expand Down Expand Up @@ -39,39 +42,92 @@ function HighlightDisplay:new(group)
return o
end

---Render one group.
---@param bufnr number to render in
---@param fmt string format string for group, links, def
---@param l number line number to render at
---@return number l next line number
function HighlightDisplay:render(bufnr, fmt, l)
local text = string.format(fmt, self.group, self.links, self.def)

vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { text })
vim.api.nvim_buf_add_highlight(bufnr, -1, self.group, l, 0, #self.group)

return l + 1
end

---Run a test similar to :so $VIMRUNTIME/syntax/hitest.vim
---Display all nvim-tree highlight groups, their link chain and actual definition
function M.hi_test()
local displays = {}
---Render many groups.
---@param header string before with underline line
---@param displays HighlightDisplay[] highlight group
---@param bufnr number to render in
---@param l number line number to start at
---@return number l next line number
local function render_displays(header, displays, bufnr, l)
local max_group_len = 0
local max_links_len = 0

-- build all highlight groups, name only
for _, highlight_group in ipairs(appearance.HIGHLIGHT_GROUPS) do
local display = HighlightDisplay:new(highlight_group.group)
table.insert(displays, display)
-- build all highlight groups, using name only
for _, display in ipairs(displays) do
max_group_len = math.max(max_group_len, #display.group)
max_links_len = math.max(max_links_len, #display.links)
end

-- create a buffer
local bufnr = vim.api.nvim_create_buf(false, true)
-- header
vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { header, (header:gsub(".", "-")) })
l = l + 2

-- render and highlight
local l = 0
local fmt = string.format("%%-%d.%ds %%-%d.%ds %%s", max_group_len, max_group_len, max_links_len, max_links_len)
for _, display in ipairs(displays) do
display:render(bufnr, fmt, l)
l = l + 1
l = display:render(bufnr, fmt, l)
end

return l
end

---Run a test similar to :so $VIMRUNTIME/syntax/hitest.vim
---Display all nvim-tree and neovim highlight groups, their link chain and actual definition
function M.hi_test()
-- create a buffer
local bufnr = vim.api.nvim_create_buf(false, true)

local l = 0

-- nvim-tree groups, ordered
local displays = {}
for _, highlight_group in ipairs(appearance.HIGHLIGHT_GROUPS) do
local display = HighlightDisplay:new(highlight_group.group)
table.insert(displays, display)
end
l = render_displays("nvim-tree", displays, bufnr, l)

vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { "" })
l = l + 1

-- built in groups, ordered opaquely by nvim
local displays_short, displays_long = {}, {}
local ok, out = pcall(vim.api.nvim_cmd, { cmd = "highlight" }, { output = true })
if ok then
for group in string.gmatch(out, "(%w*)%s+xxx") do
if group:find("NvimTree", 1, true) ~= 1 then
local display = HighlightDisplay:new(group)
if #display.group + #display.links > SHORT_LEN then
table.insert(displays_long, display)
else
table.insert(displays_short, display)
end
end
end
end

-- short ones first
l = render_displays("other, short", displays_short, bufnr, l)
vim.api.nvim_buf_set_lines(bufnr, l, -1, true, { "" })
l = l + 1

-- long
render_displays("other, long", displays_long, bufnr, l)

-- finalise and focus the buffer
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
vim.cmd.buffer(bufnr)
Expand Down
14 changes: 10 additions & 4 deletions lua/nvim-tree/appearance/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
local M = {}

---@class HighlightGroup
---@field group string
---@field link string|nil
---@field def string|nil

---@type HighlightGroup[]
-- All highlight groups: linked or directly defined.
-- Please add new groups to help and preserve order.
-- Please avoid directly defined groups to preserve accessibility for TUI.
Expand All @@ -24,10 +30,10 @@ M.HIGHLIGHT_GROUPS = {
{ group = "NvimTreeStatusLineNC", link = "StatusLineNC" },

-- File Text
{ group = "NvimTreeExecFile", link = "SpellCap" },
{ group = "NvimTreeImageFile", link = "SpellCap" },
{ group = "NvimTreeSpecialFile", link = "SpellCap" },
{ group = "NvimTreeSymlink", link = "SpellCap" },
{ group = "NvimTreeExecFile", link = "Question" },
{ group = "NvimTreeImageFile", link = "Question" },
{ group = "NvimTreeSpecialFile", link = "Title" },
{ group = "NvimTreeSymlink", link = "Underlined" },

-- Folder Text
{ group = "NvimTreeRootFolder", link = "Title" },
Expand Down