Skip to content

refactor: tidy actions submodules and improve API readability #2593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ local renderer = require "nvim-tree.renderer"
local view = require "nvim-tree.view"
local commands = require "nvim-tree.commands"
local utils = require "nvim-tree.utils"
local change_dir = require "nvim-tree.actions.root.change-dir"
local actions = require "nvim-tree.actions"
local legacy = require "nvim-tree.legacy"
local core = require "nvim-tree.core"
local reloaders = require "nvim-tree.actions.reloaders.reloaders"
local git = require "nvim-tree.git"
local filters = require "nvim-tree.explorer.filters"
local modified = require "nvim-tree.modified"
local find_file = require "nvim-tree.actions.tree.find-file"
local events = require "nvim-tree.events"
local notify = require "nvim-tree.notify"

Expand Down Expand Up @@ -51,7 +49,7 @@ function M.change_root(path, bufnr)
-- test if in vim_cwd
if utils.path_relative(path, vim_cwd) ~= path then
if vim_cwd ~= cwd then
change_dir.fn(vim_cwd)
actions.root.change_dir.fn(vim_cwd)
end
return
end
Expand All @@ -62,19 +60,19 @@ function M.change_root(path, bufnr)

-- otherwise test M.init_root
if _config.prefer_startup_root and utils.path_relative(path, M.init_root) ~= path then
change_dir.fn(M.init_root)
actions.root.change_dir.fn(M.init_root)
return
end
-- otherwise root_dirs
for _, dir in pairs(_config.root_dirs) do
dir = vim.fn.fnamemodify(dir, ":p")
if utils.path_relative(path, dir) ~= path then
change_dir.fn(dir)
actions.root.change_dir.fn(dir)
return
end
end
-- finally fall back to the folder containing the file
change_dir.fn(vim.fn.fnamemodify(path, ":p:h"))
actions.root.change_dir.fn(vim.fn.fnamemodify(path, ":p:h"))
end

function M.tab_enter()
Expand All @@ -87,7 +85,7 @@ function M.tab_enter()
end
end
view.open { focus_tree = false }
require("nvim-tree.renderer").draw()
renderer.draw()
end
end

Expand All @@ -103,7 +101,7 @@ function M.open_on_directory()
return
end

change_dir.force_dirchange(bufname, true)
actions.root.change_dir.force_dirchange(bufname, true)
end

function M.reset_highlight()
Expand Down Expand Up @@ -154,11 +152,11 @@ end
---@param name string|nil
function M.change_dir(name)
if name then
change_dir.fn(name)
actions.root.change_dir.fn(name)
end

if _config.update_focused_file.enable then
find_file.fn()
actions.tree.find_file.fn()
end
end

Expand Down Expand Up @@ -191,7 +189,7 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufWritePost", {
callback = function()
if opts.auto_reload_on_write and not opts.filesystem_watchers.enable then
reloaders.reload_explorer()
actions.reloaders.reload_explorer()
end
end,
})
Expand All @@ -201,7 +199,7 @@ local function setup_autocommands(opts)
-- update opened file buffers
if (filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
reloaders.reload_explorer()
actions.reloaders.reload_explorer()
end)
end
end,
Expand All @@ -212,7 +210,7 @@ local function setup_autocommands(opts)
-- update opened file buffers
if (filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
reloaders.reload_explorer(nil, data.buf)
actions.reloaders.reload_explorer(nil, data.buf)
end)
end
end,
Expand All @@ -222,7 +220,7 @@ local function setup_autocommands(opts)
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
callback = function()
if not opts.filesystem_watchers.enable and opts.git.enable then
reloaders.reload_git()
actions.reloaders.reload_git()
end
end,
})
Expand Down Expand Up @@ -251,7 +249,7 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufEnter", {
callback = function()
utils.debounce("BufEnter:find_file", opts.view.debounce_delay, function()
find_file.fn()
actions.tree.find_file.fn()
end)
end,
})
Expand All @@ -266,7 +264,7 @@ local function setup_autocommands(opts)
callback = function()
if utils.is_nvim_tree_buf(0) then
if vim.fn.getcwd() ~= core.get_cwd() or (opts.reload_on_bufenter and not opts.filesystem_watchers.enable) then
reloaders.reload_explorer()
actions.reloaders.reload_explorer()
end
end
end,
Expand Down Expand Up @@ -317,7 +315,7 @@ local function setup_autocommands(opts)
callback = function()
utils.debounce("Buf:modified", opts.view.debounce_delay, function()
modified.reload()
reloaders.reload_explorer()
actions.reloaders.reload_explorer()
end)
end,
})
Expand Down
6 changes: 6 additions & 0 deletions lua/nvim-tree/actions/finders/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local M = {}

M.find_file = require "nvim-tree.actions.finders.find-file"
M.search_node = require "nvim-tree.actions.finders.search-node"

return M
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/fs/copy-paste.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local core = require "nvim-tree.core"
local events = require "nvim-tree.events"
local notify = require "nvim-tree.notify"
local renderer = require "nvim-tree.renderer"
local reloaders = require "nvim-tree.actions.reloaders.reloaders"
local reloaders = require "nvim-tree.actions.reloaders"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice


local HL_POSITION = require("nvim-tree.enum").HL_POSITION

Expand Down
16 changes: 16 additions & 0 deletions lua/nvim-tree/actions/fs/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local M = {}

