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

View File

@ -1,5 +1,7 @@
local a = vim.api local a = vim.api
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local M = { local M = {
current_tab = a.nvim_get_current_tabpage(), current_tab = a.nvim_get_current_tabpage(),
@ -10,12 +12,12 @@ local M = {
} }
function M.fn(name, with_open) function M.fn(name, with_open)
if not TreeExplorer then if not core.get_explorer() then
return return
end end
local foldername = name == ".." and vim.fn.fnamemodify(utils.path_remove_trailing(TreeExplorer.cwd), ":h") or name 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) == TreeExplorer.cwd local no_cwd_change = vim.fn.expand(foldername) == core.get_cwd()
local new_tab = a.nvim_get_current_tabpage() 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 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 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)) vim.cmd("lcd " .. vim.fn.fnameescape(foldername))
end end
end end
require("nvim-tree.lib").init(foldername) require("nvim-tree.core").init(foldername)
if with_open then if with_open then
require("nvim-tree.lib").open() require("nvim-tree.lib").open()
else else

View File

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

View File

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

View File

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

View File

@ -1,4 +1,5 @@
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local M = {} local M = {}
@ -6,7 +7,7 @@ function M.fn(node)
if not node or node.name == ".." then if not node or node.name == ".." then
return require("nvim-tree.actions.change-dir").fn ".." return require("nvim-tree.actions.change-dir").fn ".."
else 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) require("nvim-tree.actions.change-dir").fn(newdir)
return require("nvim-tree.actions.find-file").fn(node.absolute_path) return require("nvim-tree.actions.find-file").fn(node.absolute_path)
end end

View File

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

View File

@ -2,6 +2,8 @@ local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local diagnostics = require "nvim-tree.diagnostics" local diagnostics = require "nvim-tree.diagnostics"
local renderer = require "nvim-tree.renderer" local renderer = require "nvim-tree.renderer"
local core = require "nvim-tree.core"
local lib = function() local lib = function()
return require "nvim-tree.lib" return require "nvim-tree.lib"
end end
@ -49,7 +51,7 @@ function M.parent_node(should_close)
node.open = false node.open = false
altered_tree = true altered_tree = true
else else
local line, parent = iter(TreeExplorer.nodes, true) local line, parent = iter(core.get_explorer().nodes, true)
if parent == nil then if parent == nil then
line = 1 line = 1
elseif should_close then elseif should_close then
@ -82,16 +84,16 @@ function M.sibling(direction)
local parent, _ local parent, _
-- Check if current node is already at root nodes -- 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 if node_path == _node.absolute_path then
line = index line = index
end end
end end
if line > 0 then if line > 0 then
parent = TreeExplorer parent = core.get_explorer()
else else
_, parent = iter(TreeExplorer.nodes, true) _, parent = iter(core.get_explorer().nodes, true)
if parent ~= nil and #parent.nodes > 1 then if parent ~= nil and #parent.nodes > 1 then
line, _ = get_line_from_node(node)(parent.nodes) line, _ = get_line_from_node(node)(parent.nodes)
end end
@ -108,7 +110,7 @@ function M.sibling(direction)
end end
local target_node = parent.nodes[index] 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 if not view.is_root_folder_visible() then
line = line - 1 line = line - 1
end end
@ -119,7 +121,7 @@ end
function M.find_git_item(where) function M.find_git_item(where)
return function() return function()
local node_cur = lib().get_node_at_cursor() 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 local cur, first, prev, nex = nil, nil, nil, nil
for line, node in pairs(nodes_by_line) do 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 view = require "nvim-tree.view"
local renderer = require "nvim-tree.renderer" local renderer = require "nvim-tree.renderer"
local explorer_module = require "nvim-tree.explorer" local explorer_module = require "nvim-tree.explorer"
local core = require "nvim-tree.core"
local M = {} local M = {}
@ -34,13 +35,13 @@ end
local event_running = false local event_running = false
function M.reload_explorer() 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 return
end end
event_running = true event_running = true
local projects = git.reload() local projects = git.reload()
refresh_nodes(TreeExplorer, projects) refresh_nodes(core.get_explorer(), projects)
if view.is_visible() then if view.is_visible() then
renderer.draw() renderer.draw()
end end
@ -49,13 +50,13 @@ function M.reload_explorer()
end end
function M.reload_git() 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 return
end end
event_running = true event_running = true
local projects = git.reload() local projects = git.reload()
M.reload_node_status(TreeExplorer, projects) M.reload_node_status(core.get_explorer(), projects)
renderer.draw() renderer.draw()
event_running = false event_running = false
end end

