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
This commit is contained in:
Alexander Courtis 2024-01-29 12:42:19 +11:00 committed by GitHub
parent 7bdb220d0f
commit fbee8a69a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 45 additions and 61 deletions

View File

@ -162,11 +162,12 @@ local function setup_autocommands(opts)
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts)) vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
end end
-- reset and draw highlights when colorscheme is changed -- reset and draw (highlights) when colorscheme is changed
create_nvim_tree_autocmd("ColorScheme", { create_nvim_tree_autocmd("ColorScheme", {
callback = function() callback = function()
appearance.setup() appearance.setup()
renderer.render_hl(view.get_bufnr()) view.reset_winhl()
renderer.draw()
end, end,
}) })

View File

@ -1,7 +1,4 @@
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 -- directly defined groups, please keep these to an absolute minimum
local DEFAULT_DEFS = { local DEFAULT_DEFS = {
@ -125,21 +122,6 @@ local DEFAULT_LINKS = {
NvimTreeDiagnosticHintFolderHL = "NvimTreeDiagnosticHintFileHL", 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 -- nvim-tree highlight groups to legacy
local LEGACY_LINKS = { local LEGACY_LINKS = {
NvimTreeModifiedIcon = "NvimTreeModifiedFile", NvimTreeModifiedIcon = "NvimTreeModifiedFile",
@ -207,11 +189,6 @@ function M.setup()
for from, to in pairs(DEFAULT_LINKS) do for from, to in pairs(DEFAULT_LINKS) do
vim.api.nvim_command("hi def link " .. from .. " " .. to) vim.api.nvim_command("hi def link " .. from .. " " .. to)
end end
-- window standard; this doesn't appear to clear on ColorScheme however we 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 end
return M return M

View File

@ -1,4 +1,3 @@
local appearance = require "nvim-tree.appearance"
local core = require "nvim-tree.core" local core = require "nvim-tree.core"
local live_filter = require "nvim-tree.live-filter" local live_filter = require "nvim-tree.live-filter"
local notify = require "nvim-tree.notify" local notify = require "nvim-tree.notify"
@ -46,7 +45,7 @@ local M = {
---@field private root_cwd string absolute path ---@field private root_cwd string absolute path
---@field private index number ---@field private index number
---@field private depth number ---@field private depth number
---@field private combined_groups string[] combined group names ---@field private combined_groups table<string, boolean> combined group names
---@field private markers boolean[] indent markers ---@field private markers boolean[] indent markers
local Builder = {} local Builder = {}
@ -246,22 +245,17 @@ function Builder:build_signs(node)
end end
end end
---Combined group name less than the 200 byte limit of highlight group names
---@private
---@param groups string[] highlight group names
---@return string name "NvimTreeCombinedHL" .. sha256
function Builder:combined_group_name(groups)
return string.format("NvimTreeCombinedHL%s", vim.fn.sha256(table.concat(groups)))
end
---Create a highlight group for groups with later groups overriding previous. ---Create a highlight group for groups with later groups overriding previous.
---Combined group name is less than the 200 byte limit of highlight group names
---@private ---@private
---@param groups string[] highlight group names ---@param groups string[] highlight group names
---@return string group_name "NvimTreeCombinedHL" .. sha256
function Builder:create_combined_group(groups) function Builder:create_combined_group(groups)
local combined_name = self:combined_group_name(groups) local combined_name = string.format("NvimTreeCombinedHL%s", vim.fn.sha256(table.concat(groups)))
-- only create if necessary -- only create if necessary
if not vim.tbl_contains(self.combined_groups, combined_name) then if not self.combined_groups[combined_name] then
self.combined_groups[combined_name] = true
local combined_hl = {} local combined_hl = {}
-- build the highlight, overriding values -- build the highlight, overriding values
@ -270,11 +264,13 @@ function Builder:create_combined_group(groups)
combined_hl = vim.tbl_extend("force", combined_hl, hl) combined_hl = vim.tbl_extend("force", combined_hl, hl)
end end
-- highlight directly in the namespace -- add highlights to the global namespace
vim.api.nvim_set_hl(appearance.NS_ID, combined_name, combined_hl) vim.api.nvim_set_hl(0, combined_name, combined_hl)
table.insert(self.combined_groups, combined_name) table.insert(self.combined_groups, combined_name)
end end
return combined_name
end end
---Calculate highlight group for icon and name. A combined highlight group will be created ---Calculate highlight group for icon and name. A combined highlight group will be created
@ -301,16 +297,14 @@ function Builder:add_highlights(node)
-- one or many icon groups -- one or many icon groups
if #icon_groups > 1 then if #icon_groups > 1 then
icon_hl_group = self:combined_group_name(icon_groups) icon_hl_group = self:create_combined_group(icon_groups)
self:create_combined_group(icon_groups)
else else
icon_hl_group = icon_groups[1] icon_hl_group = icon_groups[1]
end end
-- one or many name groups -- one or many name groups
if #name_groups > 1 then if #name_groups > 1 then
name_hl_group = self:combined_group_name(name_groups) name_hl_group = self:create_combined_group(name_groups)
self:create_combined_group(name_groups)
else else
name_hl_group = name_groups[1] name_hl_group = name_groups[1]
end end

View File

@ -1,5 +1,3 @@
local appearance = require "nvim-tree.appearance"
local M = {} local M = {}
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
@ -68,12 +66,13 @@ local function show()
style = "minimal", style = "minimal",
}) })
local extmarks = vim.api.nvim_buf_get_extmarks(0, appearance.NS_ID, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = 1 }) local ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
local extmarks = vim.api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = 1 })
vim.api.nvim_win_call(M.popup_win, function() vim.api.nvim_win_call(M.popup_win, function()
vim.api.nvim_buf_set_lines(0, 0, -1, true, { line }) vim.api.nvim_buf_set_lines(0, 0, -1, true, { line })
for _, extmark in ipairs(extmarks) do for _, extmark in ipairs(extmarks) do
local hl = extmark[4] local hl = extmark[4]
vim.api.nvim_buf_add_highlight(0, appearance.NS_ID, hl.hl_group, 0, extmark[3], hl.end_col) vim.api.nvim_buf_add_highlight(0, ns_id, hl.hl_group, 0, extmark[3], hl.end_col)
end end
vim.cmd [[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=hide ]] vim.cmd [[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=hide ]]
end) end)

