Skip to content

Commit

Permalink
refactor(#2926): move find-file and change_root to Explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Nov 3, 2024
1 parent 610a1c1 commit ad1c408
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 138 deletions.
62 changes: 0 additions & 62 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,6 @@ local M = {
init_root = "",
}

--- Update the tree root to a directory or the directory containing
---@param path string relative or absolute
---@param bufnr number|nil
function M.change_root(path, bufnr)
-- skip if current file is in ignore_list
if type(bufnr) == "number" then
local ft

if vim.fn.has("nvim-0.10") == 1 then
ft = vim.api.nvim_get_option_value("filetype", { buf = bufnr }) or ""
else
ft = vim.api.nvim_buf_get_option(bufnr, "filetype") or "" ---@diagnostic disable-line: deprecated
end

for _, value in pairs(_config.update_focused_file.update_root.ignore_list) do
if utils.str_find(path, value) or utils.str_find(ft, value) then
return
end
end
end

-- don't find inexistent
if vim.fn.filereadable(path) == 0 then
return
end

local cwd = core.get_cwd()
if cwd == nil then
return
end

local vim_cwd = vim.fn.getcwd()

-- 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)
end
return
end
-- test if in cwd
if utils.path_relative(path, cwd) ~= path then
return
end

-- 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)
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)
return
end
end
-- finally fall back to the folder containing the file
actions.root.change_dir.fn(vim.fn.fnamemodify(path, ":p:h"))
end

function M.tab_enter()
if view.is_visible({ any_tabpage = true }) then
local bufname = vim.api.nvim_buf_get_name(0)
Expand Down
71 changes: 0 additions & 71 deletions lua/nvim-tree/actions/tree/find-file.lua

This file was deleted.

2 changes: 0 additions & 2 deletions lua/nvim-tree/actions/tree/init.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
local M = {}

M.find_file = require("nvim-tree.actions.tree.find-file")
M.modifiers = require("nvim-tree.actions.tree.modifiers")
M.open = require("nvim-tree.actions.tree.open")
M.toggle = require("nvim-tree.actions.tree.toggle")
M.resize = require("nvim-tree.actions.tree.resize")

function M.setup(opts)
M.find_file.setup(opts)
M.modifiers.setup(opts)
M.open.setup(opts)
M.toggle.setup(opts)
Expand Down
6 changes: 5 additions & 1 deletion lua/nvim-tree/actions/tree/open.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local core = require("nvim-tree.core")
local lib = require("nvim-tree.lib")
local view = require("nvim-tree.view")
local finders_find_file = require("nvim-tree.actions.finders.find-file")
Expand Down Expand Up @@ -40,7 +41,10 @@ function M.fn(opts)
if M.config.update_focused_file.enable or opts.find_file then
-- update root
if opts.update_root then
require("nvim-tree").change_root(previous_path, previous_buf)
local explorer = core.get_explorer()
if explorer then
explorer:change_root(previous_path, previous_buf)
end
end

-- find
Expand Down
6 changes: 5 additions & 1 deletion lua/nvim-tree/actions/tree/toggle.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local core = require("nvim-tree.core")
local lib = require("nvim-tree.lib")
local view = require("nvim-tree.view")
local finders_find_file = require("nvim-tree.actions.finders.find-file")
Expand Down Expand Up @@ -55,7 +56,10 @@ function M.fn(opts, no_focus, cwd, bang)
if M.config.update_focused_file.enable or opts.find_file then
-- update root
if opts.update_root then
require("nvim-tree").change_root(previous_path, previous_buf)
local explorer = core.get_explorer()
if explorer then
explorer:change_root(previous_path, previous_buf)
end
end

-- find
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Api.tree.get_nodes = wrap_explorer("get_nodes")
---@field update_root boolean|nil default false
---@field focus boolean|nil default false

Api.tree.find_file = wrap(actions.tree.find_file.fn)
Api.tree.find_file = wrap_explorer("find_file")
Api.tree.search_node = wrap(actions.finders.search_node.fn)
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse_all.fn)
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn)
Expand Down
123 changes: 123 additions & 0 deletions lua/nvim-tree/explorer/init.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
local actions = require("nvim-tree.actions")
local appearance = require("nvim-tree.appearance")
local buffers = require("nvim-tree.buffers")
local core = require("nvim-tree.core")
local git = require("nvim-tree.git")
local log = require("nvim-tree.log")
local lib = require("nvim-tree.lib")
local notify = require("nvim-tree.notify")
local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")
Expand All @@ -24,6 +26,9 @@ local Renderer = require("nvim-tree.renderer")

local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON

-- set once and only once for prefer_startup_root
local init_root = vim.fn.getcwd()

local config

---@class (exact) Explorer: RootNode
Expand Down Expand Up @@ -530,6 +535,124 @@ function Explorer:place_cursor_on_node()
end
end

--- Update the tree root to a directory or the directory containing
---@param path string relative or absolute
---@param bufnr number|nil
function Explorer:change_root(path, bufnr)
-- error("Explorer:change_root")

-- skip if current file is in ignore_list
if type(bufnr) == "number" then
local ft

if vim.fn.has("nvim-0.10") == 1 then
ft = vim.api.nvim_get_option_value("filetype", { buf = bufnr }) or ""
else
ft = vim.api.nvim_buf_get_option(bufnr, "filetype") or "" ---@diagnostic disable-line: deprecated
end

for _, value in pairs(self.opts.update_focused_file.update_root.ignore_list) do
if utils.str_find(path, value) or utils.str_find(ft, value) then
return
end
end
end

-- don't find inexistent
if vim.fn.filereadable(path) == 0 then
return
end

local vim_cwd = vim.fn.getcwd()

-- test if in vim_cwd
if utils.path_relative(path, vim_cwd) ~= path then
if vim_cwd ~= self.absolute_path then
actions.root.change_dir.fn(vim_cwd)
end
return
end
-- test if in cwd
if utils.path_relative(path, self.absolute_path) ~= path then
return
end

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

--- Find file or buffer
---@param opts ApiTreeFindFileOpts|nil|boolean legacy -> opts.buf
function Explorer:find_file(opts)
-- legacy arguments
if type(opts) == "string" then
opts = {
buf = opts,
}
end
opts = opts or {}

-- do nothing if closed and open not requested
if not opts.open then
return
end

local bufnr, path

-- (optional) buffer number and path
local opts_buf = opts.buf
if type(opts_buf) == "nil" then
bufnr = vim.api.nvim_get_current_buf()
path = vim.api.nvim_buf_get_name(bufnr)
elseif type(opts_buf) == "number" then
if not vim.api.nvim_buf_is_valid(opts_buf) then
return
end
bufnr = opts_buf
path = vim.api.nvim_buf_get_name(bufnr)
elseif type(opts_buf) == "string" then
bufnr = nil
path = tostring(opts_buf)
else
return
end

if view.is_visible() then
-- focus
if opts.focus then
lib.set_target_win()
view.focus()
end
elseif opts.open then
-- open
lib.open({ current_window = opts.current_window, winid = opts.winid })
if not opts.focus then
vim.cmd("noautocmd wincmd p")
end
end

-- update root
if opts.update_root or self.opts.update_focused_file.update_root.enable then
self:change_root(path, bufnr)
end

-- find
actions.finders.find_file.fn(path)
end

---Api.tree.get_nodes
---@return Node
function Explorer:get_nodes()
Expand Down

0 comments on commit ad1c408

Please sign in to comment.