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

@ -9,6 +9,7 @@ local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local change_dir = require "nvim-tree.actions.change-dir"
local legacy = require "nvim-tree.legacy"
local core = require "nvim-tree.core"
local _config = {}
@ -58,8 +59,8 @@ function M.open_replacing_current_buffer()
end
local cwd = vim.fn.fnamemodify(bufname, ":p:h")
if not TreeExplorer or cwd ~= TreeExplorer.cwd then
lib.init(cwd)
if not core.get_explorer() or cwd ~= core.get_cwd() then
core.init(cwd)
end
view.open_in_current_win { hijack_current_buf = false, resize = false }
require("nvim-tree.renderer").draw()
@ -97,13 +98,13 @@ local function update_base_dir_with_filepath(filepath, bufnr)
end
end
if not vim.startswith(filepath, TreeExplorer.cwd) then
if not vim.startswith(filepath, core.get_explorer().cwd) then
change_dir.fn(vim.fn.fnamemodify(filepath, ":p:h"))
end
end
function M.find_file(with_open, bufnr)
if not with_open and not TreeExplorer then
if not with_open and not core.get_explorer() then
return
end
@ -248,8 +249,7 @@ function M.on_enter(netrw_disabled)
end
if should_open or should_hijack or existing_tree_wins[1] ~= nil then
lib.init(cwd)
lib.open()
lib.open(cwd)
if should_focus_other_window then
vim.cmd "noautocmd wincmd p"

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()

26
lua/nvim-tree/core.lua Normal file
View File

@ -0,0 +1,26 @@
local events = require "nvim-tree.events"
local explorer = require "nvim-tree.explorer"
local M = {}
local first_init_done = false
TreeExplorer = nil
function M.init(foldername)
TreeExplorer = explorer.Explorer.new(foldername)
if not first_init_done then
events._dispatch_ready()
first_init_done = true
end
end
function M.get_explorer()
return TreeExplorer
end
function M.get_cwd()
return TreeExplorer.cwd
end
return M

View File

@ -1,6 +1,7 @@
local a = vim.api
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local core = require "nvim-tree.core"
local M = {}
@ -103,7 +104,7 @@ local function is_using_coc()
end
function M.update()
if not M.enable or not TreeExplorer or not view.is_buf_valid(view.get_bufnr()) then
if not M.enable or not core.get_explorer() or not view.is_buf_valid(view.get_bufnr()) then
return
end
local buffer_severity
@ -125,7 +126,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(TreeExplorer.nodes, function(node)
local node, line = utils.find_node(core.get_explorer().nodes, function(node)
if M.show_on_dirs and not node.open then
return vim.startswith(bufname, node.absolute_path)
else

View File

@ -2,26 +2,13 @@ local api = vim.api
local renderer = require "nvim-tree.renderer"
local diagnostics = require "nvim-tree.diagnostics"
local explorer = require "nvim-tree.explorer"
local view = require "nvim-tree.view"
local events = require "nvim-tree.events"
local first_init_done = false
local core = require "nvim-tree.core"
local M = {
target_winid = nil,
}
TreeExplorer = nil
function M.init(foldername)
TreeExplorer = explorer.Explorer.new(foldername)
if not first_init_done then
events._dispatch_ready()
first_init_done = true
end
end
function M.get_nodes_by_line(nodes_all, line_start)
local nodes_by_line = {}
local line = line_start
@ -42,7 +29,7 @@ function M.get_nodes_by_line(nodes_all, line_start)
end
function M.get_node_at_cursor()
if not TreeExplorer then
if not core.get_explorer() then
return
end
local winnr = view.get_winnr()
@ -57,14 +44,14 @@ function M.get_node_at_cursor()
local help_text = M.get_nodes_by_line(help_lines, 1)[line]
return { name = help_text }
else
if line == 1 and TreeExplorer.cwd ~= "/" and not hide_root_folder then
if line == 1 and core.get_explorer().cwd ~= "/" and not hide_root_folder then
return { name = ".." }
end
if TreeExplorer.cwd == "/" then
if core.get_explorer().cwd == "/" then
line = line + 1
end
return M.get_nodes_by_line(TreeExplorer.nodes, view.View.hide_root_folder and 1 or 2)[line]
return M.get_nodes_by_line(core.get_explorer().nodes, view.View.hide_root_folder and 1 or 2)[line]
end
end
@ -84,7 +71,7 @@ function M.expand_or_collapse(node)
end
if #node.nodes == 0 then
TreeExplorer:expand(node)
core.get_explorer():expand(node)
end
renderer.draw()
@ -104,7 +91,7 @@ end
local function handle_buf_cwd(cwd)
local respect_buf_cwd = vim.g.nvim_tree_respect_buf_cwd or 0
if respect_buf_cwd == 1 and cwd ~= TreeExplorer.cwd then
if respect_buf_cwd == 1 and cwd ~= core.get_explorer().cwd then
require("nvim-tree.actions.change-dir").fn(cwd)
end
end
@ -130,8 +117,8 @@ end
function M.open(cwd)
M.set_target_win()
if not TreeExplorer or cwd then
M.init(cwd or vim.loop.cwd())
if not core.get_explorer() or cwd then
core.init(cwd or vim.loop.cwd())
end
if should_hijack_current_buf() then
view.open_in_current_win()

View File

@ -4,9 +4,9 @@ local M = {
}
--- Write to log file
--- @param typ as per log.types config
--- @param fmt for string.format
--- @param ... arguments for string.format
--- @param typ string as per log.types config
--- @param fmt string for string.format
--- @param ... any arguments for string.format
function M.raw(typ, fmt, ...)
if not M.path or not M.config.types[typ] and not M.config.types.all then
return

View File

@ -4,6 +4,7 @@ local _padding = require "nvim-tree.renderer.padding"
local _help = require "nvim-tree.renderer.help"
local _icons = require "nvim-tree.renderer.icons"
local git = require "nvim-tree.renderer.git"
local core = require "nvim-tree.core"
local api = vim.api
@ -223,7 +224,7 @@ local function compute_header()
if view.is_root_folder_visible() then
local root_folder_modifier = vim.g.nvim_tree_root_folder_modifier or ":~"
local root_name = utils.path_join {
utils.path_remove_trailing(vim.fn.fnamemodify(TreeExplorer.cwd, root_folder_modifier)),
utils.path_remove_trailing(vim.fn.fnamemodify(core.get_cwd(), root_folder_modifier)),
"..",
}
table.insert(lines, root_name)
@ -234,7 +235,7 @@ end
function M.draw()
local bufnr = view.get_bufnr()
if not TreeExplorer or not bufnr or not api.nvim_buf_is_loaded(bufnr) then
if not core.get_explorer() or not bufnr or not api.nvim_buf_is_loaded(bufnr) then
return
end
local cursor
@ -252,7 +253,7 @@ function M.draw()
_padding.reload_padding_function()
git.reload()
compute_header()
update_draw_data(TreeExplorer, show_arrows and 2 or 0, {})
update_draw_data(core.get_explorer(), show_arrows and 2 or 0, {})
if view.is_help_ui() then
lines, hl = _help.compute_lines()

View File

@ -1,5 +1,7 @@
local a = vim.api
local core = require "nvim-tree.core"
local M = {}
M.View = {
@ -343,7 +345,7 @@ function M._prevent_buffer_override()
end
function M.is_root_folder_visible()
return TreeExplorer.cwd ~= "/" and not M.View.hide_root_folder
return core.get_cwd() ~= "/" and not M.View.hide_root_folder
end
local DEFAULT_CONFIG = {