Skip to content

Commit 3501514

Browse files
committed
add classic, migrating nodes classes
1 parent 610a1c1 commit 3501514

File tree

11 files changed

+185
-142
lines changed

11 files changed

+185
-142
lines changed

lua/nvim-tree/classic.lua

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--
2+
-- classic
3+
--
4+
-- Copyright (c) 2014, rxi
5+
--
6+
-- This module is free software; you can redistribute it and/or modify it under
7+
-- the terms of the MIT license. See LICENSE for details.
8+
--
9+
10+
11+
local Object = {}
12+
Object.__index = Object
13+
14+
15+
function Object:new()
16+
end
17+
18+
19+
function Object:extend()
20+
local cls = {}
21+
for k, v in pairs(self) do
22+
if k:find("__") == 1 then
23+
cls[k] = v
24+
end
25+
end
26+
cls.__index = cls
27+
cls.super = self
28+
setmetatable(cls, self)
29+
return cls
30+
end
31+
32+
33+
function Object:implement(...)
34+
for _, cls in pairs({...}) do
35+
for k, v in pairs(cls) do
36+
if self[k] == nil and type(v) == "function" then
37+
self[k] = v
38+
end
39+
end
40+
end
41+
end
42+
43+
44+
function Object:is(T)
45+
local mt = getmetatable(self)
46+
while mt do
47+
if mt == T then
48+
return true
49+
end
50+
mt = getmetatable(mt)
51+
end
52+
return false
53+
end
54+
55+
56+
---Return object if it is an instance of class, otherwise nil
57+
---@generic T
58+
---@param cls T
59+
---@return T|nil
60+
function Object:as(cls)
61+
return self:is(cls) and self or nil
62+
end
63+
64+
65+
function Object:__tostring()
66+
return "Object"
67+
end
68+
69+
70+
function Object:__call(...)
71+
local obj = setmetatable({}, self)
72+
obj:new(...)
73+
return obj
74+
end
75+
76+
77+
return Object

lua/nvim-tree/core.lua

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local events = require("nvim-tree.events")
2+
local notify = require("nvim-tree.notify")
23
local view = require("nvim-tree.view")
34
local log = require("nvim-tree.log")
45

@@ -15,7 +16,21 @@ function M.init(foldername)
1516
if TreeExplorer then
1617
TreeExplorer:destroy()
1718
end
18-
TreeExplorer = require("nvim-tree.explorer"):create(foldername)
19+
20+
local err, path
21+
22+
if foldername then
23+
path, err = vim.loop.fs_realpath(foldername)
24+
else
25+
path, err = vim.loop.cwd()
26+
end
27+
if path then
28+
TreeExplorer = require("nvim-tree.explorer")(path)
29+
else
30+
notify.error(err)
31+
TreeExplorer = nil
32+
end
33+
1934
if not first_init_done then
2035
events._dispatch_ready()
2136
first_init_done = true

lua/nvim-tree/explorer/init.lua

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local buffers = require("nvim-tree.buffers")
33
local core = require("nvim-tree.core")
44
local git = require("nvim-tree.git")
55
local log = require("nvim-tree.log")
6-
local notify = require("nvim-tree.notify")
76
local utils = require("nvim-tree.utils")
87
local view = require("nvim-tree.view")
98
local node_factory = require("nvim-tree.node.factory")
@@ -36,51 +35,28 @@ local config
3635
---@field sorters Sorter
3736
---@field marks Marks
3837
---@field clipboard Clipboard
39-
local Explorer = RootNode:new()
40-
41-
---Static factory method
42-
---@param path string?
43-
---@return Explorer?
44-
function Explorer:create(path)
45-
local err
46-
47-
if path then
48-
path, err = vim.loop.fs_realpath(path)
49-
else
50-
path, err = vim.loop.cwd()
51-
end
52-
if not path then
53-
notify.error(err)
54-
return nil
55-
end
56-
57-
---@type Explorer
58-
local explorer_placeholder = nil
59-
60-
local o = RootNode:create(explorer_placeholder, path, "..", nil)
61-
62-
o = self:new(o)
63-
64-
o.explorer = o
38+
local Explorer = RootNode:extend()
6539

