feat: add dynamic resizing for mini.files preview window
This commit is contained in:
+69
-51
@@ -1,63 +1,81 @@
|
||||
---@module "data.autocmd"
|
||||
--- This module defines the autocommands for the Neovim configuration.
|
||||
-- ╭─────────────────────────────────────────────────────────╮
|
||||
-- │ Autocommands │
|
||||
-- │ Autocommands │
|
||||
-- ╰─────────────────────────────────────────────────────────╯
|
||||
local M = {}
|
||||
|
||||
M.minifiles = function(opts)
|
||||
vim.api.nvim_create_autocmd("User", {
|
||||
pattern = "MiniFilesBufferCreate",
|
||||
callback = function(args)
|
||||
local buf_id = args.data.buf_id
|
||||
local augroup = vim.api.nvim_create_augroup
|
||||
local autocmd = vim.api.nvim_create_autocmd
|
||||
|
||||
vim.keymap.set(
|
||||
"n",
|
||||
opts.mappings and opts.mappings.toggle_hidden or "g.",
|
||||
toggle_dotfiles,
|
||||
{ buffer = buf_id, desc = "Toggle hidden files" }
|
||||
)
|
||||
M.minifiles = {
|
||||
---@function Function to dynamically resize the preview window
|
||||
dynamic_resize = function()
|
||||
local function update_width_preview()
|
||||
local multiplier = 0.25
|
||||
if vim.g.minifiles_width ~= nil then
|
||||
multiplier = vim.g.minifiles_width
|
||||
end
|
||||
if package.loaded["mini.files"] then
|
||||
-- Obtain the existing config
|
||||
local config = require("mini.files").config
|
||||
-- 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
|
||||
config.windows.width_preview = multiplier
|
||||
else
|
||||
-- Otherwise, use the current window width multiplied by the multiplier
|
||||
config.windows.width_preview = math.floor(vim.o.columns * multiplier)
|
||||
end
|
||||
-- Apply the updated config
|
||||
require("mini.files").setup(config)
|
||||
end
|
||||
end
|
||||
|
||||
vim.keymap.set(
|
||||
"n",
|
||||
opts.mappings and opts.mappings.change_cwd or "gc",
|
||||
files_set_cwd,
|
||||
{ buffer = args.data.buf_id, desc = "Set cwd" }
|
||||
)
|
||||
-- Flag to determine if mini.files is currently in use
|
||||
local is_mini_files_active = false
|
||||
|
||||
map_split(
|
||||
buf_id,
|
||||
opts.mappings and opts.mappings.go_in_horizontal or "<C-w>s",
|
||||
"horizontal",
|
||||
false
|
||||
)
|
||||
map_split(
|
||||
buf_id,
|
||||
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,
|
||||
})
|
||||
-- Autocommand group to handle dynamic resizing
|
||||
augroup("MiniFilesDynamicWidth", { clear = true })
|
||||
|
||||
vim.api.nvim_create_autocmd("User", {
|
||||
pattern = "MiniFilesActionRename",
|
||||
callback = function(event)
|
||||
LazyVim.lsp.on_rename(event.data.from, event.data.to)
|
||||
end,
|
||||
})
|
||||
end
|
||||
-- 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
|
||||
|
||||
Reference in New Issue
Block a user