feat: add dynamic resizing for mini.files preview window

This commit is contained in:
2024-09-13 09:18:20 -04:00
parent ac60ae333b
commit 07acf6cfa1
+70 -52
View File
@@ -5,59 +5,77 @@
-- ╰─────────────────────────────────────────────────────────╯ -- ╰─────────────────────────────────────────────────────────╯
local M = {} local M = {}
M.minifiles = function(opts) local augroup = vim.api.nvim_create_augroup
vim.api.nvim_create_autocmd("User", { local autocmd = vim.api.nvim_create_autocmd
pattern = "MiniFilesBufferCreate",
callback = function(args)
local buf_id = args.data.buf_id
vim.keymap.set( M.minifiles = {
"n", ---@function Function to dynamically resize the preview window
opts.mappings and opts.mappings.toggle_hidden or "g.", dynamic_resize = function()
toggle_dotfiles, local function update_width_preview()
{ buffer = buf_id, desc = "Toggle hidden files" } local multiplier = 0.25
) if vim.g.minifiles_width ~= nil then
multiplier = vim.g.minifiles_width
vim.keymap.set( end
"n", if package.loaded["mini.files"] then
opts.mappings and opts.mappings.change_cwd or "gc", -- Obtain the existing config
files_set_cwd, local config = require("mini.files").config
{ buffer = args.data.buf_id, desc = "Set cwd" } -- Update the width_preview based on the current window size
) if multiplier > 1 then
-- If the multiplier is greater than 1, use as the preview width
map_split( config.windows.width_preview = multiplier
buf_id, else
opts.mappings and opts.mappings.go_in_horizontal or "<C-w>s", -- Otherwise, use the current window width multiplied by the multiplier
"horizontal", config.windows.width_preview = math.floor(vim.o.columns * multiplier)
false end
) -- Apply the updated config
map_split( require("mini.files").setup(config)
buf_id, end
opts.mappings and opts.mappings.go_in_vertical or "<C-w>v",
"vertical",
false
)
map_split(
buf_id,
opts.mappings and opts.mappings.go_in_horizontal_plus or "<C-w>S",
"horizontal",
true
)
map_split(
buf_id,
opts.mappings and opts.mappings.go_in_vertical_plus or "<C-w>V",
"vertical",
true
)
end,
})
vim.api.nvim_create_autocmd("User", {
pattern = "MiniFilesActionRename",
callback = function(event)
LazyVim.lsp.on_rename(event.data.from, event.data.to)
end,
})
end end
-- Flag to determine if mini.files is currently in use
local is_mini_files_active = false
-- Autocommand group to handle dynamic resizing
augroup("MiniFilesDynamicWidth", { clear = true })
-- Handle mini.files open event to set the flag
autocmd("User", {
group = "MiniFilesDynamicWidth",
pattern = "MiniFilesExplorerOpen",
callback = function()
is_mini_files_active = true
end,
})
-- Handle mini.files close event to reset the flag and update the preview width
autocmd("User", {
group = "MiniFilesDynamicWidth",
pattern = "MiniFilesExplorerClose",
callback = function()
is_mini_files_active = false
update_width_preview()
end,
})
-- Handle window resize event
autocmd("VimResized", {
group = "MiniFilesDynamicWidth",
callback = function()
if is_mini_files_active then
-- Defer the update until mini.files closes
autocmd("User", {
group = "MiniFilesDynamicWidth",
pattern = "MiniFilesExplorerClose",
callback = update_width_preview,
once = true, -- Ensure this runs only once
})
else
-- If mini.files is not active, update immediately
update_width_preview()
end
end,
})
end,
}
return M return M