Skip to content
7 changes: 6 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"`)



Expand Down
8 changes: 0 additions & 8 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 7 additions & 2 deletions lua/nvim-tree/_meta/config/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 5 additions & 10 deletions lua/nvim-tree/buffers.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local config = require("nvim-tree.config")

local DirectoryNode = require("nvim-tree.node.directory")

local M = {}
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions lua/nvim-tree/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
48 changes: 19 additions & 29 deletions lua/nvim-tree/diagnostics.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
34 changes: 15 additions & 19 deletions lua/nvim-tree/git/init.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
32 changes: 20 additions & 12 deletions lua/nvim-tree/git/utils.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
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[]
---@return string stdout
---@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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading
Loading