From 23c95a674f9cdf67d77ded959cd8c2dcd4cca930 Mon Sep 17 00:00:00 2001 From: kiyan Date: Sun, 6 Feb 2022 16:23:51 +0100 Subject: [PATCH] chore: move refresh/reloaders into actions.reloaders --- lua/nvim-tree.lua | 6 +-- lua/nvim-tree/actions/copy-paste.lua | 2 +- lua/nvim-tree/actions/create-file.lua | 2 +- lua/nvim-tree/actions/init.lua | 2 +- lua/nvim-tree/actions/reloaders.lua | 68 +++++++++++++++++++++++++++ lua/nvim-tree/actions/remove-file.lua | 3 +- lua/nvim-tree/actions/rename-file.lua | 2 +- lua/nvim-tree/actions/trash.lua | 5 +- lua/nvim-tree/lib.lua | 67 +++----------------------- 9 files changed, 84 insertions(+), 73 deletions(-) create mode 100644 lua/nvim-tree/actions/reloaders.lua diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index a0304e10..26020c90 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -239,7 +239,7 @@ local function setup_vim_commands() command! NvimTreeClose lua require'nvim-tree'.close() command! NvimTreeToggle lua require'nvim-tree'.toggle(false) command! NvimTreeFocus lua require'nvim-tree'.focus() - command! NvimTreeRefresh lua require'nvim-tree.lib'.refresh_tree() + command! NvimTreeRefresh lua require'nvim-tree.actions.reloaders'.reload_explorer() command! NvimTreeClipboard lua require'nvim-tree.actions.copy-paste'.print_clipboard() command! NvimTreeFindFile lua require'nvim-tree'.find_file(true) command! NvimTreeFindFileToggle lua require'nvim-tree'.toggle(true) @@ -261,8 +261,8 @@ local function setup_autocommands(opts) """ reset highlights when colorscheme is changed au ColorScheme * lua require'nvim-tree'.reset_highlight() - au BufWritePost * lua require'nvim-tree.lib'.refresh_tree() - au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree.lib'.reload_git() + au BufWritePost * lua require'nvim-tree.actions.reloaders'.reload_explorer() + au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree.actions.reloaders'.reload_git() ]] if opts.auto_close then diff --git a/lua/nvim-tree/actions/copy-paste.lua b/lua/nvim-tree/actions/copy-paste.lua index 56edacee..5c5dba67 100644 --- a/lua/nvim-tree/actions/copy-paste.lua +++ b/lua/nvim-tree/actions/copy-paste.lua @@ -108,7 +108,7 @@ local function do_paste(node, action_type, action_fn) end clipboard[action_type] = {} - return lib.refresh_tree() + return require'nvim-tree.actions.reloaders'.reload_explorer() end local function do_cut(source, destination) diff --git a/lua/nvim-tree/actions/create-file.lua b/lua/nvim-tree/actions/create-file.lua index 1494aec0..3f0dd4f4 100644 --- a/lua/nvim-tree/actions/create-file.lua +++ b/lua/nvim-tree/actions/create-file.lua @@ -102,7 +102,7 @@ function M.fn(node) a.nvim_out_write(file..' was properly created\n') end events._dispatch_folder_created(file) - lib.refresh_tree(function() + require'nvim-tree.actions.reloaders'.reload_explorer(function() focus_file(file) end) end diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index 5fa20aa4..cb42398d 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -69,7 +69,7 @@ local keypress_funcs = { toggle_ignored = lib.toggle_ignored, toggle_dotfiles = lib.toggle_dotfiles, toggle_help = lib.toggle_help, - refresh = lib.refresh_tree, + refresh = require'nvim-tree.actions.reloaders'.reload_explorer, first_sibling = require'nvim-tree.actions.movements'.sibling(-math.huge), last_sibling = require'nvim-tree.actions.movements'.sibling(math.huge), prev_sibling = require'nvim-tree.actions.movements'.sibling(-1), diff --git a/lua/nvim-tree/actions/reloaders.lua b/lua/nvim-tree/actions/reloaders.lua new file mode 100644 index 00000000..69763cf0 --- /dev/null +++ b/lua/nvim-tree/actions/reloaders.lua @@ -0,0 +1,68 @@ +local git = require "nvim-tree.git" +local diagnostics = require "nvim-tree.diagnostics" +local view = require "nvim-tree.view" +local explorer_module = require'nvim-tree.explorer' +local get_explorer = function() return require "nvim-tree.lib".Tree end + +local M = {} + +local function refresh_nodes(node, projects) + local project_root = git.get_project_root(node.absolute_path or node.cwd) + explorer_module.refresh(node.nodes, node.absolute_path or node.cwd, node, projects[project_root] or {}) + for _, _node in ipairs(node.nodes) do + if _node.nodes and _node.open then + refresh_nodes(_node, projects) + end + end +end + +function M.reload_node_status(parent_node, projects) + local project_root = git.get_project_root(parent_node.absolute_path or parent_node.cwd) + local status = projects[project_root] or {} + for _, node in ipairs(parent_node.nodes) do + if node.nodes then + node.git_status = status.dirs and status.dirs[node.absolute_path] + else + node.git_status = status.files and status.files[node.absolute_path] + end + if node.nodes and #node.nodes > 0 then + M.reload_node_status(node, projects) + end + end +end + +local event_running = false +function M.reload_explorer(callback) + local Explorer = get_explorer() + if event_running or not Explorer.cwd or vim.v.exiting ~= vim.NIL then + return + end + event_running = true + + git.reload(function(projects) + refresh_nodes(Explorer, projects) + if view.win_open() then + require"nvim-tree.lib".redraw() + if callback and type(callback) == 'function' then + callback() + end + end + diagnostics.update() + event_running = false + end) +end + +function M.reload_git() + if not git.config.enable or event_running then + return + end + event_running = true + + git.reload(function(projects) + M.reload_node_status(get_explorer(), projects) + require"nvim-tree.lib".redraw() + event_running = false + end) +end + +return M diff --git a/lua/nvim-tree/actions/remove-file.lua b/lua/nvim-tree/actions/remove-file.lua index d93b1757..d1a18211 100644 --- a/lua/nvim-tree/actions/remove-file.lua +++ b/lua/nvim-tree/actions/remove-file.lua @@ -2,7 +2,6 @@ local a = vim.api local luv = vim.loop local utils = require'nvim-tree.utils' -local lib = require'nvim-tree.lib' local events = require'nvim-tree.events' local M = {} @@ -72,7 +71,7 @@ function M.fn(node) events._dispatch_file_removed(node.absolute_path) clear_buffer(node.absolute_path) end - lib.refresh_tree() + require'nvim-tree.actions.reloaders'.reload_explorer() end end diff --git a/lua/nvim-tree/actions/rename-file.lua b/lua/nvim-tree/actions/rename-file.lua index 1d72d55e..04acd5b5 100644 --- a/lua/nvim-tree/actions/rename-file.lua +++ b/lua/nvim-tree/actions/rename-file.lua @@ -31,7 +31,7 @@ function M.fn(with_sub) a.nvim_out_write(node.absolute_path..' ➜ '..new_name..'\n') utils.rename_loaded_buffers(node.absolute_path, new_name) events._dispatch_node_renamed(abs_path, new_name) - lib.refresh_tree() + require'nvim-tree.actions.reloaders'.reload_explorer() end end diff --git a/lua/nvim-tree/actions/trash.lua b/lua/nvim-tree/actions/trash.lua index f0bd5020..602fc838 100644 --- a/lua/nvim-tree/actions/trash.lua +++ b/lua/nvim-tree/actions/trash.lua @@ -8,7 +8,6 @@ local M = { } } -local lib = require'nvim-tree.lib' local utils = require'nvim-tree.utils' local events = require'nvim-tree.events' @@ -64,13 +63,13 @@ function M.fn(node) if node.nodes ~= nil and not node.link_to then trash_path(function() events._dispatch_folder_removed(node.absolute_path) - lib.refresh_tree() + require'nvim-tree.actions.reloaders'.reload_explorer() end) else trash_path(function() events._dispatch_file_removed(node.absolute_path) clear_buffer(node.absolute_path) - lib.refresh_tree() + require'nvim-tree.actions.reloaders'.reload_explorer() end) end diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 1f41a445..df46f3a9 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -112,64 +112,6 @@ function M.expand_or_collapse(node) diagnostics.update() end -local function refresh_nodes(node, projects) - local project_root = git.get_project_root(node.absolute_path or node.cwd) - explorer.refresh(node.nodes, node.absolute_path or node.cwd, node, projects[project_root] or {}) - for _, _node in ipairs(node.nodes) do - if _node.nodes and _node.open then - refresh_nodes(_node, projects) - end - end -end - -local event_running = false -function M.refresh_tree(callback) - if event_running or not M.Tree.cwd or vim.v.exiting ~= vim.NIL then - return - end - event_running = true - - git.reload(function(projects) - refresh_nodes(M.Tree, projects) - if view.win_open() then - M.redraw() - if callback and type(callback) == 'function' then - callback() - end - end - diagnostics.update() - event_running = false - end) -end - -local function reload_node_status(parent_node, projects) - local project_root = git.get_project_root(parent_node.absolute_path or parent_node.cwd) - local status = projects[project_root] or {} - for _, node in ipairs(parent_node.nodes) do - if node.nodes then - node.git_status = status.dirs and status.dirs[node.absolute_path] - else - node.git_status = status.files and status.files[node.absolute_path] - end - if node.nodes and #node.nodes > 0 then - reload_node_status(node, projects) - end - end -end - -function M.reload_git() - if not git.config.enable or event_running then - return - end - event_running = true - - git.reload(function(projects) - reload_node_status(M.Tree, projects) - M.redraw() - event_running = false - end) -end - function M.set_index_and_redraw(fname) local i local hide_root_folder = view.View.hide_root_folder @@ -195,7 +137,7 @@ function M.set_index_and_redraw(fname) explorer.explore(node.nodes, node.absolute_path, node, {}) git.load_project_status(node.absolute_path, function(status) if status.dirs or status.files then - reload_node_status(node, git.projects) + require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects) end M.redraw() end) @@ -254,12 +196,12 @@ end function M.toggle_ignored() explorer.config.filter_ignored = not explorer.config.filter_ignored - return M.refresh_tree() + return require'nvim-tree.actions.reloaders'.reload_explorer() end function M.toggle_dotfiles() explorer.config.filter_dotfiles = not explorer.config.filter_dotfiles - return M.refresh_tree() + return require'nvim-tree.actions.reloaders'.reload_explorer() end function M.toggle_help() @@ -273,5 +215,8 @@ M.collapse_all = require'nvim-tree.actions.collapse-all'.fn M.dir_up = require'nvim-tree.actions.dir-up'.fn -- @deprecated: use nvim-tree.actions.change-dir.fn M.change_dir = require'nvim-tree.actions.change-dir'.fn +-- @deprecated: use nvim-tree.actions.reloaders.reload_explorer +M.refresh_tree = require'nvim-tree.actions.reloaders'.reload_explorer + return M