From 4a7bd720182591dbac1a7662eecae78cb276189e Mon Sep 17 00:00:00 2001 From: Miguel Palhas Date: Wed, 17 Dec 2025 12:28:53 +0000 Subject: [PATCH] wip --- lua/mappings.lua | 17 +++++++- lua/plugins/file-explorer.lua | 79 +++++++++++++++++++++++++++++------ 2 files changed, 81 insertions(+), 15 deletions(-) diff --git a/lua/mappings.lua b/lua/mappings.lua index 180ebb6..6cef3de 100644 --- a/lua/mappings.lua +++ b/lua/mappings.lua @@ -1,12 +1,25 @@ require "nvchad.mappings" +local map = vim.keymap.set + -- remove nvim-tree mappings (using mini.files instead) vim.keymap.del("n", "e") vim.keymap.del("n", "") --- add yours here +-- mini.files +map("n", "e", function() + local MiniFiles = require "mini.files" + if not MiniFiles.close() then + MiniFiles.open(vim.api.nvim_buf_get_name(0)) + end +end, { desc = "File explorer (current file)" }) -local map = vim.keymap.set +map("n", "E", function() + local MiniFiles = require "mini.files" + if not MiniFiles.close() then + MiniFiles.open(vim.uv.cwd()) + end +end, { desc = "File explorer (cwd)" }) map("n", ";", ":", { desc = "CMD enter command mode" }) map("i", "jk", "") diff --git a/lua/plugins/file-explorer.lua b/lua/plugins/file-explorer.lua index 54c4f03..340a777 100644 --- a/lua/plugins/file-explorer.lua +++ b/lua/plugins/file-explorer.lua @@ -6,7 +6,28 @@ return { { "echasnovski/mini.files", version = false, - lazy = false, + keys = { + { + "e", + function() + local MiniFiles = require "mini.files" + if not MiniFiles.close() then + MiniFiles.open(vim.api.nvim_buf_get_name(0)) + end + end, + desc = "File explorer (current file)", + }, + { + "E", + function() + local MiniFiles = require "mini.files" + if not MiniFiles.close() then + MiniFiles.open(vim.uv.cwd()) + end + end, + desc = "File explorer (cwd)", + }, + }, config = function() require("mini.files").setup { mappings = { @@ -33,20 +54,52 @@ return { }, } - local map = vim.keymap.set - map("n", "e", function() - local MiniFiles = require "mini.files" - if not MiniFiles.close() then - MiniFiles.open(vim.api.nvim_buf_get_name(0)) - end - end, { desc = "File explorer (current file)" }) + -- https://github.com/nvim-mini/mini.nvim/discussions/2173 + -- Window width based on the offset from the center, i.e. center window + -- is 60, then next over is 20, then the rest are 10. + -- Can use more resolution if you want like { 60, 20, 20, 10, 5 } + local widths = { 60, 20, 10 } - map("n", "E", function() - local MiniFiles = require "mini.files" - if not MiniFiles.close() then - MiniFiles.open(vim.uv.cwd()) + local ensure_center_layout = function(ev) + local state = MiniFiles.get_explorer_state() + if state == nil then + return end - end, { desc = "File explorer (cwd)" }) + + -- Compute "depth offset" - how many windows are between this and focused + local path_this = vim.api.nvim_buf_get_name(ev.data.buf_id):match "^minifiles://%d+/(.*)$" + local depth_this + for i, path in ipairs(state.branch) do + if path == path_this then + depth_this = i + end + end + if depth_this == nil then + return + end + local depth_offset = depth_this - state.depth_focus + + -- Adjust config of this event's window + local i = math.abs(depth_offset) + 1 + local win_config = vim.api.nvim_win_get_config(ev.data.win_id) + win_config.width = i <= #widths and widths[i] or widths[#widths] + + win_config.col = math.floor(0.5 * (vim.o.columns - widths[1])) + for j = 1, math.abs(depth_offset) do + local sign = depth_offset == 0 and 0 or (depth_offset > 0 and 1 or -1) + -- widths[j+1] for the negative case because we don't want to add the center window's width + local prev_win_width = (sign == -1 and widths[j + 1]) or widths[j] or widths[#widths] + -- Add an extra +2 each step to account for the border width + win_config.col = win_config.col + sign * (prev_win_width + 2) + end + + win_config.height = depth_offset == 0 and 25 or 20 + win_config.row = math.floor(0.5 * (vim.o.lines - win_config.height)) + win_config.border = { "🭽", "▔", "🭾", "▕", "🭿", "▁", "🭼", "▏" } + vim.api.nvim_win_set_config(ev.data.win_id, win_config) + end + + vim.api.nvim_create_autocmd("User", { pattern = "MiniFilesWindowUpdate", callback = ensure_center_layout }) end, }, }