refacto: abstract TreeExplorer in core.lua

This commit is contained in:
kiyan
2022-03-09 22:01:54 +01:00
parent d2b12d6055
commit 471afc13fe
17 changed files with 94 additions and 65 deletions

View File

@@ -1,5 +1,7 @@
local a = vim.api
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local M = {
current_tab = a.nvim_get_current_tabpage(),
@@ -10,12 +12,12 @@ local M = {
}
function M.fn(name, with_open)
if not TreeExplorer then
if not core.get_explorer() then
return
end
local foldername = name == ".." and vim.fn.fnamemodify(utils.path_remove_trailing(TreeExplorer.cwd), ":h") or name
local no_cwd_change = vim.fn.expand(foldername) == TreeExplorer.cwd
local foldername = name == ".." and vim.fn.fnamemodify(utils.path_remove_trailing(core.get_cwd()), ":h") or name
local no_cwd_change = vim.fn.expand(foldername) == core.get_cwd()
local new_tab = a.nvim_get_current_tabpage()
local is_window = (vim.v.event.scope == "window" or vim.v.event.changed_window) and new_tab == M.current_tab
if no_cwd_change or is_window then
@@ -33,7 +35,7 @@ function M.force_dirchange(foldername, with_open)
vim.cmd("lcd " .. vim.fn.fnameescape(foldername))
end
end
require("nvim-tree.lib").init(foldername)
require("nvim-tree.core").init(foldername)
if with_open then
require("nvim-tree.lib").open()
else

View File

@@ -1,5 +1,6 @@
local renderer = require "nvim-tree.renderer"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local M = {}
@@ -31,7 +32,7 @@ function M.fn(keep_buffers)
end
end
iter(TreeExplorer.nodes)
iter(core.get_explorer().nodes)
renderer.draw()
end

View File

@@ -3,6 +3,7 @@ local uv = vim.loop
local lib = require "nvim-tree.lib"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local M = {}
@@ -168,7 +169,7 @@ end
function M.copy_path(node)
local absolute_path = node.absolute_path
local relative_path = utils.path_relative(absolute_path, TreeExplorer.cwd)
local relative_path = utils.path_relative(absolute_path, core.get_cwd())
local content = node.nodes ~= nil and utils.path_add_trailing(relative_path) or relative_path
return copy_to_clipboard(content)
end

View File

@@ -4,11 +4,12 @@ local uv = vim.loop
local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
local lib = require "nvim-tree.lib"
local core = require "nvim-tree.core"
local M = {}
local function focus_file(file)
local _, i = utils.find_node(TreeExplorer.nodes, function(node)
local _, i = utils.find_node(core.get_explorer().nodes, function(node)
return node.absolute_path == file
end)
require("nvim-tree.view").set_cursor { i + 1, 1 }
@@ -62,8 +63,8 @@ function M.fn(node)
node = lib.get_last_group_node(node)
if node.name == ".." then
node = {
absolute_path = TreeExplorer.cwd,
nodes = TreeExplorer.nodes,
absolute_path = core.get_cwd(),
nodes = core.get_explorer().nodes,
open = true,
}
end

View File

@@ -1,4 +1,5 @@
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local M = {}
@@ -6,7 +7,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(utils.path_remove_trailing(TreeExplorer.cwd), ":h")
local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(core.get_cwd()), ":h")
require("nvim-tree.actions.change-dir").fn(newdir)
return require("nvim-tree.actions.find-file").fn(node.absolute_path)
end

View File

@@ -1,13 +1,14 @@
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local renderer = require "nvim-tree.renderer"
local core = require "nvim-tree.core"
local M = {}
local running = {}
function M.fn(fname)
if running[fname] or not TreeExplorer then
if running[fname] or not core.get_explorer() then
return
end
running[fname] = true
@@ -30,7 +31,7 @@ function M.fn(fname)
end
if #node.nodes == 0 then
TreeExplorer:expand(node)
core.get_explorer():expand(node)
end
if iterate_nodes(node.nodes) ~= nil then
@@ -43,7 +44,7 @@ function M.fn(fname)
end
end
local index = iterate_nodes(TreeExplorer.nodes)
local index = iterate_nodes(core.get_explorer().nodes)
if tree_altered then
renderer.draw()
end

View File

@@ -2,6 +2,8 @@ local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local diagnostics = require "nvim-tree.diagnostics"
local renderer = require "nvim-tree.renderer"
local core = require "nvim-tree.core"
local lib = function()
return require "nvim-tree.lib"
end
@@ -49,7 +51,7 @@ function M.parent_node(should_close)
node.open = false
altered_tree = true
else
local line, parent = iter(TreeExplorer.nodes, true)
local line, parent = iter(core.get_explorer().nodes, true)
if parent == nil then
line = 1
elseif should_close then
@@ -82,16 +84,16 @@ function M.sibling(direction)
local parent, _
-- Check if current node is already at root nodes
for index, _node in ipairs(TreeExplorer.nodes) do
for index, _node in ipairs(core.get_explorer().nodes) do
if node_path == _node.absolute_path then
line = index
end
end
if line > 0 then
parent = TreeExplorer
parent = core.get_explorer()
else
_, parent = iter(TreeExplorer.nodes, true)
_, parent = iter(core.get_explorer().nodes, true)
if parent ~= nil and #parent.nodes > 1 then
line, _ = get_line_from_node(node)(parent.nodes)
end
@@ -108,7 +110,7 @@ function M.sibling(direction)
end
local target_node = parent.nodes[index]
line, _ = get_line_from_node(target_node)(TreeExplorer.nodes, true)
line, _ = get_line_from_node(target_node)(core.get_explorer().nodes, true)
if not view.is_root_folder_visible() then
line = line - 1
end
@@ -119,7 +121,7 @@ end
function M.find_git_item(where)
return function()
local node_cur = lib().get_node_at_cursor()
local nodes_by_line = lib().get_nodes_by_line(TreeExplorer.nodes, view.View.hide_root_folder and 1 or 2)
local nodes_by_line = lib().get_nodes_by_line(core.get_explorer().nodes, view.View.hide_root_folder and 1 or 2)
local cur, first, prev, nex = nil, nil, nil, nil
for line, node in pairs(nodes_by_line) do

View File

@@ -3,6 +3,7 @@ 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 core = require "nvim-tree.core"
local M = {}
@@ -34,13 +35,13 @@ end
local event_running = false
function M.reload_explorer()
if event_running or not TreeExplorer or not TreeExplorer.cwd or vim.v.exiting ~= vim.NIL then
if event_running or not core.get_explorer() or vim.v.exiting ~= vim.NIL then
return
end
event_running = true
local projects = git.reload()
refresh_nodes(TreeExplorer, projects)
refresh_nodes(core.get_explorer(), projects)
if view.is_visible() then
renderer.draw()
end
@@ -49,13 +50,13 @@ function M.reload_explorer()
end
function M.reload_git()
if not TreeExplorer or not git.config.enable or event_running then
if not core.get_explorer() or not git.config.enable or event_running then
return
end
event_running = true
local projects = git.reload()
M.reload_node_status(TreeExplorer, projects)
M.reload_node_status(core.get_explorer(), projects)
renderer.draw()
event_running = false
end

View File

@@ -1,4 +1,5 @@
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local M = {}
@@ -7,7 +8,7 @@ local M = {}
---(the topmost node in the nvim-tree window)
local function get_node_path(node)
if node.name == ".." then
return utils.path_remove_trailing(TreeExplorer.cwd)
return utils.path_remove_trailing(core.get_cwd())
else
return node.absolute_path
end

View File

@@ -1,11 +1,12 @@
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local renderer = require "nvim-tree.renderer"
local core = require "nvim-tree.core"
local M = {}
function M.fn()
if not TreeExplorer then
if not core.get_explorer() then
return
end
@@ -13,7 +14,7 @@ function M.fn()
utils.clear_prompt()
local absolute_input_path = utils.path_join {
TreeExplorer.cwd,
core.get_cwd(),
input_path,
}
@@ -54,7 +55,7 @@ function M.fn()
-- if node is not open -> open it
if not node.open then
node.open = true
TreeExplorer:expand(node)
core.get_explorer():expand(node)
tree_altered = true
end
@@ -70,7 +71,7 @@ function M.fn()
return index
end
local index = search_node(TreeExplorer.nodes)
local index = search_node(core.get_explorer().nodes)
if tree_altered then
renderer.draw()