feat: add dynamic resizing for mini.files preview window
This commit is contained in:
+69
-51
@@ -1,63 +1,81 @@
|
|||||||
---@module "data.autocmd"
|
---@module "data.autocmd"
|
||||||
--- This module defines the autocommands for the Neovim configuration.
|
--- This module defines the autocommands for the Neovim configuration.
|
||||||
-- ╭─────────────────────────────────────────────────────────╮
|
-- ╭─────────────────────────────────────────────────────────╮
|
||||||
-- │ Autocommands │
|
-- │ Autocommands │
|
||||||
-- ╰─────────────────────────────────────────────────────────╯
|
-- ╰─────────────────────────────────────────────────────────╯
|
||||||
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
|
||||||
|
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(
|
-- Flag to determine if mini.files is currently in use
|
||||||
"n",
|
local is_mini_files_active = false
|
||||||
opts.mappings and opts.mappings.change_cwd or "gc",
|
|
||||||
files_set_cwd,
|
|
||||||
{ buffer = args.data.buf_id, desc = "Set cwd" }
|
|
||||||
)
|
|
||||||
|
|
||||||
map_split(
|
-- Autocommand group to handle dynamic resizing
|
||||||
buf_id,
|
augroup("MiniFilesDynamicWidth", { clear = true })
|
||||||
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,
|
|
||||||
})
|
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd("User", {
|
-- Handle mini.files open event to set the flag
|
||||||
pattern = "MiniFilesActionRename",
|
autocmd("User", {
|
||||||
callback = function(event)
|
group = "MiniFilesDynamicWidth",
|
||||||
LazyVim.lsp.on_rename(event.data.from, event.data.to)
|
pattern = "MiniFilesExplorerOpen",
|
||||||
end,
|
callback = function()
|
||||||
})
|
is_mini_files_active = true
|
||||||
end
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user