move folder specifics from icons to Directory

This commit is contained in:
Alexander Courtis 2024-10-29 17:50:46 +11:00
parent 7988cb9882
commit 962f1ef256
2 changed files with 65 additions and 76 deletions

View File

@ -1,7 +1,6 @@
local git_utils = require("nvim-tree.git.utils")
local icons = require("nvim-tree.renderer.components.icons")
local notify = require("nvim-tree.notify")
local Node = require("nvim-tree.node")
---@class (exact) DirectoryNode: Node
@ -209,22 +208,53 @@ function DirectoryNode:expand_or_collapse(toggle_group)
self.explorer.renderer:draw()
end
---Icon and name for the directory
---@private
---@return HighlightedString icon
---@return HighlightedString name
function DirectoryNode:icon_name()
local has_children = #self.nodes ~= 0 or self.has_children
function DirectoryNode:highlighted_icon()
local str, hl
local icon_str, icon_hl = icons.get_folder_icon(self, has_children)
if #icon_str > 0 and icon_hl == nil then
if self.open then
icon_hl = "NvimTreeOpenedFolderIcon"
else
icon_hl = "NvimTreeClosedFolderIcon"
-- devicon if enabled and available
if self.explorer.opts.renderer.icons.show.folder and self.explorer.opts.renderer.icons.web_devicons.folder.enable then
str, hl = icons.get_icon(self.name, nil)
if not self.explorer.opts.renderer.icons.web_devicons.folder.color then
hl = nil
end
end
-- default icon
if not str then
if #self.nodes ~= 0 or self.has_children then
if self.open then
str = self.explorer.opts.renderer.icons.glyphs.folder.open
else
str = self.explorer.opts.renderer.icons.glyphs.folder.default
end
else
if self.open then
str = self.explorer.opts.renderer.icons.glyphs.folder.empty_open
else
str = self.explorer.opts.renderer.icons.glyphs.folder.empty
end
end
end
-- default color
if #str > 0 and hl == nil then
if self.open then
hl = "NvimTreeOpenedFolderIcon"
else
hl = "NvimTreeClosedFolderIcon"
end
end
return { str = str, hl = { hl } }
end
---@private
---@return HighlightedString icon
function DirectoryNode:highlighted_name()
local str, hl
local name = self.name
local next = self.group_next
while next do
@ -240,18 +270,26 @@ function DirectoryNode:icon_name()
notify.warn(string.format("Invalid return type for field renderer.group_empty. Expected string, got %s", type(new_name)))
end
end
local name_str = string.format("%s%s", name, self.explorer.opts.renderer.add_trailing and "/" or "")
str = string.format("%s%s", name, self.explorer.opts.renderer.add_trailing and "/" or "")
local name_hl = "NvimTreeFolderName"
hl = "NvimTreeFolderName"
if vim.tbl_contains(self.explorer.opts.renderer.special_files, self.absolute_path) or vim.tbl_contains(self.explorer.opts.renderer.special_files, self.name) then
name_hl = "NvimTreeSpecialFolderName"
hl = "NvimTreeSpecialFolderName"
elseif self.open then
name_hl = "NvimTreeOpenedFolderName"
elseif not has_children then
name_hl = "NvimTreeEmptyFolderName"
hl = "NvimTreeOpenedFolderName"
elseif #self.nodes == 0 and not self.has_children then
hl = "NvimTreeEmptyFolderName"
end
return { str = icon_str, hl = { icon_hl } }, { str = name_str, hl = { name_hl } }
return { str = str, hl = { hl } }
end
---TODO builder call highlighted_name and highlighted_icon separately
---Highlighted icon and name for the directory
---@return HighlightedString icon
---@return HighlightedString name
function DirectoryNode:icon_name()
return self:highlighted_icon(), self:highlighted_name()
end
---Create a sanitized partial copy of a node, populating children recursively.

View File

@ -1,5 +1,3 @@
local DirectoryLinkNode = nil --circular dependency
---@class DevIcon
---@field icon string
---@field color string
@ -27,50 +25,6 @@ local function empty()
return "", nil
end
---@param dir DirectoryNode
---@param has_children boolean
---@return string icon
---@return string? hl_group
local function get_folder_icon_default(dir, has_children)
local icon
if dir:is(DirectoryLinkNode) then
if dir.open then
icon = M.config.glyphs.folder.symlink_open
else
icon = M.config.glyphs.folder.symlink
end
elseif dir.open then
if has_children then
icon = M.config.glyphs.folder.open
else
icon = M.config.glyphs.folder.empty_open
end
else
if has_children then
icon = M.config.glyphs.folder.default
else
icon = M.config.glyphs.folder.empty
end
end
return icon, nil
end
---@param node DirectoryNode
---@param has_children boolean
---@return string icon
---@return string? hl_group
local function get_folder_icon_webdev(node, has_children)
local icon, hl_group = M.devicons.get_icon(node.name, nil)
if not M.config.web_devicons.folder.color then
hl_group = nil
end
if icon ~= nil then
return icon, hl_group
else
return get_folder_icon_default(node, has_children)
end
end
---@return string icon
---@return string? hl_group
local function get_file_icon_default()
@ -119,29 +73,26 @@ local function config_file_icon()
end
end
local function config_folder_icon()
if M.config.show.folder then
if M.devicons and M.config.web_devicons.folder.enable then
M.get_folder_icon = get_folder_icon_webdev
else
M.get_folder_icon = get_folder_icon_default
end
else
M.get_folder_icon = empty
---Wrapper around nvim-web-devicons, nil if not present
---@param name string
---@param ext string?
---@return string? icon
---@return string? hl_group
function M.get_icon(name, ext)
if M.devicons then
return M.devicons.get_icon(name, ext)
end
end
function M.reset_config()
config_symlinks()
config_file_icon()
config_folder_icon()
end
function M.setup(opts)
M.config = opts.renderer.icons
M.devicons = pcall(require, "nvim-web-devicons") and require("nvim-web-devicons") or nil
DirectoryLinkNode = require("nvim-tree.node.directory-link")
end
return M