Skip to content

Commit fbee8a6

Browse files
authored
fix(#2643): correctly apply linked highlight groups in tree window (#2653)
* fix(#2643): correctly apply linked highlight groups in tree window * fix(#2643): recreate and apply combined highlight groups on colorscheme change
1 parent 7bdb220 commit fbee8a6

File tree

6 files changed

+45
-61
lines changed

6 files changed

+45
-61
lines changed

lua/nvim-tree.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,12 @@ local function setup_autocommands(opts)
162162
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
163163
end
164164

165-
-- reset and draw highlights when colorscheme is changed
165+
-- reset and draw (highlights) when colorscheme is changed
166166
create_nvim_tree_autocmd("ColorScheme", {
167167
callback = function()
168168
appearance.setup()
169-
renderer.render_hl(view.get_bufnr())
169+
view.reset_winhl()
170+
renderer.draw()
170171
end,
171172
})
172173

lua/nvim-tree/appearance.lua

+1-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
local M = {
2-
-- namespace for all tree window highlights
3-
NS_ID = vim.api.nvim_create_namespace "nvim_tree",
4-
}
1+
local M = {}
52

63
-- directly defined groups, please keep these to an absolute minimum
74
local DEFAULT_DEFS = {
@@ -125,21 +122,6 @@ local DEFAULT_LINKS = {
125122
NvimTreeDiagnosticHintFolderHL = "NvimTreeDiagnosticHintFileHL",
126123
}
127124

128-
-- namespace standard links
129-
local NS_LINKS = {
130-
EndOfBuffer = "NvimTreeEndOfBuffer",
131-
CursorLine = "NvimTreeCursorLine",
132-
CursorLineNr = "NvimTreeCursorLineNr",
133-
LineNr = "NvimTreeLineNr",
134-
WinSeparator = "NvimTreeWinSeparator",
135-
StatusLine = "NvimTreeStatusLine",
136-
StatusLineNC = "NvimTreeStatuslineNC",
137-
SignColumn = "NvimTreeSignColumn",
138-
Normal = "NvimTreeNormal",
139-
NormalNC = "NvimTreeNormalNC",
140-
NormalFloat = "NvimTreeNormalFloat",
141-
}
142-
143125
-- nvim-tree highlight groups to legacy
144126
local LEGACY_LINKS = {
145127
NvimTreeModifiedIcon = "NvimTreeModifiedFile",
@@ -207,11 +189,6 @@ function M.setup()
207189
for from, to in pairs(DEFAULT_LINKS) do
208190
vim.api.nvim_command("hi def link " .. from .. " " .. to)
209191
end
210-
211-
-- window standard; this doesn't appear to clear on ColorScheme however we err on the side of caution
212-
for from, to in pairs(NS_LINKS) do
213-
vim.api.nvim_set_hl(M.NS_ID, from, { link = to })
214-
end
215192
end
216193

217194
return M

lua/nvim-tree/renderer/builder.lua

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local appearance = require "nvim-tree.appearance"
21
local core = require "nvim-tree.core"
32
local live_filter = require "nvim-tree.live-filter"
43
local notify = require "nvim-tree.notify"
@@ -46,7 +45,7 @@ local M = {
4645
---@field private root_cwd string absolute path
4746
---@field private index number
4847
---@field private depth number
49-
---@field private combined_groups string[] combined group names
48+
---@field private combined_groups table<string, boolean> combined group names
5049
---@field private markers boolean[] indent markers
5150
local Builder = {}
5251

@@ -246,22 +245,17 @@ function Builder:build_signs(node)
246245
end
247246
end
248247

249-
---Combined group name less than the 200 byte limit of highlight group names
250-
---@private
251-
---@param groups string[] highlight group names
252-
---@return string name "NvimTreeCombinedHL" .. sha256
253-
function Builder:combined_group_name(groups)
254-
return string.format("NvimTreeCombinedHL%s", vim.fn.sha256(table.concat(groups)))
255-
end
256-
257248
---Create a highlight group for groups with later groups overriding previous.
249+
---Combined group name is less than the 200 byte limit of highlight group names
258250
---@private
259251
---@param groups string[] highlight group names
252+
---@return string group_name "NvimTreeCombinedHL" .. sha256
260253
function Builder:create_combined_group(groups)
261-
local combined_name = self:combined_group_name(groups)
254+
local combined_name = string.format("NvimTreeCombinedHL%s", vim.fn.sha256(table.concat(groups)))
262255

263256
-- only create if necessary
264-
if not vim.tbl_contains(self.combined_groups, combined_name) then
257+
if not self.combined_groups[combined_name] then
258+
self.combined_groups[combined_name] = true
265259
local combined_hl = {}
266260

267261
-- build the highlight, overriding values
@@ -270,11 +264,13 @@ function Builder:create_combined_group(groups)
270264
combined_hl = vim.tbl_extend("force", combined_hl, hl)
271265
end
272266

273-
-- highlight directly in the namespace
274-
vim.api.nvim_set_hl(appearance.NS_ID, combined_name, combined_hl)
267+
-- add highlights to the global namespace
268+
vim.api.nvim_set_hl(0, combined_name, combined_hl)
275269

276270
table.insert(self.combined_groups, combined_name)
277271
end
272+
273+
return combined_name
278274
end
279275

280276
---Calculate highlight group for icon and name. A combined highlight group will be created
@@ -301,16 +297,14 @@ function Builder:add_highlights(node)
301297

302298
-- one or many icon groups
303299
if #icon_groups > 1 then
304-
icon_hl_group = self:combined_group_name(icon_groups)
305-
self:create_combined_group(icon_groups)
300+
icon_hl_group = self:create_combined_group(icon_groups)
306301
else
307302
icon_hl_group = icon_groups[1]
308303
end
309304

310305
-- one or many name groups
311306
if #name_groups > 1 then
312-
name_hl_group = self:combined_group_name(name_groups)
313-
self:create_combined_group(name_groups)
307+
name_hl_group = self:create_combined_group(name_groups)
314308
else
315309
name_hl_group = name_groups[1]
316310
end

lua/nvim-tree/renderer/components/full-name.lua

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local appearance = require "nvim-tree.appearance"
2-
31
local M = {}
42

53
local utils = require "nvim-tree.utils"
@@ -68,12 +66,13 @@ local function show()
6866
style = "minimal",
6967
})
7068

71-
local extmarks = vim.api.nvim_buf_get_extmarks(0, appearance.NS_ID, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = 1 })
69+
local ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
70+
local extmarks = vim.api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = 1 })
7271
vim.api.nvim_win_call(M.popup_win, function()
7372
vim.api.nvim_buf_set_lines(0, 0, -1, true, { line })
7473
for _, extmark in ipairs(extmarks) do
7574
local hl = extmark[4]
76-
vim.api.nvim_buf_add_highlight(0, appearance.NS_ID, hl.hl_group, 0, extmark[3], hl.end_col)
75+
vim.api.nvim_buf_add_highlight(0, ns_id, hl.hl_group, 0, extmark[3], hl.end_col)
7776
end
7877
vim.cmd [[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=hide ]]
7978
end)