66-
o.uid_explorer = vim.loop.hrtime()
67-
o.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. o.uid_explorer, {})
40+
---@param path string
41+
function Explorer:new(path)
42+
Explorer.super.new(self, self, path, "..", nil)
6843

69-
o.open = true
70-
o.opts = config
44+
self.uid_explorer = vim.loop.hrtime()
45+
self.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. self.uid_explorer, {})
7146

72-
o.sorters = Sorters:create(config)
73-
o.renderer = Renderer:new(config, o)
74-
o.filters = Filters:new(config, o)
75-
o.live_filter = LiveFilter:new(config, o)
76-
o.marks = Marks:new(config, o)
77-
o.clipboard = Clipboard:new(config, o)
47+
self.open = true
48+
self.opts = config
7849

79-
o:create_autocmds()
50+
self.sorters = Sorters:create(config)
51+
self.renderer = Renderer:new(config, self)
52+
self.filters = Filters:new(config, self)
53+
self.live_filter = LiveFilter:new(config, self)
54+
self.marks = Marks:new(config, self)
55+
self.clipboard = Clipboard:new(config, self)
8056

81-
o:_load(o)
57+
self:create_autocmds()
8258

83-
return o
59+
self:_load(self)
8460
end
8561

8662
function Explorer:destroy()

lua/nvim-tree/node/directory-link.lua

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,26 @@ local utils = require("nvim-tree.utils")
33

44
local DirectoryNode = require("nvim-tree.node.directory")
55

6-
---@class (exact) DirectoryLinkNode: DirectoryNode
7-
---@field link_to string absolute path
8-
---@field private fs_stat_target uv.fs_stat.result
9-
local DirectoryLinkNode = DirectoryNode:new()
6+
---@class (exact) DirectoryLinkNode: DirectoryNode, LinkNode
7+
local DirectoryLinkNode = DirectoryNode:extend()
108

11-
---Static factory method
129
---@param explorer Explorer
1310
---@param parent DirectoryNode
1411
---@param absolute_path string
1512
---@param link_to string
1613
---@param name string
1714
---@param fs_stat uv.fs_stat.result?
1815
---@param fs_stat_target uv.fs_stat.result
19-
---@return DirectoryLinkNode? nil on vim.loop.fs_realpath failure
20-
function DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
16+
function DirectoryLinkNode:new(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
2117
-- create DirectoryNode with the target path for the watcher
22-
local o = DirectoryNode:create(explorer, parent, link_to, name, fs_stat)
23-
24-
o = self:new(o)
18+
DirectoryLinkNode.super.new(self, explorer, parent, link_to, name, fs_stat)
2519

2620
-- reset absolute path to the link itself
27-
o.absolute_path = absolute_path
28-
29-
o.type = "link"
30-
o.link_to = link_to
31-
o.fs_stat_target = fs_stat_target
21+
self.absolute_path = absolute_path
3222

33-
return o
23+
self.type = "link"
24+
self.link_to = link_to
25+
self.fs_stat_target = fs_stat_target
3426
end
3527

3628
function DirectoryLinkNode:destroy()

lua/nvim-tree/node/directory.lua

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,38 @@ local Node = require("nvim-tree.node")
1010
---@field open boolean
1111
---@field hidden_stats table? -- Each field of this table is a key for source and value for count
1212
---@field private watcher Watcher?
13-
local DirectoryNode = Node:new()
13+
local DirectoryNode = Node:extend()
1414

15-
---Static factory method
1615
---@param explorer Explorer
1716
---@param parent DirectoryNode?
1817
---@param absolute_path string
1918
---@param name string
2019
---@param fs_stat uv.fs_stat.result|nil
21-
---@return DirectoryNode
22-
function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat)
20+
function DirectoryNode:new(explorer, parent, absolute_path, name, fs_stat)
21+
DirectoryNode.super.new(self)
22+
2323
local handle = vim.loop.fs_scandir(absolute_path)
2424
local has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil or false
2525

