Skip to content

Commit 3fc8de1

Browse files
authored
chore: migrate to classic (#2991)
* add classic, migrating nodes classes * add mixins to classic * typechecked optargs constructors for nodes * typechecked optargs constructors for watcher and event * luacheck * typechecked optargs constructors for GitRunner * typechecked optargs constructors for Sorter * typechecked optargs constructors for decorators, WIP * typechecked optargs constructors for decorators, WIP * typechecked optargs constructors for decorators * remove class * replace enums with named maps * Renderer and Builder use classic, tidy opts * LiveFilter uses classic, tidy opts * Filter uses classic, tidy opts * add FilterTypes named map * move toggles into filters * Marks uses classic, tidy opts * Sorter uses classic, tidy opts * Clipboard uses classic, tidy opts * use supers for node methods * HighlightDisplay uses classic * protected :new * Watcher tidy * Revert "use supers for node methods" This reverts commit 9fc7a86. * Watcher tidy * format * format * Filters private methods * format * Sorter type safety * Sorter type safety * Sorter type safety * Sorter type safety * Sorter type safety * Sorter type safety * tidy Runner * tidy hi-test name
1 parent 610a1c1 commit 3fc8de1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1028
-1148
lines changed

lua/nvim-tree/actions/fs/clipboard.lua

+21-24
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,39 @@ local notify = require("nvim-tree.notify")
77

88
local find_file = require("nvim-tree.actions.finders.find-file").fn
99

10+
local Class = require("nvim-tree.classic")
1011
local DirectoryNode = require("nvim-tree.node.directory")
1112

1213
---@alias ClipboardAction "copy" | "cut"
1314
---@alias ClipboardData table<ClipboardAction, Node[]>
1415

1516
---@alias ClipboardActionFn fun(source: string, dest: string): boolean, string?
1617

17-
---@class Clipboard to handle all actions.fs clipboard API
18-
---@field config table hydrated user opts.filters
18+
---@class (exact) Clipboard: Class
1919
---@field private explorer Explorer
2020
---@field private data ClipboardData
2121
---@field private clipboard_name string
2222
---@field private reg string
23-
local Clipboard = {}
24-
25-
---@param opts table user options
26-
---@param explorer Explorer
27-
---@return Clipboard
28-
function Clipboard:new(opts, explorer)
29-
---@type Clipboard
30-
local o = {
31-
explorer = explorer,
32-
data = {
33-
copy = {},
34-
cut = {},
35-
},
36-
clipboard_name = opts.actions.use_system_clipboard and "system" or "neovim",
37-
reg = opts.actions.use_system_clipboard and "+" or "1",
38-
config = {
39-
filesystem_watchers = opts.filesystem_watchers,
40-
},
23+
local Clipboard = Class:extend()
24+
25+
---@class Clipboard
26+
---@overload fun(args: ClipboardArgs): Clipboard
27+
28+
---@class (exact) ClipboardArgs
29+
---@field explorer Explorer
30+
31+
---@protected
32+
---@param args ClipboardArgs
33+
function Clipboard:new(args)
34+
self.explorer = args.explorer
35+
36+
self.data = {
37+
copy = {},
38+
cut = {},
4139
}
4240

43-
setmetatable(o, self)
44-
self.__index = self
45-
return o
41+
self.clipboard_name = self.explorer.opts.actions.use_system_clipboard and "system" or "neovim"
42+
self.reg = self.explorer.opts.actions.use_system_clipboard and "+" or "1"
4643
end
4744

4845
---@param source string
@@ -252,7 +249,7 @@ function Clipboard:do_paste(node, action, action_fn)
252249
end
253250

254251
self.data[action] = {}
255-
if not self.config.filesystem_watchers.enable then
252+
if not self.explorer.opts.filesystem_watchers.enable then
256253
self.explorer:reload_explorer()
257254
end
258255
end

lua/nvim-tree/actions/tree/modifiers/init.lua

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ local M = {}
22

33
M.collapse_all = require("nvim-tree.actions.tree.modifiers.collapse-all")
44
M.expand_all = require("nvim-tree.actions.tree.modifiers.expand-all")
5-
M.toggles = require("nvim-tree.actions.tree.modifiers.toggles")
65

76
function M.setup(opts)
87
M.expand_all.setup(opts)

lua/nvim-tree/actions/tree/modifiers/toggles.lua

-73
This file was deleted.

lua/nvim-tree/api.lua

+25-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local core = require("nvim-tree.core")
22
local view = require("nvim-tree.view")
33
local utils = require("nvim-tree.utils")
44
local actions = require("nvim-tree.actions")
5-
local appearance_diagnostics = require("nvim-tree.appearance.diagnostics")
5+
local appearance_hi_test = require("nvim-tree.appearance.hi-test")
66
local events = require("nvim-tree.events")
77
local help = require("nvim-tree.help")
88
local keymap = require("nvim-tree.keymap")
@@ -89,6 +89,22 @@ local function wrap_node_or_nil(fn)
8989
end
9090
end
9191

92+
---Invoke a member's method on the singleton explorer.
93+
---Print error when setup not called.
94+
---@param explorer_member string explorer member name
95+
---@param member_method string method name to invoke on member
96+
---@param ... any passed to method
97+
---@return fun(...): any
98+
local function wrap_explorer_member_args(explorer_member, member_method, ...)
99+
local method_args = ...
100+
return wrap(function(...)
101+
local explorer = core.get_explorer()
102+
if explorer then
103+
return explorer[explorer_member][member_method](explorer[explorer_member], method_args, ...)
104+
end
105+
end)
106+
end
107+
92108
---Invoke a member's method on the singleton explorer.
93109
---Print error when setup not called.
94110
---@param explorer_member string explorer member name
@@ -165,13 +181,13 @@ Api.tree.find_file = wrap(actions.tree.find_file.fn)
165181
Api.tree.search_node = wrap(actions.finders.search_node.fn)
166182
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse_all.fn)
167183
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn)
168-
Api.tree.toggle_enable_filters = wrap(actions.tree.modifiers.toggles.enable)
169-
Api.tree.toggle_gitignore_filter = wrap(actions.tree.modifiers.toggles.git_ignored)
170-
Api.tree.toggle_git_clean_filter = wrap(actions.tree.modifiers.toggles.git_clean)
171-
Api.tree.toggle_no_buffer_filter = wrap(actions.tree.modifiers.toggles.no_buffer)
172-
Api.tree.toggle_custom_filter = wrap(actions.tree.modifiers.toggles.custom)
173-
Api.tree.toggle_hidden_filter = wrap(actions.tree.modifiers.toggles.dotfiles)
174-
Api.tree.toggle_no_bookmark_filter = wrap(actions.tree.modifiers.toggles.no_bookmark)
184+
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
185+
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored")
186+
Api.tree.toggle_git_clean_filter = wrap_explorer_member_args("filters", "toggle", "git_clean")
187+
Api.tree.toggle_no_buffer_filter = wrap_explorer_member_args("filters", "toggle", "no_buffer")
188+
Api.tree.toggle_custom_filter = wrap_explorer_member_args("filters", "toggle", "custom")
189+
Api.tree.toggle_hidden_filter = wrap_explorer_member_args("filters", "toggle", "dotfiles")
190+
Api.tree.toggle_no_bookmark_filter = wrap_explorer_member_args("filters", "toggle", "no_bookmark")
175191
Api.tree.toggle_help = wrap(help.toggle)
176192
Api.tree.is_tree_buf = wrap(utils.is_nvim_tree_buf)
177193