M.copy_paste = require "nvim-tree.actions.fs.copy-paste"
M.create_file = require "nvim-tree.actions.fs.create-file"
M.remove_file = require "nvim-tree.actions.fs.remove-file"
M.rename_file = require "nvim-tree.actions.fs.rename-file"
M.trash = require "nvim-tree.actions.fs.trash"

function M.setup(opts)
M.copy_paste.setup(opts)
M.remove_file.setup(opts)
M.rename_file.setup(opts)
M.trash.setup(opts)
end

return M
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/fs/remove-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function M.fn(node)
local function do_remove()
M.remove(node)
if not M.config.filesystem_watchers.enable then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
require("nvim-tree.actions.reloaders").reload_explorer()
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function M.fn(default_modifier)

M.rename(node, prepend .. new_file_path .. append)
if not M.config.filesystem_watchers.enable then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
require("nvim-tree.actions.reloaders").reload_explorer()
end

find_file(utils.path_remove_trailing(new_file_path))
Expand Down
5 changes: 3 additions & 2 deletions lua/nvim-tree/actions/fs/trash.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local lib = require "nvim-tree.lib"
local notify = require "nvim-tree.notify"
local reloaders = require "nvim-tree.actions.reloaders"

local M = {
config = {},
Expand Down Expand Up @@ -59,7 +60,7 @@ function M.remove(node)
end
events._dispatch_folder_removed(node.absolute_path)
if not M.config.filesystem_watchers.enable then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
reloaders.reload_explorer()
end
end)
else
Expand All @@ -72,7 +73,7 @@ function M.remove(node)
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
if not M.config.filesystem_watchers.enable then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
reloaders.reload_explorer()
end
end)
end
Expand Down
24 changes: 12 additions & 12 deletions lua/nvim-tree/actions/init.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
local M = {}

M.finders = require "nvim-tree.actions.finders"
M.fs = require "nvim-tree.actions.fs"
M.moves = require "nvim-tree.actions.moves"
M.node = require "nvim-tree.actions.node"
M.reloaders = require "nvim-tree.actions.reloaders"
M.root = require "nvim-tree.actions.root"
M.tree = require "nvim-tree.actions.tree"

function M.setup(opts)
require("nvim-tree.actions.fs.trash").setup(opts)
require("nvim-tree.actions.node.system-open").setup(opts)
require("nvim-tree.actions.node.file-popup").setup(opts)
require("nvim-tree.actions.node.open-file").setup(opts)
require("nvim-tree.actions.root.change-dir").setup(opts)
require("nvim-tree.actions.fs.rename-file").setup(opts)
require("nvim-tree.actions.fs.remove-file").setup(opts)
require("nvim-tree.actions.fs.copy-paste").setup(opts)
require("nvim-tree.actions.tree-modifiers.expand-all").setup(opts)
require("nvim-tree.actions.tree.find-file").setup(opts)
require("nvim-tree.actions.tree.open").setup(opts)
require("nvim-tree.actions.tree.toggle").setup(opts)
M.fs.setup(opts)
M.node.setup(opts)
M.root.setup(opts)
M.tree.setup(opts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This @PostConstruct style is an unfortunate necessity for lua's "dependency injection".

I'll be mindful to add more of this as I touch files with odd instantiation mechanisms.

end

return M
7 changes: 7 additions & 0 deletions lua/nvim-tree/actions/moves/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local M = {}

M.item = require "nvim-tree.actions.moves.item"
M.parent = require "nvim-tree.actions.moves.parent"
M.sibling = require "nvim-tree.actions.moves.sibling"

return M
14 changes: 14 additions & 0 deletions lua/nvim-tree/actions/node/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local M = {}

M.file_popup = require "nvim-tree.actions.node.file-popup"
M.open_file = require "nvim-tree.actions.node.open-file"
M.run_command = require "nvim-tree.actions.node.run-command"
M.system_open = require "nvim-tree.actions.node.system-open"

function M.setup(opts)
require("nvim-tree.actions.node.system-open").setup(opts)
require("nvim-tree.actions.node.file-popup").setup(opts)
require("nvim-tree.actions.node.open-file").setup(opts)
end

return M
10 changes: 10 additions & 0 deletions lua/nvim-tree/actions/root/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local M = {}

M.change_dir = require "nvim-tree.actions.root.change-dir"
M.dir_up = require "nvim-tree.actions.root.dir-up"

function M.setup(opts)
M.change_dir.setup(opts)
end

return M
15 changes: 15 additions & 0 deletions lua/nvim-tree/actions/tree/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local M = {}

M.find_file = require "nvim-tree.actions.tree.find-file"
M.modifiers = require "nvim-tree.actions.tree.modifiers"
M.open = require "nvim-tree.actions.tree.open"
M.toggle = require "nvim-tree.actions.tree.toggle"

function M.setup(opts)
M.find_file.setup(opts)
M.modifiers.setup(opts)
M.open.setup(opts)
M.toggle.setup(opts)
end

return M
11 changes: 11 additions & 0 deletions lua/nvim-tree/actions/tree/modifiers/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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)
end

return M
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local lib = require "nvim-tree.lib"
local utils = require "nvim-tree.utils"
local filters = require "nvim-tree.explorer.filters"
local reloaders = require "nvim-tree.actions.reloaders.reloaders"
local reloaders = require "nvim-tree.actions.reloaders"

local M = {}

Expand Down
Loading