Skip to content

Commit 9818738

Browse files
committed
one and only one hl namespace, required winhl removal
1 parent b1e3124 commit 9818738

File tree

5 files changed

+41
-35
lines changed

5 files changed

+41
-35
lines changed

lua/nvim-tree.lua

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ function M.open_on_directory()
104104
actions.root.change_dir.force_dirchange(bufname, true)
105105
end
106106

107-
function M.reset_highlight()
108-
colors.setup()
109-
view.reset_winhl()
110-
renderer.render_hl(view.get_bufnr())
111-
end
112-
113107
function M.place_cursor_on_node()
114108
local search = vim.fn.searchcount()
115109
if search and search.exact_match == 1 then
@@ -168,8 +162,13 @@ local function setup_autocommands(opts)
168162
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
169163
end
170164

171-
-- reset highlights when colorscheme is changed
172-
create_nvim_tree_autocmd("ColorScheme", { callback = M.reset_highlight })
165+
-- reset and draw highlights when colorscheme is changed
166+
create_nvim_tree_autocmd("ColorScheme", {
167+
callback = function()
168+
colors.setup()
169+
renderer.render_hl(view.get_bufnr())
170+
end,
171+
})
173172

174173
-- prevent new opened file from opening in the same window as nvim-tree
175174
create_nvim_tree_autocmd("BufWipeout", {

lua/nvim-tree/colors.lua

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

36
-- directly defined groups, please keep these to an absolute minimum
47
local DEFAULT_DEFS = {
@@ -122,6 +125,21 @@ local DEFAULT_LINKS = {
122125
NvimTreeDiagnosticHintFolderHL = "NvimTreeDiagnosticHintFileHL",
123126
}
124127

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+
125143
-- nvim-tree highlight groups to legacy
126144
local LEGACY_LINKS = {
127145
NvimTreeModifiedIcon = "NvimTreeModifiedFile",
@@ -189,6 +207,11 @@ function M.setup()
189207
for from, to in pairs(DEFAULT_LINKS) do
190208
vim.api.nvim_command("hi def link " .. from .. " " .. to)
191209
end
210+
211+
-- window namespace; these don't appear to be cleared on colorscheme however 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
192215
end
193216

194217
return M

lua/nvim-tree/renderer/builder.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local colors = require "nvim-tree.colors"
12
local utils = require "nvim-tree.utils"
23
local notify = require "nvim-tree.notify"
34

@@ -281,8 +282,10 @@ function Builder:_create_combined_group(groups)
281282
combined_hl = vim.tbl_extend("force", combined_hl, hl)
282283
end
283284

284-
-- create and note the name
285-
vim.api.nvim_set_hl(0, combined_name, combined_hl)
285+
-- highlight directly in the namespace
286+
vim.api.nvim_set_hl_ns_fast(colors.NS_ID)
287+
vim.api.nvim_set_hl(colors.NS_ID, combined_name, combined_hl)
288+
286289
self.combined_groups[combined_name] = true
287290
end
288291
end

lua/nvim-tree/renderer/init.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local colors = require "nvim-tree.colors"
12
local core = require "nvim-tree.core"
23
local log = require "nvim-tree.log"
34
local view = require "nvim-tree.view"
@@ -24,8 +25,6 @@ local M = {
2425

2526
local SIGN_GROUP = "NvimTreeRendererSigns"
2627

27-
local namespace_id = vim.api.nvim_create_namespace "NvimTreeHighlights"
28-
2928
local function _draw(bufnr, lines, hl, sign_names)
3029
vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
3130
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
@@ -41,11 +40,11 @@ function M.render_hl(bufnr, hl)
4140
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
4241
return
4342
end
44-
vim.api.nvim_buf_clear_namespace(bufnr, namespace_id, 0, -1)
43+
vim.api.nvim_buf_clear_namespace(bufnr, colors.NS_ID, 0, -1)
4544
for _, data in ipairs(hl or M.last_highlights) do
4645
if type(data[1]) == "table" then
4746
for _, group in ipairs(data[1]) do
48-
vim.api.nvim_buf_add_highlight(bufnr, namespace_id, group, data[2], data[3], data[4])
47+
vim.api.nvim_buf_add_highlight(bufnr, colors.NS_ID, group, data[2], data[3], data[4])
4948
end
5049
end
5150
end

lua/nvim-tree/view.lua

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local colors = require "nvim-tree.colors"
12
local events = require "nvim-tree.events"
23
local utils = require "nvim-tree.utils"
34
local log = require "nvim-tree.log"
@@ -38,19 +39,6 @@ M.View = {
3839
cursorlineopt = "both",
3940
colorcolumn = "0",
4041
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-
}, ","),
5442
},
5543
}
5644

@@ -147,6 +135,7 @@ local function set_window_options_and_buffer()
147135
vim.opt_local[k] = v
148136
end
149137
vim.opt.eventignore = eventignore
138+
vim.api.nvim_win_set_hl_ns(0, colors.NS_ID)
150139
end
151140

152141
---@return table
@@ -539,13 +528,6 @@ function M.is_root_folder_visible(cwd)
539528
return cwd ~= "/" and not M.View.hide_root_folder
540529
end
541530

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-
549531
function M.setup(opts)
550532
local options = opts.view or {}
551533
M.View.centralize_selection = options.centralize_selection

0 commit comments

Comments
 (0)