commenting preloads

This commit is contained in:
DarthMooMancer
2025-04-14 20:22:01 -04:00
parent 6696f4b64e
commit 120b1571cb
9 changed files with 8 additions and 8 deletions
+177
View File
@@ -0,0 +1,177 @@
local M = {}
local utils = require("Polydev.utils")
M.c_build = nil
M.c_run = nil
M.new_c_header_file = nil
M.config = {
project_root = "~/Projects/C",
keybinds = {
["<leader>pb"] = "CBuild",
["<leader>pr"] = "CRun",
["<leader>nh"] = "NewCHeaderFile",
},
build_attributes = ""
}
function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
for key, command in pairs(M.config.keybinds) do
if command == "CBuild" then M.c_build = key end
if command == "CRun" then M.c_run = key end
if command == "NewCHeaderFile" then M.new_c_header_file = key end
end
vim.api.nvim_create_user_command("NewCHeaderFile", M.create_new_header_file, {})
vim.api.nvim_create_user_command("CBuild", M.build, {})
vim.api.nvim_create_user_command("CRun", M.run, {})
vim.keymap.set("n", M.c_build, ":CBuild<CR>", { silent = true })
vim.keymap.set("n", M.c_run, ":CRun<CR>", { silent = true })
vim.keymap.set("n", M.new_c_header_file, ":NewCHeaderFile<CR>", { silent = true })
end
function M.create_project()
vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
if not project_name or project_name == "" then
print("Project creation canceled.")
return
end
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build", "/include" }) do
vim.fn.mkdir(project_root .. path, "p")
end
utils.write_file(project_root .. "/build/" .. project_name .. ".polydev", project_name)
utils.write_file(project_root .. "/src/main.c", [[
/* If you have vcpkg and want to use it:
vcpkg new --application
vcpkg add port <package_name>
*/
#include <stdio.h>
int main() {
printf("%s", "Hello World");
return 0;
}
]])
utils.write_file(project_root .. "/CMakeLists.txt", string.format([[
cmake_minimum_required(VERSION 3.10)
project(%s)
include_directories(include)
set(SOURCES src/main.c)
add_executable(%s ${SOURCES})
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)
# find_package(SDL3 CONFIG REQUIRED) # An example of how to import libraries from vcpkg
# target_link_libraries(Game PRIVATE SDL3::SDL3)
]], project_name, project_name))
utils.write_file(project_root .. "/CMakePresets.json", string.format([[
# For the purposes of vcpkg, DO NOT REMOVE, but feel free to change as needed
{
"version": 2,
"configurePresets": [
{
"name": "vcpkg",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
}
}
]
}
]]))
utils.write_file(project_root .. "/CMakeUserPresets.json", string.format([[
{
"version": 2,
"configurePresets": [
{
"name": "default",
"inherits": "vcpkg",
"environment": {
"VCPKG_ROOT": "/Users/<USER>/vcpkg"
}
}
]
}
]]))
vim.cmd("edit " .. project_root .. "/src/main.c")
end)
end
function M.create_new_file()
vim.ui.input({ prompt = "Enter file name: " }, function(class_name)
if not class_name or class_name == "" then return print("File creation canceled.") end
local root_dir = M.get_project_root()
if not root_dir then return print("Error: Project root not found.") end
utils.write_file(root_dir .. "/src/" .. class_name .. ".c", "")
end)
end
function M.create_new_header_file()
vim.ui.input({ prompt = "Enter header file name: " }, function(header_name)
if not header_name then return print("Header file creation canceled.") end
local root_dir = M.get_project_root()
if not root_dir then return print("Error: Project root not found.") end
local guard_macro = header_name:upper():gsub("[^A-Z0-9]", "_") .. "_H"
local content = string.format([[
#ifndef %s
#define %s
#include <stdio.h>
// Function prototypes
void example_function();
#endif // %s
]], guard_macro, guard_macro, guard_macro)
utils.write_file(root_dir .. "/include/" .. header_name .. ".h", content)
end)
end
function M.get_project_root()
local dir = vim.fn.expand("%:p:h")
while dir ~= "/" do
if vim.fn.isdirectory(dir .. "/src") == 1 or vim.fn.filereadable(dir .. "/CMakeLists.txt") == 1 then
return dir
end
dir = vim.fn.fnamemodify(dir, ":h")
end
end
function M.build()
local root = M.get_project_root()
if not root then return print("Error: Project root not found.") end
local build_dir = root .. "/build"
local cmd = "cd " .. build_dir .. " && cmake --build ."
local term_buf = utils.open_float_terminal(cmd)
vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
local output = vim.fn.system(cmd)
local success = vim.v.shell_error == 0
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, vim.list_extend({ success and "Compilation successful!" or "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })))
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
end
function M.run()
local root = M.get_project_root()
if not root then return print("Error: Project root not found.") end
local build_dir = root .. "/build"
local files = vim.fn.glob(build_dir .. "/*.polydev", true, true)
if #files == 0 then return print("Error: No .polydev file found in build directory.") end
utils.open_float_terminal("cd " .. build_dir .. " && ./" .. files[1]:match("([^/]+)%.polydev$"))
end
return M
+176
View File
@@ -0,0 +1,176 @@
local M = {}
local utils = require("Polydev.utils")
M.c_build = nil
M.c_run = nil
M.new_c_header_file = nil
M.config = {
project_root = "~/Projects/C",
keybinds = {
["<leader>pb"] = "CBuild",
["<leader>pr"] = "CRun",
["<leader>nh"] = "NewCHeaderFile",
},
build_attributes = ""
}
function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
for key, command in pairs(M.config.keybinds) do
if command == "CBuild" then M.c_build = key end
if command == "CRun" then M.c_run = key end
if command == "NewCHeaderFile" then M.new_c_header_file = key end
end
vim.api.nvim_create_user_command("NewCHeaderFile", M.create_new_header_file, {})
vim.api.nvim_create_user_command("CBuild", M.build, {})
vim.api.nvim_create_user_command("CRun", M.run, {})
vim.keymap.set("n", M.c_build, ":CBuild<CR>", { silent = true })
vim.keymap.set("n", M.c_run, ":CRun<CR>", { silent = true })
vim.keymap.set("n", M.new_c_header_file, ":NewCHeaderFile<CR>", { silent = true })
end
function M.create_project()
vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
if not project_name or project_name == "" then
print("Project creation canceled.")
return
end
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build", "/include" }) do
vim.fn.mkdir(project_root .. path, "p")
end
utils.write_file(project_root .. "/build/" .. project_name .. ".polydev", project_name)
utils.write_file(project_root .. "/src/main.cpp", [[
/* If you have vcpkg and want to use it:
vcpkg new --application
vcpkg add port <package_name>
*/
#include <iostream>
int main() {
std::cout << "Hello World" << std::endl;
return 0;
}
]])
utils.write_file(project_root .. "/CMakeLists.txt", string.format([[
cmake_minimum_required(VERSION 3.10)
project(%s)
include_directories(include)
set(SOURCES src/main.c)
add_executable(%s ${SOURCES})
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)
# find_package(SDL3 CONFIG REQUIRED) # An example of how to import libraries from vcpkg
# target_link_libraries(Game PRIVATE SDL3::SDL3)
]], project_name, project_name))
utils.write_file(project_root .. "/CMakePresets.json", string.format([[
# For the purposes of vcpkg, DO NOT REMOVE, but feel free to change as needed
{
"version": 2,
"configurePresets": [
{
"name": "vcpkg",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
}
}
]
}
]]))
utils.write_file(project_root .. "/CMakeUserPresets.json", string.format([[
{
"version": 2,
"configurePresets": [
{
"name": "default",
"inherits": "vcpkg",
"environment": {
"VCPKG_ROOT": "/Users/<USER>/vcpkg"
}
}
]
}
]]))
vim.cmd("edit " .. project_root .. "/src/main.cpp")
end)
end
function M.create_new_file()
vim.ui.input({ prompt = "Enter file name: " }, function(class_name)
if not class_name or class_name == "" then return print("File creation canceled.") end
local root_dir = M.get_project_root()
if not root_dir then return print("Error: Project root not found.") end
utils.write_file(root_dir .. "/src/" .. class_name .. ".cpp", "")
end)
end
function M.create_new_header_file()
vim.ui.input({ prompt = "Enter header file name: " }, function(header_name)
if not header_name then return print("Header file creation canceled.") end
local root_dir = M.get_project_root()
if not root_dir then return print("Error: Project root not found.") end
local guard_macro = header_name:upper():gsub("[^A-Z0-9]", "_") .. "_HPP"
local content = string.format([[
#ifndef %s
#define %s
#include <iostream>
// Function prototypes
void example_function();
#endif // %s
]], guard_macro, guard_macro, guard_macro)
utils.write_file(root_dir .. "/include/" .. header_name .. ".hpp", content)
end)
end
function M.get_project_root()
local dir = vim.fn.expand("%:p:h")
while dir ~= "/" do
if vim.fn.isdirectory(dir .. "/src") == 1 or vim.fn.filereadable(dir .. "/CMakeLists.txt") == 1 then
return dir
end
dir = vim.fn.fnamemodify(dir, ":h")
end
end
function M.build()
local root = M.get_project_root()
if not root then return print("Error: Project root not found.") end
local build_dir = root .. "/build"
local cmd = "cd " .. build_dir .. " && cmake --build ."
local term_buf = utils.open_float_terminal(cmd)
vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
local output = vim.fn.system(cmd)
local success = vim.v.shell_error == 0
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, vim.list_extend({ success and "Compilation successful!" or "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })))
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
end
function M.run()
local root = M.get_project_root()
if not root then return print("Error: Project root not found.") end
local build_dir = root .. "/build"
local files = vim.fn.glob(build_dir .. "/*.polydev", true, true)
if #files == 0 then return print("Error: No .polydev file found in build directory.") end
utils.open_float_terminal("cd " .. build_dir .. " && ./" .. files[1]:match("([^/]+)%.polydev$"))
end
return M
+61
View File
@@ -0,0 +1,61 @@
local M = {}
local utils = require("Polydev.utils")
M.html_build = nil
M.html_run = nil
M.config = {
project_root = "~/Projects/Html",
}
function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
end
function M.create_project()
vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
if not project_name or project_name == "" then return print("Project creation canceled.") end
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
local main_html_content = [[
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
]]
utils.write_file(project_root .. "index.html", main_html_content)
end)
end
function M.create_new_file()
vim.ui.input({ prompt = "Enter page name: " }, function(page)
if not page or page == "" then return print("Class creation canceled.") end
local html_content = string.format([[
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
]])
utils.write_file("./" .. page .. ".html", html_content)
end)
end
return M
+89
View File
@@ -0,0 +1,89 @@
local M = {}
local utils = require("Polydev.utils")
M.java_build = nil
M.java_run = nil
M.config = {
project_root = "~/Projects/Java",
keybinds = {
["<leader>pb"] = "JavaBuild",
["<leader>pr"] = "JavaRun",
},
}
function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
for key, command in pairs(M.config.keybinds) do
if command == "JavaBuild" then M.java_build = key end
if command == "JavaRun" then M.java_run = key end
end
vim.keymap.set("n", M.java_build, ":JavaBuild<CR>", { silent = true })
vim.keymap.set("n", M.java_run, ":JavaRun<CR>", { silent = true })
vim.api.nvim_create_user_command("JavaBuild", M.build, {})
vim.api.nvim_create_user_command("JavaRun", M.run, {})
end
function M.create_project()
vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
if not project_name or project_name == "" then return print("Project creation canceled.") end
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build" }) do vim.fn.mkdir(project_root .. path, "p") end
local main_java_content = [[
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
]]
utils.write_file(project_root .. "/src/Main.java", main_java_content)
end)
end
function M.create_new_file()
vim.ui.input({ prompt = "Enter class name: " }, function(class_name)
if not class_name or class_name == "" then return print("Class creation canceled.") end
local root_dir = vim.fn.expand("%:p:h"):match("(.*)/src")
if not root_dir then return print("Error: src directory not found.") end
local java_content = string.format([[
public class %s {
// New File
}
]], class_name)
utils.write_file(root_dir .. "/src/" .. class_name .. ".java", java_content)
end)
end
function M.build()
local project_root = vim.fn.expand("%:p:h"):match("(.*)/src")
if not project_root then return print("Error: Could not detect project root directory.") end
local out_dir = project_root .. "/build"
vim.fn.mkdir(out_dir, "p")
local compile_command = string.format("javac -d %s %s/*.java", out_dir, project_root .. "/src")
local compile_status = vim.fn.system(compile_command)
if vim.v.shell_error == 0 then
print("Compilation successful!")
else
print("Error during compilation. Opening terminal for details...")
local term_buf = utils.open_float_terminal(compile_command)
vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, vim.split(compile_status, "\n", { trimempty = true }))
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
end
end
function M.run()
local project_root = vim.fn.expand("%:p:h"):match("(.*)/src")
if not project_root then return print("Error: Must be inside the 'src' directory.") end
utils.open_float_terminal("java -cp " .. project_root .. "/build" .. " Main")
end
return M
+79
View File
@@ -0,0 +1,79 @@
local M = {}
local utils = require("Polydev.utils")
M.lua_run = nil
M.config = {
project_root = "~/Projects/Lua",
keybinds = {
["<leader>pr"] = "LuaRun",
},
}
function M.get_project_root()
local dir = vim.fn.expand("%:p:h")
while dir ~= "/" do
local polydev_file = vim.fn.glob(dir .. "/*.polydev")
if polydev_file ~= "" then return dir end
dir = vim.fn.fnamemodify(dir, ":h")
end
return nil
end
function M.get_project_name()
local root = M.get_project_root()
if not root then return nil end
local polydev_file = vim.fn.glob(root .. "/*.polydev")
return polydev_file:match("([^/]+)%.polydev$")
end
function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
for key, command in pairs(M.config.keybinds) do
if command == "LuaRun" then M.lua_run = key end
end
vim.api.nvim_create_user_command("LuaRun", M.run, {})
vim.keymap.set("n", M.lua_run, ":LuaRun<CR>", { silent = true })
end
function M.create_project()
vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
if not project_name or project_name == "" then return print("Project creation canceled.") end
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
for _, path in ipairs({ "/lua/" .. project_name }) do vim.fn.mkdir(project_root .. path, "p") end
utils.write_file(project_root .. "/lua/" .. project_name .. "/init.lua", [[
local M = {}
return M
]])
utils.write_file(project_root .. "/" .. project_name .. ".polydev", "[[DO NOT REMOVE THIS FILE]]")
vim.cmd("edit " .. project_root .. "/lua/" .. project_name .. "/init.lua")
end)
end
function M.create_new_file()
vim.ui.input({ prompt = "Enter file name: " }, function(class_name)
if not class_name or class_name == "" then return print("File creation canceled.") end
local root_dir = M.get_project_root()
local project_name = M.get_project_name()
if not root_dir or not project_name then return print("Error: Project root not found.") end
utils.write_file(root_dir .. "/lua/" .. project_name .. "/" .. class_name .. ".lua", "")
end)
end
function M.run()
local root = M.get_project_root()
local project_name = M.get_project_name()
if not root or not project_name then return print("Error: Project root not found.") end
local init_lua_path = root .. "/lua/" .. project_name .. "/init.lua"
if vim.fn.filereadable(init_lua_path) == 1 then
utils.open_float_terminal("lua " .. init_lua_path)
else
print("Error: " .. init_lua_path .. " not found in " .. root)
end
end
return M
+179
View File
@@ -0,0 +1,179 @@
local M = {}
local utils = require("Polydev.utils")
M.c_run = nil
M.new_pip_module = nil
M.run_custom = nil
M.config = {
project_root = "~/Projects/Python",
keybinds = {
["<leader>pr"] = "PythonRun",
["<leader>pb"] = "PythonPip",
["<leader>pc"] = "PythonCustom"
},
}
function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
for key, command in pairs(M.config.keybinds) do
if command == "PythonRun" then M.c_run = key end
if command == "PythonPip" then M.new_pip_module = key end
if command == "PythonCustom" then M.run_custom = key end
end
local root = M.get_project_root()
if root then
local venv_path = root .. "/venv/bin/activate"
if vim.fn.filereadable(venv_path) == 1 then
vim.env.VIRTUAL_ENV = venv_path
vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH
end
end
vim.api.nvim_create_user_command("PythonPip", M.install_dependency, {})
vim.api.nvim_create_user_command("PythonRun", M.run, {})
vim.api.nvim_create_user_command("PythonCustom", M.custom_run, {})
vim.keymap.set("n", M.c_run, ":PythonRun<CR>", { silent = true })
vim.keymap.set("n", M.new_pip_module, ":PythonPip<CR>", { silent = true })
vim.keymap.set("n", M.run_custom, ":PythonCustom<CR>", { silent = true })
end
function M.get_project_root()
local dir = vim.fn.expand("%:p:h")
while dir ~= "/" do
if vim.fn.isdirectory(dir .. "/venv") == 1 or vim.fn.filereadable(dir .. "/main.py") == 1 then
return dir
end
dir = vim.fn.fnamemodify(dir, ":h")
end
return nil
end
function M.create_project()
vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
if not project_name or project_name == "" then
print("Project creation canceled.")
return
end
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
for _, path in ipairs({ "/include" }) do
vim.fn.mkdir(project_root .. path, "p")
end
utils.write_file(project_root .. "/main.py", [[
def main():
print("Hello, World!")
if __name__ == "__main__":
main()
]])
utils.write_file(project_root .. "/include/__init__.py", "")
utils.write_file(project_root .. "/requirements.txt", "")
utils.write_file(project_root .. "/setup.py", string.format([[
from setuptools import setup, find_packages
setup(
name="%s",
version="0.1.0",
packages=find_packages(),
install_requires=[],
python_requires=">=3.6",
)
]], project_name))
vim.cmd("edit " .. project_root .. "/main.py")
local venv_path = project_root .. "/venv"
vim.fn.system("python3 -m venv " .. venv_path)
vim.g.python3_host_prog = venv_path .. "/bin/python"
vim.fn.setenv("VIRTUAL_ENV", venv_path)
vim.fn.setenv("PATH", venv_path .. "/bin:" .. vim.fn.getenv("PATH"))
print("Virtual environment activated for " .. project_name)
end)
end
function M.create_new_file()
vim.ui.input({ prompt = "Enter file name: " }, function(file_name)
if not file_name or file_name == "" then
return print("File creation canceled.")
end
if not file_name:match("%.py$") then
file_name = file_name .. ".py"
end
local root_dir = M.get_project_root()
if not root_dir then
return print("Error: Project root not found.")
end
local file_path = root_dir .. "/include/" .. file_name
utils.write_file(file_path, "")
end)
end
function M.install_dependency()
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
if not pip_module or pip_module == "" then
return print("Pip canceled")
end
utils.open_float_terminal("pip install " .. pip_module)
end)
end
function M.custom_run()
vim.ui.input({ prompt = "Enter File Name: " }, function(custom_file)
if not custom_file or custom_file == "" then
return print("Run canceled")
end
local root = M.get_project_root()
-- Detect current shell
local shell = vim.fn.getenv("SHELL") or ""
local venv_cmd
if shell:match("fish") then
local venv_fish = root .. "/venv/bin/activate.fish"
venv_cmd = vim.fn.filereadable(venv_fish) == 1 and "source " .. venv_fish .. " && " or ""
else
local venv_bash = root .. "/venv/bin/activate"
venv_cmd = vim.fn.filereadable(venv_bash) == 1 and "source " .. venv_bash .. " && " or ""
end
utils.open_float_terminal(venv_cmd .. "python3 " .. custom_file .. ".py")
end)
end
function M.run()
local root = M.get_project_root()
if not root then
return print("Error: Project root not found.")
end
local main_file = root .. "/main.py"
if vim.fn.filereadable(main_file) == 0 then
return print("Error: main.py not found in project root.")
end
-- Detect current shell
local shell = vim.fn.getenv("SHELL") or ""
local venv_cmd
if shell:match("fish") then
local venv_fish = root .. "/venv/bin/activate.fish"
venv_cmd = vim.fn.filereadable(venv_fish) == 1 and "source " .. venv_fish .. " && " or ""
else
local venv_bash = root .. "/venv/bin/activate"
venv_cmd = vim.fn.filereadable(venv_bash) == 1 and "source " .. venv_bash .. " && " or ""
end
utils.open_float_terminal(venv_cmd .. "python3 " .. main_file)
end
return M
+75
View File
@@ -0,0 +1,75 @@
local M = {}
local utils = require("Polydev.utils")
M.rust_build = nil
M.rust_run = nil
M.config = {
project_root = "~/Projects/Rust",
keybinds = {
["<leader>pb"] = "RustBuild",
["<leader>pr"] = "RustRun",
},
}
function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
for key, command in pairs(M.config.keybinds) do
if command == "RustBuild" then M.rust_build = key end
if command == "RustRun" then M.rust_run = key end
end
vim.api.nvim_create_user_command("RustBuild", M.build, {})
vim.api.nvim_create_user_command("RustRun", M.run, {})
vim.keymap.set("n", M.rust_build, ":RustBuild<CR>", { silent = true })
vim.keymap.set("n", M.rust_run, ":RustRun<CR>", { silent = true })
end
function M.create_project()
vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
if not project_name or project_name == "" then return print("Project creation canceled.") end
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
vim.fn.system("cargo new " .. project_root)
vim.cmd("edit " .. project_root .. "/src/main.rs")
end)
end
function M.create_new_file()
vim.ui.input({ prompt = "Enter file name: " }, function(class_name)
if not class_name or class_name == "" then return print("File creation canceled.") end
local root_dir = M.get_project_root()
if not root_dir then return print("Error: Project root not found.") end
utils.write_file(root_dir .. "/src/" .. class_name .. ".rs", "")
end)
end
function M.get_project_root()
local dir = vim.fn.expand("%:p:h")
while dir ~= "/" do
if vim.fn.isdirectory(dir .. "/src") == 1 then
return dir
end
dir = vim.fn.fnamemodify(dir, ":h")
end
end
function M.build()
local root = M.get_project_root()
if not root then return print("Error: Project root not found.") end
local cmd = "cargo build"
local term_buf = utils.open_float_terminal(cmd)
vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
local output = vim.fn.system(cmd)
local success = vim.v.shell_error == 0
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, vim.list_extend({ success and "Compilation successful!" or "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })))
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
end
function M.run()
local root = M.get_project_root()
if not root then return print("Error: Project root not found.") end
utils.open_float_terminal("cargo run")
end
return M