From 35e7ff10ce90388ab5418f71446b1b7d9d8066b4 Mon Sep 17 00:00:00 2001 From: aminnairi <18418459+aminnairi@users.noreply.github.com> Date: Sun, 24 Dec 2023 12:23:13 +0100 Subject: [PATCH] added dynamic window resizing of width using nvim tree --- nvim/lua/settings/lazy/plugins/nvimtree.lua | 100 +++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/nvim/lua/settings/lazy/plugins/nvimtree.lua b/nvim/lua/settings/lazy/plugins/nvimtree.lua index 5e18726..ae5e4eb 100644 --- a/nvim/lua/settings/lazy/plugins/nvimtree.lua +++ b/nvim/lua/settings/lazy/plugins/nvimtree.lua @@ -1,3 +1,78 @@ +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 = { @@ -10,11 +85,19 @@ return { 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 + }) + require("nvim-tree").setup({ auto_reload_on_write = true, sort_by = "case_sensitive", view = { - width = {}, + width = get_width, }, filters = { dotfiles = false, @@ -30,6 +113,21 @@ return { whichKey.register({ ["n"] = { name = "NvimTree", + w = { + name = "NvimTree Width", + i = { + increment_width, + "Increment NvimTree Width" + }, + d = { + decrement_width, + "Increment NvimTree Width" + }, + a = { + dynamic_width, + "Automatic NvimTree Width" + } + }, t = { function() nvimTreeApi.tree.toggle()