lua/nvim-tree/renderer/init.lua

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local appearance = require "nvim-tree.appearance"
21
local core = require "nvim-tree.core"
32
local log = require "nvim-tree.log"
43
local view = require "nvim-tree.view"
@@ -9,12 +8,12 @@ local icon_component = require "nvim-tree.renderer.components.icons"
98
local full_name = require "nvim-tree.renderer.components.full-name"
109
local Builder = require "nvim-tree.renderer.builder"
1110

12-
local M = {
13-
last_hl_args = {},
14-
}
11+
local M = {}
1512

1613
local SIGN_GROUP = "NvimTreeRendererSigns"
1714

15+
local namespace_id = vim.api.nvim_create_namespace "NvimTreeHighlights"
16+
1817
---@param bufnr number
1918
---@param lines string[]
2019
---@param hl_args AddHighlightArgs[]
@@ -34,11 +33,11 @@ function M.render_hl(bufnr, hl)
3433
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
3534
return
3635
end
37-
vim.api.nvim_buf_clear_namespace(bufnr, appearance.NS_ID, 0, -1)
38-
for _, data in ipairs(hl or M.last_hl_args) do
36+
vim.api.nvim_buf_clear_namespace(bufnr, namespace_id, 0, -1)
37+
for _, data in ipairs(hl) do
3938
if type(data[1]) == "table" then
4039
for _, group in ipairs(data[1]) do
41-
vim.api.nvim_buf_add_highlight(bufnr, appearance.NS_ID, group, data[2], data[3], data[4])
40+
vim.api.nvim_buf_add_highlight(bufnr, namespace_id, group, data[2], data[3], data[4])
4241
end
4342
end
4443
end
@@ -59,8 +58,6 @@ function M.draw()
5958

6059
_draw(bufnr, builder.lines, builder.hl_args, builder.signs)
6160

62-
M.last_hl_args = builder.hl_args
63-
6461
if cursor and #builder.lines >= cursor[1] then
6562
vim.api.nvim_win_set_cursor(view.get_winnr(), cursor)
6663
end

lua/nvim-tree/view.lua

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local appearance = require "nvim-tree.appearance"
21
local events = require "nvim-tree.events"
32
local utils = require "nvim-tree.utils"
43
local log = require "nvim-tree.log"
@@ -39,6 +38,19 @@ M.View = {
3938
cursorlineopt = "both",
4039
colorcolumn = "0",
4140
wrap = false,
41+
winhl = table.concat({
42+
"EndOfBuffer:NvimTreeEndOfBuffer",
43+
"CursorLine:NvimTreeCursorLine",
44+
"CursorLineNr:NvimTreeCursorLineNr",
45+
"LineNr:NvimTreeLineNr",
46+
"WinSeparator:NvimTreeWinSeparator",
47+
"StatusLine:NvimTreeStatusLine",
48+
"StatusLineNC:NvimTreeStatuslineNC",
49+
"SignColumn:NvimTreeSignColumn",
50+
"Normal:NvimTreeNormal",
51+
"NormalNC:NvimTreeNormalNC",
52+
"NormalFloat:NvimTreeNormalFloat",
53+
}, ","),
4254
},
4355
}
4456

@@ -135,9 +147,6 @@ local function set_window_options_and_buffer()
135147
vim.opt_local[k] = v
136148
end
137149
vim.opt.eventignore = eventignore
138-
139-
-- use highlights from the nvim_tree namespace
140-
vim.api.nvim_win_set_hl_ns(M.get_winnr(), appearance.NS_ID)
141150
end
142151

143152
---@return table
@@ -530,6 +539,13 @@ function M.is_root_folder_visible(cwd)
530539
return cwd ~= "/" and not M.View.hide_root_folder
531540
end
532541

542+
-- used on ColorScheme event
543+
function M.reset_winhl()
544+
if M.get_winnr() and vim.api.nvim_win_is_valid(M.get_winnr()) then
545+
vim.wo[M.get_winnr()].winhl = M.View.winopts.winhl
546+
end
547+
end
548+
533549
function M.setup(opts)
534550
local options = opts.view or {}
535551
M.View.centralize_selection = options.centralize_selection

0 commit comments

Comments
 (0)