@@ -289,7 +305,7 @@ Api.config.mappings.get_keymap = wrap(keymap.get_keymap)
289305
Api.config.mappings.get_keymap_default = wrap(keymap.get_keymap_default)
290306
Api.config.mappings.default_on_attach = keymap.default_on_attach
291307

292-
Api.diagnostics.hi_test = wrap(appearance_diagnostics.hi_test)
308+
Api.diagnostics.hi_test = wrap(appearance_hi_test)
293309

294310
Api.commands.get = wrap(function()
295311
return require("nvim-tree.commands").get()

lua/nvim-tree/appearance/diagnostics.lua renamed to lua/nvim-tree/appearance/hi-test.lua

+22-23
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
11
local appearance = require("nvim-tree.appearance")
22

3+
local Class = require("nvim-tree.classic")
4+
35
-- others with name and links less than this arbitrary value are short
46
local SHORT_LEN = 50
57

6-
local M = {}
7-
8-
---@class HighlightDisplay for :NvimTreeHiTest
8+
---@class (exact) HighlightDisplay: Class for :NvimTreeHiTest
99
---@field group string nvim-tree highlight group name
1010
---@field links string link chain to a concretely defined group
1111
---@field def string :hi concrete definition after following any links
12-
local HighlightDisplay = {}
12+
local HighlightDisplay = Class:extend()
1313

14-
---@param group string nvim-tree highlight group name
15-
---@return HighlightDisplay
16-
function HighlightDisplay:new(group)
17-
local o = {}
18-
setmetatable(o, self)
19-
self.__index = self
14+
---@class HighlightDisplay
15+
---@overload fun(args: HighlightDisplayArgs): HighlightDisplay
16+
17+
---@class (exact) HighlightDisplayArgs
18+
---@field group string nvim-tree highlight group name
2019

21-
o.group = group
22-
local concrete = o.group
20+
---@protected
21+
---@param args HighlightDisplayArgs
22+
function HighlightDisplay:new(args)
23+
self.group = args.group
24+
25+
local concrete = self.group
2326

2427
-- maybe follow links
2528
local links = {}
26-
local link = vim.api.nvim_get_hl(0, { name = o.group }).link
29+
local link = vim.api.nvim_get_hl(0, { name = self.group }).link
2730
while link do
2831
table.insert(links, link)
2932
concrete = link
3033
link = vim.api.nvim_get_hl(0, { name = link }).link
3134
end
32-
o.links = table.concat(links, " ")
35+
self.links = table.concat(links, " ")
3336

3437
-- concrete definition
3538
local ok, res = pcall(vim.api.nvim_cmd, { cmd = "highlight", args = { concrete } }, { output = true })
3639
if ok and type(res) == "string" then
37-
o.def = res:gsub(".*xxx *", "")
40+
self.def = res:gsub(".*xxx *", "")
3841
else
39-
o.def = ""
42+
self.def = ""
4043
end
41-
42-
return o
4344
end
4445

4546
---Render one group.
@@ -87,7 +88,7 @@ end
8788

8889
---Run a test similar to :so $VIMRUNTIME/syntax/hitest.vim
8990
---Display all nvim-tree and neovim highlight groups, their link chain and actual definition
90-
function M.hi_test()
91+
return function()
9192
-- create a buffer
9293
local bufnr = vim.api.nvim_create_buf(false, true)
9394

@@ -96,7 +97,7 @@ function M.hi_test()
9697
-- nvim-tree groups, ordered
9798
local displays = {}
9899
for _, highlight_group in ipairs(appearance.HIGHLIGHT_GROUPS) do
99-
local display = HighlightDisplay:new(highlight_group.group)
100+
local display = HighlightDisplay({ group = highlight_group.group })
100101
table.insert(displays, display)
101102
end
102103
l = render_displays("nvim-tree", displays, bufnr, l)
@@ -110,7 +111,7 @@ function M.hi_test()
110111
if ok then
111112
for group in string.gmatch(out, "(%w*)%s+xxx") do
112113
if group:find("NvimTree", 1, true) ~= 1 then
113-
local display = HighlightDisplay:new(group)
114+
local display = HighlightDisplay({ group = group })
114115
if #display.group + #display.links > SHORT_LEN then
115116
table.insert(displays_long, display)
116117
else
@@ -137,5 +138,3 @@ function M.hi_test()
137138

138139
vim.cmd.buffer(bufnr)
139140
end
140-
141-
return M

lua/nvim-tree/class.lua

-47
This file was deleted.

0 commit comments

Comments
 (0)