26-
---@type DirectoryNode
27-
local o = {
28-
type = "directory",
29-
explorer = explorer,
30-
absolute_path = absolute_path,
31-
executable = false,
32-
fs_stat = fs_stat,
33-
git_status = nil,
34-
hidden = false,
35-
name = name,
36-
parent = parent,
37-
watcher = nil,
38-
diag_status = nil,
39-
is_dot = false,
40-
41-
has_children = has_children,
42-
group_next = nil,
43-
nodes = {},
44-
open = false,
45-
hidden_stats = nil,
46-
}
47-
o = self:new(o)
48-
49-
o.watcher = require("nvim-tree.explorer.watch").create_watcher(o)
50-
51-
return o
26+
self.type = "directory"
27+
self.explorer = explorer
28+
self.absolute_path = absolute_path
29+
self.executable = false
30+
self.fs_stat = fs_stat
31+
self.git_status = nil
32+
self.hidden = false
33+
self.name = name
34+
self.parent = parent
35+
self.watcher = nil
36+
self.diag_status = nil
37+
38+
self.has_children = has_children
39+
self.group_next = nil
40+
self.nodes = {}
41+
self.open = false
42+
self.hidden_stats = nil
43+
44+
self.watcher = require("nvim-tree.explorer.watch").create_watcher(self)
5245
end
5346

5447
function DirectoryNode:destroy()

lua/nvim-tree/node/factory.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ function M.create_node(explorer, parent, absolute_path, stat, name)
2121
if stat.type == "directory" then
2222
-- directory must be readable and enumerable
2323
if vim.loop.fs_access(absolute_path, "R") and Watcher.is_fs_event_capable(absolute_path) then
24-
return DirectoryNode:create(explorer, parent, absolute_path, name, stat)
24+
return DirectoryNode(explorer, parent, absolute_path, name, stat)
2525
end
2626
elseif stat.type == "file" then
2727
-- any file
28-
return FileNode:create(explorer, parent, absolute_path, name, stat)
28+
return FileNode(explorer, parent, absolute_path, name, stat)
2929
elseif stat.type == "link" then
3030
-- link target path and stat must resolve
3131
local link_to = vim.loop.fs_realpath(absolute_path)
@@ -36,9 +36,9 @@ function M.create_node(explorer, parent, absolute_path, stat, name)
3636

3737
-- choose directory or file
3838
if link_to_stat.type == "directory" then
39-
return DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name, stat, link_to_stat)
39+
return DirectoryLinkNode(explorer, parent, absolute_path, link_to, name, stat, link_to_stat)
4040
else
41-
return FileLinkNode:create(explorer, parent, absolute_path, link_to, name, stat, link_to_stat)
41+
return FileLinkNode(explorer, parent, absolute_path, link_to, name, stat, link_to_stat)
4242
end
4343
end
4444

lua/nvim-tree/node/file-link.lua

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,22 @@ local utils = require("nvim-tree.utils")
33

44
local FileNode = require("nvim-tree.node.file")
55

6-
---@class (exact) FileLinkNode: FileNode
7-
---@field link_to string absolute path
8-
---@field private fs_stat_target uv.fs_stat.result
9-
local FileLinkNode = FileNode:new()
6+
---@class (exact) FileLinkNode: FileNode, LinkNode
7+
local FileLinkNode = FileNode:extend()
108

11-
---Static factory method
129
---@param explorer Explorer
1310
---@param parent DirectoryNode
1411
---@param absolute_path string
1512
---@param link_to string
1613
---@param name string
1714
---@param fs_stat uv.fs_stat.result?
1815
---@param fs_stat_target uv.fs_stat.result
19-
---@return FileLinkNode? nil on vim.loop.fs_realpath failure
20-
function FileLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
21-
local o = FileNode:create(explorer, parent, absolute_path, name, fs_stat)
16+
function FileLinkNode:new(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
17+
FileLinkNode.super.new(self, explorer, parent, absolute_path, name, fs_stat)
2218

23-
o = self:new(o)
24-
25-
o.type = "link"
26-
o.link_to = link_to
27-
o.fs_stat_target = fs_stat_target
28-
29-
return o
19+
self.type = "link"
20+
self.link_to = link_to
21+
self.fs_stat_target = fs_stat_target
3022
end
3123

3224
function FileLinkNode:destroy()

0 commit comments

Comments
 (0)