Update to build and run, update to getting project root name
This commit is contained in:
+18
-59
@@ -201,13 +201,7 @@ function M.create_new_file()
|
|||||||
if not class_name then return print("File creation canceled.") end
|
if not class_name then return print("File creation canceled.") end
|
||||||
local root_dir = vim.fn.expand("%:p:h"):match("(.*)/src")
|
local root_dir = vim.fn.expand("%:p:h"):match("(.*)/src")
|
||||||
if not root_dir then return print("Error: src directory not found.") end
|
if not root_dir then return print("Error: src directory not found.") end
|
||||||
local c_content = [[
|
local c_content = [[]]
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
printf("Hello, World!\n");
|
|
||||||
return 0;
|
|
||||||
}]]
|
|
||||||
write_file(root_dir .. "/src/" .. class_name .. ".c", c_content)
|
write_file(root_dir .. "/src/" .. class_name .. ".c", c_content)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@@ -234,69 +228,34 @@ void example_function();
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.get_project_root()
|
function M.get_project_root()
|
||||||
local current_dir = vim.fn.expand("%:p:h")
|
local dir = vim.fn.expand("%:p:h")
|
||||||
local root_dir = nil
|
while dir ~= "/" do
|
||||||
while current_dir ~= "/" do
|
if vim.fn.isdirectory(dir .. "/src") == 1 or vim.fn.filereadable(dir .. "/CMakeLists.txt") == 1 then
|
||||||
if vim.fn.isdirectory(current_dir .. "/src") == 1 then
|
return dir
|
||||||
root_dir = current_dir
|
|
||||||
break
|
|
||||||
end
|
end
|
||||||
-- Check if CMakeLists.txt exists (in case src directory isn't found)
|
dir = vim.fn.fnamemodify(dir, ":h")
|
||||||
if vim.fn.filereadable(current_dir .. "/CMakeLists.txt") == 1 then
|
|
||||||
root_dir = current_dir
|
|
||||||
break
|
|
||||||
end
|
end
|
||||||
current_dir = vim.fn.fnamemodify(current_dir, ":h") -- Go up one level
|
|
||||||
end
|
|
||||||
|
|
||||||
return root_dir
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.build()
|
function M.build()
|
||||||
local project_root = M.get_project_root()
|
local root = M.get_project_root()
|
||||||
if not project_root then
|
if not root then return print("Error: Project root not found.") end
|
||||||
print("Error: Project root not found.")
|
local cmd = "cd " .. root .. "/build && cmake .. && make"
|
||||||
return
|
local term_buf = M.open_float_terminal(cmd)
|
||||||
end
|
|
||||||
|
|
||||||
local build_dir = project_root .. "/build"
|
|
||||||
local compile_command = "cd " .. build_dir .. " && cmake .. && make"
|
|
||||||
|
|
||||||
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)
|
||||||
|
local output = vim.fn.system(cmd)
|
||||||
local compile_status = vim.fn.system(compile_command)
|
local success = vim.v.shell_error == 0
|
||||||
local message = vim.v.shell_error == 0 and {"Compilation successful!"} or {"Error during compilation:"}
|
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, { success and "Compilation successful!" or "Error during compilation:", output })
|
||||||
|
|
||||||
if vim.v.shell_error ~= 0 then
|
|
||||||
for _, line in ipairs(vim.split(compile_status, "\n")) do
|
|
||||||
table.insert(message, line)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
function M.run()
|
function M.run()
|
||||||
local project_root = M.get_project_root()
|
local root = M.get_project_root()
|
||||||
if not project_root then
|
if not root then return print("Error: Project root not found.") end
|
||||||
print("Error: Project root not found.")
|
local build_dir = root .. "/build"
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local build_dir = project_root .. "/build"
|
|
||||||
local files = vim.fn.glob(build_dir .. "/*.polydev", true, true)
|
local files = vim.fn.glob(build_dir .. "/*.polydev", true, true)
|
||||||
if #files == 0 then
|
if #files == 0 then return print("Error: No .polydev file found in build directory.") end
|
||||||
print("Error: No .polydev file found in the build directory.")
|
M.open_float_terminal("cd " .. build_dir .. " && ./" .. files[1]:match("([^/]+)%.polydev$"))
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Directly use the .polydev file (no need to filter further)
|
|
||||||
local project_name = vim.fn.fnamemodify(files[1], ":t")
|
|
||||||
local name = project_name:gsub("%.polydev$", "")
|
|
||||||
|
|
||||||
M.open_float_terminal("cd " .. build_dir .. " && ./" .. name)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user