Update header file creation

made the code simpler like the c file creation
This commit is contained in:
DarthMooMancer
2025-03-13 13:18:33 -04:00
parent d4d7b8e343
commit bbf869e507
+5 -29
View File
@@ -214,29 +214,14 @@ end
function M.create_new_header_file() function M.create_new_header_file()
vim.ui.input({ prompt = "Enter header file name: " }, function(header_name) vim.ui.input({ prompt = "Enter header file name: " }, function(header_name)
if not header_name or header_name == "" then if not header_name then return print("Header file creation canceled.") end
print("Header File creation canceled.") local root_dir = vim.fn.expand("%:p:h"):match("(.*)/src")
return if not root_dir then return print("Error: src directory not found.") end
end
local current_dir = vim.fn.expand("%:p:h")
local root_dir = current_dir:match("(.*)/src")
if not root_dir then
print("Error: src directory not found.")
return
end
local header_file_path = root_dir .. "/include/" .. header_name .. ".h"
-- Convert header name to uppercase + underscores for the include guard
local guard_macro = header_name:upper():gsub("[^A-Z0-9]", "_") .. "_H" local guard_macro = header_name:upper():gsub("[^A-Z0-9]", "_") .. "_H"
local content = string.format([[
-- Header file template
local header_content = string.format([[
#ifndef %s #ifndef %s
#define %s #define %s
// Necessary includes
#include <stdio.h> #include <stdio.h>
// Function prototypes // Function prototypes
@@ -244,16 +229,7 @@ void example_function();
#endif // %s #endif // %s
]], guard_macro, guard_macro, guard_macro) ]], guard_macro, guard_macro, guard_macro)
write_file(root_dir .. "/include/" .. header_name .. ".h", content)
local file = io.open(header_file_path, "w")
if file then
file:write(header_content)
file:close()
vim.cmd("edit " .. header_file_path)
print(header_name .. ".h created successfully!")
else
print("Error creating " .. header_name .. ".h")
end
end) end)
end end