typechecked optargs constructors for nodes
This commit is contained in:
@@ -8,23 +8,23 @@ local LinkNode = require("nvim-tree.node.link")
|
||||
local DirectoryLinkNode = DirectoryNode:extend()
|
||||
DirectoryLinkNode:implement(LinkNode)
|
||||
|
||||
---@param explorer Explorer
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path string
|
||||
---@param link_to string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result?
|
||||
---@param fs_stat_target uv.fs_stat.result
|
||||
function DirectoryLinkNode:new(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
|
||||
-- create DirectoryNode with the target path for the watcher
|
||||
DirectoryLinkNode.super.new(self, explorer, parent, link_to, name, fs_stat)
|
||||
---@class DirectoryLinkNode
|
||||
---@overload fun(opts: LinkNodeArgs): DirectoryLinkNode
|
||||
|
||||
---@protected
|
||||
---@param args LinkNodeArgs
|
||||
function DirectoryLinkNode:new(args)
|
||||
LinkNode.new(self, args)
|
||||
|
||||
-- create DirectoryNode with watcher on link_to
|
||||
local absolute_path = args.absolute_path
|
||||
args.absolute_path = args.link_to
|
||||
DirectoryLinkNode.super.new(self, args)
|
||||
|
||||
self.type = "link"
|
||||
|
||||
-- reset absolute path to the link itself
|
||||
self.absolute_path = absolute_path
|
||||
|
||||
self.type = "link"
|
||||
self.link_to = link_to
|
||||
self.fs_stat_target = fs_stat_target
|
||||
end
|
||||
|
||||
function DirectoryLinkNode:destroy()
|
||||
@@ -76,7 +76,6 @@ end
|
||||
function DirectoryLinkNode:clone()
|
||||
local clone = DirectoryNode.clone(self) --[[@as DirectoryLinkNode]]
|
||||
|
||||
clone.type = self.type
|
||||
clone.link_to = self.link_to
|
||||
clone.fs_stat_target = self.fs_stat_target
|
||||
|
||||
|
||||
@@ -12,36 +12,26 @@ local Node = require("nvim-tree.node")
|
||||
---@field private watcher Watcher?
|
||||
local DirectoryNode = Node:extend()
|
||||
|
||||
---@param explorer Explorer
|
||||
---@param parent DirectoryNode?
|
||||
---@param absolute_path string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result|nil
|
||||
function DirectoryNode:new(explorer, parent, absolute_path, name, fs_stat)
|
||||
DirectoryNode.super.new(self)
|
||||
---@class DirectoryNode
|
||||
---@overload fun(opts: NodeArgs): DirectoryNode
|
||||
|
||||
local handle = vim.loop.fs_scandir(absolute_path)
|
||||
---@protected
|
||||
---@param args NodeArgs
|
||||
function DirectoryNode:new(args)
|
||||
DirectoryNode.super.new(self, args)
|
||||
|
||||
local handle = vim.loop.fs_scandir(args.absolute_path)
|
||||
local has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil or false
|
||||
|
||||
self.type = "directory"
|
||||
self.explorer = explorer
|
||||
self.absolute_path = absolute_path
|
||||
self.executable = false
|
||||
self.fs_stat = fs_stat
|
||||
self.git_status = nil
|
||||
self.hidden = false
|
||||
self.name = name
|
||||
self.parent = parent
|
||||
self.watcher = nil
|
||||
self.diag_status = nil
|
||||
self.type = "directory"
|
||||
|
||||
self.has_children = has_children
|
||||
self.group_next = nil
|
||||
self.nodes = {}
|
||||
self.open = false
|
||||
self.hidden_stats = nil
|
||||
self.has_children = has_children
|
||||
self.group_next = nil
|
||||
self.nodes = {}
|
||||
self.open = false
|
||||
self.hidden_stats = nil
|
||||
|
||||
self.watcher = require("nvim-tree.explorer.watch").create_watcher(self)
|
||||
self.watcher = require("nvim-tree.explorer.watch").create_watcher(self)
|
||||
end
|
||||
|
||||
function DirectoryNode:destroy()
|
||||
|
||||
@@ -7,38 +7,38 @@ local Watcher = require("nvim-tree.watcher")
|
||||
local M = {}
|
||||
|
||||
---Factory function to create the appropriate Node
|
||||
---@param explorer Explorer
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path string
|
||||
---@param stat uv.fs_stat.result? -- on nil stat return nil Node
|
||||
---@param name string
|
||||
---nil on invalid stat or invalid link target stat
|
||||
---@param args NodeArgs
|
||||
---@return Node?
|
||||
function M.create_node(explorer, parent, absolute_path, stat, name)
|
||||
if not stat then
|
||||
function M.create(args)
|
||||
if not args.fs_stat then
|
||||
return nil
|
||||
end
|
||||
|
||||
if stat.type == "directory" then
|
||||
if args.fs_stat.type == "directory" then
|
||||
-- directory must be readable and enumerable
|
||||
if vim.loop.fs_access(absolute_path, "R") and Watcher.is_fs_event_capable(absolute_path) then
|
||||
return DirectoryNode(explorer, parent, absolute_path, name, stat)
|
||||
if vim.loop.fs_access(args.absolute_path, "R") and Watcher.is_fs_event_capable(args.absolute_path) then
|
||||
return DirectoryNode(args)
|
||||
end
|
||||
elseif stat.type == "file" then
|
||||
-- any file
|
||||
return FileNode(explorer, parent, absolute_path, name, stat)
|
||||
elseif stat.type == "link" then
|
||||
elseif args.fs_stat.type == "file" then
|
||||
return FileNode(args)
|
||||
elseif args.fs_stat.type == "link" then
|
||||
-- link target path and stat must resolve
|
||||
local link_to = vim.loop.fs_realpath(absolute_path)
|
||||
local link_to = vim.loop.fs_realpath(args.absolute_path)
|
||||
local link_to_stat = link_to and vim.loop.fs_stat(link_to)
|
||||
if not link_to or not link_to_stat then
|
||||
return
|
||||
end
|
||||
|
||||
---@cast args LinkNodeArgs
|
||||
args.link_to = link_to
|
||||
args.fs_stat_target = link_to_stat
|
||||
|
||||
-- choose directory or file
|
||||
if link_to_stat.type == "directory" then
|
||||
return DirectoryLinkNode(explorer, parent, absolute_path, link_to, name, stat, link_to_stat)
|
||||
return DirectoryLinkNode(args)
|
||||
else
|
||||
return FileLinkNode(explorer, parent, absolute_path, link_to, name, stat, link_to_stat)
|
||||
return FileLinkNode(args)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -8,19 +8,16 @@ local LinkNode = require("nvim-tree.node.link")
|
||||
local FileLinkNode = FileNode:extend()
|
||||
FileLinkNode:implement(LinkNode)
|
||||
|
||||
---@param explorer Explorer
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path string
|
||||
---@param link_to string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result?
|
||||
---@param fs_stat_target uv.fs_stat.result
|
||||
function FileLinkNode:new(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
|
||||
FileLinkNode.super.new(self, explorer, parent, absolute_path, name, fs_stat)
|
||||
---@class FileLinkNode
|
||||
---@overload fun(opts: LinkNodeArgs): FileLinkNode
|
||||
|
||||
---@protected
|
||||
---@param args LinkNodeArgs
|
||||
function FileLinkNode:new(args)
|
||||
LinkNode.new(self, args)
|
||||
FileLinkNode.super.new(self, args)
|
||||
|
||||
self.type = "link"
|
||||
self.link_to = link_to
|
||||
self.fs_stat_target = fs_stat_target
|
||||
end
|
||||
|
||||
function FileLinkNode:destroy()
|
||||
@@ -65,7 +62,6 @@ end
|
||||
function FileLinkNode:clone()
|
||||
local clone = FileNode.clone(self) --[[@as FileLinkNode]]
|
||||
|
||||
clone.type = self.type
|
||||
clone.link_to = self.link_to
|
||||
clone.fs_stat_target = self.fs_stat_target
|
||||
|
||||
|
||||
@@ -17,26 +17,17 @@ local PICTURE_MAP = {
|
||||
---@field extension string
|
||||
local FileNode = Node:extend()
|
||||
|
||||
---@param explorer Explorer
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result?
|
||||
function FileNode:new(explorer, parent, absolute_path, name, fs_stat)
|
||||
FileNode.super.new(self)
|
||||
---@class FileNode
|
||||
---@overload fun(opts: NodeArgs): FileNode
|
||||
|
||||
self.type = "file"
|
||||
self.explorer = explorer
|
||||
self.absolute_path = absolute_path
|
||||
self.executable = utils.is_executable(absolute_path)
|
||||
self.fs_stat = fs_stat
|
||||
self.git_status = nil
|
||||
self.hidden = false
|
||||
self.name = name
|
||||
self.parent = parent
|
||||
self.diag_status = nil
|
||||
---@protected
|
||||
---@param args NodeArgs
|
||||
function FileNode:new(args)
|
||||
FileNode.super.new(self, args)
|
||||
|
||||
self.extension = string.match(name, ".?[^.]+%.(.*)") or ""
|
||||
self.type = "file"
|
||||
self.extension = string.match(args.name, ".?[^.]+%.(.*)") or ""
|
||||
self.executable = utils.is_executable(args.absolute_path)
|
||||
end
|
||||
|
||||
function FileNode:destroy()
|
||||
|
||||
@@ -2,7 +2,7 @@ local Object = require("nvim-tree.classic")
|
||||
|
||||
---Abstract Node class.
|
||||
---@class (exact) Node: Object
|
||||
---@field type NODE_TYPE
|
||||
---@field type "file" | "directory" | "link" uv.fs_stat.result.type
|
||||
---@field explorer Explorer
|
||||
---@field absolute_path string
|
||||
---@field executable boolean
|
||||
@@ -15,8 +15,26 @@ local Object = require("nvim-tree.classic")
|
||||
---@field private is_dot boolean cached is_dotfile
|
||||
local Node = Object:extend()
|
||||
|
||||
function Node:new()
|
||||
self.is_dot = false
|
||||
---@class (exact) NodeArgs
|
||||
---@field explorer Explorer
|
||||
---@field parent DirectoryNode?
|
||||
---@field absolute_path string
|
||||
---@field name string
|
||||
---@field fs_stat uv.fs_stat.result?
|
||||
|
||||
---@protected
|
||||
---@param args NodeArgs
|
||||
function Node:new(args)
|
||||
self.explorer = args.explorer
|
||||
self.absolute_path = args.absolute_path
|
||||
self.executable = false
|
||||
self.fs_stat = args.fs_stat
|
||||
self.git_status = nil
|
||||
self.hidden = false
|
||||
self.name = args.name
|
||||
self.parent = args.parent
|
||||
self.diag_status = nil
|
||||
self.is_dot = false
|
||||
end
|
||||
|
||||
function Node:destroy()
|
||||
|
||||
@@ -5,4 +5,17 @@ local Object = require("nvim-tree.classic")
|
||||
---@field protected fs_stat_target uv.fs_stat.result
|
||||
local LinkNode = Object:extend()
|
||||
|
||||
---@class (exact) LinkNodeArgs: NodeArgs
|
||||
---@field link_to string
|
||||
---@field fs_stat_target uv.fs_stat.result
|
||||
---
|
||||
---@protected
|
||||
---@param args LinkNodeArgs
|
||||
function LinkNode:new(args)
|
||||
LinkNode.super.new(self, args)
|
||||
|
||||
self.link_to = args.link_to
|
||||
self.fs_stat_target = args.fs_stat_target
|
||||
end
|
||||
|
||||
return LinkNode
|
||||
|
||||
@@ -3,12 +3,13 @@ local DirectoryNode = require("nvim-tree.node.directory")
|
||||
---@class (exact) RootNode: DirectoryNode
|
||||
local RootNode = DirectoryNode:extend()
|
||||
|
||||
---@param explorer Explorer
|
||||
---@param absolute_path string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result|nil
|
||||
function RootNode:new(explorer, absolute_path, name, fs_stat)
|
||||
RootNode.super.new(self, explorer, nil, absolute_path, name, fs_stat)
|
||||
---@class RootNode
|
||||
---@overload fun(opts: NodeArgs): RootNode
|
||||
|
||||
---@protected
|
||||
---@param args NodeArgs
|
||||
function RootNode:new(args)
|
||||
RootNode.super.new(self, args)
|
||||
end
|
||||
|
||||
---Root is never a dotfile
|
||||
|
||||
Reference in New Issue
Block a user