From ea92e7bf7ccd1815b60342706356c373bb7df216 Mon Sep 17 00:00:00 2001 From: kiyan Date: Mon, 7 Feb 2022 22:07:08 +0100 Subject: [PATCH] refacto: set tree explorer in the global state also remove the redraw method and use renderer.draw immediately --- .luacheckrc | 1 + lua/nvim-tree.lua | 4 ++-- lua/nvim-tree/actions/change-dir.lua | 4 ++-- lua/nvim-tree/actions/collapse-all.lua | 6 ++++-- lua/nvim-tree/actions/copy-paste.lua | 2 +- lua/nvim-tree/actions/create-file.lua | 6 +++--- lua/nvim-tree/actions/dir-up.lua | 2 +- lua/nvim-tree/actions/find-file.lua | 16 ++++++--------- lua/nvim-tree/actions/init.lua | 6 +++--- lua/nvim-tree/actions/movements.lua | 13 +++++++------ lua/nvim-tree/actions/reloaders.lua | 16 ++++++--------- lua/nvim-tree/actions/toggle-help.lua | 8 -------- lua/nvim-tree/actions/toggle-ignore.lua | 15 -------------- lua/nvim-tree/actions/toggles.lua | 23 ++++++++++++++++++++++ lua/nvim-tree/diagnostics.lua | 3 +-- lua/nvim-tree/explorer/init.lua | 3 ++- lua/nvim-tree/lib.lua | 26 ++++++++++++------------- lua/nvim-tree/renderer/init.lua | 24 +++++++++++------------ 18 files changed, 85 insertions(+), 93 deletions(-) delete mode 100644 lua/nvim-tree/actions/toggle-help.lua delete mode 100644 lua/nvim-tree/actions/toggle-ignore.lua create mode 100644 lua/nvim-tree/actions/toggles.lua diff --git a/.luacheckrc b/.luacheckrc index d37661a7..1719373f 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -10,4 +10,5 @@ ignore = { -- Global objects defined by the C code globals = { "vim", + "TreeExplorer" } diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 3ecf4a4b..8948f485 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -131,7 +131,7 @@ local function update_base_dir_with_filepath(filepath, bufnr) end end - if not vim.startswith(filepath, lib.Tree.cwd or vim.loop.cwd()) then + if not vim.startswith(filepath, TreeExplorer.cwd or vim.loop.cwd()) then ChangeDir.fn(vim.fn.fnamemodify(filepath, ':p:h')) end end @@ -190,7 +190,7 @@ function M.open_on_directory() end view.close() - if bufname ~= lib.Tree.cwd then + if bufname ~= TreeExplorer.cwd then ChangeDir.fn(bufname) end M.hijack_current_window() diff --git a/lua/nvim-tree/actions/change-dir.lua b/lua/nvim-tree/actions/change-dir.lua index cf82759c..3ade0d76 100644 --- a/lua/nvim-tree/actions/change-dir.lua +++ b/lua/nvim-tree/actions/change-dir.lua @@ -9,8 +9,8 @@ local M = { } function M.fn(name) - local foldername = name == '..' and vim.fn.fnamemodify(lib().Tree.cwd, ':h') or name - local no_cwd_change = vim.fn.expand(foldername) == lib().Tree.cwd + local foldername = name == '..' and vim.fn.fnamemodify(TreeExplorer.cwd, ':h') or name + local no_cwd_change = vim.fn.expand(foldername) == TreeExplorer.cwd local new_tab = a.nvim_get_current_tabpage() local is_window = vim.v.event.scope == "window" and new_tab == M.current_tab if no_cwd_change or is_window then diff --git a/lua/nvim-tree/actions/collapse-all.lua b/lua/nvim-tree/actions/collapse-all.lua index f8855981..d0b951dc 100644 --- a/lua/nvim-tree/actions/collapse-all.lua +++ b/lua/nvim-tree/actions/collapse-all.lua @@ -1,3 +1,5 @@ +local renderer = require"nvim-tree.renderer" + local M = {} function M.fn() @@ -12,8 +14,8 @@ function M.fn() end end - iter(require'nvim-tree.lib'.Tree.nodes) - require'nvim-tree.lib'.redraw() + iter(TreeExplorer.nodes) + renderer.draw() end return M diff --git a/lua/nvim-tree/actions/copy-paste.lua b/lua/nvim-tree/actions/copy-paste.lua index 5c5dba67..2202b7b1 100644 --- a/lua/nvim-tree/actions/copy-paste.lua +++ b/lua/nvim-tree/actions/copy-paste.lua @@ -158,7 +158,7 @@ end function M.copy_path(node) local absolute_path = node.absolute_path - local relative_path = utils.path_relative(absolute_path, lib.Tree.cwd) + local relative_path = utils.path_relative(absolute_path, TreeExplorer.cwd) local content = node.nodes ~= nil and utils.path_add_trailing(relative_path) or relative_path return copy_to_clipboard(content) end diff --git a/lua/nvim-tree/actions/create-file.lua b/lua/nvim-tree/actions/create-file.lua index 3f0dd4f4..07648dc4 100644 --- a/lua/nvim-tree/actions/create-file.lua +++ b/lua/nvim-tree/actions/create-file.lua @@ -9,7 +9,7 @@ local M = {} local function focus_file(file) local _, i = utils.find_node( - lib.Tree.nodes, + TreeExplorer.nodes, function(node) return node.absolute_path == file end ) require'nvim-tree.view'.set_cursor({i+1, 1}) @@ -61,8 +61,8 @@ function M.fn(node) node = lib.get_last_group_node(node) if node.name == '..' then node = { - absolute_path = lib.Tree.cwd, - nodes = lib.Tree.nodes, + absolute_path = TreeExplorer.cwd, + nodes = TreeExplorer.nodes, open = true, } end diff --git a/lua/nvim-tree/actions/dir-up.lua b/lua/nvim-tree/actions/dir-up.lua index 73fe6c0a..00fd428b 100644 --- a/lua/nvim-tree/actions/dir-up.lua +++ b/lua/nvim-tree/actions/dir-up.lua @@ -4,7 +4,7 @@ function M.fn(node) if not node or node.name == ".." then return require'nvim-tree.actions.change-dir'.fn('..') else - local newdir = vim.fn.fnamemodify(require'nvim-tree.lib'.Tree.cwd, ':h') + local newdir = vim.fn.fnamemodify(TreeExplorer.cwd, ':h') require'nvim-tree.actions.change-dir'.fn(newdir) return require"nvim-tree.actions.find-file".fn(node.absolute_path) end diff --git a/lua/nvim-tree/actions/find-file.lua b/lua/nvim-tree/actions/find-file.lua index 39f7c747..c34d3ecd 100644 --- a/lua/nvim-tree/actions/find-file.lua +++ b/lua/nvim-tree/actions/find-file.lua @@ -2,21 +2,17 @@ local view = require'nvim-tree.view' local utils = require'nvim-tree.utils' local explorer_module = require"nvim-tree.explorer" local git = require"nvim-tree.git" +local renderer = require"nvim-tree.renderer" local M = {} -local function get_explorer() - return require"nvim-tree.lib".Tree -end - function M.fn(fname) local i local hide_root_folder = view.View.hide_root_folder - local Explorer = get_explorer() - if not Explorer then + if not TreeExplorer then return end - if Explorer.cwd == '/' or hide_root_folder then + if TreeExplorer.cwd == '/' or hide_root_folder then i = 0 else i = 1 @@ -40,7 +36,7 @@ function M.fn(fname) if status.dirs or status.files then require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects) end - require"nvim-tree.lib".redraw() + renderer.draw() end) end if node.open == false then @@ -56,9 +52,9 @@ function M.fn(fname) end end - local index = iterate_nodes(Explorer.nodes) + local index = iterate_nodes(TreeExplorer.nodes) if tree_altered then - require"nvim-tree.lib".redraw() + renderer.draw() end if index and view.win_open() then view.set_cursor({index, 0}) diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index cb2220f4..9d0a2213 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -66,9 +66,9 @@ local keypress_funcs = { remove = require'nvim-tree.actions.remove-file'.fn, rename = require'nvim-tree.actions.rename-file'.fn(false), system_open = require'nvim-tree.actions.system-open'.fn, - toggle_dotfiles = require"nvim-tree.actions.toggle-ignore".dotfiles, - toggle_help = require"nvim-tree.actions.toggle-help".fn, - toggle_ignored = require"nvim-tree.actions.toggle-ignore".ignored, + toggle_dotfiles = require"nvim-tree.actions.toggles".dotfiles, + toggle_help = require"nvim-tree.actions.toggles".help, + toggle_ignored = require"nvim-tree.actions.toggles".ignored, trash = require'nvim-tree.actions.trash'.fn, } diff --git a/lua/nvim-tree/actions/movements.lua b/lua/nvim-tree/actions/movements.lua index eee508a9..79699ce1 100644 --- a/lua/nvim-tree/actions/movements.lua +++ b/lua/nvim-tree/actions/movements.lua @@ -2,6 +2,7 @@ local utils = require'nvim-tree.utils' local view = require'nvim-tree.view' local diagnostics = require'nvim-tree.diagnostics' local config = require"nvim-tree.config" +local renderer = require"nvim-tree.renderer" local lib = function() return require'nvim-tree.lib' end local M = {} @@ -44,7 +45,7 @@ function M.parent_node(should_close) node.open = false altered_tree = true else - local line, parent = iter(lib().Tree.nodes, true) + local line, parent = iter(TreeExplorer.nodes, true) if parent == nil then line = 1 elseif should_close then @@ -57,7 +58,7 @@ function M.parent_node(should_close) if altered_tree then diagnostics.update() - lib().redraw() + renderer.draw() end end end @@ -73,16 +74,16 @@ function M.sibling(direction) local parent, _ -- Check if current node is already at root nodes - for index, _node in ipairs(lib().Tree.nodes) do + for index, _node in ipairs(TreeExplorer.nodes) do if node_path == _node.absolute_path then line = index end end if line > 0 then - parent = lib().Tree + parent = TreeExplorer else - _, parent = iter(lib().Tree.nodes, true) + _, parent = iter(TreeExplorer.nodes, true) if parent ~= nil and #parent.nodes > 1 then line, _ = get_line_from_node(node)(parent.nodes) end @@ -99,7 +100,7 @@ function M.sibling(direction) end local target_node = parent.nodes[index] - line, _ = get_line_from_node(target_node)(lib().Tree.nodes, true) + line, _ = get_line_from_node(target_node)(TreeExplorer.nodes, true) view.set_cursor({line, 0}) end end diff --git a/lua/nvim-tree/actions/reloaders.lua b/lua/nvim-tree/actions/reloaders.lua index 82d0a8db..8f928a6a 100644 --- a/lua/nvim-tree/actions/reloaders.lua +++ b/lua/nvim-tree/actions/reloaders.lua @@ -1,14 +1,11 @@ local git = require "nvim-tree.git" local diagnostics = require "nvim-tree.diagnostics" local view = require "nvim-tree.view" +local renderer = require "nvim-tree.renderer" local explorer_module = require'nvim-tree.explorer' local M = {} -local function get_explorer() - return require "nvim-tree.lib".Tree -end - local function refresh_nodes(node, projects) local project_root = git.get_project_root(node.absolute_path or node.cwd) explorer_module.reload(node, node.absolute_path or node.cwd, projects[project_root] or {}) @@ -36,16 +33,15 @@ 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 + if event_running or not TreeExplorer.cwd or vim.v.exiting ~= vim.NIL then return end event_running = true git.reload(function(projects) - refresh_nodes(Explorer, projects) + refresh_nodes(TreeExplorer, projects) if view.win_open() then - require"nvim-tree.lib".redraw() + renderer.draw() if callback and type(callback) == 'function' then callback() end @@ -62,8 +58,8 @@ function M.reload_git() event_running = true git.reload(function(projects) - M.reload_node_status(get_explorer(), projects) - require"nvim-tree.lib".redraw() + M.reload_node_status(TreeExplorer, projects) + renderer.draw() event_running = false end) end diff --git a/lua/nvim-tree/actions/toggle-help.lua b/lua/nvim-tree/actions/toggle-help.lua deleted file mode 100644 index 4cef663d..00000000 --- a/lua/nvim-tree/actions/toggle-help.lua +++ /dev/null @@ -1,8 +0,0 @@ -local M = {} - -function M.fn() - require"nvim-tree.view".toggle_help() - return require"nvim-tree.lib".redraw() -end - -return M diff --git a/lua/nvim-tree/actions/toggle-ignore.lua b/lua/nvim-tree/actions/toggle-ignore.lua deleted file mode 100644 index 13927986..00000000 --- a/lua/nvim-tree/actions/toggle-ignore.lua +++ /dev/null @@ -1,15 +0,0 @@ -local M = {} - -function M.ignored() - local config = require"nvim-tree.explorer.utils".config - config.filter_ignored = not config.filter_ignored - return require'nvim-tree.actions.reloaders'.reload_explorer() -end - -function M.dotfiles() - local config = require"nvim-tree.explorer.utils".config - config.filter_dotfiles = not config.filter_dotfiles - return require'nvim-tree.actions.reloaders'.reload_explorer() -end - -return M diff --git a/lua/nvim-tree/actions/toggles.lua b/lua/nvim-tree/actions/toggles.lua new file mode 100644 index 00000000..adf9c021 --- /dev/null +++ b/lua/nvim-tree/actions/toggles.lua @@ -0,0 +1,23 @@ +local view = require"nvim-tree.view" +local eutils = require"nvim-tree.explorer.utils" +local renderer = require"nvim-tree.renderer" +local reloaders = require"nvim-tree.actions.reloaders" + +local M = {} + +function M.ignored() + eutils.config.filter_ignored = not eutils.config.filter_ignored + return reloaders.reload_explorer() +end + +function M.dotfiles() + eutils.config.filter_dotfiles = not eutils.config.filter_dotfiles + return reloaders.reload_explorer() +end + +function M.help() + view.toggle_help() + return renderer.draw() +end + +return M diff --git a/lua/nvim-tree/diagnostics.lua b/lua/nvim-tree/diagnostics.lua index 8267c3d1..ca25e3b1 100644 --- a/lua/nvim-tree/diagnostics.lua +++ b/lua/nvim-tree/diagnostics.lua @@ -111,7 +111,6 @@ function M.update() buffer_severity = from_nvim_lsp() end - local nodes = require'nvim-tree.lib'.Tree.nodes if #signs then vim.fn.sign_unplacelist(vim.tbl_map(function(sign) return { @@ -124,7 +123,7 @@ function M.update() end for bufname, severity in pairs(buffer_severity) do if 0 < severity and severity < 5 then - local node, line = utils.find_node(nodes, function(node) + local node, line = utils.find_node(TreeExplorer.nodes, function(node) if M.show_on_dirs and not node.open then return vim.startswith(bufname, node.absolute_path) else diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index f65b32fa..2a90030b 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -1,6 +1,7 @@ local uv = vim.loop local git = require"nvim-tree.git" +local renderer = require"nvim-tree.renderer" local M = {} @@ -29,7 +30,7 @@ function Explorer:_load(cwd, node) end function Explorer:expand(node) - self.init_cb = require"nvim-tree.lib".redraw + self.init_cb = renderer.draw self:_load(node.link_to or node.absolute_path, node) end diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index a2d162f1..89ba23f3 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -12,10 +12,12 @@ local M = { target_winid = nil, } +TreeExplorer = nil + function M.init(with_open, foldername) - M.Tree = explorer.Explorer.new(foldername) - M.Tree:init(function() - M.redraw() + TreeExplorer = explorer.Explorer.new(foldername) + TreeExplorer:init(function() + renderer.draw() if with_open then M.open() end @@ -27,10 +29,6 @@ function M.init(with_open, foldername) end) end -function M.redraw() - renderer.draw(M.Tree, true) -end - local function get_node_at_line(line) local index = view.View.hide_root_folder and 1 or 2 local function iter(nodes) @@ -61,14 +59,14 @@ function M.get_node_at_cursor() local help_text = get_node_at_line(line+1)(help_lines) return {name = help_text} else - if line == 1 and M.Tree.cwd ~= "/" and not hide_root_folder then + if line == 1 and TreeExplorer.cwd ~= "/" and not hide_root_folder then return { name = ".." } end - if M.Tree.cwd == "/" then + if TreeExplorer.cwd == "/" then line = line + 1 end - return get_node_at_line(line)(M.Tree.nodes) + return get_node_at_line(line)(TreeExplorer.nodes) end end @@ -85,9 +83,9 @@ function M.expand_or_collapse(node) node.open = not node.open if node.has_children then node.has_children = false end if #node.nodes == 0 then - M.Tree:expand(node) + TreeExplorer:expand(node) else - M.redraw() + renderer.draw() end diagnostics.update() @@ -111,11 +109,11 @@ function M.open() local should_redraw = view.open() local respect_buf_cwd = vim.g.nvim_tree_respect_buf_cwd or 0 - if respect_buf_cwd == 1 and cwd ~= M.Tree.cwd then + if respect_buf_cwd == 1 and cwd ~= TreeExplorer.cwd then require'nvim-tree.actions.change-dir'.fn(cwd) end if should_redraw then - M.redraw() + renderer.draw() end end diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index d20789ea..e9fe576d 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -370,25 +370,23 @@ end local M = {} -function M.draw(tree, reload) +function M.draw() if not api.nvim_buf_is_loaded(view.View.bufnr) then return end local cursor if view.win_open() then cursor = api.nvim_win_get_cursor(view.get_winnr()) end - if reload then - index = 0 - lines = {} - hl = {} + index = 0 + lines = {} + hl = {} - local show_arrows = - vim.g.nvim_tree_indent_markers ~= 1 - and icon_state.show_folder_icon - and icon_state.show_folder_arrows - _padding.reload_padding_function() - icon_state = config.get_icon_state() - update_draw_data(tree, show_arrows and 2 or 0, {}) - end + local show_arrows = + vim.g.nvim_tree_indent_markers ~= 1 + and icon_state.show_folder_icon + and icon_state.show_folder_arrows + _padding.reload_padding_function() + icon_state = config.get_icon_state() + update_draw_data(TreeExplorer, show_arrows and 2 or 0, {}) if view.is_help_ui() then lines, hl = _help.compute_lines()