Skip to content

Commit

Permalink
chore: migrate to classic (#2991)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alex-courtis authored Nov 9, 2024
1 parent 610a1c1 commit 3fc8de1
Show file tree
Hide file tree
Showing 42 changed files with 1,028 additions and 1,148 deletions.
45 changes: 21 additions & 24 deletions lua/nvim-tree/actions/fs/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,39 @@ local notify = require("nvim-tree.notify")

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

local Class = require("nvim-tree.classic")
local DirectoryNode = require("nvim-tree.node.directory")

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

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

---@class Clipboard to handle all actions.fs clipboard API
---@field config table hydrated user opts.filters
---@class (exact) Clipboard: Class
---@field private explorer Explorer
---@field private data ClipboardData
---@field private clipboard_name string
---@field private reg string
local Clipboard = {}

---@param opts table user options
---@param explorer Explorer
---@return Clipboard
function Clipboard:new(opts, explorer)
---@type Clipboard
local o = {
explorer = explorer,
data = {
copy = {},
cut = {},
},
clipboard_name = opts.actions.use_system_clipboard and "system" or "neovim",
reg = opts.actions.use_system_clipboard and "+" or "1",
config = {
filesystem_watchers = opts.filesystem_watchers,
},
local Clipboard = Class:extend()

---@class Clipboard
---@overload fun(args: ClipboardArgs): Clipboard

---@class (exact) ClipboardArgs
---@field explorer Explorer

---@protected
---@param args ClipboardArgs
function Clipboard:new(args)
self.explorer = args.explorer

self.data = {
copy = {},
cut = {},
}

setmetatable(o, self)
self.__index = self
return o
self.clipboard_name = self.explorer.opts.actions.use_system_clipboard and "system" or "neovim"
self.reg = self.explorer.opts.actions.use_system_clipboard and "+" or "1"
end

---@param source string
Expand Down Expand Up @@ -252,7 +249,7 @@ function Clipboard:do_paste(node, action, action_fn)
end

self.data[action] = {}
if not self.config.filesystem_watchers.enable then
if not self.explorer.opts.filesystem_watchers.enable then
self.explorer:reload_explorer()
end
end
Expand Down
1 change: 0 additions & 1 deletion lua/nvim-tree/actions/tree/modifiers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local M = {}

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

function M.setup(opts)
M.expand_all.setup(opts)
Expand Down
73 changes: 0 additions & 73 deletions lua/nvim-tree/actions/tree/modifiers/toggles.lua

This file was deleted.

34 changes: 25 additions & 9 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local core = require("nvim-tree.core")
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils")
local actions = require("nvim-tree.actions")
local appearance_diagnostics = require("nvim-tree.appearance.diagnostics")
local appearance_hi_test = require("nvim-tree.appearance.hi-test")
local events = require("nvim-tree.events")
local help = require("nvim-tree.help")
local keymap = require("nvim-tree.keymap")
Expand Down Expand Up @@ -89,6 +89,22 @@ local function wrap_node_or_nil(fn)
end
end

---Invoke a member's method on the singleton explorer.
---Print error when setup not called.
---@param explorer_member string explorer member name
---@param member_method string method name to invoke on member
---@param ... any passed to method
---@return fun(...): any
local function wrap_explorer_member_args(explorer_member, member_method, ...)
local method_args = ...
return wrap(function(...)
local explorer = core.get_explorer()
if explorer then
return explorer[explorer_member][member_method](explorer[explorer_member], method_args, ...)
end
end)
end

---Invoke a member's method on the singleton explorer.
---Print error when setup not called.
---@param explorer_member string explorer member name
Expand Down Expand Up @@ -165,13 +181,13 @@ Api.tree.find_file = wrap(actions.tree.find_file.fn)
Api.tree.search_node = wrap(actions.finders.search_node.fn)
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse_all.fn)
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn)
Api.tree.toggle_enable_filters = wrap(actions.tree.modifiers.toggles.enable)
Api.tree.toggle_gitignore_filter = wrap(actions.tree.modifiers.toggles.git_ignored)
Api.tree.toggle_git_clean_filter = wrap(actions.tree.modifiers.toggles.git_clean)
Api.tree.toggle_no_buffer_filter = wrap(actions.tree.modifiers.toggles.no_buffer)
Api.tree.toggle_custom_filter = wrap(actions.tree.modifiers.toggles.custom)
Api.tree.toggle_hidden_filter = wrap(actions.tree.modifiers.toggles.dotfiles)
Api.tree.toggle_no_bookmark_filter = wrap(actions.tree.modifiers.toggles.no_bookmark)
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored")
Api.tree.toggle_git_clean_filter = wrap_explorer_member_args("filters", "toggle", "git_clean")
Api.tree.toggle_no_buffer_filter = wrap_explorer_member_args("filters", "toggle", "no_buffer")
Api.tree.toggle_custom_filter = wrap_explorer_member_args("filters", "toggle", "custom")
Api.tree.toggle_hidden_filter = wrap_explorer_member_args("filters", "toggle", "dotfiles")
Api.tree.toggle_no_bookmark_filter = wrap_explorer_member_args("filters", "toggle", "no_bookmark")
Api.tree.toggle_help = wrap(help.toggle)
Api.tree.is_tree_buf = wrap(utils.is_nvim_tree_buf)