View File

@ -1,4 +1,3 @@
local appearance = require "nvim-tree.appearance"
local core = require "nvim-tree.core" local core = require "nvim-tree.core"
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
@ -9,12 +8,12 @@ local icon_component = require "nvim-tree.renderer.components.icons"
local full_name = require "nvim-tree.renderer.components.full-name" local full_name = require "nvim-tree.renderer.components.full-name"
local Builder = require "nvim-tree.renderer.builder" local Builder = require "nvim-tree.renderer.builder"
local M = { local M = {}
last_hl_args = {},
}
local SIGN_GROUP = "NvimTreeRendererSigns" local SIGN_GROUP = "NvimTreeRendererSigns"
local namespace_id = vim.api.nvim_create_namespace "NvimTreeHighlights"
---@param bufnr number ---@param bufnr number
---@param lines string[] ---@param lines string[]
---@param hl_args AddHighlightArgs[] ---@param hl_args AddHighlightArgs[]
@ -34,11 +33,11 @@ function M.render_hl(bufnr, hl)
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
return return
end end
vim.api.nvim_buf_clear_namespace(bufnr, appearance.NS_ID, 0, -1) vim.api.nvim_buf_clear_namespace(bufnr, namespace_id, 0, -1)
for _, data in ipairs(hl or M.last_hl_args) do for _, data in ipairs(hl) do
if type(data[1]) == "table" then if type(data[1]) == "table" then
for _, group in ipairs(data[1]) do for _, group in ipairs(data[1]) do
vim.api.nvim_buf_add_highlight(bufnr, appearance.NS_ID, group, data[2], data[3], data[4]) vim.api.nvim_buf_add_highlight(bufnr, namespace_id, group, data[2], data[3], data[4])
end end
end end
end end
@ -59,8 +58,6 @@ function M.draw()
_draw(bufnr, builder.lines, builder.hl_args, builder.signs) _draw(bufnr, builder.lines, builder.hl_args, builder.signs)
M.last_hl_args = builder.hl_args
if cursor and #builder.lines >= cursor[1] then if cursor and #builder.lines >= cursor[1] then
vim.api.nvim_win_set_cursor(view.get_winnr(), cursor) vim.api.nvim_win_set_cursor(view.get_winnr(), cursor)
end end

View File

@ -1,4 +1,3 @@
local appearance = require "nvim-tree.appearance"
local events = require "nvim-tree.events" local events = require "nvim-tree.events"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
@ -39,6 +38,19 @@ M.View = {
cursorlineopt = "both", cursorlineopt = "both",
colorcolumn = "0", colorcolumn = "0",
wrap = false, 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",
}, ","),
}, },
} }
@ -135,9 +147,6 @@ local function set_window_options_and_buffer()
vim.opt_local[k] = v vim.opt_local[k] = v
end end
vim.opt.eventignore = eventignore vim.opt.eventignore = eventignore
-- use highlights from the nvim_tree namespace
vim.api.nvim_win_set_hl_ns(M.get_winnr(), appearance.NS_ID)
end end
---@return table ---@return table
@ -530,6 +539,13 @@ function M.is_root_folder_visible(cwd)
return cwd ~= "/" and not M.View.hide_root_folder return cwd ~= "/" and not M.View.hide_root_folder
end 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) function M.setup(opts)
local options = opts.view or {} local options = opts.view or {}
M.View.centralize_selection = options.centralize_selection M.View.centralize_selection = options.centralize_selection