From d86d94ee36c97d65f754a34403801149d6bc9d78 Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Thu, 13 Mar 2025 14:26:49 -0400 Subject: [PATCH] Update c.lua --- lua/Polydev/c.lua | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lua/Polydev/c.lua b/lua/Polydev/c.lua index b3d6837..3194fb0 100644 --- a/lua/Polydev/c.lua +++ b/lua/Polydev/c.lua @@ -98,7 +98,6 @@ local function write_file(path, content) print(path .. " created successfully!") end --- Create a new Java project function M.create_project() vim.ui.input({ prompt = "Enter project name: " }, function(project_name) if not project_name or project_name == "" then @@ -137,11 +136,24 @@ end function M.create_new_file() vim.ui.input({ prompt = "Enter file name: " }, function(class_name) - if not class_name then return print("File creation canceled.") end - local root_dir = vim.fn.expand("%:p:h"):match("(.*)/src") - if not root_dir then return print("Error: src directory not found.") end - local c_content = [[]] - write_file(root_dir .. "/src/" .. class_name .. ".c", c_content) + if not class_name or class_name == "" then + return print("File creation canceled.") + end + + local root_dir = M.get_project_root() -- Use a function to reliably find the project root + if not root_dir then + return print("Error: Project root not found.") + end + + local file_path = root_dir .. "/src/" .. class_name .. ".c" + local file = io.open(file_path, "w") + if file then + file:close() + vim.cmd("edit " .. file_path) + print(class_name .. ".c created successfully!") + else + print("Error creating " .. class_name .. ".c") + end end) end