added dynamic window resizing of width using nvim tree

This commit is contained in:
aminnairi
2023-12-24 12:23:13 +01:00
parent c1e74d555d
commit 35e7ff10ce
+99 -1
View File
@@ -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({
["<leader>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()