diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 63cf5d1c154..c07c76cf7a4 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -281,15 +281,14 @@ function M.setup(config_user) manage_netrw() - require("nvim-tree.log").setup(config.g) + log.start() if log.enabled("config") then log.line("config", "default config + user") log.raw("config", "%s\n", vim.inspect(config.g)) end - require("nvim-tree.appearance").setup() - require("nvim-tree.renderer.components").setup(config.g) + require("nvim-tree.appearance").highlight() require("nvim-tree.view-state").initialize() diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index 0b4f22ac1d7..0740b813841 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -192,8 +192,4 @@ function M.rename_full(node) prompt_to_rename(node, ":p") end -function M.setup(opts) - config.g.filesystem_watchers = opts.filesystem_watchers -end - return M diff --git a/lua/nvim-tree/appearance/init.lua b/lua/nvim-tree/appearance/init.lua index 61714af644e..9918ca4dd0b 100644 --- a/lua/nvim-tree/appearance/init.lua +++ b/lua/nvim-tree/appearance/init.lua @@ -182,7 +182,8 @@ M.LEGACY_LINKS = { NvimTreeDiagnosticHintFolderHL = "NvimTreeLspDiagnosticsHintFolderText", } -function M.setup() +---Create all highlight groups and links. Idempotent. +function M.highlight() -- non-linked for _, g in ipairs(M.HIGHLIGHT_GROUPS) do if g.def then diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua index 8e819961a6d..5c3403d36d7 100644 --- a/lua/nvim-tree/config.lua +++ b/lua/nvim-tree/config.lua @@ -511,6 +511,21 @@ local function process_config(g) -- Open -- g.actions.open_file.window_picker.chars = tostring(g.actions.open_file.window_picker.chars):upper() + + -- + -- Padding + -- + if g.renderer.indent_width < 1 then + g.renderer.indent_width = 1 + end + for k, v in pairs(g.renderer.indent_markers.icons) do + if #v == 0 then + g.renderer.indent_markers.icons[k] = " " + else + -- return the first character from the UTF-8 encoded string; we may use utf8.codes from Lua 5.3 when available + g.renderer.indent_markers.icons[k] = v:match("[%z\1-\127\194-\244][\128-\191]*") + end + end end ---Validate user config and migrate legacy. diff --git a/lua/nvim-tree/events.lua b/lua/nvim-tree/events.lua index 85e455ece52..ebabd2c4137 100644 --- a/lua/nvim-tree/events.lua +++ b/lua/nvim-tree/events.lua @@ -1,4 +1,3 @@ -local notify = require("nvim-tree.notify") local Event = require("nvim-tree._meta.api.events").Event local M = {} @@ -25,7 +24,7 @@ local function dispatch(event_name, payload) for _, handler in pairs(get_handlers(event_name)) do local success, error = pcall(handler, payload) if not success then - notify.error("Handler for event " .. event_name .. " errored. " .. vim.inspect(error)) + require("nvim-tree.notify").error("Handler for event " .. event_name .. " errored. " .. vim.inspect(error)) end end end diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index eb9b5f5d097..84af434edba 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -88,7 +88,7 @@ function Explorer:create_autocmds() vim.api.nvim_create_autocmd("ColorScheme", { group = self.augroup_id, callback = function() - appearance.setup() + appearance.highlight() view.reset_winhl() self.renderer:draw() end, diff --git a/lua/nvim-tree/legacy.lua b/lua/nvim-tree/legacy.lua index 7d42338abd5..ed432c51ed3 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -1,5 +1,3 @@ -local notify = require("nvim-tree.notify") - local M = {} --- Create empty sub-tables if not present @@ -120,25 +118,32 @@ end ---@param u nvim_tree.config user supplied subset of config local function deprecated_config(u) if type(u.view) == "table" and u.view.hide_root_folder then - notify.info("view.hide_root_folder is deprecated, please set renderer.root_folder_label = false") + require("nvim-tree.notify").info( + "view.hide_root_folder is deprecated, please set renderer.root_folder_label = false" + ) end end ---@param u nvim_tree.config user supplied subset of config local function removed_config(u) if u.auto_close then - notify.warn("auto close feature has been removed: https://github.com/nvim-tree/nvim-tree.lua/wiki/Auto-Close") + require("nvim-tree.notify").warn( + "auto close feature has been removed: https://github.com/nvim-tree/nvim-tree.lua/wiki/Auto-Close" + ) u["auto_close"] = nil end if u.focus_empty_on_setup then - notify.warn("focus_empty_on_setup has been removed: https://github.com/nvim-tree/nvim-tree.lua/wiki/Open-At-Startup") + require("nvim-tree.notify").warn( + "focus_empty_on_setup has been removed: https://github.com/nvim-tree/nvim-tree.lua/wiki/Open-At-Startup" + ) u["focus_empty_on_setup"] = nil end if u.create_in_closed_folder then - notify.warn( - "create_in_closed_folder has been removed and is now the default behaviour. You may use api.fs.create to add a file under your desired node.") + require("nvim-tree.notify").warn( + "create_in_closed_folder has been removed and is now the default behaviour. You may use api.fs.create to add a file under your desired node." + ) end u["create_in_closed_folder"] = nil end diff --git a/lua/nvim-tree/log.lua b/lua/nvim-tree/log.lua index 9665c1335e6..a564e3ca1bc 100644 --- a/lua/nvim-tree/log.lua +++ b/lua/nvim-tree/log.lua @@ -1,3 +1,5 @@ +local config = require("nvim-tree.config") + ---@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "profile" | "watcher" ---@type table @@ -110,14 +112,17 @@ function M.enabled(typ) return file_path ~= nil and (types[typ] or types.all) end -function M.setup(opts) - if opts.log and opts.log.enable and opts.log.types then - types = opts.log.types +--- Create the log file and enable logging, if globally configured +function M.start() + if config.g.log and config.g.log.enable and config.g.log.types then + types = config.g.log.types file_path = string.format("%s/nvim-tree.log", vim.fn.stdpath("log"), os.date("%H:%M:%S"), vim.env.USER) - if opts.log.truncate then + if config.g.log.truncate then os.remove(file_path) end - require("nvim-tree.notify").debug("nvim-tree.lua logging to " .. file_path) + if config.g.notify.threshold <= vim.log.levels.DEBUG then + require("nvim-tree.notify").debug("nvim-tree.lua logging to " .. file_path) + end end end diff --git a/lua/nvim-tree/renderer/components/devicons.lua b/lua/nvim-tree/renderer/components/devicons.lua index ad91d058c15..5d7bd275161 100644 --- a/lua/nvim-tree/renderer/components/devicons.lua +++ b/lua/nvim-tree/renderer/components/devicons.lua @@ -1,3 +1,5 @@ +local config = require("nvim-tree.config") + ---@alias devicons_get_icon fun(name: string, ext: string?, opts: table?): string?, string? ---@alias devicons_setup fun(opts: table?) @@ -6,11 +8,18 @@ ---@field get_icon devicons_get_icon local devicons +--One shot lazy discovery and setup done +local initialized = false + local M = {} ---Wrapper around nvim-web-devicons, nils if devicons not available ---@type devicons_get_icon function M.get_icon(name, ext, opts) + if not initialized then + M.initialize() + end + if devicons then return devicons.get_icon(name, ext, opts) else @@ -19,9 +28,8 @@ function M.get_icon(name, ext, opts) end ---Attempt to use nvim-web-devicons if present and enabled for file or folder ----@param opts table -function M.setup(opts) - if opts.renderer.icons.show.file or opts.renderer.icons.show.folder then +function M.initialize() + if config.g.renderer.icons.show.file or config.g.renderer.icons.show.folder then local ok, di = pcall(require, "nvim-web-devicons") if ok then devicons = di --[[@as DevIcons]] @@ -30,6 +38,7 @@ function M.setup(opts) devicons.setup() end end + initialized = true end return M diff --git a/lua/nvim-tree/renderer/components/init.lua b/lua/nvim-tree/renderer/components/init.lua deleted file mode 100644 index a06827422dd..00000000000 --- a/lua/nvim-tree/renderer/components/init.lua +++ /dev/null @@ -1,11 +0,0 @@ -local M = {} - -M.devicons = require("nvim-tree.renderer.components.devicons") -M.padding = require("nvim-tree.renderer.components.padding") - -function M.setup(opts) - M.devicons.setup(opts) - M.padding.setup(opts) -end - -return M diff --git a/lua/nvim-tree/renderer/components/padding.lua b/lua/nvim-tree/renderer/components/padding.lua index be84ae0b9de..9e04addfdaf 100644 --- a/lua/nvim-tree/renderer/components/padding.lua +++ b/lua/nvim-tree/renderer/components/padding.lua @@ -1,3 +1,4 @@ +local config = require("nvim-tree.config") local DirectoryNode = require("nvim-tree.node.directory") local M = {} @@ -27,21 +28,21 @@ local function get_padding_indent_markers(depth, idx, nodes_number, markers, wit if depth > 0 then local has_folder_sibling = check_siblings_for_folder(node, with_arrows) - local indent = string.rep(" ", M.config.indent_width - 1) + local indent = string.rep(" ", config.g.renderer.indent_width - 1) markers[depth] = idx ~= nodes_number for i = 1, depth - early_stop do local glyph if idx == nodes_number and i == depth then - local bottom_width = M.config.indent_width - 2 + (with_arrows and not inline_arrows and has_folder_sibling and 2 or 0) - glyph = M.config.indent_markers.icons.corner - .. string.rep(M.config.indent_markers.icons.bottom, bottom_width) - .. (M.config.indent_width > 1 and " " or "") + local bottom_width = config.g.renderer.indent_width - 2 + (with_arrows and not inline_arrows and has_folder_sibling and 2 or 0) + glyph = config.g.renderer.indent_markers.icons.corner + .. string.rep(config.g.renderer.indent_markers.icons.bottom, bottom_width) + .. (config.g.renderer.indent_width > 1 and " " or "") elseif markers[i] and i == depth then - glyph = M.config.indent_markers.icons.item .. indent + glyph = config.g.renderer.indent_markers.icons.item .. indent elseif markers[i] then - glyph = M.config.indent_markers.icons.edge .. indent + glyph = config.g.renderer.indent_markers.icons.edge .. indent else - glyph = M.config.indent_markers.icons.none .. indent + glyph = config.g.renderer.indent_markers.icons.none .. indent end if not with_arrows or (inline_arrows and (depth ~= i or not node.nodes)) then @@ -68,10 +69,10 @@ end function M.get_indent_markers(depth, idx, nodes_number, node, markers, early_stop) local str = "" - local show_arrows = M.config.icons.show.folder_arrow - local show_markers = M.config.indent_markers.enable - local inline_arrows = M.config.indent_markers.inline_arrows - local indent_width = M.config.indent_width + local show_arrows = config.g.renderer.icons.show.folder_arrow + local show_markers = config.g.renderer.indent_markers.enable + local inline_arrows = config.g.renderer.indent_markers.inline_arrows + local indent_width = config.g.renderer.indent_width if show_markers then str = str .. get_padding_indent_markers(depth, idx, nodes_number, markers, show_arrows, inline_arrows, node, early_stop or 0) @@ -85,7 +86,7 @@ end ---@param node Node ---@return nvim_tree.api.highlighted_string[]? function M.get_arrows(node) - if not M.config.icons.show.folder_arrow then + if not config.g.renderer.icons.show.folder_arrow then return end @@ -95,38 +96,18 @@ function M.get_arrows(node) local dir = node:as(DirectoryNode) if dir then if dir.open then - str = M.config.icons.glyphs.folder["arrow_open"] .. M.config.icons.padding.folder_arrow + str = config.g.renderer.icons.glyphs.folder["arrow_open"] .. config.g.renderer.icons.padding.folder_arrow hl = "NvimTreeFolderArrowOpen" else - str = M.config.icons.glyphs.folder["arrow_closed"] .. M.config.icons.padding.folder_arrow + str = config.g.renderer.icons.glyphs.folder["arrow_closed"] .. config.g.renderer.icons.padding.folder_arrow end - elseif M.config.indent_markers.enable then + elseif config.g.renderer.indent_markers.enable then str = "" else - str = " " .. string.rep(" ", #M.config.icons.padding.folder_arrow) + str = " " .. string.rep(" ", #config.g.renderer.icons.padding.folder_arrow) end return { str = str, hl = { hl } } end -function M.setup(opts) - M.config = opts.renderer - - if M.config.indent_width < 1 then - M.config.indent_width = 1 - end - - local function check_marker(symbol) - if #symbol == 0 then - return " " - end - -- return the first character from the UTF-8 encoded string; we may use utf8.codes from Lua 5.3 when available - return symbol:match("[%z\1-\127\194-\244][\128-\191]*") - end - - for k, v in pairs(M.config.indent_markers.icons) do - M.config.indent_markers.icons[k] = check_marker(v) - end -end - return M