chore: resolve undefined-field warnings, fix link git statuses, rewrite devicons (#2968)

* add todo

* refactor(#2886): multi instance: node class refactoring: extract links, *_git_status (#2944)

* extract DirectoryLinkNode and FileLinkNode, move Node methods to children

* temporarily move DirectoryNode methods into BaseNode for easier reviewing

* move mostly unchanged DirectoryNode methods back to BaseNode

* tidy

* git.git_status_file takes an array

* update git status of links

* luacheck hack

* safer git_status_dir

* refactor(#2886): multi instance: node class refactoring: DirectoryNode:expand_or_collapse (#2957)

move expand_or_collapse to DirectoryNode

* refactor(#2886): multi instance: node group functions refactoring (#2959)

* move last_group_node to DirectoryNode

* move add BaseNode:as and more doc

* revert parameter name changes

* revert parameter name changes

* add Class

* move group methods into DN

* tidy group methods

* tidy group methods

* tidy group methods

* tidy group methods

* parent is DirectoryNode

* tidy expand all

* BaseNode -> Node

* move watcher to DirectoryNode

* last_group_node is DirectoryNode only

* simplify create-file

* simplify parent

* simplify collapse-all

* simplify live-filter

* style

* move lib.get_cursor_position to Explorer

* move lib.get_node_at_cursor to Explorer

* move lib.get_nodes to Explorer

* move place_cursor_on_node to Explorer

* resolve resource leak in purge_all_state

* move many autocommands into Explorer

* post merge tidy

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* Revert "chore: resolve undefined-field"

This reverts commit be546ff18d41f28466b065c857e1e041659bd2c8.

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* Revert "chore: resolve undefined-field"

This reverts commit e82db1c44d.

* chore: resolve undefined-field

* chore: class new is now generic

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* Revert "chore: resolve undefined-field"

This reverts commit 0e9b844d22.

* move icon builders into node classes

* move icon builders into node classes

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* chore: resolve undefined-field

* move folder specifics from icons to Directory

* move folder specifics from icons to Directory

* move folder specifics from icons to Directory

* move folder specifics from icons to Directory

* move file specifics from icons to File

* clean up sorters

* chore: resolve undefined-field

* tidy hl icon name

* file devicon uses library to fall back

* file devicon uses library to fall back

* file devicon uses library to fall back
This commit is contained in:
Alexander Courtis
2024-11-03 14:06:12 +11:00
committed by GitHub
parent c22124b374
commit 610a1c189b
43 changed files with 1073 additions and 887 deletions

View File

@@ -1,10 +1,11 @@
local git = require("nvim-tree.git")
local git_utils = require("nvim-tree.git.utils")
local utils = require("nvim-tree.utils")
local DirectoryNode = require("nvim-tree.node.directory")
---@class (exact) DirectoryLinkNode: DirectoryNode
---@field link_to string absolute path
---@field fs_stat_target uv.fs_stat.result
---@field private fs_stat_target uv.fs_stat.result
local DirectoryLinkNode = DirectoryNode:new()
---Static factory method
@@ -20,7 +21,7 @@ function DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name
-- create DirectoryNode with the target path for the watcher
local o = DirectoryNode:create(explorer, parent, link_to, name, fs_stat)
o = self:new(o) --[[@as DirectoryLinkNode]]
o = self:new(o)
-- reset absolute path to the link itself
o.absolute_path = absolute_path
@@ -36,11 +37,44 @@ function DirectoryLinkNode:destroy()
DirectoryNode.destroy(self)
end
-----Update the directory GitStatus of link target and the file status of the link itself
-----@param parent_ignored boolean
-----@param status table|nil
function DirectoryLinkNode:update_git_status(parent_ignored, status)
self.git_status = git.git_status_dir(parent_ignored, status, self.link_to, self.absolute_path)
---Update the directory git_status of link target and the file status of the link itself
---@param parent_ignored boolean
---@param project GitProject?
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
---@return HighlightedString name
function DirectoryLinkNode:highlighted_icon()
if not self.explorer.opts.renderer.icons.show.folder then
return self:highlighted_icon_empty()
end
local str, hl
if self.open then
str = self.explorer.opts.renderer.icons.glyphs.folder.symlink_open
hl = "NvimTreeOpenedFolderIcon"
else
str = self.explorer.opts.renderer.icons.glyphs.folder.symlink
hl = "NvimTreeClosedFolderIcon"
end
return { str = str, hl = { hl } }
end
---Maybe override name with arrow
---@return HighlightedString name
function DirectoryLinkNode:highlighted_name()
local name = DirectoryNode.highlighted_name(self)
if self.explorer.opts.renderer.symlink_destination then
local link_to = utils.path_relative(self.link_to, self.explorer.absolute_path)
name.str = string.format("%s%s%s", name.str, self.explorer.opts.renderer.icons.symlink_arrow, link_to)
name.hl = { "NvimTreeSymlinkFolderName" }
end
return name
end
---Create a sanitized partial copy of a node, populating children recursively.

View File

@@ -1,6 +1,6 @@
local git = require("nvim-tree.git")
local watch = require("nvim-tree.explorer.watch")
local git_utils = require("nvim-tree.git.utils")
local icons = require("nvim-tree.renderer.components.devicons")
local notify = require("nvim-tree.notify")
local Node = require("nvim-tree.node")
---@class (exact) DirectoryNode: Node
@@ -8,8 +8,8 @@ local Node = require("nvim-tree.node")
---@field group_next DirectoryNode? -- If node is grouped, this points to the next child dir/link node
---@field nodes Node[]
---@field open boolean
---@field watcher Watcher?
---@field hidden_stats table? -- Each field of this table is a key for source and value for count
---@field private watcher Watcher?
local DirectoryNode = Node:new()
---Static factory method
@@ -32,11 +32,11 @@ function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat)
fs_stat = fs_stat,
git_status = nil,
hidden = false,
is_dot = false,
name = name,
parent = parent,
watcher = nil,
diag_status = nil,
is_dot = false,
has_children = has_children,
group_next = nil,
@@ -44,9 +44,9 @@ function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat)
open = false,
hidden_stats = nil,
}
o = self:new(o) --[[@as DirectoryNode]]
o = self:new(o)
o.watcher = watch.create_watcher(o)
o.watcher = require("nvim-tree.explorer.watch").create_watcher(o)
return o
end
@@ -66,41 +66,41 @@ function DirectoryNode:destroy()
Node.destroy(self)
end
---Update the GitStatus of the directory
---Update the git_status of the directory
---@param parent_ignored boolean
---@param status table|nil
function DirectoryNode:update_git_status(parent_ignored, status)
self.git_status = git.git_status_dir(parent_ignored, status, self.absolute_path, nil)
---@param project GitProject?
function DirectoryNode:update_git_status(parent_ignored, project)
self.git_status = git_utils.git_status_dir(parent_ignored, project, self.absolute_path, nil)
end
---@return GitStatus|nil
function DirectoryNode:get_git_status()
---@return GitXY[]?
function DirectoryNode:get_git_xy()
if not self.git_status or not self.explorer.opts.git.show_on_dirs then
return nil
end
local status = {}
local xys = {}
if not self:last_group_node().open or self.explorer.opts.git.show_on_open_dirs then
-- dir is closed or we should show on open_dirs
if self.git_status.file ~= nil then
table.insert(status, self.git_status.file)
table.insert(xys, self.git_status.file)
end
if self.git_status.dir ~= nil then
if self.git_status.dir.direct ~= nil then
for _, s in pairs(self.git_status.dir.direct) do
table.insert(status, s)
table.insert(xys, s)
end
end
if self.git_status.dir.indirect ~= nil then
for _, s in pairs(self.git_status.dir.indirect) do
table.insert(status, s)
table.insert(xys, s)
end
end
end
else
-- dir is open and we shouldn't show on open_dirs
if self.git_status.file ~= nil then
table.insert(status, self.git_status.file)
table.insert(xys, self.git_status.file)
end
if self.git_status.dir ~= nil and self.git_status.dir.direct ~= nil then
local deleted = {
@@ -111,34 +111,18 @@ function DirectoryNode:get_git_status()
}
for _, s in pairs(self.git_status.dir.direct) do
if deleted[s] then
table.insert(status, s)
table.insert(xys, s)
end
end
end
end
if #status == 0 then
if #xys == 0 then
return nil
else
return status
return xys
end
end
---Refresh contents and git status for a single node
function DirectoryNode:refresh()
local node = self:get_parent_of_group() or self
local toplevel = git.get_toplevel(self.absolute_path)
git.reload_project(toplevel, self.absolute_path, function()
local project = git.get_project(toplevel) or {}
self.explorer:reload(node, project)
node:update_parent_statuses(project, toplevel)
self.explorer.renderer:draw()
end)
end
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
---@return DirectoryNode
function DirectoryNode:last_group_node()
@@ -191,7 +175,7 @@ function DirectoryNode:ungroup_empty_folders()
end
end
---@param toggle_group boolean
---@param toggle_group boolean?
function DirectoryNode:expand_or_collapse(toggle_group)
toggle_group = toggle_group or false
if self.has_children then
@@ -224,6 +208,84 @@ function DirectoryNode:expand_or_collapse(toggle_group)
self.explorer.renderer:draw()
end
---@return HighlightedString icon
function DirectoryNode:highlighted_icon()
if not self.explorer.opts.renderer.icons.show.folder then
return self:highlighted_icon_empty()
end
local str, hl
-- devicon if enabled and available
if self.explorer.opts.renderer.icons.web_devicons.folder.enable then
str, hl = icons.get_icon(self.name)
if not self.explorer.opts.renderer.icons.web_devicons.folder.color then
hl = nil
end
end
-- default icon from opts
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 hl
if not hl then
if self.open then
hl = "NvimTreeOpenedFolderIcon"
else
hl = "NvimTreeClosedFolderIcon"
end
end
return { str = str, hl = { hl } }
end
---@return HighlightedString icon
function DirectoryNode:highlighted_name()
local str, hl
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
str = string.format("%s%s", name, self.explorer.opts.renderer.add_trailing and "/" or "")
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
hl = "NvimTreeSpecialFolderName"
elseif self.open then
hl = "NvimTreeOpenedFolderName"
elseif #self.nodes == 0 and not self.has_children then
hl = "NvimTreeEmptyFolderName"
end
return { str = str, hl = { hl } }
end
---Create a sanitized partial copy of a node, populating children recursively.
---@return DirectoryNode cloned
function DirectoryNode:clone()

View File

@@ -1,10 +1,11 @@
local git = require("nvim-tree.git")
local git_utils = require("nvim-tree.git.utils")
local utils = require("nvim-tree.utils")
local FileNode = require("nvim-tree.node.file")
---@class (exact) FileLinkNode: FileNode
---@field link_to string absolute path
---@field fs_stat_target uv.fs_stat.result
---@field private fs_stat_target uv.fs_stat.result
local FileLinkNode = FileNode:new()
---Static factory method
@@ -19,7 +20,7 @@ local FileLinkNode = FileNode:new()
function FileLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
local o = FileNode:create(explorer, parent, absolute_path, name, fs_stat)
o = self:new(o) --[[@as FileLinkNode]]
o = self:new(o)
o.type = "link"
o.link_to = link_to
@@ -32,11 +33,37 @@ function FileLinkNode:destroy()
FileNode.destroy(self)
end
-----Update the GitStatus of the target otherwise the link itself
-----@param parent_ignored boolean
-----@param status table|nil
function FileLinkNode:update_git_status(parent_ignored, status)
self.git_status = git.git_status_file(parent_ignored, status, self.link_to, self.absolute_path)
---Update the git_status of the target otherwise the link itself
---@param parent_ignored boolean
---@param project GitProject?
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
---@return HighlightedString icon
function FileLinkNode:highlighted_icon()
if not self.explorer.opts.renderer.icons.show.file then
return self:highlighted_icon_empty()
end
local str, hl
-- default icon from opts
str = self.explorer.opts.renderer.icons.glyphs.symlink
hl = "NvimTreeSymlinkIcon"
return { str = str, hl = { hl } }
end
---@return HighlightedString name
function FileLinkNode:highlighted_name()
local str = self.name
if self.explorer.opts.renderer.symlink_destination then
local link_to = utils.path_relative(self.link_to, self.explorer.absolute_path)
str = string.format("%s%s%s", str, self.explorer.opts.renderer.icons.symlink_arrow, link_to)
end
return { str = str, hl = { "NvimTreeSymlink" } }
end
---Create a sanitized partial copy of a node

View File

@@ -1,8 +1,18 @@
local git = require("nvim-tree.git")
local git_utils = require("nvim-tree.git.utils")
local icons = require("nvim-tree.renderer.components.devicons")
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()
@@ -24,14 +34,14 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat)
fs_stat = fs_stat,
git_status = nil,
hidden = false,
is_dot = false,
name = name,
parent = parent,
diag_status = nil,
is_dot = false,
extension = string.match(name, ".?[^.]+%.(.*)") or "",
}
o = self:new(o) --[[@as FileNode]]
o = self:new(o)
return o
end
@@ -42,13 +52,13 @@ end
---Update the GitStatus of the file
---@param parent_ignored boolean
---@param status table|nil
function FileNode:update_git_status(parent_ignored, status)
self.git_status = git.git_status_file(parent_ignored, status, self.absolute_path, nil)
---@param project GitProject?
function FileNode:update_git_status(parent_ignored, project)
self.git_status = git_utils.git_status_file(parent_ignored, project, self.absolute_path, nil)
end
---@return GitStatus|nil
function FileNode:get_git_status()
---@return GitXY[]?
function FileNode:get_git_xy()
if not self.git_status then
return nil
end
@@ -56,6 +66,49 @@ function FileNode:get_git_status()
return self.git_status.file and { self.git_status.file }
end
---@return HighlightedString icon
function FileNode:highlighted_icon()
if not self.explorer.opts.renderer.icons.show.file then
return self:highlighted_icon_empty()
end
local str, hl
-- devicon if enabled and available, fallback to default
if self.explorer.opts.renderer.icons.web_devicons.file.enable then
str, hl = icons.get_icon(self.name, nil, { default = true })
if not self.explorer.opts.renderer.icons.web_devicons.file.color then
hl = nil
end
end
-- default icon from opts
if not str then
str = self.explorer.opts.renderer.icons.glyphs.default
end
-- default hl
if not hl then
hl = "NvimTreeFileIcon"
end
return { str = str, hl = { hl } }
end
---@return HighlightedString name
function FileNode:highlighted_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
return { str = self.name, hl = { hl } }
end
---Create a sanitized partial copy of a node
---@return FileNode cloned
function FileNode:clone()

