Update c.lua
This commit is contained in:
+55
-28
@@ -200,9 +200,9 @@ end
|
|||||||
|
|
||||||
-- Create a new Java file
|
-- Create a new Java file
|
||||||
function M.create_new_file()
|
function M.create_new_file()
|
||||||
vim.ui.input({ prompt = "Enter class name: " }, function(class_name)
|
vim.ui.input({ prompt = "Enter fil name: " }, function(class_name)
|
||||||
if not class_name or class_name == "" then
|
if not class_name or class_name == "" then
|
||||||
print("Class creation canceled.")
|
print("File creation canceled.")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -213,63 +213,91 @@ function M.create_new_file()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local java_file_path = root_dir .. "/src/" .. class_name .. ".java"
|
local c_file_path = root_dir .. "/src/" .. class_name .. ".c"
|
||||||
local java_content = string.format([[
|
local c_content = [[]]
|
||||||
public class %s {
|
|
||||||
// New File
|
|
||||||
}
|
|
||||||
]], class_name)
|
|
||||||
|
|
||||||
local file = io.open(java_file_path, "w")
|
local file = io.open(c_file_path, "w")
|
||||||
if file then
|
if file then
|
||||||
file:write(java_content)
|
file:write(c_content)
|
||||||
file:close()
|
file:close()
|
||||||
vim.cmd("edit " .. java_file_path)
|
vim.cmd("edit " .. c_file_path)
|
||||||
print( class_name .. ".java created successfully!")
|
print( " " .. class_name .. ".c created successfully!")
|
||||||
else
|
else
|
||||||
print("Error creating " .. class_name .. ".java")
|
print("Error creating " .. class_name .. ".c")
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.create_new_header_file()
|
||||||
|
vim.ui.input({ prompt = "Enter header file name: " }, function(header_name)
|
||||||
|
if not header_name or header_name == "" then
|
||||||
|
print("Header File creation canceled.")
|
||||||
|
return
|
||||||
|
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"
|
||||||
|
|
||||||
|
-- Header file template
|
||||||
|
local header_content = string.format([[
|
||||||
|
#ifndef %s
|
||||||
|
#define %s
|
||||||
|
|
||||||
|
// Necessary includes
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Function prototypes
|
||||||
|
void example_function();
|
||||||
|
|
||||||
|
#endif // %s
|
||||||
|
]], guard_macro, guard_macro, guard_macro)
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
function M.build()
|
function M.build()
|
||||||
-- Get the current file's directory
|
|
||||||
local current_dir = vim.fn.expand("%:p:h")
|
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")
|
local project_root = current_dir:match("(.*)/src")
|
||||||
if not project_root then
|
if not project_root then
|
||||||
print("Error: src directory not found.")
|
print("Error: src directory not found.")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Define the build directory
|
|
||||||
local build_dir = project_root .. "/build"
|
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 files = vim.fn.glob(build_dir .. "/*.txt", true, true)
|
||||||
if #files == 0 then
|
if #files == 0 then
|
||||||
print("Error: No .txt file found in the build directory.")
|
print("Error: No .txt file found in the build directory.")
|
||||||
return
|
return
|
||||||
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")
|
||||||
local project_name = vim.fn.fnamemodify(files[1], ":r") -- :r removes the file extension
|
|
||||||
|
|
||||||
-- Build command
|
|
||||||
local compile_command = "cd " .. build_dir .. " && cmake .. && make"
|
local compile_command = "cd " .. build_dir .. " && cmake .. && make"
|
||||||
|
|
||||||
-- Open floating terminal to execute the build command
|
|
||||||
local term_buf = M.open_float_terminal(compile_command)
|
local term_buf = M.open_float_terminal(compile_command)
|
||||||
vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
|
vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
|
||||||
|
|
||||||
-- Run compile command and capture output
|
|
||||||
local compile_status = vim.fn.system(compile_command)
|
local compile_status = vim.fn.system(compile_command)
|
||||||
|
|
||||||
-- Set message based on compile result
|
|
||||||
local message
|
local message
|
||||||
if vim.v.shell_error ~= 0 then
|
if vim.v.shell_error ~= 0 then
|
||||||
message = {"Error during compilation:"}
|
message = {"Error during compilation:"}
|
||||||
-- Add each line of the compile_status as individual strings in the message array
|
|
||||||
for _, line in ipairs(vim.split(compile_status, "\n")) do
|
for _, line in ipairs(vim.split(compile_status, "\n")) do
|
||||||
table.insert(message, line)
|
table.insert(message, line)
|
||||||
end
|
end
|
||||||
@@ -277,7 +305,6 @@ function M.build()
|
|||||||
message = {"Compilation successful!"}
|
message = {"Compilation successful!"}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Display message in terminal
|
|
||||||
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, message)
|
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, message)
|
||||||
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user