Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ local M = {
init_root = "",
}

--- Helper function to execute some explorer method safely
---@param fn string # key of explorer
---@return function|nil
local function explorer_fn(fn)
local explorer = core.get_explorer()
if explorer then
return explorer[fn]
end
end

--- Update the tree root to a directory or the directory containing
---@param path string relative or absolute
---@param bufnr number|nil
Expand Down Expand Up @@ -47,7 +57,7 @@ function M.change_root(path, bufnr)
-- test if in vim_cwd
if utils.path_relative(path, vim_cwd) ~= path then
if vim_cwd ~= cwd then
actions.root.change_dir.fn(vim_cwd)
explorer_fn("change_dir")(vim_cwd)
end
return
end
Expand All @@ -58,19 +68,19 @@ function M.change_root(path, bufnr)

-- otherwise test M.init_root
if _config.prefer_startup_root and utils.path_relative(path, M.init_root) ~= path then
actions.root.change_dir.fn(M.init_root)
explorer_fn("change_dir")(M.init_root)
return
end
-- otherwise root_dirs
for _, dir in pairs(_config.root_dirs) do
dir = vim.fn.fnamemodify(dir, ":p")
if utils.path_relative(path, dir) ~= path then
actions.root.change_dir.fn(dir)
explorer_fn("change_dir")(dir)
return
end
end
-- finally fall back to the folder containing the file
actions.root.change_dir.fn(vim.fn.fnamemodify(path, ":p:h"))
explorer_fn("change_dir")(vim.fn.fnamemodify(path, ":p:h"))
end

function M.tab_enter()
Expand Down Expand Up @@ -110,7 +120,7 @@ function M.open_on_directory()
return
end

actions.root.change_dir.force_dirchange(bufname, true)
explorer_fn("force_dirchange")(bufname, true)
end

---@return table
Expand All @@ -134,7 +144,7 @@ end
---@param name string|nil
function M.change_dir(name)
if name then
actions.root.change_dir.fn(name)
explorer_fn("change_dir")(name)
end

if _config.update_focused_file.update_root.enable then
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/finders/find-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function M.fn(path)
dir.open = true
end
if #dir.nodes == 0 then
core.get_explorer():expand(dir)
core.get_explorer():expand_dir_node(dir)
if dir.group_next and incremented_line then
line = line - 1
end
Expand Down
2 changes: 0 additions & 2 deletions lua/nvim-tree/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ M.finders = require("nvim-tree.actions.finders")
M.fs = require("nvim-tree.actions.fs")
M.moves = require("nvim-tree.actions.moves")
M.node = require("nvim-tree.actions.node")
M.root = require("nvim-tree.actions.root")
M.tree = require("nvim-tree.actions.tree")

function M.setup(opts)
M.fs.setup(opts)
M.node.setup(opts)
M.root.setup(opts)
M.tree.setup(opts)
end

Expand Down
105 changes: 0 additions & 105 deletions lua/nvim-tree/actions/root/change-dir.lua

This file was deleted.

9 changes: 0 additions & 9 deletions lua/nvim-tree/actions/root/init.lua

This file was deleted.

4 changes: 2 additions & 2 deletions lua/nvim-tree/actions/tree/modifiers/expand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local function expand(node)
node = node:last_group_node()
node.open = true
if #node.nodes == 0 then
core.get_explorer():expand(node)
core.get_explorer():expand_dir_node(node)
end
end

Expand Down Expand Up @@ -66,7 +66,7 @@ local function should_expand(expansion_count, node, should_descend)

if not dir.open and should_descend(expansion_count, node) then
if #node.nodes == 0 then
core.get_explorer():expand(dir) -- populate node.group_next
core.get_explorer():expand_dir_node(dir) -- populate node.group_next
end

if dir.group_next then
Expand Down
21 changes: 2 additions & 19 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local keymap = require("nvim-tree.keymap")
local notify = require("nvim-tree.notify")

local DirectoryNode = require("nvim-tree.node.directory")
local FileNode = require("nvim-tree.node.file")
local FileLinkNode = require("nvim-tree.node.file-link")
local RootNode = require("nvim-tree.node.root")
local UserDecorator = require("nvim-tree.renderer.decorator.user")
Expand Down Expand Up @@ -158,23 +157,7 @@ Api.tree.change_root = wrap(function(...)
require("nvim-tree").change_dir(...)
end)

Api.tree.change_root_to_node = wrap_node(function(node)
if node.name == ".." or node:is(RootNode) then
actions.root.change_dir.fn("..")
return
end

if node:is(FileNode) and node.parent ~= nil then
actions.root.change_dir.fn(node.parent:last_group_node().absolute_path)
return
end

if node:is(DirectoryNode) then
actions.root.change_dir.fn(node:last_group_node().absolute_path)
return
end
end)

Api.tree.change_root_to_node = wrap_node(wrap_explorer("change_dir_to_node"))
Api.tree.change_root_to_parent = wrap_node(wrap_explorer("dir_up"))
Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor")
Api.tree.get_nodes = wrap_explorer("get_nodes")
Expand Down Expand Up @@ -281,7 +264,7 @@ local function open_or_expand_or_dir_up(mode, toggle_group)
local dir = node:as(DirectoryNode)

if root or node.name == ".." then
actions.root.change_dir.fn("..")
wrap_explorer("change_dir")("..")
elseif dir then
dir:expand_or_collapse(toggle_group)
elseif not toggle_group then
Expand Down
Loading
Loading