From ad1c40893d9e26fb33e9043b2274f3e94186eec4 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 3 Nov 2024 14:56:53 +1100 Subject: [PATCH] refactor(#2926): move find-file and change_root to Explorer --- lua/nvim-tree.lua | 62 ------------ lua/nvim-tree/actions/tree/find-file.lua | 71 ------------- lua/nvim-tree/actions/tree/init.lua | 2 - lua/nvim-tree/actions/tree/open.lua | 6 +- lua/nvim-tree/actions/tree/toggle.lua | 6 +- lua/nvim-tree/api.lua | 2 +- lua/nvim-tree/explorer/init.lua | 123 +++++++++++++++++++++++ 7 files changed, 134 insertions(+), 138 deletions(-) delete mode 100644 lua/nvim-tree/actions/tree/find-file.lua diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 0ece7f1916e..554535dd7db 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -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) diff --git a/lua/nvim-tree/actions/tree/find-file.lua b/lua/nvim-tree/actions/tree/find-file.lua deleted file mode 100644 index 8a05bf6db45..00000000000 --- a/lua/nvim-tree/actions/tree/find-file.lua +++ /dev/null @@ -1,71 +0,0 @@ -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") - -local M = {} - ---- Find file or buffer ----@param opts ApiTreeFindFileOpts|nil|boolean legacy -> opts.buf -function M.fn(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 and not core.get_explorer() 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 M.config.update_focused_file.update_root.enable then - require("nvim-tree").change_root(path, bufnr) - end - - -- find - finders_find_file.fn(path) -end - -function M.setup(opts) - M.config = opts or {} -end - -return M diff --git a/lua/nvim-tree/actions/tree/init.lua b/lua/nvim-tree/actions/tree/init.lua index 2ff9e9b71cf..a68eca3d5f2 100644 --- a/lua/nvim-tree/actions/tree/init.lua +++ b/lua/nvim-tree/actions/tree/init.lua @@ -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) diff --git a/lua/nvim-tree/actions/tree/open.lua b/lua/nvim-tree/actions/tree/open.lua index ff2da837b87..d13a2ccbdcb 100644 --- a/lua/nvim-tree/actions/tree/open.lua +++ b/lua/nvim-tree/actions/tree/open.lua @@ -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") @@ -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 diff --git a/lua/nvim-tree/actions/tree/toggle.lua b/lua/nvim-tree/actions/tree/toggle.lua index 10aa978467e..44a3081aaf8 100644 --- a/lua/nvim-tree/actions/tree/toggle.lua +++ b/lua/nvim-tree/actions/tree/toggle.lua @@ -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") @@ -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 diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 85762656a1d..98ac58da337 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -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) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index ecb042d2dd3..5e35cd97465 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -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") @@ -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 @@ -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()