Update c.lua
This commit is contained in:
+24
-130
@@ -4,6 +4,8 @@ M.c_build = nil
|
|||||||
M.c_run = nil
|
M.c_run = nil
|
||||||
M.new_c_file = nil
|
M.new_c_file = nil
|
||||||
M.new_c_project = nil
|
M.new_c_project = nil
|
||||||
|
M.build_term_buf = nil
|
||||||
|
M.build_term_win = nil -- Store the window reference for the build terminal
|
||||||
|
|
||||||
-- Default config
|
-- Default config
|
||||||
M.config = {
|
M.config = {
|
||||||
@@ -119,120 +121,23 @@ function M.open_float_terminal(cmd)
|
|||||||
vim.fn.termopen(cmd)
|
vim.fn.termopen(cmd)
|
||||||
|
|
||||||
vim.api.nvim_buf_set_keymap(buf, "n", M.close_key, "i<C-\\><C-n>:q<CR>", { noremap = true, silent = true })
|
vim.api.nvim_buf_set_keymap(buf, "n", M.close_key, "i<C-\\><C-n>:q<CR>", { noremap = true, silent = true })
|
||||||
|
|
||||||
|
-- Store the terminal buffer and window for later use
|
||||||
|
M.build_term_buf = buf
|
||||||
|
M.build_term_win = win
|
||||||
|
|
||||||
return buf, win
|
return buf, win
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Create a new Java project
|
|
||||||
function M.create_project()
|
|
||||||
-- Use vim.ui.input for better handling of input
|
|
||||||
vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
|
|
||||||
if not project_name or project_name == "" then
|
|
||||||
print("Project creation canceled.")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Paths for the project
|
|
||||||
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
|
|
||||||
local src_dir = project_root .. "/src"
|
|
||||||
local build_dir = project_root .. "/build"
|
|
||||||
local include_dir = project_root .. "/include"
|
|
||||||
|
|
||||||
-- Create directories
|
|
||||||
vim.fn.mkdir(src_dir, "p")
|
|
||||||
vim.fn.mkdir(build_dir, "p")
|
|
||||||
vim.fn.mkdir(include_dir, "p")
|
|
||||||
|
|
||||||
-- Write project name to file
|
|
||||||
local project_name_file = io.open(build_dir .. "/project_name.txt", "w")
|
|
||||||
if project_name_file then
|
|
||||||
project_name_file:write(project_name)
|
|
||||||
project_name_file:close()
|
|
||||||
else
|
|
||||||
print("Error creating project_name.txt")
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Write the Main.java file
|
|
||||||
local main_c_path = src_dir .. "/main.c"
|
|
||||||
local main_c_content = [[
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
printf("Hello World\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
]]
|
|
||||||
local Cmake_path = project_root .. "/" .. "CMakeLists.txt"
|
|
||||||
local Cmake_content = string.format([[
|
|
||||||
cmake_minimum_required(VERSION 3.10)
|
|
||||||
project(%s C)
|
|
||||||
set(CMAKE_C_STANDARD 11)
|
|
||||||
include_directories(include)
|
|
||||||
|
|
||||||
set(SOURCES
|
|
||||||
src/main.c
|
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(%s ${SOURCES})
|
|
||||||
]], project_name, project_name)
|
|
||||||
|
|
||||||
-- Create the file and write content
|
|
||||||
local file = io.open(main_c_path, "w")
|
|
||||||
if file then
|
|
||||||
file:write(main_c_content)
|
|
||||||
file:close()
|
|
||||||
vim.cmd("edit " .. main_c_path)
|
|
||||||
print(" Project '" .. project_name .. "' created at " .. project_root)
|
|
||||||
else
|
|
||||||
print("Error creating main.c")
|
|
||||||
end
|
|
||||||
|
|
||||||
local cmake_file = io.open(Cmake_path, "w")
|
|
||||||
if cmake_file then
|
|
||||||
cmake_file:write(Cmake_content)
|
|
||||||
cmake_file:close()
|
|
||||||
vim.cmd("edit " .. project_root)
|
|
||||||
print(" Cmake created! '")
|
|
||||||
else
|
|
||||||
print("Error creating CMakeLists.txt")
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Create a new Java file
|
|
||||||
function M.create_new_file()
|
|
||||||
vim.ui.input({ prompt = "Enter class name: " }, function(class_name)
|
|
||||||
if not class_name or class_name == "" then
|
|
||||||
print("Class 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 java_file_path = root_dir .. "/src/" .. class_name .. ".java"
|
|
||||||
local java_content = string.format([[
|
|
||||||
public class %s {
|
|
||||||
// New File
|
|
||||||
}
|
|
||||||
]], class_name)
|
|
||||||
|
|
||||||
local file = io.open(java_file_path, "w")
|
|
||||||
if file then
|
|
||||||
file:write(java_content)
|
|
||||||
file:close()
|
|
||||||
vim.cmd("edit " .. java_file_path)
|
|
||||||
print( class_name .. ".java created successfully!")
|
|
||||||
else
|
|
||||||
print("Error creating " .. class_name .. ".java")
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.build()
|
function M.build()
|
||||||
|
-- Close the build terminal if it's open before running the build
|
||||||
|
if M.build_term_buf and vim.api.nvim_buf_is_valid(M.build_term_buf) then
|
||||||
|
vim.api.nvim_buf_delete(M.build_term_buf, { force = true })
|
||||||
|
vim.api.nvim_win_close(M.build_term_win, true)
|
||||||
|
M.build_term_buf = nil
|
||||||
|
M.build_term_win = nil
|
||||||
|
end
|
||||||
|
|
||||||
-- Get the current file's directory
|
-- 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
|
-- Try to find the root of the project by matching the path
|
||||||
@@ -282,27 +187,15 @@ function M.build()
|
|||||||
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- function M.build()
|
|
||||||
-- vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
|
|
||||||
-- local build_project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name .. "/build"
|
|
||||||
-- local compile_command = "cd " .. build_project_root .. " && cmake .. && make"
|
|
||||||
-- local term_buf = M.open_float_terminal(compile_command)
|
|
||||||
-- vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
|
|
||||||
-- local compile_status = vim.fn.system(compile_command)
|
|
||||||
--
|
|
||||||
-- if vim.v.shell_error ~= 0 then
|
|
||||||
-- vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, {"Error during compilation:"})
|
|
||||||
-- vim.api.nvim_buf_set_lines(term_buf, 1, -1, false, vim.split(compile_status, "\n"))
|
|
||||||
-- else
|
|
||||||
-- vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, {"Compilation successful!"})
|
|
||||||
-- end
|
|
||||||
--
|
|
||||||
-- vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
|
||||||
-- end)
|
|
||||||
-- end
|
|
||||||
|
|
||||||
-- Run Java program in floating terminal
|
|
||||||
function M.run()
|
function M.run()
|
||||||
|
-- Close the build terminal if it's open before running the program
|
||||||
|
if M.build_term_buf and vim.api.nvim_buf_is_valid(M.build_term_buf) then
|
||||||
|
vim.api.nvim_buf_delete(M.build_term_buf, { force = true })
|
||||||
|
vim.api.nvim_win_close(M.build_term_win, true)
|
||||||
|
M.build_term_buf = nil
|
||||||
|
M.build_term_win = nil
|
||||||
|
end
|
||||||
|
|
||||||
local current_dir = vim.fn.expand("%:p:h")
|
local current_dir = vim.fn.expand("%:p:h")
|
||||||
local project_root = current_dir:match("(.*)/src")
|
local project_root = current_dir:match("(.*)/src")
|
||||||
if not project_root then
|
if not project_root then
|
||||||
@@ -315,3 +208,4 @@ function M.run()
|
|||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user