Expand Down Expand Up @@ -289,7 +305,7 @@ Api.config.mappings.get_keymap = wrap(keymap.get_keymap)
Api.config.mappings.get_keymap_default = wrap(keymap.get_keymap_default)
Api.config.mappings.default_on_attach = keymap.default_on_attach

Api.diagnostics.hi_test = wrap(appearance_diagnostics.hi_test)
Api.diagnostics.hi_test = wrap(appearance_hi_test)

Api.commands.get = wrap(function()
return require("nvim-tree.commands").get()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
local appearance = require("nvim-tree.appearance")

local Class = require("nvim-tree.classic")

-- others with name and links less than this arbitrary value are short
local SHORT_LEN = 50

local M = {}

---@class HighlightDisplay for :NvimTreeHiTest
---@class (exact) HighlightDisplay: Class for :NvimTreeHiTest
---@field group string nvim-tree highlight group name
---@field links string link chain to a concretely defined group
---@field def string :hi concrete definition after following any links
local HighlightDisplay = {}
local HighlightDisplay = Class:extend()

---@param group string nvim-tree highlight group name
---@return HighlightDisplay
function HighlightDisplay:new(group)
local o = {}
setmetatable(o, self)
self.__index = self
---@class HighlightDisplay
---@overload fun(args: HighlightDisplayArgs): HighlightDisplay

---@class (exact) HighlightDisplayArgs
---@field group string nvim-tree highlight group name

o.group = group
local concrete = o.group
---@protected
---@param args HighlightDisplayArgs
function HighlightDisplay:new(args)
self.group = args.group

local concrete = self.group

-- maybe follow links
local links = {}
local link = vim.api.nvim_get_hl(0, { name = o.group }).link
local link = vim.api.nvim_get_hl(0, { name = self.group }).link
while link do
table.insert(links, link)
concrete = link
link = vim.api.nvim_get_hl(0, { name = link }).link
end
o.links = table.concat(links, " ")
self.links = table.concat(links, " ")

-- concrete definition
local ok, res = pcall(vim.api.nvim_cmd, { cmd = "highlight", args = { concrete } }, { output = true })
if ok and type(res) == "string" then
o.def = res:gsub(".*xxx *", "")
self.def = res:gsub(".*xxx *", "")
else
o.def = ""
self.def = ""
end

return o
end

---Render one group.
Expand Down Expand Up @@ -87,7 +88,7 @@ end

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

Expand All @@ -96,7 +97,7 @@ function M.hi_test()
-- nvim-tree groups, ordered
local displays = {}
for _, highlight_group in ipairs(appearance.HIGHLIGHT_GROUPS) do
local display = HighlightDisplay:new(highlight_group.group)
local display = HighlightDisplay({ group = highlight_group.group })
table.insert(displays, display)
end
l = render_displays("nvim-tree", displays, bufnr, l)
Expand All @@ -110,7 +111,7 @@ function M.hi_test()
if ok then
for group in string.gmatch(out, "(%w*)%s+xxx") do
if group:find("NvimTree", 1, true) ~= 1 then
local display = HighlightDisplay:new(group)
local display = HighlightDisplay({ group = group })
if #display.group + #display.links > SHORT_LEN then
table.insert(displays_long, display)
else
Expand All @@ -137,5 +138,3 @@ function M.hi_test()

vim.cmd.buffer(bufnr)
end

return M
47 changes: 0 additions & 47 deletions lua/nvim-tree/class.lua

This file was deleted.

Loading

0 comments on commit 3fc8de1

Please sign in to comment.