View File

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

View File

@ -1,11 +1,12 @@
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local renderer = require "nvim-tree.renderer" local renderer = require "nvim-tree.renderer"
local core = require "nvim-tree.core"
local M = {} local M = {}
function M.fn() function M.fn()
if not TreeExplorer then if not core.get_explorer() then
return return
end end
@ -13,7 +14,7 @@ function M.fn()
utils.clear_prompt() utils.clear_prompt()
local absolute_input_path = utils.path_join { local absolute_input_path = utils.path_join {
TreeExplorer.cwd, core.get_cwd(),
input_path, input_path,
} }
@ -54,7 +55,7 @@ function M.fn()
-- if node is not open -> open it -- if node is not open -> open it
if not node.open then if not node.open then
node.open = true node.open = true
TreeExplorer:expand(node) core.get_explorer():expand(node)
tree_altered = true tree_altered = true
end end
@ -70,7 +71,7 @@ function M.fn()
return index return index
end end
local index = search_node(TreeExplorer.nodes) local index = search_node(core.get_explorer().nodes)
if tree_altered then if tree_altered then
renderer.draw() 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 a = vim.api
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local core = require "nvim-tree.core"
local M = {} local M = {}
@ -103,7 +104,7 @@ local function is_using_coc()
end end
function M.update() 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 return
end end
local buffer_severity local buffer_severity
@ -125,7 +126,7 @@ function M.update()
end end
for bufname, severity in pairs(buffer_severity) do for bufname, severity in pairs(buffer_severity) do
if 0 < severity and severity < 5 then 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 if M.show_on_dirs and not node.open then
return vim.startswith(bufname, node.absolute_path) return vim.startswith(bufname, node.absolute_path)
else else

View File

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

View File

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

View File

@ -4,6 +4,7 @@ local _padding = require "nvim-tree.renderer.padding"
local _help = require "nvim-tree.renderer.help" local _help = require "nvim-tree.renderer.help"
local _icons = require "nvim-tree.renderer.icons" local _icons = require "nvim-tree.renderer.icons"
local git = require "nvim-tree.renderer.git" local git = require "nvim-tree.renderer.git"
local core = require "nvim-tree.core"
local api = vim.api local api = vim.api
@ -223,7 +224,7 @@ local function compute_header()
if view.is_root_folder_visible() then if view.is_root_folder_visible() then
local root_folder_modifier = vim.g.nvim_tree_root_folder_modifier or ":~" local root_folder_modifier = vim.g.nvim_tree_root_folder_modifier or ":~"
local root_name = utils.path_join { 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) table.insert(lines, root_name)
@ -234,7 +235,7 @@ end
function M.draw() function M.draw()
local bufnr = view.get_bufnr() 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 return
end end
local cursor local cursor
@ -252,7 +253,7 @@ function M.draw()
_padding.reload_padding_function() _padding.reload_padding_function()
git.reload() git.reload()
compute_header() 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 if view.is_help_ui() then
lines, hl = _help.compute_lines() lines, hl = _help.compute_lines()

View File

@ -1,5 +1,7 @@
local a = vim.api local a = vim.api
local core = require "nvim-tree.core"
local M = {} local M = {}
M.View = { M.View = {
@ -343,7 +345,7 @@ function M._prevent_buffer_override()
end end
function M.is_root_folder_visible() 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 end
local DEFAULT_CONFIG = { local DEFAULT_CONFIG = {