From c3ba889aa050e60dae64c35d88fde201d7a23cbd Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Mon, 10 Mar 2025 14:35:19 -0400 Subject: [PATCH] Update c.lua --- lua/Polydev/c.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lua/Polydev/c.lua b/lua/Polydev/c.lua index 7b0a2e3..e269b3c 100644 --- a/lua/Polydev/c.lua +++ b/lua/Polydev/c.lua @@ -233,20 +233,19 @@ public class %s { end function M.build() - -- Get the current file's directory local current_dir = vim.fn.expand("%:p:h") - -- Try to find the root of the project by matching the path local project_root = current_dir:match("(.*)/src") + if not project_root then print("Error: src directory not found.") return end - -- Define the build directory local build_dir = project_root .. "/build" - -- List files in the build directory and find the .txt file local files = vim.fn.glob(build_dir .. "/*.txt", true, true) local project_name = nil + + -- Look for the project name by checking for files in the build directory for _, file in ipairs(files) do if vim.fn.match(file, "CMakeCache.txt") == -1 then project_name = vim.fn.fnamemodify(file, ":r") @@ -254,16 +253,19 @@ function M.build() end end - -- Extract the project name from the .txt file's name (without the .txt extension) - local project_name = vim.fn.fnamemodify(files[1], ":r") -- :r removes the file extension + if not project_name then + print("Error: No valid project name file found in the build directory.") + return + end -- Build command local compile_command = "cd " .. build_dir .. " && cmake .. && make && chmod +x " .. project_name + -- Open floating terminal to execute the build command local term_buf = M.open_float_terminal(compile_command) vim.api.nvim_buf_set_option(term_buf, "modifiable", true) - -- Run compile command and capture output + -- Run the compile command and capture output local compile_status = vim.fn.system(compile_command) -- Set message based on compile result