diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 528cbf73f1b..6fb8a18489d 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1915,9 +1915,14 @@ Config: bookmarks *nvim-tree-config-bookmarks* Config: help *nvim-tree-config-help* *nvim_tree.config.help* + {sort_by} *nvim_tree.config.help.sort_by* + + Sort help entries alphabetically by: + • `"key"`: map lhs + • `"desc"`: description Fields: ~ - • {sort_by}? (`"key"|"desc"`, default: `"key"`) Alphabetically. + • {sort_by}? (`nvim_tree.config.help.sort_by`) (default: `"key"`) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index db711e84ce1..dec52e85d42 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -285,19 +285,11 @@ function M.setup(config_user) end require("nvim-tree.actions").setup(config.g) - require("nvim-tree.keymap").setup(config.g) require("nvim-tree.appearance").setup() - require("nvim-tree.diagnostics").setup(config.g) require("nvim-tree.explorer"):setup(config.g) require("nvim-tree.explorer.watch").setup(config.g) - require("nvim-tree.git").setup(config.g) - require("nvim-tree.git.utils").setup(config.g) require("nvim-tree.view").setup(config.g) - require("nvim-tree.lib").setup(config.g) require("nvim-tree.renderer.components").setup(config.g) - require("nvim-tree.buffers").setup(config.g) - require("nvim-tree.help").setup(config.g) - require("nvim-tree.watcher").setup(config.g) setup_autocommands() diff --git a/lua/nvim-tree/_meta/config/help.lua b/lua/nvim-tree/_meta/config/help.lua index b47a10e628e..4e31ce03ff0 100644 --- a/lua/nvim-tree/_meta/config/help.lua +++ b/lua/nvim-tree/_meta/config/help.lua @@ -3,9 +3,14 @@ error("Cannot require a meta file") +---{sort_by} [nvim_tree.config.help.sort_by]() +--- +---Sort help entries alphabetically by: +---- `"key"`: map lhs +---- `"desc"`: description +---@alias nvim_tree.config.help.sort_by "key"|"desc" --- ---@class nvim_tree.config.help --- ----Alphabetically. ---(default: `"key"`) ----@field sort_by? "key"|"desc" +---@field sort_by? nvim_tree.config.help.sort_by diff --git a/lua/nvim-tree/buffers.lua b/lua/nvim-tree/buffers.lua index 4ccbe78adbc..a9a1cbef7de 100644 --- a/lua/nvim-tree/buffers.lua +++ b/lua/nvim-tree/buffers.lua @@ -1,3 +1,5 @@ +local config = require("nvim-tree.config") + local DirectoryNode = require("nvim-tree.node.directory") local M = {} @@ -26,7 +28,7 @@ end ---@param node Node ---@return boolean function M.is_modified(node) - if not M.config.modified.enable then + if not config.g.modified.enable then return false end @@ -36,11 +38,11 @@ function M.is_modified(node) local dir = node:as(DirectoryNode) if dir then - if not M.config.modified.show_on_dirs then + if not config.g.modified.show_on_dirs then return false end - if dir.open and not M.config.modified.show_on_open_dirs then + if dir.open and not config.g.modified.show_on_open_dirs then return false end end @@ -55,11 +57,4 @@ function M.is_opened(node) return node and vim.fn.bufloaded(node.absolute_path) > 0 end ----@param opts nvim_tree.config -function M.setup(opts) - M.config = { - modified = opts.modified, - } -end - return M diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua index 7d1d3cad905..f9c5bf66914 100644 --- a/lua/nvim-tree/config.lua +++ b/lua/nvim-tree/config.lua @@ -11,10 +11,10 @@ local M = { ---@type nvim_tree.config immutable default config d = {}, - ---@type nvim_tree.config? global current config, nil until setup called + ---@type nvim_tree.config? global current config, nil until setup called, mutable g = nil, - ---@type nvim_tree.config? raw user config, nil when no user config passed to setup + ---@type nvim_tree.config? immutable raw user config, nil when no user config passed to setup u = nil, } diff --git a/lua/nvim-tree/diagnostics.lua b/lua/nvim-tree/diagnostics.lua index 853966b983a..3c8c527e2b7 100644 --- a/lua/nvim-tree/diagnostics.lua +++ b/lua/nvim-tree/diagnostics.lua @@ -1,3 +1,4 @@ +local config = require("nvim-tree.config") local core = require("nvim-tree.core") local utils = require("nvim-tree.utils") local view = require("nvim-tree.view") @@ -38,11 +39,16 @@ local function uniformize_path(path) end ---Severity is within diagnostics.severity.min, diagnostics.severity.max ----@param severity lsp.DiagnosticSeverity ----@param config table +---Alway in range when using vim.diagnostic.Opts: +---@see nvim_tree.config.diagnostics.diagnostic_opts +---@param severity vim.diagnostic.Severity ---@return boolean -local function is_severity_in_range(severity, config) - return config.max <= severity and severity <= config.min +local function is_severity_in_range(severity) + if config.g.diagnostics.diagnostic_opts then + return true + else + return severity >= config.g.diagnostics.severity.max and severity <= config.g.diagnostics.severity.min + end end ---Handle any COC exceptions, preventing any propagation @@ -86,7 +92,7 @@ local function from_coc() local bufname = uniformize_path(diagnostic.file) local coc_severity = COC_SEVERITY_LEVELS[diagnostic.severity] local highest_severity = buffer_severity[bufname] or coc_severity - if is_severity_in_range(highest_severity, M.severity) then + if is_severity_in_range(highest_severity) then buffer_severity[bufname] = math.min(highest_severity, coc_severity) end end @@ -122,7 +128,7 @@ end ---On disabling LSP, a reset event will be sent for all buffers. ---@param ev table standard event with data.diagnostics populated function M.update_lsp(ev) - if not M.enable or not ev or not ev.data or not ev.data.diagnostics then + if not config.g.diagnostics.enable or not ev or not ev.data or not ev.data.diagnostics then return end @@ -138,7 +144,7 @@ function M.update_lsp(ev) -- most severe (lowest) severity in user range for _, diagnostic in ipairs(diagnostics) do - if diagnostic.severity >= M.severity.max and diagnostic.severity <= M.severity.min then + if is_severity_in_range(diagnostic.severity) then if not new_severity or diagnostic.severity < new_severity then new_severity = diagnostic.severity end @@ -150,7 +156,7 @@ function M.update_lsp(ev) NODE_SEVERITIES[bufname] = new_severity NODE_SEVERITIES_VERSION = NODE_SEVERITIES_VERSION + 1 - utils.debounce("DiagnosticChanged redraw", M.debounce_delay, function() + utils.debounce("DiagnosticChanged redraw", config.g.diagnostics.debounce_delay, function() local profile_redraw = log.profile_start("DiagnosticChanged redraw") local explorer = core.get_explorer() @@ -168,10 +174,10 @@ end ---Fired on CocDiagnosticChanged events: ---debounced retrieval, cache update, version increment and draw function M.update_coc() - if not M.enable then + if not config.g.diagnostics.enable then return end - utils.debounce("CocDiagnosticChanged update", M.debounce_delay, function() + utils.debounce("CocDiagnosticChanged update", config.g.diagnostics.debounce_delay, function() local profile = log.profile_start("CocDiagnosticChanged update") NODE_SEVERITIES = from_coc() NODE_SEVERITIES_VERSION = NODE_SEVERITIES_VERSION + 1 @@ -198,12 +204,12 @@ end ---@param node Node ---@return DiagStatus|nil function M.get_diag_status(node) - if not M.enable then + if not config.g.diagnostics.enable then return nil end -- dir but we shouldn't show on dirs at all - if node:is(DirectoryNode) and not M.show_on_dirs then + if node:is(DirectoryNode) and not config.g.diagnostics.show_on_dirs then return nil end @@ -222,26 +228,10 @@ function M.get_diag_status(node) end -- dir is closed or we should show on open_dirs - if not dir.open or M.show_on_open_dirs then + if not dir.open or config.g.diagnostics.show_on_open_dirs then return node.diag_status end return nil end -function M.setup(opts) - M.enable = opts.diagnostics.enable - M.debounce_delay = opts.diagnostics.debounce_delay - M.severity = opts.diagnostics.diagnostic_opts and { - min = vim.diagnostic.severity.HINT, - max = vim.diagnostic.severity.ERROR - } or opts.diagnostics.severity - - if M.enable then - log.line("diagnostics", "setup") - end - - M.show_on_dirs = opts.diagnostics.show_on_dirs - M.show_on_open_dirs = opts.diagnostics.show_on_open_dirs -end - return M diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 2ad0243ed06..e7e3d8361e1 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -1,5 +1,6 @@ local log = require("nvim-tree.log") local utils = require("nvim-tree.utils") +local config = require("nvim-tree.config") local git_utils = require("nvim-tree.git.utils") local GitRunner = require("nvim-tree.git.runner") @@ -88,7 +89,7 @@ end ---@return nvim_tree.git.Project[] maybe empty function M.reload_all_projects() - if not M.config.git.enable then + if not config.g.git.enable then return {} end @@ -106,7 +107,7 @@ end function M.reload_project(toplevel, path, callback) local project = M._projects_by_toplevel[toplevel] --[[@as nvim_tree.git.Project]] - if not toplevel or not project or not M.config.git.enable then + if not toplevel or not project or not config.g.git.enable then if callback then callback() end @@ -126,7 +127,7 @@ function M.reload_project(toplevel, path, callback) path = path, list_untracked = git_utils.should_show_untracked(toplevel), list_ignored = true, - timeout = M.config.git.timeout, + timeout = config.g.git.timeout, } if callback then @@ -161,7 +162,7 @@ function M.get_toplevel(path) return nil end - if not M.config.git.enable then + if not config.g.git.enable then return nil end @@ -194,15 +195,15 @@ function M.get_toplevel(path) local toplevel_norm = vim.fn.fnamemodify(toplevel, ":p") -- ignore disabled paths - if type(M.config.git.disable_for_dirs) == "table" then - for _, disabled_for_dir in ipairs(M.config.git.disable_for_dirs) do + if type(config.g.git.disable_for_dirs) == "table" then + for _, disabled_for_dir in ipairs(config.g.git.disable_for_dirs --[[@as string[] ]]) do local disabled_norm = vim.fn.fnamemodify(disabled_for_dir, ":p") if toplevel_norm == disabled_norm then return nil end end - elseif type(M.config.git.disable_for_dirs) == "function" then - if M.config.git.disable_for_dirs(toplevel_norm) then + elseif type(config.g.git.disable_for_dirs) == "function" then + if config.g.git.disable_for_dirs(toplevel_norm) then return nil end end @@ -220,7 +221,7 @@ function M.get_toplevel(path) end local function reload_tree_at(toplevel) - if not M.config.git.enable or not toplevel then + if not config.g.git.enable or not toplevel then return nil end @@ -259,7 +260,7 @@ end ---@param path string absolute ---@return nvim_tree.git.Project maybe empty function M.load_project(path) - if not M.config.git.enable then + if not config.g.git.enable then return {} end @@ -278,17 +279,17 @@ function M.load_project(path) toplevel = toplevel, list_untracked = git_utils.should_show_untracked(toplevel), list_ignored = true, - timeout = M.config.git.timeout, + timeout = config.g.git.timeout, }) local watcher = nil - if M.config.filesystem_watchers.enable then + if config.g.filesystem_watchers.enable then log.line("watcher", "git start") ---@param w Watcher local callback = function(w) log.line("watcher", "git event scheduled '%s'", w.data.toplevel) - utils.debounce("git:watcher:" .. w.data.toplevel, M.config.filesystem_watchers.debounce_delay, function() + utils.debounce("git:watcher:" .. w.data.toplevel, config.g.filesystem_watchers.debounce_delay, function() if w.destroyed then return end @@ -403,12 +404,7 @@ end function M.disable_git_integration() log.line("git", "disabling git integration") M.purge_state() - M.config.git.enable = false -end - -function M.setup(opts) - M.config.git = opts.git - M.config.filesystem_watchers = opts.filesystem_watchers + config.g.git.enable = false end return M diff --git a/lua/nvim-tree/git/utils.lua b/lua/nvim-tree/git/utils.lua index 25d83f35d85..149abc20ce5 100644 --- a/lua/nvim-tree/git/utils.lua +++ b/lua/nvim-tree/git/utils.lua @@ -1,9 +1,24 @@ local log = require("nvim-tree.log") local utils = require("nvim-tree.utils") +local config = require("nvim-tree.config") -local M = { - use_cygpath = false, -} +local M = {} + +---@type boolean? +local use_cygpath_cached = nil + +---true when cygwin enabled and present +---@return boolean +local function use_cygpath() + if use_cygpath_cached == nil then + if config.g.git.cygwin_support then + use_cygpath_cached = vim.fn.executable("cygpath") == 1 + else + use_cygpath_cached = false + end + end + return use_cygpath_cached +end --- Execute system command ---@param cmd string[] @@ -11,7 +26,7 @@ local M = { ---@return integer exit code local function system(cmd) if vim.fn.has("nvim-0.10") == 1 then - local obj = vim.system(cmd):wait(M.opts.git.timeout) + local obj = vim.system(cmd):wait(config.g.git.timeout) return obj.stdout or "", obj.code else return vim.fn.system(cmd), vim.v.shell_error @@ -50,7 +65,7 @@ function M.get_toplevel(cwd) if vim.fn.has("win32") == 1 then -- msys2 git support -- cygpath calls must in array format to avoid shell compatibility issues - if M.use_cygpath then + if use_cygpath() then toplevel = vim.fn.system({ "cygpath", "-w", toplevel }) if vim.v.shell_error ~= 0 then return nil, nil @@ -195,11 +210,4 @@ function M.git_status_dir(parent_ignored, project, path, path_fallback) return ns end -function M.setup(opts) - if opts.git.cygwin_support then - M.use_cygpath = vim.fn.executable("cygpath") == 1 - end - M.opts = opts -end - return M diff --git a/lua/nvim-tree/help.lua b/lua/nvim-tree/help.lua index 7757b706bbe..897f7ea5dfe 100644 --- a/lua/nvim-tree/help.lua +++ b/lua/nvim-tree/help.lua @@ -1,5 +1,6 @@ local keymap = require("nvim-tree.keymap") -local api = {} -- circular dependency +local api = require("nvim-tree.api") +local config = require("nvim-tree.config") local PAT_MOUSE = "^<.*Mouse" local PAT_CTRL = "^ "nx" local merged = {} @@ -111,7 +110,7 @@ local function compute(map) -- sorter function for mappings local sort_fn - if M.config.sort_by == "desc" then + if config.g.help.sort_by == "desc" then sort_fn = function(a, b) return a.desc:lower() < b.desc:lower() end @@ -234,10 +233,10 @@ local function open() -- style it a bit like the tree vim.wo[M.winnr].winhl = WIN_HL - vim.wo[M.winnr].cursorline = M.config.cursorline + vim.wo[M.winnr].cursorline = config.g.view.cursorline local function toggle_sort() - M.config.sort_by = (M.config.sort_by == "desc") and "key" or "desc" + config.g.help.sort_by = (config.g.help.sort_by == "desc") and "key" or "desc" open() end @@ -281,11 +280,4 @@ function M.toggle() end end -function M.setup(opts) - M.config.cursorline = opts.view.cursorline - M.config.sort_by = opts.help.sort_by - - api = require("nvim-tree.api") -end - return M diff --git a/lua/nvim-tree/keymap.lua b/lua/nvim-tree/keymap.lua index 622d0092ee6..044bf6c9a9b 100644 --- a/lua/nvim-tree/keymap.lua +++ b/lua/nvim-tree/keymap.lua @@ -1,5 +1,18 @@ local M = {} +local config = require("nvim-tree.config") + +---Execute the user or default on attach +---@see nvim_tree.config.on_attach +---@param bufnr integer +function M.on_attach(bufnr) + if type(config.g.on_attach) == "function" then + config.g.on_attach(bufnr) + else + M.on_attach_default(bufnr) + end +end + --- Apply mappings to a scratch buffer and return buffer local mappings ---@param fn fun(bufnr: integer) on_attach or on_attach_default ---@return table as per vim.api.nvim_buf_get_keymap @@ -106,12 +119,4 @@ function M.on_attach_default(bufnr) -- END_ON_ATTACH_DEFAULT end -function M.setup(opts) - if type(opts.on_attach) ~= "function" then - M.on_attach = M.on_attach_default - else - M.on_attach = opts.on_attach - end -end - return M diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 788af98d5bd..8915aa88a39 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -1,6 +1,7 @@ local view = require("nvim-tree.view") local core = require("nvim-tree.core") local notify = require("nvim-tree.notify") +local config = require("nvim-tree.config") ---@class LibOpenOpts ---@field path string|nil path @@ -25,7 +26,7 @@ end ---@param cwd string local function handle_buf_cwd(cwd) local explorer = core.get_explorer() - if M.respect_buf_cwd and cwd ~= core.get_cwd() and explorer then + if config.g.respect_buf_cwd and cwd ~= core.get_cwd() and explorer then explorer:change_dir(cwd) end end @@ -54,8 +55,8 @@ local function should_hijack_current_buf() ft = vim.api.nvim_buf_get_option(bufnr, "ft") ---@diagnostic disable-line: deprecated end - local should_hijack_unnamed = M.hijack_unnamed_buffer_when_opening and bufname == "" and not bufmodified and ft == "" - local should_hijack_dir = bufname ~= "" and vim.fn.isdirectory(bufname) == 1 and M.hijack_directories.enable + local should_hijack_unnamed = config.g.hijack_unnamed_buffer_when_opening and bufname == "" and not bufmodified and ft == "" + local should_hijack_dir = bufname ~= "" and vim.fn.isdirectory(bufname) == 1 and config.g.hijack_directories.enable return should_hijack_dir or should_hijack_unnamed end @@ -76,7 +77,7 @@ function M.prompt(prompt_input, prompt_select, items_short, items_long, kind, ca return "" end - if M.select_prompts then + if config.g.select_prompts then vim.ui.select(items_short, { prompt = prompt_select, kind = kind, format_item = format_item }, function(item_short) callback(item_short) end) @@ -132,12 +133,4 @@ function M.open(opts) view.restore_tab_state() end -function M.setup(opts) - M.hijack_unnamed_buffer_when_opening = opts.hijack_unnamed_buffer_when_opening - M.hijack_directories = opts.hijack_directories - M.respect_buf_cwd = opts.respect_buf_cwd - M.select_prompts = opts.select_prompts - M.group_empty = opts.renderer.group_empty -end - return M diff --git a/lua/nvim-tree/watcher.lua b/lua/nvim-tree/watcher.lua index ecae249de4c..f188f6aece0 100644 --- a/lua/nvim-tree/watcher.lua +++ b/lua/nvim-tree/watcher.lua @@ -1,6 +1,7 @@ local notify = require("nvim-tree.notify") local log = require("nvim-tree.log") local utils = require("nvim-tree.utils") +local config = require("nvim-tree.config") local Class = require("nvim-tree.classic") @@ -13,9 +14,7 @@ local FS_EVENT_FLAGS = { recursive = false, } -local M = { - config = {}, -} +local M = {} ---Registry of all events ---@type Event[] @@ -79,7 +78,7 @@ function Event:start() log.line("watcher", "event_cb '%s' '%s' FAIL : %s", self.path, filename, err) -- do nothing if watchers have already been disabled - if not M.config.filesystem_watchers.enable then + if not config.g.filesystem_watchers.enable then return end @@ -238,7 +237,7 @@ M.Watcher = Watcher ---@param msg string function M.disable_watchers(msg) notify.warn(string.format("Disabling watchers: %s", msg)) - M.config.filesystem_watchers.enable = false + config.g.filesystem_watchers.enable = false require("nvim-tree").purge_all_state() end @@ -279,8 +278,4 @@ function M.is_fs_event_capable(path) return true end -function M.setup(opts) - M.config.filesystem_watchers = opts.filesystem_watchers -end - return M