This commit is contained in:
2026-09-25 20:54:28 +08:00
parent 88c9035c43
commit 07532d215f
33 changed files with 843 additions and 628 deletions
+19 -11
View File
@@ -4,6 +4,14 @@
local M = {}
--- 获取 CPU 核心数,用于并行构建
---@return integer
local function cpu_count()
local info = vim.loop.cpu_info()
return info and #info or 8
end
local NPROC = tostring(cpu_count())
--- 检测当前项目类型
---@return "rust"|"cmake"|"python"|"go"|"unknown"
function M.detect()
@@ -62,31 +70,31 @@ local function run_in_terminal(cmd, title)
term:toggle()
end
--- 构建(使用底部终端)
--- 构建(使用底部终端,多核并行)
function M.build()
local project = M.detect()
if project == "rust" then
run_in_terminal("cargo build", "Cargo Build")
run_in_terminal("cargo build -j " .. NPROC, "Cargo Build")
elseif project == "cmake" then
vim.cmd("CMakeBuild")
elseif project == "go" then
run_in_terminal("go build ./...", "Go Build")
run_in_terminal("go build -p " .. NPROC .. " ./...", "Go Build")
else
vim.notify("未识别的项目类型,无法自动构建", vim.log.levels.WARN)
end
end
--- 运行(使用底部终端)
--- 运行(使用底部终端,编译阶段并行)
function M.run()
local project = M.detect()
if project == "rust" then
run_in_terminal("cargo run", "Cargo Run")
run_in_terminal("cargo run -j " .. NPROC, "Cargo Run")
elseif project == "cmake" then
vim.cmd("CMakeRun")
elseif project == "go" then
run_in_terminal("go run .", "Go Run")
run_in_terminal("go run -p " .. NPROC .. " .", "Go Run")
elseif project == "python" then
run_in_terminal("python " .. vim.fn.expand("%"), "Python Run")
else
@@ -107,19 +115,19 @@ function M.debug()
end
end
--- 测试(使用底部终端)
--- 测试(使用底部终端,并行执行)
function M.test()
local project = M.detect()
if project == "rust" then
run_in_terminal("cargo test", "Cargo Test")
run_in_terminal("cargo test -j " .. NPROC, "Cargo Test")
elseif project == "cmake" then
vim.cmd("CMakeBuild")
run_in_terminal("ctest --test-dir build", "CTest")
run_in_terminal("ctest --test-dir build -j " .. NPROC, "CTest")
elseif project == "go" then
run_in_terminal("go test ./...", "Go Test")
run_in_terminal("go test -p " .. NPROC .. " ./...", "Go Test")
elseif project == "python" then
run_in_terminal("pytest", "Pytest")
run_in_terminal("pytest -n " .. NPROC, "Pytest")
else
vim.notify("未识别的项目类型,无法自动测试", vim.log.levels.WARN)
end