Skip to content

Commit

Permalink
one and only one hl namespace, required winhl removal
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Jan 7, 2024
1 parent 8f213a9 commit bd44ba9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
15 changes: 7 additions & 8 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@ function M.open_on_directory()
actions.root.change_dir.force_dirchange(bufname, true)
end

function M.reset_highlight()
colors.setup()
view.reset_winhl()
renderer.render_hl(view.get_bufnr())
end

function M.place_cursor_on_node()
local search = vim.fn.searchcount()
if search and search.exact_match == 1 then
Expand Down Expand Up @@ -168,8 +162,13 @@ local function setup_autocommands(opts)
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
end

-- reset highlights when colorscheme is changed
create_nvim_tree_autocmd("ColorScheme", { callback = M.reset_highlight })
-- reset and draw highlights when colorscheme is changed
create_nvim_tree_autocmd("ColorScheme", {
callback = function()
colors.setup()
renderer.render_hl(view.get_bufnr())
end,
})

-- prevent new opened file from opening in the same window as nvim-tree
create_nvim_tree_autocmd("BufWipeout", {
Expand Down
25 changes: 24 additions & 1 deletion lua/nvim-tree/colors.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
local M = {}
local M = {
-- namespace for all tree window highlights
NS_ID = vim.api.nvim_create_namespace "nvim_tree",
}

-- directly defined groups, please keep these to an absolute minimum
local DEFAULT_DEFS = {
Expand Down Expand Up @@ -122,6 +125,21 @@ local DEFAULT_LINKS = {
NvimTreeDiagnosticHintFolderHL = "NvimTreeDiagnosticHintFileHL",
}

-- namespace standard links
local NS_LINKS = {
EndOfBuffer = "NvimTreeEndOfBuffer",
CursorLine = "NvimTreeCursorLine",
CursorLineNr = "NvimTreeCursorLineNr",
LineNr = "NvimTreeLineNr",
WinSeparator = "NvimTreeWinSeparator",
StatusLine = "NvimTreeStatusLine",
StatusLineNC = "NvimTreeStatuslineNC",
SignColumn = "NvimTreeSignColumn",
Normal = "NvimTreeNormal",
NormalNC = "NvimTreeNormalNC",
NormalFloat = "NvimTreeNormalFloat",
}

-- nvim-tree highlight groups to legacy
local LEGACY_LINKS = {
NvimTreeModifiedIcon = "NvimTreeModifiedFile",
Expand Down Expand Up @@ -189,6 +207,11 @@ function M.setup()
for from, to in pairs(DEFAULT_LINKS) do
vim.api.nvim_command("hi def link " .. from .. " " .. to)
end

-- window namespace; these don't appear to be cleared on colorscheme however err on the side of caution
for from, to in pairs(NS_LINKS) do
vim.api.nvim_set_hl(M.NS_ID, from, { link = to })
end
end

return M
7 changes: 5 additions & 2 deletions lua/nvim-tree/renderer/builder.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local colors = require "nvim-tree.colors"
local utils = require "nvim-tree.utils"
local notify = require "nvim-tree.notify"

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

-- create and note the name
vim.api.nvim_set_hl(0, combined_name, combined_hl)
-- highlight directly in the namespace
vim.api.nvim_set_hl_ns_fast(colors.NS_ID)
vim.api.nvim_set_hl(colors.NS_ID, combined_name, combined_hl)

self.combined_groups[combined_name] = true
end
end
Expand Down
7 changes: 3 additions & 4 deletions lua/nvim-tree/renderer/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local colors = require "nvim-tree.colors"
local core = require "nvim-tree.core"
local log = require "nvim-tree.log"
local view = require "nvim-tree.view"
Expand All @@ -24,8 +25,6 @@ local M = {

local SIGN_GROUP = "NvimTreeRendererSigns"

local namespace_id = vim.api.nvim_create_namespace "NvimTreeHighlights"

local function _draw(bufnr, lines, hl, sign_names)
vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
Expand All @@ -41,11 +40,11 @@ function M.render_hl(bufnr, hl)
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
return
end
vim.api.nvim_buf_clear_namespace(bufnr, namespace_id, 0, -1)
vim.api.nvim_buf_clear_namespace(bufnr, colors.NS_ID, 0, -1)
for _, data in ipairs(hl or M.last_highlights) do
if type(data[1]) == "table" then
for _, group in ipairs(data[1]) do
vim.api.nvim_buf_add_highlight(bufnr, namespace_id, group, data[2], data[3], data[4])
vim.api.nvim_buf_add_highlight(bufnr, colors.NS_ID, group, data[2], data[3], data[4])
end
end
end
Expand Down
22 changes: 2 additions & 20 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local colors = require "nvim-tree.colors"
local events = require "nvim-tree.events"
local utils = require "nvim-tree.utils"
local log = require "nvim-tree.log"
Expand Down Expand Up @@ -38,19 +39,6 @@ M.View = {
cursorlineopt = "both",
colorcolumn = "0",
wrap = false,
winhl = table.concat({
"EndOfBuffer:NvimTreeEndOfBuffer",
"CursorLine:NvimTreeCursorLine",
"CursorLineNr:NvimTreeCursorLineNr",
"LineNr:NvimTreeLineNr",
"WinSeparator:NvimTreeWinSeparator",
"StatusLine:NvimTreeStatusLine",
"StatusLineNC:NvimTreeStatuslineNC",
"SignColumn:NvimTreeSignColumn",
"Normal:NvimTreeNormal",
"NormalNC:NvimTreeNormalNC",
"NormalFloat:NvimTreeNormalFloat",
}, ","),
},
}

Expand Down Expand Up @@ -147,6 +135,7 @@ local function set_window_options_and_buffer()
vim.opt_local[k] = v
end
vim.opt.eventignore = eventignore
vim.api.nvim_win_set_hl_ns(0, colors.NS_ID)
end

---@return table
Expand Down Expand Up @@ -539,13 +528,6 @@ function M.is_root_folder_visible(cwd)
return cwd ~= "/" and not M.View.hide_root_folder
end

-- used on ColorScheme event
function M.reset_winhl()
if M.get_winnr() and vim.api.nvim_win_is_valid(M.get_winnr()) then
vim.wo[M.get_winnr()].winhl = M.View.winopts.winhl
end
end

function M.setup(opts)
local options = opts.view or {}
M.View.centralize_selection = options.centralize_selection
Expand Down

0 comments on commit bd44ba9

Please sign in to comment.