View File

@@ -1,5 +1,3 @@
local git = require("nvim-tree.git")
local Class = require("nvim-tree.class")
---Abstract Node class.
@@ -10,41 +8,28 @@ local Class = require("nvim-tree.class")
---@field absolute_path string
---@field executable boolean
---@field fs_stat uv.fs_stat.result?
---@field git_status GitStatus?
---@field git_status GitNodeStatus?
---@field hidden boolean
---@field name string
---@field parent DirectoryNode?
---@field diag_status DiagStatus?
---@field is_dot boolean cached is_dotfile
---@field private is_dot boolean cached is_dotfile
local Node = Class:new()
function Node:destroy()
end
--luacheck: push ignore 212
---Update the GitStatus of the node
---Update the git_status of the node
---Abstract
---@param parent_ignored boolean
---@param status table?
function Node:update_git_status(parent_ignored, status) ---@diagnostic disable-line: unused-local
---TODO find a way to declare abstract methods
---@param project GitProject?
function Node:update_git_status(parent_ignored, project)
self:nop(parent_ignored, project)
end
--luacheck: pop
---@return GitStatus?
function Node:get_git_status()
end
---@param projects table
function Node:reload_node_status(projects)
local toplevel = git.get_toplevel(self.absolute_path)
local status = projects[toplevel] or {}
for _, node in ipairs(self.nodes) do
node:update_git_status(self:is_git_ignored(), status)
if node.nodes and #node.nodes > 0 then
node:reload_node_status(projects)
end
end
---Short-format statuses
---@return GitXY[]?
function Node:get_git_xy()
end
---@return boolean
@@ -66,38 +51,6 @@ function Node:is_dotfile()
return false
end
---@param project table?
---@param root string?
function Node:update_parent_statuses(project, root)
local node = self
while project and node do
-- step up to the containing project
if node.absolute_path == root then
-- stop at the top of the tree
if not node.parent then
break
end
root = git.get_toplevel(node.parent.absolute_path)
-- stop when no more projects
if not root then
break
end
-- update the containing project
project = git.get_project(root)
git.reload_project(root, node.absolute_path, nil)
end
-- update status
node:update_git_status(node.parent and node.parent:is_git_ignored() or false, project)
-- maybe parent
node = node.parent
end
end
---Get the highest parent of grouped nodes, nil when not grouped
---@return DirectoryNode?
function Node:get_parent_of_group()
@@ -115,6 +68,34 @@ function Node:get_parent_of_group()
end
end
---Empty highlighted icon
---@protected
---@return HighlightedString icon
function Node:highlighted_icon_empty()
return { str = "", hl = {} }
end
---Highlighted icon for the node
---Empty for base Node
---@return HighlightedString icon
function Node:highlighted_icon()
return self:highlighted_icon_empty()
end
---Empty highlighted name
---@protected
---@return HighlightedString name
function Node:highlighted_name_empty()
return { str = "", hl = {} }
end
---Highlighted name for the node
---Empty for base Node
---@return HighlightedString icon
function Node:highlighted_name()
return self:highlighted_name_empty()
end
---Create a sanitized partial copy of a node, populating children recursively.
---@return Node cloned
function Node:clone()
@@ -130,10 +111,10 @@ function Node:clone()
fs_stat = self.fs_stat,
git_status = self.git_status,
hidden = self.hidden,
is_dot = self.is_dot,
name = self.name,
parent = nil,
diag_status = nil,
is_dot = self.is_dot,
}
return clone

View File

@@ -12,7 +12,7 @@ local RootNode = DirectoryNode:new()
function RootNode:create(explorer, absolute_path, name, fs_stat)
local o = DirectoryNode:create(explorer, nil, absolute_path, name, fs_stat)
o = self:new(o) --[[@as RootNode]]
o = self:new(o)
return o
end