From 9a9868c991d2c9d0d2f5bc4c065a839f79c8daa1 Mon Sep 17 00:00:00 2001 From: Dominik Polakovics Date: Thu, 12 Dec 2024 22:29:03 +0100 Subject: [PATCH] fix: ensure that dir exists before creating files --- lua/chatgpt_nvim/handler.lua | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lua/chatgpt_nvim/handler.lua b/lua/chatgpt_nvim/handler.lua index 2b63bda..dfa1629 100644 --- a/lua/chatgpt_nvim/handler.lua +++ b/lua/chatgpt_nvim/handler.lua @@ -1,17 +1,35 @@ local M = {} +local uv = vim.loop + +local function ensure_dir(path) + local st = uv.fs_stat(path) + if st and st.type == 'directory' then + return true + end + local parent = path:match("(.*)/") + if parent and parent ~= "" then + ensure_dir(parent) + end + uv.fs_mkdir(path, 511) + return true +end function M.get_clipboard_content() return vim.fn.getreg('+') end function M.write_file(filepath, content) - local fd = vim.loop.fs_open(filepath, "w", 438) + local dir = filepath:match("(.*)/") + if dir and dir ~= "" then + ensure_dir(dir) + end + local fd = uv.fs_open(filepath, "w", 438) if not fd then vim.api.nvim_err_writeln("Could not open file for writing: " .. filepath) return end - vim.loop.fs_write(fd, content, -1) - vim.loop.fs_close(fd) + uv.fs_write(fd, content, -1) + uv.fs_close(fd) end function M.finish()