diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index dec52e85d42..f29005e5961 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -2,7 +2,9 @@ local api = require("nvim-tree.api") local log = require("nvim-tree.log") local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") -local actions = require("nvim-tree.actions") +local find_file = require("nvim-tree.actions.tree.find-file") +local change_dir = require("nvim-tree.actions.tree.change-dir") +local full_name = require("nvim-tree.renderer.components.full-name") local core = require("nvim-tree.core") local notify = require("nvim-tree.notify") local config = require("nvim-tree.config") @@ -169,7 +171,7 @@ local function setup_autocommands() if config.g.sync_root_with_cwd then create_nvim_tree_autocmd("DirChanged", { callback = function() - actions.tree.change_dir.fn(vim.loop.cwd()) + change_dir.fn(vim.loop.cwd()) end, }) end @@ -181,7 +183,7 @@ local function setup_autocommands() return end utils.debounce("BufEnter:find_file", config.g.view.debounce_delay, function() - actions.tree.find_file.fn() + find_file.fn() end) end, }) @@ -248,6 +250,9 @@ local function setup_autocommands() end end, }) + + -- renderer.full name + full_name.setup_autocommands() end function M.purge_all_state() @@ -276,7 +281,6 @@ function M.setup(config_user) manage_netrw() - require("nvim-tree.notify").setup(config.g) require("nvim-tree.log").setup(config.g) if log.enabled("config") then @@ -284,10 +288,7 @@ function M.setup(config_user) log.raw("config", "%s\n", vim.inspect(config.g)) end - require("nvim-tree.actions").setup(config.g) require("nvim-tree.appearance").setup() - require("nvim-tree.explorer"):setup(config.g) - require("nvim-tree.explorer.watch").setup(config.g) require("nvim-tree.view").setup(config.g) require("nvim-tree.renderer.components").setup(config.g) diff --git a/lua/nvim-tree/actions/finders/init.lua b/lua/nvim-tree/actions/finders/init.lua deleted file mode 100644 index 55ae5a9d156..00000000000 --- a/lua/nvim-tree/actions/finders/init.lua +++ /dev/null @@ -1,6 +0,0 @@ -local M = {} - -M.find_file = require("nvim-tree.actions.finders.find-file") -M.search_node = require("nvim-tree.actions.finders.search-node") - -return M diff --git a/lua/nvim-tree/actions/fs/init.lua b/lua/nvim-tree/actions/fs/init.lua deleted file mode 100644 index b308a86a01e..00000000000 --- a/lua/nvim-tree/actions/fs/init.lua +++ /dev/null @@ -1,14 +0,0 @@ -local M = {} - -M.create_file = require("nvim-tree.actions.fs.create-file") -M.remove_file = require("nvim-tree.actions.fs.remove-file") -M.rename_file = require("nvim-tree.actions.fs.rename-file") -M.trash = require("nvim-tree.actions.fs.trash") - -function M.setup(opts) - M.remove_file.setup(opts) - M.rename_file.setup(opts) - M.trash.setup(opts) -end - -return M diff --git a/lua/nvim-tree/actions/fs/remove-file.lua b/lua/nvim-tree/actions/fs/remove-file.lua index e818f0709bc..2aa08770877 100644 --- a/lua/nvim-tree/actions/fs/remove-file.lua +++ b/lua/nvim-tree/actions/fs/remove-file.lua @@ -1,9 +1,9 @@ local core = require("nvim-tree.core") local utils = require("nvim-tree.utils") local events = require("nvim-tree.events") -local view = require("nvim-tree.view") local lib = require("nvim-tree.lib") local notify = require("nvim-tree.notify") +local config = require("nvim-tree.config") local DirectoryLinkNode = require("nvim-tree.node.directory-link") local DirectoryNode = require("nvim-tree.node.directory") @@ -18,7 +18,7 @@ local M = { local function close_windows(windows) -- When floating, prevent closing the last non-floating window. -- For details see #2503, #3187. - if view.View.float.enable then + if config.g.view.float.enable then local non_float_count = 0 for _, win in ipairs(vim.api.nvim_list_wins()) do if vim.api.nvim_win_get_config(win).relative == "" then @@ -43,15 +43,15 @@ local function clear_buffer(absolute_path) for _, buf in pairs(bufs) do if buf.name == absolute_path then local tree_winnr = vim.api.nvim_get_current_win() - if buf.hidden == 0 and (#bufs > 1 or view.View.float.enable) then + if buf.hidden == 0 and (#bufs > 1 or config.g.view.float.enable) then vim.api.nvim_set_current_win(buf.windows[1]) vim.cmd(":bn") end vim.api.nvim_buf_delete(buf.bufnr, { force = true }) - if not view.View.float.quit_on_focus_loss then + if not config.g.view.float.quit_on_focus_loss then vim.api.nvim_set_current_win(tree_winnr) end - if M.config.actions.remove_file.close_window then + if config.g.actions.remove_file.close_window then close_windows(buf.windows) end return @@ -141,18 +141,18 @@ local function remove_one(node) notify.info(notify.render_path(node.absolute_path) .. " was properly removed.") end local explorer = core.get_explorer() - if not M.config.filesystem_watchers.enable and explorer then + if not config.g.filesystem_watchers.enable and explorer then explorer:reload_explorer() end end - if M.config.ui.confirm.remove then + if config.g.ui.confirm.remove then local prompt_select = "Remove " .. node.name .. "?" - local prompt_input, items_short, items_long = utils.confirm_prompt(prompt_select, M.config.ui.confirm.default_yes) + local prompt_input, items_short, items_long = utils.confirm_prompt(prompt_select, config.g.ui.confirm.default_yes) lib.prompt(prompt_input, prompt_select, items_short, items_long, "nvimtree_remove", function(item_short) utils.clear_prompt() - if item_short == "y" or item_short == (M.config.ui.confirm.default_yes and "") then + if item_short == "y" or item_short == (config.g.ui.confirm.default_yes and "") then do_remove() end end) @@ -181,18 +181,18 @@ local function remove_many(nodes) notify.info(string.format("%d nodes properly removed.", removed)) end local explorer = core.get_explorer() - if not M.config.filesystem_watchers.enable and explorer then + if not config.g.filesystem_watchers.enable and explorer then explorer:reload_explorer() end end - if M.config.ui.confirm.remove then + if config.g.ui.confirm.remove then local prompt_select = string.format("Remove %d selected?", #nodes) - local prompt_input, items_short, items_long = utils.confirm_prompt(prompt_select, M.config.ui.confirm.default_yes) + local prompt_input, items_short, items_long = utils.confirm_prompt(prompt_select, config.g.ui.confirm.default_yes) lib.prompt(prompt_input, prompt_select, items_short, items_long, "nvimtree_remove", function(item_short) utils.clear_prompt() - if item_short == "y" or item_short == (M.config.ui.confirm.default_yes and "") then + if item_short == "y" or item_short == (config.g.ui.confirm.default_yes and "") then execute() end end) @@ -210,10 +210,4 @@ function M.fn(node_or_nodes) end end -function M.setup(opts) - M.config.ui = opts.ui - M.config.actions = opts.actions - M.config.filesystem_watchers = opts.filesystem_watchers -end - return M diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index ee1c4857984..0b4f22ac1d7 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -2,6 +2,7 @@ local core = require("nvim-tree.core") local utils = require("nvim-tree.utils") local events = require("nvim-tree.events") local notify = require("nvim-tree.notify") +local config = require("nvim-tree.config") local find_file = require("nvim-tree.actions.finders.find-file").fn @@ -33,7 +34,7 @@ local function err_fmt(from, to, reason) end local function rename_file_exists(node, to) - if not utils.is_macos then + if not config.os.macos then return utils.file_exists(to) end @@ -163,7 +164,7 @@ local function prompt_to_rename(node, modifier) local full_new_path = prepend .. new_file_path .. append M.rename(node, full_new_path) - if not M.config.filesystem_watchers.enable then + if not config.g.filesystem_watchers.enable then explorer:reload_explorer() end @@ -192,7 +193,7 @@ function M.rename_full(node) end function M.setup(opts) - M.config.filesystem_watchers = opts.filesystem_watchers + config.g.filesystem_watchers = opts.filesystem_watchers end return M diff --git a/lua/nvim-tree/actions/fs/trash.lua b/lua/nvim-tree/actions/fs/trash.lua index ba038e8cf1c..ef62a42e884 100644 --- a/lua/nvim-tree/actions/fs/trash.lua +++ b/lua/nvim-tree/actions/fs/trash.lua @@ -3,6 +3,7 @@ local lib = require("nvim-tree.lib") local notify = require("nvim-tree.notify") local utils = require("nvim-tree.utils") local events = require("nvim-tree.events") +local config = require("nvim-tree.config") local DirectoryLinkNode = require("nvim-tree.node.directory-link") local DirectoryNode = require("nvim-tree.node.directory") @@ -32,9 +33,9 @@ end ---@param node Node function M.remove(node) - local binary = M.config.trash.cmd:gsub(" .*$", "") + local binary = config.g.trash.cmd:gsub(" .*$", "") if vim.fn.executable(binary) == 0 then - notify.warn(string.format("trash.cmd '%s' is not an executable.", M.config.trash.cmd)) + notify.warn(string.format("trash.cmd '%s' is not an executable.", config.g.trash.cmd)) return end @@ -45,8 +46,8 @@ function M.remove(node) -- trashes a path (file or folder) local function trash_path(on_exit) - local need_sync_wait = utils.is_windows - local job = vim.fn.jobstart(M.config.trash.cmd .. " " .. vim.fn.shellescape(node.absolute_path), { + local need_sync_wait = config.os.windows + local job = vim.fn.jobstart(config.g.trash.cmd .. " " .. vim.fn.shellescape(node.absolute_path), { detach = not need_sync_wait, on_exit = on_exit, on_stderr = on_stderr, @@ -65,7 +66,7 @@ function M.remove(node) return end events._dispatch_folder_removed(node.absolute_path) - if not M.config.filesystem_watchers.enable and explorer then + if not config.g.filesystem_watchers.enable and explorer then explorer:reload_explorer() end end) @@ -78,7 +79,7 @@ function M.remove(node) end events._dispatch_file_removed(node.absolute_path) clear_buffer(node.absolute_path) - if not M.config.filesystem_watchers.enable and explorer then + if not config.g.filesystem_watchers.enable and explorer then explorer:reload_explorer() end end) @@ -96,13 +97,13 @@ local function trash_one(node) M.remove(node) end - if M.config.ui.confirm.trash then + if config.g.ui.confirm.trash then local prompt_select = "Trash " .. node.name .. "?" - local prompt_input, items_short, items_long = utils.confirm_prompt(prompt_select, M.config.ui.confirm.default_yes) + local prompt_input, items_short, items_long = utils.confirm_prompt(prompt_select, config.g.ui.confirm.default_yes) lib.prompt(prompt_input, prompt_select, items_short, items_long, "nvimtree_trash", function(item_short) utils.clear_prompt() - if item_short == "y" or item_short == (M.config.ui.confirm.default_yes and "") then + if item_short == "y" or item_short == (config.g.ui.confirm.default_yes and "") then do_trash() end end) @@ -128,13 +129,13 @@ local function trash_many(nodes) end end - if M.config.ui.confirm.trash then + if config.g.ui.confirm.trash then local prompt_select = string.format("Trash %d selected?", #nodes) - local prompt_input, items_short, items_long = utils.confirm_prompt(prompt_select, M.config.ui.confirm.default_yes) + local prompt_input, items_short, items_long = utils.confirm_prompt(prompt_select, config.g.ui.confirm.default_yes) lib.prompt(prompt_input, prompt_select, items_short, items_long, "nvimtree_trash", function(item_short) utils.clear_prompt() - if item_short == "y" or item_short == (M.config.ui.confirm.default_yes and "") then + if item_short == "y" or item_short == (config.g.ui.confirm.default_yes and "") then execute() end end) @@ -152,10 +153,4 @@ function M.fn(node_or_nodes) end end -function M.setup(opts) - M.config.ui = opts.ui - M.config.trash = opts.trash - M.config.filesystem_watchers = opts.filesystem_watchers -end - return M diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua deleted file mode 100644 index d117a24119a..00000000000 --- a/lua/nvim-tree/actions/init.lua +++ /dev/null @@ -1,15 +0,0 @@ -local M = {} - -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.tree = require("nvim-tree.actions.tree") - -function M.setup(opts) - M.fs.setup(opts) - M.node.setup(opts) - M.tree.setup(opts) -end - -return M diff --git a/lua/nvim-tree/actions/moves/init.lua b/lua/nvim-tree/actions/moves/init.lua deleted file mode 100644 index b1e5be74afe..00000000000 --- a/lua/nvim-tree/actions/moves/init.lua +++ /dev/null @@ -1,7 +0,0 @@ -local M = {} - -M.item = require("nvim-tree.actions.moves.item") -M.parent = require("nvim-tree.actions.moves.parent") -M.sibling = require("nvim-tree.actions.moves.sibling") - -return M diff --git a/lua/nvim-tree/actions/node/file-popup.lua b/lua/nvim-tree/actions/node/file-popup.lua index fb6d022af6e..cec98280008 100644 --- a/lua/nvim-tree/actions/node/file-popup.lua +++ b/lua/nvim-tree/actions/node/file-popup.lua @@ -1,4 +1,5 @@ local utils = require("nvim-tree.utils") +local config = require("nvim-tree.config") local M = {} @@ -38,7 +39,7 @@ local function setup_window(node) local max_width = vim.fn.max(vim.tbl_map(function(n) return #n end, lines)) - local open_win_config = vim.tbl_extend("force", M.open_win_config, { + local open_win_config = vim.tbl_extend("force", config.g.actions.file_popup.open_win_config, { width = max_width + 1, height = #lines, noautocmd = true, @@ -89,8 +90,4 @@ function M.toggle_file_info(node) }) end -function M.setup(opts) - M.open_win_config = opts.actions.file_popup.open_win_config -end - return M diff --git a/lua/nvim-tree/actions/node/init.lua b/lua/nvim-tree/actions/node/init.lua deleted file mode 100644 index f7075959b5e..00000000000 --- a/lua/nvim-tree/actions/node/init.lua +++ /dev/null @@ -1,15 +0,0 @@ -local M = {} - -M.file_popup = require("nvim-tree.actions.node.file-popup") -M.open_file = require("nvim-tree.actions.node.open-file") -M.run_command = require("nvim-tree.actions.node.run-command") -M.system_open = require("nvim-tree.actions.node.system-open") -M.buffer = require("nvim-tree.actions.node.buffer") - -function M.setup(opts) - require("nvim-tree.actions.node.system-open").setup(opts) - require("nvim-tree.actions.node.file-popup").setup(opts) - require("nvim-tree.actions.node.open-file").setup(opts) -end - -return M diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 984a732e20e..80dc1d1c41f 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -4,6 +4,7 @@ local notify = require("nvim-tree.notify") local utils = require("nvim-tree.utils") local full_name = require("nvim-tree.renderer.components.full-name") local view = require("nvim-tree.view") +local config = require("nvim-tree.config") local DirectoryNode = require("nvim-tree.node.directory") local FileLinkNode = require("nvim-tree.node.file-link") @@ -36,7 +37,7 @@ local function usable_win_ids() return vim.tbl_filter(function(id) local bufid = vim.api.nvim_win_get_buf(id) - for option, v in pairs(M.window_picker.exclude) do + for option, v in pairs(config.g.actions.open_file.window_picker.exclude) do local ok, option_value if vim.fn.has("nvim-0.10") == 1 then ok, option_value = pcall(vim.api.nvim_get_option_value, option, { buf = bufid }) @@ -74,8 +75,9 @@ local function pick_win_id() return selectable[1] end - if #M.window_picker.chars < #selectable then - notify.error(string.format("More windows (%d) than actions.open_file.window_picker.chars (%d).", #selectable, #M.window_picker.chars)) + if #config.g.actions.open_file.window_picker.chars < #selectable then + notify.error(string.format("More windows (%d) than actions.open_file.window_picker.chars (%d).", #selectable, + #config.g.actions.open_file.window_picker.chars)) return nil end @@ -126,7 +128,7 @@ local function pick_win_id() -- Setup UI for _, id in ipairs(selectable) do - local char = M.window_picker.chars:sub(i, i) + local char = config.g.actions.open_file.window_picker.chars:sub(i, i) local ok_status, statusline, ok_hl, winhl if vim.fn.has("nvim-0.10") == 1 then @@ -152,7 +154,7 @@ local function pick_win_id() end i = i + 1 - if i > #M.window_picker.chars then + if i > #config.g.actions.open_file.window_picker.chars then break end end @@ -194,7 +196,7 @@ local function pick_win_id() vim.o.laststatus = laststatus vim.opt.fillchars = fillchars - if not vim.tbl_contains(vim.split(M.window_picker.chars, ""), resp) then + if not vim.tbl_contains(vim.split(config.g.actions.open_file.window_picker.chars, ""), resp) then return end @@ -202,10 +204,10 @@ local function pick_win_id() end local function open_file_in_tab(filename) - if M.quit_on_open then + if config.g.actions.open_file.quit_on_open then view.close() end - if M.relative_path then + if config.g.actions.open_file.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) end vim.cmd.tabnew() @@ -220,20 +222,20 @@ local function open_file_in_tab(filename) end local function drop(filename) - if M.quit_on_open then + if config.g.actions.open_file.quit_on_open then view.close() end - if M.relative_path then + if config.g.actions.open_file.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) end vim.cmd("drop " .. vim.fn.fnameescape(filename)) end local function tab_drop(filename) - if M.quit_on_open then + if config.g.actions.open_file.quit_on_open then view.close() end - if M.relative_path then + if config.g.actions.open_file.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) end vim.cmd("tab :drop " .. vim.fn.fnameescape(filename)) @@ -257,7 +259,7 @@ end local function get_target_winid(mode) local target_winid - if not M.window_picker.enable or string.find(mode, "no_picker") then + if not config.g.actions.open_file.window_picker.enable or string.find(mode, "no_picker") then target_winid = lib.target_winid local usable_wins = usable_win_ids() -- first available usable window @@ -270,8 +272,8 @@ local function get_target_winid(mode) end else -- pick a window - if type(M.window_picker.picker) == "function" then - target_winid = M.window_picker.picker() + if type(config.g.actions.open_file.window_picker.picker) == "function" then + target_winid = config.g.actions.open_file.window_picker.picker() else target_winid = pick_win_id() end @@ -315,13 +317,13 @@ local function open_in_new_window(filename, mode) -- non-floating, non-nvim-tree windows local win_ids = vim.tbl_filter(function(id) - local config = vim.api.nvim_win_get_config(id) + local win_config = vim.api.nvim_win_get_config(id) local bufnr = vim.api.nvim_win_get_buf(id) - return config and config.relative == "" or utils.is_nvim_tree_buf(bufnr) + return win_config and win_config.relative == "" or utils.is_nvim_tree_buf(bufnr) end, vim.api.nvim_list_wins()) local create_new_window = #win_ids == 1 -- This implies that the nvim-tree window is the only one - local new_window_side = (view.View.side == "right") and "aboveleft" or "belowright" + local new_window_side = (config.g.view.side == "right") and "aboveleft" or "belowright" -- Target is invalid: create new window if not vim.tbl_contains(win_ids, target_winid) then @@ -353,7 +355,7 @@ local function open_in_new_window(filename, mode) end end - if (mode == "preview" or mode == "preview_no_picker") and view.View.float.enable then + if (mode == "preview" or mode == "preview_no_picker") and config.g.view.float.enable then -- ignore "WinLeave" autocmd on preview -- because the registered "WinLeave" -- will kill the floating window immediately @@ -363,7 +365,7 @@ local function open_in_new_window(filename, mode) end local fname - if M.relative_path then + if config.g.actions.open_file.relative_path then fname = utils.escape_special_chars(vim.fn.fnameescape(utils.path_relative(filename, vim.fn.getcwd()))) else fname = utils.escape_special_chars(vim.fn.fnameescape(filename)) @@ -394,7 +396,7 @@ end local function edit_in_current_buf(filename) require("nvim-tree.view").abandon_current_window() - if M.relative_path then + if config.g.actions.open_file.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) end vim.cmd("keepalt keepjumps edit " .. vim.fn.fnameescape(filename)) @@ -438,7 +440,7 @@ function M.fn(mode, filename) vim.bo.bufhidden = "" end - if M.resize_window then + if config.g.actions.open_file.resize_window then view.resize() end @@ -446,7 +448,7 @@ function M.fn(mode, filename) return on_preview(buf_loaded) end - if M.quit_on_open then + if config.g.actions.open_file.quit_on_open then view.close() end end @@ -564,14 +566,4 @@ function M.tab(node) open_or_expand_or_dir_up(node, "tabnew") end -function M.setup(opts) - M.quit_on_open = opts.actions.open_file.quit_on_open - M.resize_window = opts.actions.open_file.resize_window - M.relative_path = opts.actions.open_file.relative_path - if opts.actions.open_file.window_picker.chars then - opts.actions.open_file.window_picker.chars = tostring(opts.actions.open_file.window_picker.chars):upper() - end - M.window_picker = opts.actions.open_file.window_picker -end - return M diff --git a/lua/nvim-tree/actions/node/system-open.lua b/lua/nvim-tree/actions/node/system-open.lua index 9cc11c492fe..be2b158cd67 100644 --- a/lua/nvim-tree/actions/node/system-open.lua +++ b/lua/nvim-tree/actions/node/system-open.lua @@ -1,18 +1,32 @@ local notify = require("nvim-tree.notify") -local utils = require("nvim-tree.utils") +local config = require("nvim-tree.config") local M = {} ---@param node Node local function user(node) - if #M.config.system_open.cmd == 0 then - require("nvim-tree.utils").notify.warn("Cannot open file with system application. Unrecognized platform.") + local cmd = config.g.system_open.cmd + local args = config.g.system_open.args + + if #cmd == 0 then + if config.os.windows then + cmd = "cmd" + args = { "/c", "start", '""' } + elseif config.os.macos then + cmd = "open" + elseif config.os.unix then + cmd = "xdg-open" + end + end + + if #cmd == 0 then + notify.warn("Cannot open file with system application. Unrecognized platform.") return end local process = { - cmd = M.config.system_open.cmd, - args = M.config.system_open.args, + cmd = cmd, + args = args, errors = "\n", stderr = vim.loop.new_pipe(false), } @@ -61,30 +75,11 @@ end ---@param node Node function M.fn(node) - M.open(node) -end - --- TODO #2430 always use native once 0.10 is the minimum neovim version -function M.setup(opts) - M.config = {} - M.config.system_open = opts.system_open or {} - - if vim.fn.has("nvim-0.10") == 1 and #M.config.system_open.cmd == 0 then - M.open = native + -- TODO #2430 always use native once 0.10 is the minimum neovim version + if vim.fn.has("nvim-0.19") == 1 and #config.g.system_open.cmd == 0 then + native(node) else - M.open = user - if #M.config.system_open.cmd == 0 then - if utils.is_windows then - M.config.system_open = { - cmd = "cmd", - args = { "/c", "start", '""' }, - } - elseif utils.is_macos then - M.config.system_open.cmd = "open" - elseif utils.is_unix then - M.config.system_open.cmd = "xdg-open" - end - end + user(node) end end diff --git a/lua/nvim-tree/actions/tree/change-dir.lua b/lua/nvim-tree/actions/tree/change-dir.lua index 8bf2af7345c..b4b194b366b 100644 --- a/lua/nvim-tree/actions/tree/change-dir.lua +++ b/lua/nvim-tree/actions/tree/change-dir.lua @@ -1,4 +1,5 @@ local core = require("nvim-tree.core") +local config = require("nvim-tree.config") local find_file = require("nvim-tree.actions.tree.find-file") local M = {} @@ -10,14 +11,9 @@ function M.fn(name) explorer:change_dir(name) end - if M.config.update_focused_file.update_root.enable then + if config.g.update_focused_file.update_root.enable then find_file.fn() end end ----@param config nvim_tree.config -function M.setup(config) - M.config = config -end - return M diff --git a/lua/nvim-tree/actions/tree/find-file.lua b/lua/nvim-tree/actions/tree/find-file.lua index 0290ce2e17f..d58c9cbfb51 100644 --- a/lua/nvim-tree/actions/tree/find-file.lua +++ b/lua/nvim-tree/actions/tree/find-file.lua @@ -1,6 +1,7 @@ local core = require("nvim-tree.core") local lib = require("nvim-tree.lib") local view = require("nvim-tree.view") +local config = require("nvim-tree.config") local finders_find_file = require("nvim-tree.actions.finders.find-file") local M = {} @@ -56,7 +57,7 @@ function M.fn(opts) end -- update root - if opts.update_root or M.config.update_focused_file.update_root.enable then + if opts.update_root or config.g.update_focused_file.update_root.enable then require("nvim-tree").change_root(path, bufnr) end @@ -64,8 +65,4 @@ function M.fn(opts) 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 deleted file mode 100644 index ca0733d7cb3..00000000000 --- a/lua/nvim-tree/actions/tree/init.lua +++ /dev/null @@ -1,18 +0,0 @@ -local M = {} - -M.change_dir = require("nvim-tree.actions.tree.change-dir") -M.find_file = require("nvim-tree.actions.tree.find-file") -M.collapse = require("nvim-tree.actions.tree.collapse") -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.change_dir.setup(opts) - M.find_file.setup(opts) - M.open.setup(opts) - M.toggle.setup(opts) - M.resize.setup(opts) -end - -return M diff --git a/lua/nvim-tree/actions/tree/open.lua b/lua/nvim-tree/actions/tree/open.lua index 8c559d8cb54..5244706a033 100644 --- a/lua/nvim-tree/actions/tree/open.lua +++ b/lua/nvim-tree/actions/tree/open.lua @@ -1,5 +1,6 @@ local lib = require("nvim-tree.lib") local view = require("nvim-tree.view") +local config = require("nvim-tree.config") local finders_find_file = require("nvim-tree.actions.finders.find-file") local M = {} @@ -37,7 +38,7 @@ function M.fn(opts) end -- find file - if M.config.update_focused_file.enable or opts.find_file then + if config.g.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) @@ -48,8 +49,4 @@ function M.fn(opts) end end -function M.setup(opts) - M.config = opts or {} -end - return M diff --git a/lua/nvim-tree/actions/tree/resize.lua b/lua/nvim-tree/actions/tree/resize.lua index 7498efdfcca..31fbd53306b 100644 --- a/lua/nvim-tree/actions/tree/resize.lua +++ b/lua/nvim-tree/actions/tree/resize.lua @@ -44,8 +44,4 @@ function M.fn(opts) end end -function M.setup(opts) - M.config = opts or {} -end - return M diff --git a/lua/nvim-tree/actions/tree/toggle.lua b/lua/nvim-tree/actions/tree/toggle.lua index cb9e557838a..f38b0999d90 100644 --- a/lua/nvim-tree/actions/tree/toggle.lua +++ b/lua/nvim-tree/actions/tree/toggle.lua @@ -1,5 +1,6 @@ local lib = require("nvim-tree.lib") local view = require("nvim-tree.view") +local config = require("nvim-tree.config") local finders_find_file = require("nvim-tree.actions.finders.find-file") local M = {} @@ -52,7 +53,7 @@ function M.fn(opts, no_focus, cwd, bang) }) -- find file - if M.config.update_focused_file.enable or opts.find_file then + if config.g.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) @@ -69,8 +70,4 @@ function M.fn(opts, no_focus, cwd, bang) end end -function M.setup(opts) - M.config = opts or {} -end - return M diff --git a/lua/nvim-tree/api/impl.lua b/lua/nvim-tree/api/impl.lua index 217e3e854ca..11304e75408 100644 --- a/lua/nvim-tree/api/impl.lua +++ b/lua/nvim-tree/api/impl.lua @@ -155,17 +155,17 @@ function M.hydrate_post_setup(api) api.fs.copy.filename = en(function(e, n) e.clipboard:copy_filename(n) end) api.fs.copy.node = ev(function(e, n) e.clipboard:copy(n) end) api.fs.copy.relative_path = en(function(e, n) e.clipboard:copy_path(n) end) - api.fs.create = _n(function(n) require("nvim-tree.actions").fs.create_file.fn(n) end) + api.fs.create = _n(function(n) require("nvim-tree.actions.fs.create-file").fn(n) end) api.fs.cut = ev(function(e, n) e.clipboard:cut(n) end) api.fs.paste = en(function(e, n) e.clipboard:paste(n) end) api.fs.print_clipboard = e_(function(e) e.clipboard:print_clipboard() end) - api.fs.remove = _v(function(n) require("nvim-tree.actions").fs.remove_file.fn(n) end) - api.fs.rename = _n(function(n) require("nvim-tree.actions").fs.rename_file.rename_node(n) end) - api.fs.rename_basename = _n(function(n) require("nvim-tree.actions").fs.rename_file.rename_basename(n) end) - api.fs.rename_full = _n(function(n) require("nvim-tree.actions").fs.rename_file.rename_full(n) end) - api.fs.rename_node = _n(function(n) require("nvim-tree.actions").fs.rename_file.rename_node(n) end) - api.fs.rename_sub = _n(function(n) require("nvim-tree.actions").fs.rename_file.rename_sub(n) end) - api.fs.trash = _v(function(n) require("nvim-tree.actions").fs.trash.fn(n) end) + api.fs.remove = _v(function(n) require("nvim-tree.actions.fs.remove-file").fn(n) end) + api.fs.rename = _n(function(n) require("nvim-tree.actions.fs.rename-file").rename_node(n) end) + api.fs.rename_basename = _n(function(n) require("nvim-tree.actions.fs.rename-file").rename_basename(n) end) + api.fs.rename_full = _n(function(n) require("nvim-tree.actions.fs.rename-file").rename_full(n) end) + api.fs.rename_node = _n(function(n) require("nvim-tree.actions.fs.rename-file").rename_node(n) end) + api.fs.rename_sub = _n(function(n) require("nvim-tree.actions.fs.rename-file").rename_sub(n) end) + api.fs.trash = _v(function(n) require("nvim-tree.actions.fs.trash").fn(n) end) api.git.reload = e_(function(e) e:reload_git() end) @@ -182,65 +182,65 @@ function M.hydrate_post_setup(api) api.marks.navigate.select = e_(function(e) e.marks:navigate_select() end) api.marks.toggle = ev(function(e, n) e.marks:toggle(n) end) - api.node.buffer.delete = _n(function(n, opts) require("nvim-tree.actions").node.buffer.delete(n, opts) end) - api.node.buffer.wipe = _n(function(n, opts) require("nvim-tree.actions").node.buffer.wipe(n, opts) end) - api.node.collapse = _n(function(n, opts) require("nvim-tree.actions").tree.collapse.node(n, opts) end) + api.node.buffer.delete = _n(function(n, opts) require("nvim-tree.actions.node.buffer").delete(n, opts) end) + api.node.buffer.wipe = _n(function(n, opts) require("nvim-tree.actions.node.buffer").wipe(n, opts) end) + api.node.collapse = _n(function(n, opts) require("nvim-tree.actions.tree.collapse").node(n, opts) end) api.node.expand = en(function(e, n, opts) e:expand_node(n, opts) end) - api.node.navigate.diagnostics.next = __(function() require("nvim-tree.actions").moves.item.diagnostics_next() end) - api.node.navigate.diagnostics.next_recursive = __(function() require("nvim-tree.actions").moves.item.diagnostics_next_recursive() end) - api.node.navigate.diagnostics.prev = __(function() require("nvim-tree.actions").moves.item.diagnostics_prev() end) - api.node.navigate.diagnostics.prev_recursive = __(function() require("nvim-tree.actions").moves.item.diagnostics_prev_recursive() end) - api.node.navigate.git.next = __(function() require("nvim-tree.actions").moves.item.git_next() end) - api.node.navigate.git.next_recursive = __(function() require("nvim-tree.actions").moves.item.git_next_recursive() end) - api.node.navigate.git.next_skip_gitignored = __(function() require("nvim-tree.actions").moves.item.git_next_skip_gitignored() end) - api.node.navigate.git.prev = __(function() require("nvim-tree.actions").moves.item.git_prev() end) - api.node.navigate.git.prev_recursive = __(function() require("nvim-tree.actions").moves.item.git_prev_recursive() end) - api.node.navigate.git.prev_skip_gitignored = __(function() require("nvim-tree.actions").moves.item.git_prev_skip_gitignored() end) - api.node.navigate.opened.next = __(function() require("nvim-tree.actions").moves.item.opened_next() end) - api.node.navigate.opened.prev = __(function() require("nvim-tree.actions").moves.item.opened_prev() end) - api.node.navigate.parent = _n(function(n) require("nvim-tree.actions").moves.parent.move(n) end) - api.node.navigate.parent_close = _n(function(n) require("nvim-tree.actions").moves.parent.move_close(n) end) - api.node.navigate.sibling.first = _n(function(n) require("nvim-tree.actions").moves.sibling.first(n) end) - api.node.navigate.sibling.last = _n(function(n) require("nvim-tree.actions").moves.sibling.last(n) end) - api.node.navigate.sibling.next = _n(function(n) require("nvim-tree.actions").moves.sibling.next(n) end) - api.node.navigate.sibling.prev = _n(function(n) require("nvim-tree.actions").moves.sibling.prev(n) end) - api.node.open.drop = _n(function(n) require("nvim-tree.actions").node.open_file.drop(n) end) - api.node.open.edit = _n(function(n) require("nvim-tree.actions").node.open_file.edit(n) end) - api.node.open.horizontal = _n(function(n) require("nvim-tree.actions").node.open_file.horizontal(n) end) - api.node.open.horizontal_no_picker = _n(function(n) require("nvim-tree.actions").node.open_file.horizontal_no_picker(n) end) - api.node.open.no_window_picker = _n(function(n) require("nvim-tree.actions").node.open_file.no_window_picker(n) end) - api.node.open.preview = _n(function(n) require("nvim-tree.actions").node.open_file.preview(n) end) - api.node.open.preview_no_picker = _n(function(n) require("nvim-tree.actions").node.open_file.preview_no_picker(n) end) - api.node.open.replace_tree_buffer = _n(function(n) require("nvim-tree.actions").node.open_file.replace_tree_buffer(n) end) - api.node.open.tab = _n(function(n) require("nvim-tree.actions").node.open_file.tab(n) end) - api.node.open.tab_drop = _n(function(n) require("nvim-tree.actions").node.open_file.tab_drop(n) end) - api.node.open.toggle_group_empty = _n(function(n) require("nvim-tree.actions").node.open_file.toggle_group_empty(n) end) - api.node.open.vertical = _n(function(n) require("nvim-tree.actions").node.open_file.vertical(n) end) - api.node.open.vertical_no_picker = _n(function(n) require("nvim-tree.actions").node.open_file.vertical_no_picker(n) end) - api.node.run.cmd = _n(function(n) require("nvim-tree.actions").node.run_command.run_file_command(n) end) - api.node.run.system = _n(function(n) require("nvim-tree.actions").node.system_open.fn(n) end) - api.node.show_info_popup = _n(function(n) require("nvim-tree.actions").node.file_popup.toggle_file_info(n) end) - - api.tree.change_root = __(function(path) require("nvim-tree.actions").tree.change_dir.fn(path) end) + api.node.navigate.diagnostics.next = __(function() require("nvim-tree.actions.moves.item").diagnostics_next() end) + api.node.navigate.diagnostics.next_recursive = __(function() require("nvim-tree.actions.moves.item").diagnostics_next_recursive() end) + api.node.navigate.diagnostics.prev = __(function() require("nvim-tree.actions.moves.item").diagnostics_prev() end) + api.node.navigate.diagnostics.prev_recursive = __(function() require("nvim-tree.actions.moves.item").diagnostics_prev_recursive() end) + api.node.navigate.git.next = __(function() require("nvim-tree.actions.moves.item").git_next() end) + api.node.navigate.git.next_recursive = __(function() require("nvim-tree.actions.moves.item").git_next_recursive() end) + api.node.navigate.git.next_skip_gitignored = __(function() require("nvim-tree.actions.moves.item").git_next_skip_gitignored() end) + api.node.navigate.git.prev = __(function() require("nvim-tree.actions.moves.item").git_prev() end) + api.node.navigate.git.prev_recursive = __(function() require("nvim-tree.actions.moves.item").git_prev_recursive() end) + api.node.navigate.git.prev_skip_gitignored = __(function() require("nvim-tree.actions.moves.item").git_prev_skip_gitignored() end) + api.node.navigate.opened.next = __(function() require("nvim-tree.actions.moves.item").opened_next() end) + api.node.navigate.opened.prev = __(function() require("nvim-tree.actions.moves.item").opened_prev() end) + api.node.navigate.parent = _n(function(n) require("nvim-tree.actions.moves.parent").move(n) end) + api.node.navigate.parent_close = _n(function(n) require("nvim-tree.actions.moves.parent").move_close(n) end) + api.node.navigate.sibling.first = _n(function(n) require("nvim-tree.actions.moves.sibling").first(n) end) + api.node.navigate.sibling.last = _n(function(n) require("nvim-tree.actions.moves.sibling").last(n) end) + api.node.navigate.sibling.next = _n(function(n) require("nvim-tree.actions.moves.sibling").next(n) end) + api.node.navigate.sibling.prev = _n(function(n) require("nvim-tree.actions.moves.sibling").prev(n) end) + api.node.open.drop = _n(function(n) require("nvim-tree.actions.node.open-file").drop(n) end) + api.node.open.edit = _n(function(n) require("nvim-tree.actions.node.open-file").edit(n) end) + api.node.open.horizontal = _n(function(n) require("nvim-tree.actions.node.open-file").horizontal(n) end) + api.node.open.horizontal_no_picker = _n(function(n) require("nvim-tree.actions.node.open-file").horizontal_no_picker(n) end) + api.node.open.no_window_picker = _n(function(n) require("nvim-tree.actions.node.open-file").no_window_picker(n) end) + api.node.open.preview = _n(function(n) require("nvim-tree.actions.node.open-file").preview(n) end) + api.node.open.preview_no_picker = _n(function(n) require("nvim-tree.actions.node.open-file").preview_no_picker(n) end) + api.node.open.replace_tree_buffer = _n(function(n) require("nvim-tree.actions.node.open-file").replace_tree_buffer(n) end) + api.node.open.tab = _n(function(n) require("nvim-tree.actions.node.open-file").tab(n) end) + api.node.open.tab_drop = _n(function(n) require("nvim-tree.actions.node.open-file").tab_drop(n) end) + api.node.open.toggle_group_empty = _n(function(n) require("nvim-tree.actions.node.open-file").toggle_group_empty(n) end) + api.node.open.vertical = _n(function(n) require("nvim-tree.actions.node.open-file").vertical(n) end) + api.node.open.vertical_no_picker = _n(function(n) require("nvim-tree.actions.node.open-file").vertical_no_picker(n) end) + api.node.run.cmd = _n(function(n) require("nvim-tree.actions.node.run-command").run_file_command(n) end) + api.node.run.system = _n(function(n) require("nvim-tree.actions.node.system-open").fn(n) end) + api.node.show_info_popup = _n(function(n) require("nvim-tree.actions.node.file-popup").toggle_file_info(n) end) + + api.tree.change_root = __(function(path) require("nvim-tree.actions.tree.change-dir").fn(path) end) api.tree.change_root_to_node = en(function(e, n) e:change_dir_to_node(n) end) api.tree.change_root_to_parent = en(function(e, n) e:dir_up(n) end) api.tree.close = __(function() require("nvim-tree.view").close() end) api.tree.close_in_all_tabs = __(function() require("nvim-tree.view").close_all_tabs() end) api.tree.close_in_this_tab = __(function() require("nvim-tree.view").close_this_tab_only() end) - api.tree.collapse_all = __(function(opts) require("nvim-tree.actions").tree.collapse.all(opts) end) + api.tree.collapse_all = __(function(opts) require("nvim-tree.actions.tree.collapse").all(opts) end) api.tree.expand_all = en(function(e, n, opts) e:expand_all(n, opts) end) - api.tree.find_file = __(function(opts) require("nvim-tree.actions").tree.find_file.fn(opts) end) - api.tree.focus = __(function(opts) require("nvim-tree.actions").tree.open.fn(opts) end) + api.tree.find_file = __(function(opts) require("nvim-tree.actions.tree.find_file").fn(opts) end) + api.tree.focus = __(function(opts) require("nvim-tree.actions.tree.open").fn(opts) end) api.tree.get_node_under_cursor = en(function(e) return e:get_node_at_cursor() end) api.tree.get_nodes = en(function(e) return e:get_nodes() end) api.tree.is_tree_buf = __(function(bufnr) return require("nvim-tree.utils").is_nvim_tree_buf(bufnr) end) api.tree.is_visible = __(function(opts) return require("nvim-tree.view").is_visible(opts) end) - api.tree.open = __(function(opts) require("nvim-tree.actions").tree.open.fn(opts) end) + api.tree.open = __(function(opts) require("nvim-tree.actions.tree.open").fn(opts) end) api.tree.reload = e_(function(e) e:reload_explorer() end) api.tree.reload_git = e_(function(e) e:reload_git() end) - api.tree.resize = __(function(opts) require("nvim-tree.actions").tree.resize.fn(opts) end) - api.tree.search_node = __(function() require("nvim-tree.actions").finders.search_node.fn() end) - api.tree.toggle = __(function(opts) require("nvim-tree.actions").tree.toggle.fn(opts) end) + api.tree.resize = __(function(opts) require("nvim-tree.actions.tree.resize").fn(opts) end) + api.tree.search_node = __(function() require("nvim-tree.actions.finders.search-node").fn() end) + api.tree.toggle = __(function(opts) require("nvim-tree.actions.tree.toggle").fn(opts) end) api.tree.toggle_help = __(function() require("nvim-tree.help").toggle() end) api.tree.winid = __(function(opts) return require("nvim-tree.view").winid(opts) end) diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua index 5b1211ccf43..5f44f811287 100644 --- a/lua/nvim-tree/config.lua +++ b/lua/nvim-tree/config.lua @@ -1,21 +1,31 @@ --This will be required after api, before setup. --This file should have minimal requires that are cheap and have no dependencies or are already required. -local notify = require("nvim-tree.notify") -local legacy = require("nvim-tree.legacy") -local utils = require("nvim-tree.utils") - --- short names like g are used rather than getters to keep code brief - +--Short names like g are used rather than getters to keep code brief local M = { - ---@type nvim_tree.config immutable default config + ---Immutable default config + ---@type nvim_tree.config d = {}, - ---@type nvim_tree.config? global current config, nil until setup called, mutable + ---Global current config, nil until setup called, mutable + ---@type nvim_tree.config? g = nil, - ---@type nvim_tree.config? immutable raw user config, nil when no user config passed to setup + ---Immutable raw user config, nil when no user config passed to setup + ---@type nvim_tree.config? u = nil, + + ---Immutable OS, detected once on first require + ---@type table<"unix"|"macos"|"wsl"|"windows", boolean> + os = nil +} + +M.os = { + unix = vim.fn.has("unix") == 1, + macos = vim.fn.has("mac") == 1 or vim.fn.has("macunix") == 1, + wsl = vim.fn.has("wsl") == 1, + -- false for WSL + windows = vim.fn.has("win32") == 1 or vim.fn.has("win32unix") == 1, } M.d = { -- config-default-start @@ -304,14 +314,6 @@ M.d = { -- config-default-start }, } -- config-default-end --- Immediately apply OS specific localisations to defaults -if utils.is_macos or utils.is_windows then - M.d.trash.cmd = "trash" -end -if utils.is_windows then - M.d.filesystem_watchers.max_events = 1000 -end - local FIELD_SKIP_VALIDATE = { open_win_config = true, } @@ -484,7 +486,30 @@ local function validate_config(u) validate(u, M.d, ACCEPTED_STRINGS, ACCEPTED_TYPES, ACCEPTED_ENUMS, "") if msg then - notify.warn(msg .. "\n\nsee :help nvim-tree-config for available configuration options") + require("nvim-tree.notify").warn(msg .. "\n\nsee :help nvim-tree-config for available configuration options") + end +end + +---Localise the (default) config with OS specifics +---@param d nvim_tree.config +local function localise_config(d) + -- Trash + if M.os.macos or M.os.windows then + d.trash.cmd = "trash" + end + + -- Watchers + if M.os.windows then + d.filesystem_watchers.max_events = 1000 + end +end + +---Normalise the (user) config +---@param u nvim_tree.config +local function process_config(u) + -- Open + if u.actions.open_file.window_picker.chars then + u.actions.open_file.window_picker.chars = tostring(u.actions.open_file.window_picker.chars):upper() end end @@ -495,7 +520,7 @@ end function M.setup(u) if not u or type(u) ~= "table" then if u then - notify.warn(string.format("invalid config type \"%s\" passed to setup, using defaults", type(u))) + require("nvim-tree.notify").warn(string.format("invalid config type \"%s\" passed to setup, using defaults", type(u))) end M.g = vim.deepcopy(M.d) M.u = nil @@ -505,10 +530,12 @@ function M.setup(u) -- retain user for reference M.u = vim.deepcopy(u) - legacy.migrate_config(u) + require("nvim-tree.legacy").migrate_config(u) validate_config(u) + process_config(u) + -- set global to the validated and populated user config M.g = vim.tbl_deep_extend("force", M.d, u) end @@ -531,4 +558,7 @@ function M.g_clone() return vim.deepcopy(M.g) end +---Immediately localise the defaults once and once only +localise_config(M.d) + return M diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index fcfce4821ea..eb9b5f5d097 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -1,5 +1,6 @@ local appearance = require("nvim-tree.appearance") local buffers = require("nvim-tree.buffers") +local config = require("nvim-tree.config") local core = require("nvim-tree.core") local git = require("nvim-tree.git") local log = require("nvim-tree.log") @@ -25,8 +26,6 @@ local FileNode = require("nvim-tree.node.file") local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON local find_file = require("nvim-tree.actions.finders.find-file") -local config - ---@class (exact) Explorer: RootNode ---@field uid_explorer number vim.loop.hrtime() at construction time ---@field opts table user options @@ -59,7 +58,7 @@ function Explorer:new(args) self.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. self.uid_explorer, {}) self.open = true - self.opts = config + self.opts = config.g self.sorters = Sorter({ explorer = self }) @@ -300,7 +299,7 @@ function Explorer:reload(node, project) ) local single_child = node:single_child_directory() - if config.renderer.group_empty and node.parent and single_child then + if self.opts.renderer.group_empty and node.parent and single_child then node.group_next = single_child local ns = self:reload(single_child, project) node.nodes = ns or {} @@ -448,7 +447,7 @@ function Explorer:explore(node, project, parent) local is_root = not node.parent local single_child = node:single_child_directory() - if config.renderer.group_empty and not is_root and single_child then + if self.opts.renderer.group_empty and not is_root and single_child then local child_cwd = single_child.link_to or single_child.absolute_path local child_project = git.load_project(child_cwd) node.group_next = single_child @@ -755,14 +754,14 @@ end ---@return boolean function Explorer:prevent_cwd_change(foldername) local is_same_cwd = foldername == self.absolute_path - local is_restricted_above = config.actions.change_dir.restrict_above_cwd and foldername < vim.fn.getcwd(-1, -1) + local is_restricted_above = self.opts.actions.change_dir.restrict_above_cwd and foldername < vim.fn.getcwd(-1, -1) return is_same_cwd or is_restricted_above end ---@private ---@return boolean function Explorer:should_change_dir() - return config.actions.change_dir.enable and vim.tbl_isempty(vim.v.event) + return self.opts.actions.change_dir.enable and vim.tbl_isempty(vim.v.event) end ---@private @@ -782,7 +781,7 @@ function Explorer:force_dirchange(foldername, should_open_view, should_init) local valid_dir = vim.fn.isdirectory(foldername) == 1 -- prevent problems on non existing dirs if valid_dir then if self:should_change_dir() then - self:cd(config.actions.change_dir.global, foldername) + self:cd(self.opts.actions.change_dir.global, foldername) end if should_init ~= false then @@ -835,8 +834,4 @@ function Explorer:change_dir_to_node(node) end end -function Explorer:setup(opts) - config = opts -end - return Explorer diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 81e6da81657..3f2316690e9 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -1,5 +1,6 @@ local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") +local config = require("nvim-tree.config") local Class = require("nvim-tree.classic") local Iterator = require("nvim-tree.iterators.node-iterator") @@ -56,7 +57,7 @@ local overlay_bufnr = 0 local overlay_winnr = 0 local function remove_overlay(self) - if view.View.float.enable and view.View.float.quit_on_focus_loss then + if config.g.view.float.enable and config.g.view.float.quit_on_focus_loss then -- return to normal nvim-tree float behaviour when filter window is closed vim.api.nvim_create_autocmd("WinLeave", { pattern = "NvimTree_*", @@ -166,7 +167,7 @@ local function calculate_overlay_win_width(self) end local function create_overlay(self) - if view.View.float.enable then + if config.g.view.float.enable then -- don't close nvim-tree float when focus is changed to filter window vim.api.nvim_clear_autocmds({ event = "WinLeave", diff --git a/lua/nvim-tree/explorer/watch.lua b/lua/nvim-tree/explorer/watch.lua index 8641a291b39..a0884304f0e 100644 --- a/lua/nvim-tree/explorer/watch.lua +++ b/lua/nvim-tree/explorer/watch.lua @@ -2,12 +2,13 @@ local log = require("nvim-tree.log") local git = require("nvim-tree.git") local utils = require("nvim-tree.utils") local notify = require("nvim-tree.notify") +local config = require("nvim-tree.config") local Watcher = require("nvim-tree.watcher").Watcher -local M = { - config = {}, - uid = 0, -} +local M = {} + +-- monotonically increasing, unique +local uid = 0 ---@param path string ---@return boolean @@ -47,11 +48,11 @@ local function is_folder_ignored(path) ---Return true when p matches an entry in dirs, escaping for windows ---@param p string absolute path - ---@param dirs string[] absolute or relative + ---@param dirs string[] regexes ---@return boolean local function matches_dirs(p, dirs) for _, dir in ipairs(dirs) do - if utils.is_windows then + if config.os.windows then dir = dir:gsub("/", "\\\\") or dir end @@ -62,22 +63,22 @@ local function is_folder_ignored(path) return false end - if type(M.config.filesystem_watchers.ignore_dirs) == "table" then - if matches_dirs(path, M.config.filesystem_watchers.ignore_dirs) then + if type(config.g.filesystem_watchers.ignore_dirs) == "table" then + if matches_dirs(path, config.g.filesystem_watchers.ignore_dirs --[[@as string[] ]]) then return true end - elseif type(M.config.filesystem_watchers.ignore_dirs) == "function" then - if M.config.filesystem_watchers.ignore_dirs(path) then + elseif type(config.g.filesystem_watchers.ignore_dirs) == "function" then + if config.g.filesystem_watchers.ignore_dirs(path) then return true end end - if type(M.config.filesystem_watchers.whitelist_dirs) == "table" and #M.config.filesystem_watchers.whitelist_dirs > 0 then - if not matches_dirs(path, M.config.filesystem_watchers.whitelist_dirs) then + if type(config.g.filesystem_watchers.whitelist_dirs) == "table" and #config.g.filesystem_watchers.whitelist_dirs > 0 then + if not matches_dirs(path, config.g.filesystem_watchers.whitelist_dirs --[[@as string[] ]]) then return true end - elseif type(M.config.filesystem_watchers.whitelist_dirs) == "function" then - if not M.config.filesystem_watchers.whitelist_dirs(path) then + elseif type(config.g.filesystem_watchers.whitelist_dirs) == "function" then + if not config.g.filesystem_watchers.whitelist_dirs(path) then return true end end @@ -88,7 +89,7 @@ end ---@param node DirectoryNode ---@return Watcher|nil function M.create_watcher(node) - if not M.config.filesystem_watchers.enable or type(node) ~= "table" then + if not config.g.filesystem_watchers.enable or type(node) ~= "table" then return nil end @@ -105,18 +106,18 @@ function M.create_watcher(node) watcher.data.outstanding_events = watcher.data.outstanding_events + 1 -- disable watcher when outstanding exceeds max - if M.config.filesystem_watchers.max_events > 0 and watcher.data.outstanding_events > M.config.filesystem_watchers.max_events then + if config.g.filesystem_watchers.max_events > 0 and watcher.data.outstanding_events > config.g.filesystem_watchers.max_events then notify.error(string.format( "Observed %d consecutive file system events with interval < %dms, exceeding filesystem_watchers.max_events=%s. Disabling watcher for directory '%s'. Consider adding this directory to filesystem_watchers.ignore_dirs", watcher.data.outstanding_events, - M.config.filesystem_watchers.debounce_delay, - M.config.filesystem_watchers.max_events, + config.g.filesystem_watchers.debounce_delay, + config.g.filesystem_watchers.max_events, node.absolute_path )) node:destroy_watcher() end - utils.debounce(watcher.data.context, M.config.filesystem_watchers.debounce_delay, function() + utils.debounce(watcher.data.context, config.g.filesystem_watchers.debounce_delay, function() if watcher.destroyed then return end @@ -133,20 +134,15 @@ function M.create_watcher(node) end) end - M.uid = M.uid + 1 + uid = uid + 1 return Watcher:create({ path = path, callback = callback, data = { - context = "explorer:watch:" .. path .. ":" .. M.uid, + context = "explorer:watch:" .. path .. ":" .. uid, outstanding_events = 0, -- unprocessed events that have not been debounced } }) end -function M.setup(opts) - M.config.filesystem_watchers = opts.filesystem_watchers - M.uid = 0 -end - return M diff --git a/lua/nvim-tree/notify.lua b/lua/nvim-tree/notify.lua index 7449960041f..c7a63ef9bef 100644 --- a/lua/nvim-tree/notify.lua +++ b/lua/nvim-tree/notify.lua @@ -1,9 +1,6 @@ -local M = {} +local config = require("nvim-tree.config") -local config = { - threshold = vim.log.levels.INFO, - absolute_path = true, -} +local M = {} local title_support ---@return boolean @@ -27,8 +24,11 @@ local modes = { } do + ---@param level vim.log.levels + ---@param msg string local dispatch = function(level, msg) - if level < config.threshold or not msg then + local threshold = config.g and config.g.notify.threshold or config.d.notify.threshold + if level < threshold or not msg then return end @@ -52,17 +52,11 @@ end ---@param path string ---@return string function M.render_path(path) - if config.absolute_path then + if config.g and config.g.notify.absolute_path then return path else return vim.fn.fnamemodify(path .. "/", ":h:t") end end -function M.setup(opts) - opts = opts or {} - config.threshold = opts.notify.threshold or vim.log.levels.INFO - config.absolute_path = opts.notify.absolute_path -end - return M diff --git a/lua/nvim-tree/renderer/components/full-name.lua b/lua/nvim-tree/renderer/components/full-name.lua index 6c41f3909d9..adf5a4af6ad 100644 --- a/lua/nvim-tree/renderer/components/full-name.lua +++ b/lua/nvim-tree/renderer/components/full-name.lua @@ -1,3 +1,5 @@ +local config = require("nvim-tree.config") + local M = {} local utils = require("nvim-tree.utils") @@ -33,7 +35,7 @@ local function effective_win_width() return win_width - win_info[1].textoff end -local function show(opts) +local function show() local line_nr = vim.api.nvim_win_get_cursor(0)[1] if vim.wo.wrap then return @@ -96,15 +98,14 @@ local function show(opts) end end vim.cmd([[ setlocal nowrap noswapfile nobuflisted buftype=nofile bufhidden=wipe ]]) - if opts.view.cursorline then + if config.g.view.cursorline then vim.cmd([[ setlocal cursorline cursorlineopt=both ]]) end end) end -M.setup = function(opts) - M.config = opts.renderer - if not M.config.full_name then +function M.setup_autocommands() + if not config.g.renderer.full_name then return end @@ -124,7 +125,7 @@ M.setup = function(opts) pattern = { "NvimTree_*" }, callback = function() if utils.is_nvim_tree_buf(0) then - show(opts) + show() end end, }) diff --git a/lua/nvim-tree/renderer/components/init.lua b/lua/nvim-tree/renderer/components/init.lua index 748bf889497..a06827422dd 100644 --- a/lua/nvim-tree/renderer/components/init.lua +++ b/lua/nvim-tree/renderer/components/init.lua @@ -1,11 +1,9 @@ local M = {} -M.full_name = require("nvim-tree.renderer.components.full-name") M.devicons = require("nvim-tree.renderer.components.devicons") M.padding = require("nvim-tree.renderer.components.padding") function M.setup(opts) - M.full_name.setup(opts) M.devicons.setup(opts) M.padding.setup(opts) end diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 32106d543e1..bb731f8ceb6 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -1,13 +1,9 @@ +local config = require("nvim-tree.config") + local M = { debouncers = {}, } -M.is_unix = vim.fn.has("unix") == 1 -M.is_macos = vim.fn.has("mac") == 1 or vim.fn.has("macunix") == 1 -M.is_wsl = vim.fn.has("wsl") == 1 --- false for WSL -M.is_windows = vim.fn.has("win32") == 1 or vim.fn.has("win32unix") == 1 - ---@param haystack string ---@param needle string ---@return boolean @@ -78,7 +74,7 @@ function M.path_relative(path, relative_to) end local norm_path = path - if M.is_windows then + if config.os.windows then norm_path = win_norm_path(norm_path) end @@ -187,13 +183,13 @@ function M.rename_loaded_buffers(old_path, new_path) end local is_windows_drive = function(path) - return (M.is_windows) and (path:match("^%a:\\$") ~= nil) + return (config.os.windows) and (path:match("^%a:\\$") ~= nil) end ---@param path string path to file or directory ---@return boolean function M.file_exists(path) - if not (M.is_windows or M.is_wsl) then + if not (config.os.windows or config.os.wsl) then local _, error = vim.loop.fs_stat(path) return error == nil end @@ -236,7 +232,7 @@ end ---@param path string ---@return string function M.canonical_path(path) - if M.is_windows and path:match("^%a:") then + if config.os.windows and path:match("^%a:") then return path:sub(1, 1):upper() .. path:sub(2) end return path @@ -257,7 +253,7 @@ function M.escape_special_chars(path) if path == nil then return path end - return M.is_windows and escape_special_char_for_windows(path) or path + return config.os.windows and escape_special_char_for_windows(path) or path end local function round(value) @@ -447,7 +443,7 @@ end ---@param absolute_path string ---@return boolean function M.is_executable(absolute_path) - if M.is_windows or M.is_wsl then + if config.os.windows or config.os.wsl then --- executable detection on windows is buggy and not performant hence it is disabled return false else diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 638f52353b3..6e36c8e50da 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -2,6 +2,7 @@ local events = require("nvim-tree.events") local utils = require("nvim-tree.utils") local log = require("nvim-tree.log") local notify = require("nvim-tree.notify") +local config = require("nvim-tree.config") ---@class OpenInWinOpts ---@field hijack_current_buf boolean|nil default true @@ -18,15 +19,13 @@ local DEFAULT_LINES_EXCLUDED = { local DEFAULT_PADDING = 1 M.View = { - adaptive_size = false, - centralize_selection = false, - tabpages = {}, - cursors = {}, - hide_root_folder = false, - live_filter = { + adaptive_size = false, + tabpages = {}, + cursors = {}, + live_filter = { prev_focused_node = nil, }, - winopts = { + winopts = { relativenumber = false, number = false, list = false, @@ -178,17 +177,17 @@ local function set_window_options_and_buffer() end end ----@return table +---@return vim.api.keyset.win_config local function open_win_config() - if type(M.View.float.open_win_config) == "function" then - return M.View.float.open_win_config() + if type(config.g.view.float.open_win_config) == "function" then + return config.g.view.float.open_win_config() else - return M.View.float.open_win_config + return config.g.view.float.open_win_config --[[ @as vim.api.keyset.win_config ]] end end local function open_window() - if M.View.float.enable then + if config.g.view.float.enable then vim.api.nvim_open_win(0, true, open_win_config()) else vim.api.nvim_command("vsp") @@ -275,7 +274,7 @@ end ---@param tabpage integer|nil function M.close(tabpage) - if M.View.tab.sync.close then + if config.g.tab.sync.close then M.close_all_tabs() elseif tabpage then close(tabpage) @@ -348,7 +347,7 @@ end ---@param size string|number|nil function M.resize(size) - if M.View.float.enable and not M.View.adaptive_size then + if config.g.view.float.enable and not M.View.adaptive_size then -- if the floating windows's adaptive size is not desired, then the -- float size should be defined in view.float.open_win_config return @@ -370,7 +369,6 @@ function M.resize(size) if size then M.View.width = size - M.View.height = size end if not M.is_visible() then @@ -383,7 +381,7 @@ function M.resize(size) if new_size ~= vim.api.nvim_win_get_width(winnr) then vim.api.nvim_win_set_width(winnr, new_size) - if not M.View.preserve_window_proportions then + if not config.g.view.preserve_window_proportions then vim.cmd(":wincmd =") end end @@ -392,7 +390,7 @@ function M.resize(size) end function M.reposition_window() - local move_to = move_tbl[M.View.side] + local move_to = move_tbl[config.g.view.side] vim.api.nvim_command("wincmd " .. move_to) M.resize() end @@ -575,7 +573,7 @@ end ---@param cwd string|nil ---@return boolean function M.is_root_folder_visible(cwd) - return cwd ~= "/" and not M.View.hide_root_folder + return cwd ~= "/" and config.g.renderer.root_folder_label ~= false end -- used on ColorScheme event @@ -603,9 +601,9 @@ function M.configure_width(width) M.View.root_excluded = vim.tbl_contains(lines_excluded, "root") M.View.padding = width.padding or DEFAULT_PADDING elseif width == nil then - if M.config.width ~= nil then + if config.g.view.width ~= nil then -- if we had input config - fallback to it - M.configure_width(M.config.width) + M.configure_width(config.g.view.width) else -- otherwise - restore initial width M.View.width = M.View.initial_width @@ -616,23 +614,15 @@ function M.configure_width(width) end end +---@param opts nvim_tree.config function M.setup(opts) local options = opts.view or {} - M.View.centralize_selection = options.centralize_selection - M.View.side = (options.side == "right") and "right" or "left" - M.View.height = options.height - M.View.hide_root_folder = opts.renderer.root_folder_label == false - M.View.tab = opts.tab - M.View.preserve_window_proportions = options.preserve_window_proportions M.View.winopts.cursorline = options.cursorline M.View.winopts.cursorlineopt = options.cursorlineopt M.View.winopts.number = options.number M.View.winopts.relativenumber = options.relativenumber M.View.winopts.signcolumn = options.signcolumn - M.View.float = options.float - M.on_attach = opts.on_attach - M.config = options M.configure_width(options.width) M.View.initial_width = get_width() diff --git a/lua/nvim-tree/watcher.lua b/lua/nvim-tree/watcher.lua index f188f6aece0..b29988538ab 100644 --- a/lua/nvim-tree/watcher.lua +++ b/lua/nvim-tree/watcher.lua @@ -89,7 +89,7 @@ function Event:start() end local message = string.format("File system watcher failed (%s) for path %s, halting watcher.", err, self.path) - if err == "EPERM" and (utils.is_windows or utils.is_wsl) then + if err == "EPERM" and (config.os.windows or config.os.wsl) then -- on directory removal windows will cascade the filesystem events out of order log.line("watcher", message) self:destroy() @@ -258,7 +258,7 @@ end ---@param path string ---@return boolean function M.is_fs_event_capable(path) - if not utils.is_windows then + if not config.os.windows then return true end