local function use_nvim_tree_width(options) if type(options) ~= "table" then return end ---@type number local offset = options.offset ---@type number local initial_width = options.initial_width local initial_width_type = type(initial_width) local on_width_updated = options.on_width_updated if type(on_width_updated) ~= "function" then on_width_updated = function() end end if type(offset) ~= "number" then offset = 10 end if initial_width_type ~= "number" and initial_width_type ~= "table" then initial_width = 30 end ---@type number|table local previous_width = initial_width if initial_width_type ~= "number" then previous_width = 30 end ---@type number|table local width = initial_width local function get_width() return width end local function increment_width() if type(width) == "table" then width = previous_width end local new_width = width + offset previous_width = new_width width = new_width on_width_updated(new_width) end local function decrement_width() if type(width) == "table" then width = previous_width end local new_width = width - offset previous_width = new_width width = new_width on_width_updated(new_width) end local function dynamic_width() width = {} on_width_updated(width) end return get_width, increment_width, decrement_width, dynamic_width end return { "nvim-tree/nvim-tree.lua", dependencies = { "nvim-tree/nvim-web-devicons", }, config = function() vim.g.loaded_netrw = 1 vim.g.loaded_netrwPlugin = 1 vim.opt.termguicolors = true local nvimTreeApi = require("nvim-tree.api") local get_width, increment_width, decrement_width, dynamic_width = use_nvim_tree_width({ offset = 5, initial_width = 30, on_width_updated = function(width) vim.cmd.NvimTreeResize(width) end }) local function on_nvim_tree_attach(buffer) local function opts(desc) return { desc = "nvim-tree: " .. desc, buffer = buffer, noremap = true, silent = true, nowait = true } end -- default mappings nvimTreeApi.config.mappings.default_on_attach(buffer) -- custom mappings vim.keymap.set('n', 'l', nvimTreeApi.node.open.edit, opts('Open file/directory')) vim.keymap.set('n', 'h', nvimTreeApi.node.navigate.parent_close, opts('Help')) end require("nvim-tree").setup({ on_attach = on_nvim_tree_attach, auto_reload_on_write = true, sort_by = "case_sensitive", filters = { dotfiles = false, }, notify = { -- Notifies everything from the INFO severity to ERROR threshold = vim.log.levels.INFO, -- Notifies only the relative path instead of the absolute absolute_path = false, }, -- Move to the first letter of each folder/files instead of the beginning of the line hijack_cursor = true, -- Disable completely netrw, just in case disable_netrw = true, -- Reload the tree whenever a buffer is opened reload_on_bufenter = true, -- Allows using vim.ui.select for various operation instead of the defaut implementation select_prompts = true, view = { -- dynamically get the width of the tree width = get_width, -- Always open the tree on the left side of the screen side = "left", -- Never display the number line for the tree number = false, }, renderer = { -- Never add trailing slashes for folders name add_trailing = false, -- Never group directories in the tree for those that only have one child node group_empty = false, -- Never highlight any files in a special way special_files = {}, -- Highlight files that have diagnostics highlight_diagnostics = true }, update_focused_file = { -- Focus the file in the tree whenever it is focused in the editor enable = true }, diagnostics = { -- Enable diagnostics for the tree enable = true, -- Show diagnostics for the parent folder of the incriminated file show_on_dirs = true } }) local whichKey = require("which-key") whichKey.add({ { "n", group = "NvimTree" }, { "nt", function() nvimTreeApi.tree.toggle() end, desc = "Toggle" }, { "nf", function() nvimTreeApi.tree.focus() end, desc = "Focus" }, { "ne", function() nvimTreeApi.tree.expand_all() end, desc = "Expand All" }, { "nc", function() nvimTreeApi.tree.collapse_all() end, desc = "Collapse All" }, { "nw", group = "Width" }, { "nwi", increment_width, desc = "Increase" }, { "nwd", decrement_width, desc = "Decrease" }, { "nwa", dynamic_width, desc = "Decrease" }, { "nt", function() nvimTreeApi.tree.toggle() end, desc = "Toggle" }, }) end, }