move icon builders into node classes
This commit is contained in:
parent
05f4376605
commit
31545f2801
@ -1,4 +1,6 @@
|
||||
local git_utils = require("nvim-tree.git.utils")
|
||||
local icons = require("nvim-tree.renderer.components.icons")
|
||||
local utils = require("nvim-tree.utils")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
@ -43,6 +45,21 @@ function DirectoryLinkNode:update_git_status(parent_ignored, project)
|
||||
self.git_status = git_utils.git_status_dir(parent_ignored, project, self.link_to, self.absolute_path)
|
||||
end
|
||||
|
||||
---Icon and name for the directory link
|
||||
---@return HighlightedString icon
|
||||
---@return HighlightedString name
|
||||
function DirectoryLinkNode:icon_name()
|
||||
local icon, name = DirectoryNode.icon_name(self)
|
||||
|
||||
if self.explorer.opts.renderer.symlink_destination then
|
||||
local link_to = utils.path_relative(self.link_to, self.explorer.absolute_path)
|
||||
icon.hl = { string.format("%s%s%s", name.str, icons.i.symlink_arrow, link_to) }
|
||||
name.hl = { "NvimTreeSymlinkFolderName" }
|
||||
end
|
||||
|
||||
return icon, name
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node, populating children recursively.
|
||||
---@return DirectoryLinkNode cloned
|
||||
function DirectoryLinkNode:clone()
|
||||
|
||||
@ -1,4 +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")
|
||||
|
||||
@ -207,6 +209,52 @@ function DirectoryNode:expand_or_collapse(toggle_group)
|
||||
self.explorer.renderer:draw()
|
||||
end
|
||||
|
||||
---Icon and name for the directory
|
||||
---@return HighlightedString icon
|
||||
---@return HighlightedString name
|
||||
function DirectoryNode:icon_name()
|
||||
local has_children = #self.nodes ~= 0 or self.has_children
|
||||
local icon, icon_hl = icons.get_folder_icon(self, has_children)
|
||||
|
||||
local name = self.name
|
||||
local next = self.group_next
|
||||
while next do
|
||||
name = string.format("%s/%s", name, next.name)
|
||||
next = next.group_next
|
||||
end
|
||||
|
||||
if self.group_next and type(self.explorer.opts.renderer.group_empty) == "function" then
|
||||
local new_name = self.explorer.opts.renderer.group_empty(name)
|
||||
if type(new_name) == "string" then
|
||||
name = new_name
|
||||
else
|
||||
notify.warn(string.format("Invalid return type for field renderer.group_empty. Expected string, got %s", type(new_name)))
|
||||
end
|
||||
end
|
||||
|
||||
local foldername = string.format("%s%s", name, self.explorer.opts.renderer.add_trailing and "/" or "")
|
||||
foldername = name
|
||||
|
||||
if #icon > 0 and icon_hl == nil then
|
||||
if self.open then
|
||||
icon_hl = "NvimTreeOpenedFolderIcon"
|
||||
else
|
||||
icon_hl = "NvimTreeClosedFolderIcon"
|
||||
end
|
||||
end
|
||||
|
||||
local foldername_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
|
||||
foldername_hl = "NvimTreeSpecialFolderName"
|
||||
elseif self.open then
|
||||
foldername_hl = "NvimTreeOpenedFolderName"
|
||||
elseif not has_children then
|
||||
foldername_hl = "NvimTreeEmptyFolderName"
|
||||
end
|
||||
|
||||
return { str = icon, hl = { icon_hl } }, { str = foldername, hl = { foldername_hl } }
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node, populating children recursively.
|
||||
---@return DirectoryNode cloned
|
||||
function DirectoryNode:clone()
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
local git_utils = require("nvim-tree.git.utils")
|
||||
local icons = require("nvim-tree.renderer.components.icons")
|
||||
local utils = require("nvim-tree.utils")
|
||||
|
||||
local FileNode = require("nvim-tree.node.file")
|
||||
|
||||
@ -39,6 +41,21 @@ function FileLinkNode:update_git_status(parent_ignored, project)
|
||||
self.git_status = git_utils.git_status_file(parent_ignored, project, self.link_to, self.absolute_path)
|
||||
end
|
||||
|
||||
---Icon and name for the file link
|
||||
---@return HighlightedString icon
|
||||
---@return HighlightedString name
|
||||
function FileLinkNode:icon_name()
|
||||
local icon = icons.i.symlink
|
||||
local arrow = icons.i.symlink_arrow
|
||||
local symlink_formatted = self.name
|
||||
if self.explorer.opts.renderer.symlink_destination then
|
||||
local link_to = utils.path_relative(self.link_to, self.explorer.absolute_path)
|
||||
symlink_formatted = string.format("%s%s%s", symlink_formatted, arrow, link_to)
|
||||
end
|
||||
|
||||
return { str = icon, hl = { "NvimTreeSymlinkIcon" } }, { str = symlink_formatted, hl = { "NvimTreeSymlink" } }
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node
|
||||
---@return FileLinkNode cloned
|
||||
function FileLinkNode:clone()
|
||||
|
||||
@ -1,8 +1,18 @@
|
||||
local git_utils = require("nvim-tree.git.utils")
|
||||
local icons = require("nvim-tree.renderer.components.icons")
|
||||
local utils = require("nvim-tree.utils")
|
||||
|
||||
local Node = require("nvim-tree.node")
|
||||
|
||||
local PICTURE_MAP = {
|
||||
jpg = true,
|
||||
jpeg = true,
|
||||
png = true,
|
||||
gif = true,
|
||||
webp = true,
|
||||
jxl = true,
|
||||
}
|
||||
|
||||
---@class (exact) FileNode: Node
|
||||
---@field extension string
|
||||
local FileNode = Node:new()
|
||||
@ -56,6 +66,23 @@ function FileNode:get_git_xy()
|
||||
return self.git_status.file and { self.git_status.file }
|
||||
end
|
||||
|
||||
---Icon and name for the file
|
||||
---@return HighlightedString icon
|
||||
---@return HighlightedString name
|
||||
function FileNode:icon_name()
|
||||
local hl
|
||||
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
|
||||
hl = "NvimTreeSpecialFile"
|
||||
elseif self.executable then
|
||||
hl = "NvimTreeExecFile"
|
||||
elseif PICTURE_MAP[self.extension] then
|
||||
hl = "NvimTreeImageFile"
|
||||
end
|
||||
|
||||
local icon, hl_group = icons.get_file_icon(self.name, self.extension)
|
||||
return { str = icon, hl = { hl_group } }, { str = self.name, hl = { hl } }
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node
|
||||
---@return FileNode cloned
|
||||
function FileNode:clone()
|
||||
|
||||
@ -68,6 +68,14 @@ function Node:get_parent_of_group()
|
||||
end
|
||||
end
|
||||
|
||||
---Icon and name for the node
|
||||
---Empty for base Node
|
||||
---@return HighlightedString icon
|
||||
---@return HighlightedString name
|
||||
function Node:icon_name()
|
||||
return { str = "", hl = {} }, { str = self.name, hl = {} }
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node, populating children recursively.
|
||||
---@return Node cloned
|
||||
function Node:clone()
|
||||
|
||||
@ -2,9 +2,7 @@ local notify = require("nvim-tree.notify")
|
||||
local utils = require("nvim-tree.utils")
|
||||
local view = require("nvim-tree.view")
|
||||
|
||||
local DirectoryLinkNode = require("nvim-tree.node.directory-link")
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
local FileLinkNode = require("nvim-tree.node.file-link")
|
||||
|
||||
local DecoratorBookmarks = require("nvim-tree.renderer.decorator.bookmarks")
|
||||
local DecoratorCopied = require("nvim-tree.renderer.decorator.copied")
|
||||
@ -16,16 +14,6 @@ local DecoratorHidden = require("nvim-tree.renderer.decorator.hidden")
|
||||
local DecoratorOpened = require("nvim-tree.renderer.decorator.opened")
|
||||
|
||||
local pad = require("nvim-tree.renderer.components.padding")
|
||||
local icons = require("nvim-tree.renderer.components.icons")
|
||||
|
||||
local PICTURE_MAP = {
|
||||
jpg = true,
|
||||
jpeg = true,
|
||||
png = true,
|
||||
gif = true,
|
||||
webp = true,
|
||||
jxl = true,
|
||||
}
|
||||
|
||||
---@class (exact) HighlightedString
|
||||
---@field str string
|
||||
@ -100,27 +88,6 @@ function Builder:insert_highlight(groups, start, end_)
|
||||
table.insert(self.hl_args, { groups, self.index, start, end_ or -1 })
|
||||
end
|
||||
|
||||
---@private
|
||||
function Builder:get_folder_name(node)
|
||||
local name = node.name
|
||||
local next = node.group_next
|
||||
while next do
|
||||
name = string.format("%s/%s", name, next.name)
|
||||
next = next.group_next
|
||||
end
|
||||
|
||||
if node.group_next and type(self.opts.renderer.group_empty) == "function" then
|
||||
local new_name = self.opts.renderer.group_empty(name)
|
||||
if type(new_name) == "string" then
|
||||
name = new_name
|
||||
else
|
||||
notify.warn(string.format("Invalid return type for field renderer.group_empty. Expected string, got %s", type(new_name)))
|
||||
end
|
||||
end
|
||||
|
||||
return string.format("%s%s", name, self.opts.renderer.add_trailing and "/" or "")
|
||||
end
|
||||
|
||||
---@private
|
||||
---@param highlighted_strings HighlightedString[]
|
||||
---@return string
|
||||
@ -141,78 +108,6 @@ function Builder:unwrap_highlighted_strings(highlighted_strings)
|
||||
return string
|
||||
end
|
||||
|
||||
---@private
|
||||
---@param node Node
|
||||
---@return HighlightedString icon
|
||||
---@return HighlightedString name
|
||||
function Builder:build_folder(node)
|
||||
local has_children = #node.nodes ~= 0 or node.has_children
|
||||
local icon, icon_hl = icons.get_folder_icon(node, has_children)
|
||||
local foldername = self:get_folder_name(node)
|
||||
|
||||
if #icon > 0 and icon_hl == nil then
|
||||
if node.open then
|
||||
icon_hl = "NvimTreeOpenedFolderIcon"
|
||||
else
|
||||
icon_hl = "NvimTreeClosedFolderIcon"
|
||||
end
|
||||
end
|
||||
|
||||
local foldername_hl = "NvimTreeFolderName"
|
||||
if node.link_to and self.opts.renderer.symlink_destination then
|
||||
local arrow = icons.i.symlink_arrow
|
||||
local link_to = utils.path_relative(node.link_to, self.explorer.absolute_path)
|
||||
foldername = string.format("%s%s%s", foldername, arrow, link_to)
|
||||
foldername_hl = "NvimTreeSymlinkFolderName"
|
||||
elseif
|
||||
vim.tbl_contains(self.opts.renderer.special_files, node.absolute_path) or vim.tbl_contains(self.opts.renderer.special_files, node.name)
|
||||
then
|
||||
foldername_hl = "NvimTreeSpecialFolderName"
|
||||
elseif node.open then
|
||||
foldername_hl = "NvimTreeOpenedFolderName"
|
||||
elseif not has_children then
|
||||
foldername_hl = "NvimTreeEmptyFolderName"
|
||||
end
|
||||
|
||||
return { str = icon, hl = { icon_hl } }, { str = foldername, hl = { foldername_hl } }
|
||||
end
|
||||
|
||||
---@private
|
||||
---@param node table
|
||||
---@return HighlightedString icon
|
||||
---@return HighlightedString name
|
||||
function Builder:build_symlink(node)
|
||||
local icon = icons.i.symlink
|
||||
local arrow = icons.i.symlink_arrow
|
||||
local symlink_formatted = node.name
|
||||
if self.opts.renderer.symlink_destination then
|
||||
local link_to = utils.path_relative(node.link_to, self.explorer.absolute_path)
|
||||
symlink_formatted = string.format("%s%s%s", symlink_formatted, arrow, link_to)
|
||||
end
|
||||
|
||||
return { str = icon, hl = { "NvimTreeSymlinkIcon" } }, { str = symlink_formatted, hl = { "NvimTreeSymlink" } }
|
||||
end
|
||||
|
||||
---@private
|
||||
---@param node Node
|
||||
---@return HighlightedString icon
|
||||
---@return HighlightedString name
|
||||
function Builder:build_file(node)
|
||||
local hl
|
||||
if
|
||||
vim.tbl_contains(self.opts.renderer.special_files, node.absolute_path) or vim.tbl_contains(self.opts.renderer.special_files, node.name)
|
||||
then
|
||||
hl = "NvimTreeSpecialFile"
|
||||
elseif node.executable then
|
||||
hl = "NvimTreeExecFile"
|
||||
elseif PICTURE_MAP[node.extension] then
|
||||
hl = "NvimTreeImageFile"
|
||||
end
|
||||
|
||||
local icon, hl_group = icons.get_file_icon(node.name, node.extension)
|
||||
return { str = icon, hl = { hl_group } }, { str = node.name, hl = { hl } }
|
||||
end
|
||||
|
||||
---@private
|
||||
---@param indent_markers HighlightedString[]
|
||||
---@param arrows HighlightedString[]|nil
|
||||
@ -357,14 +252,7 @@ function Builder:build_line(node, idx, num_children)
|
||||
local arrows = pad.get_arrows(node)
|
||||
|
||||
-- main components
|
||||
local icon, name
|
||||
if node:is(DirectoryNode) then
|
||||
icon, name = self:build_folder(node)
|
||||
elseif node:is(DirectoryLinkNode) or node:is(FileLinkNode) then
|
||||
icon, name = self:build_symlink(node)
|
||||
else
|
||||
icon, name = self:build_file(node)
|
||||
end
|
||||
local icon, name = node:icon_name()
|
||||
|
||||
-- highighting
|
||||
local icon_hl_group, name_hl_group = self:add_highlights(node)
|
||||
@ -376,11 +264,12 @@ function Builder:build_line(node, idx, num_children)
|
||||
|
||||
self.index = self.index + 1
|
||||
|
||||
if node:is(DirectoryNode) then
|
||||
node = node:last_group_node()
|
||||
if node.open then
|
||||
local dir = node:as(DirectoryNode)
|
||||
if dir then
|
||||
dir = dir:last_group_node()
|
||||
if dir.open then
|
||||
self.depth = self.depth + 1
|
||||
self:build_lines(node)
|
||||
self:build_lines(dir)
|
||||
self.depth = self.depth - 1
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
local DirectoryLinkNode = require("nvim-tree.node.directory-link")
|
||||
local DirectoryLinkNode = nil --circular dependency
|
||||
|
||||
local M = { i = {} }
|
||||
|
||||
@ -127,6 +127,7 @@ 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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user