refactor(renderer): remove set_folder_hl function from icons
This greatly simplifies the folder highlighting logic. It wasn't clear before and the code was probably doing more than it should. The logic with highlight_opened_file wasn't working at all, so i just removed it. This should allow us to greatly extend the order in which the components are rendered.
This commit is contained in:
@@ -6,7 +6,7 @@ local icons = require "nvim-tree.renderer.components.icons"
|
|||||||
|
|
||||||
-- TODO(refactor): the builder abstraction is not perfect yet. We shouldn't leak data in components.
|
-- TODO(refactor): the builder abstraction is not perfect yet. We shouldn't leak data in components.
|
||||||
-- Components should return only and icon / highlight group pair at most.
|
-- Components should return only and icon / highlight group pair at most.
|
||||||
-- The code was mostly moved from renderer/init.lua and rearranged, so it's still under construction.
|
-- Only missing git refactoring
|
||||||
|
|
||||||
local Builder = {}
|
local Builder = {}
|
||||||
Builder.__index = Builder
|
Builder.__index = Builder
|
||||||
@@ -84,39 +84,28 @@ function Builder:_build_folder(node, padding, git_hl)
|
|||||||
local has_children = #node.nodes ~= 0 or node.has_children
|
local has_children = #node.nodes ~= 0 or node.has_children
|
||||||
local icon = icons.get_folder_icon(node.open, node.link_to ~= nil, has_children)
|
local icon = icons.get_folder_icon(node.open, node.link_to ~= nil, has_children)
|
||||||
local git_icon = git.get_icons(node, self.index, offset, #icon, self.highlights) or ""
|
local git_icon = git.get_icons(node, self.index, offset, #icon, self.highlights) or ""
|
||||||
|
local line = padding .. icon .. git_icon .. name .. self.trailing_slash
|
||||||
|
|
||||||
local folder_hl = "NvimTreeFolderName"
|
self:_insert_line(line)
|
||||||
|
|
||||||
|
if #icon > 0 then
|
||||||
|
self:_insert_highlight("NvimTreeFolderIcon", offset, offset + #icon)
|
||||||
|
end
|
||||||
|
|
||||||
|
local foldername_hl = "NvimTreeFolderName"
|
||||||
if self.special_map[node.absolute_path] then
|
if self.special_map[node.absolute_path] then
|
||||||
folder_hl = "NvimTreeSpecialFolderName"
|
foldername_hl = "NvimTreeSpecialFolderName"
|
||||||
elseif node.open then
|
elseif node.open then
|
||||||
folder_hl = "NvimTreeOpenedFolderName"
|
foldername_hl = "NvimTreeOpenedFolderName"
|
||||||
elseif not has_children then
|
elseif not has_children then
|
||||||
folder_hl = "NvimTreeEmptyFolderName"
|
foldername_hl = "NvimTreeEmptyFolderName"
|
||||||
end
|
end
|
||||||
|
|
||||||
icons.set_folder_hl(
|
self:_insert_highlight(foldername_hl, offset + #icon + #git_icon, #line)
|
||||||
self.index,
|
|
||||||
offset,
|
|
||||||
#icon + #git_icon,
|
|
||||||
#name,
|
|
||||||
"NvimTreeFolderIcon",
|
|
||||||
folder_hl,
|
|
||||||
self.highlights,
|
|
||||||
self.open_file_highlight
|
|
||||||
)
|
|
||||||
if git_hl then
|
if git_hl then
|
||||||
icons.set_folder_hl(
|
self:_insert_highlight(git_hl, offset + #icon + #git_icon, #line)
|
||||||
self.index,
|
|
||||||
offset,
|
|
||||||
#icon + #git_icon,
|
|
||||||
#name,
|
|
||||||
git_hl,
|
|
||||||
git_hl,
|
|
||||||
self.highlights,
|
|
||||||
self.open_file_highlight
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
self:_insert_line(padding .. icon .. git_icon .. name .. self.trailing_slash)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO: missing git icon for symlinks
|
-- TODO: missing git icon for symlinks
|
||||||
|
|||||||
@@ -11,14 +11,6 @@ local function empty()
|
|||||||
return ""
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_trailing_length()
|
|
||||||
return vim.g.nvim_tree_add_trailing and 1 or 0
|
|
||||||
end
|
|
||||||
|
|
||||||
local function set_folder_hl_default(line, depth, git_icon_len, _, hl_group, _, hl)
|
|
||||||
table.insert(hl, { hl_group, line, depth + git_icon_len, -1 })
|
|
||||||
end
|
|
||||||
|
|
||||||
local function get_folder_icon(open, is_symlink, has_children)
|
local function get_folder_icon(open, is_symlink, has_children)
|
||||||
local n
|
local n
|
||||||
if is_symlink and open then
|
if is_symlink and open then
|
||||||
@@ -41,12 +33,6 @@ local function get_folder_icon(open, is_symlink, has_children)
|
|||||||
return n .. M.padding
|
return n .. M.padding
|
||||||
end
|
end
|
||||||
|
|
||||||
local function set_folder_hl(line, depth, icon_len, name_len, hl_icongroup, hl_fnamegroup, hl, should_hl_opened_files)
|
|
||||||
local hl_icon = should_hl_opened_files and hl_icongroup or "NvimTreeFolderIcon"
|
|
||||||
table.insert(hl, { hl_icon, line, depth, depth + icon_len })
|
|
||||||
table.insert(hl, { hl_fnamegroup, line, depth + icon_len, depth + icon_len + name_len + get_trailing_length() })
|
|
||||||
end
|
|
||||||
|
|
||||||
local function get_file_icon_default()
|
local function get_file_icon_default()
|
||||||
local hl_group = "NvimTreeFileIcon"
|
local hl_group = "NvimTreeFileIcon"
|
||||||
local icon = M.icons.default
|
local icon = M.icons.default
|
||||||
@@ -95,10 +81,8 @@ end
|
|||||||
local function config_folder_icon()
|
local function config_folder_icon()
|
||||||
if M.configs.show_folder_icon then
|
if M.configs.show_folder_icon then
|
||||||
M.get_folder_icon = get_folder_icon
|
M.get_folder_icon = get_folder_icon
|
||||||
M.set_folder_hl = set_folder_hl
|
|
||||||
else
|
else
|
||||||
M.get_folder_icon = empty
|
M.get_folder_icon = empty
|
||||||
M.set_folder_hl = set_folder_hl_default
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user