diff --git a/lua/mould/init.lua b/lua/mould/init.lua index ab05545..103a91f 100644 --- a/lua/mould/init.lua +++ b/lua/mould/init.lua @@ -1,6 +1,17 @@ local M = {} -local function open_floating_terminal(cmd) +-- Helper function to get a set of files currently in a directory +local function get_dir_files(dir) + local files = {} + if vim.fn.isdirectory(dir) == 1 then + for _, f in ipairs(vim.fn.readdir(dir)) do + files[f] = true + end + end + return files +end + +local function open_floating_terminal(cmd, target_dir) local buf = vim.api.nvim_create_buf(false, true) local width = math.floor(vim.o.columns * 0.9) local height = math.floor(vim.o.lines * 0.9) @@ -23,8 +34,12 @@ local function open_floating_terminal(cmd) local original_buf = vim.api.nvim_get_current_buf() local original_file = vim.api.nvim_buf_get_name(original_buf) + -- Snapshot the directory contents BEFORE the command runs + local files_before = get_dir_files(target_dir) + vim.fn.termopen(cmd, { on_exit = function() + -- Close the floating window and delete the terminal buffer vim.api.nvim_win_close(win, true) vim.api.nvim_buf_delete(buf, { force = true }) @@ -34,6 +49,25 @@ local function open_floating_terminal(cmd) vim.cmd("checktime " .. vim.fn.fnameescape(original_file)) end) end + + -- Snapshot the directory AFTER the command finishes + local files_after = get_dir_files(target_dir) + + -- Compare to find the newly created file + for f, _ in pairs(files_after) do + if not files_before[f] then + local new_filepath = target_dir .. "/" .. f + + -- Open the new file (wrapped in schedule to ensure it runs safely after term closes) + vim.schedule(function() + vim.cmd("edit " .. vim.fn.fnameescape(new_filepath)) + -- Optional: Let the user know it worked + vim.notify("mould.nvim: Opened " .. f, vim.log.levels.INFO) + end) + + break -- Assuming only one file is generated; we can stop looking + end + end end, }) @@ -47,8 +81,12 @@ function M.open() return end + -- Get the directory of the current file + local target_dir = vim.fn.fnamemodify(filepath, ":p:h") local cmd = string.format("mould %s", vim.fn.shellescape(filepath)) - open_floating_terminal(cmd) + + -- Pass the target directory to our terminal function + open_floating_terminal(cmd, target_dir) end return M