初始化dotfiles
This commit is contained in:
91
nvim/lua/runner.lua
Normal file
91
nvim/lua/runner.lua
Normal file
@ -0,0 +1,91 @@
|
||||
-- ============================================================================
|
||||
-- 统一构建/运行/调试分发器
|
||||
-- 根据项目类型自动选择正确的工具
|
||||
-- ============================================================================
|
||||
|
||||
local M = {}
|
||||
|
||||
--- 检测当前项目类型
|
||||
---@return "rust"|"cmake"|"python"|"go"|"unknown"
|
||||
function M.detect()
|
||||
local cwd = vim.fn.getcwd()
|
||||
|
||||
if vim.fn.filereadable(cwd .. "/Cargo.toml") == 1 then return "rust" end
|
||||
if vim.fn.filereadable(cwd .. "/CMakeLists.txt") == 1 then return "cmake" end
|
||||
if vim.fn.filereadable(cwd .. "/go.mod") == 1 then return "go" end
|
||||
if vim.fn.filereadable(cwd .. "/pyproject.toml") == 1 then return "python" end
|
||||
if vim.fn.filereadable(cwd .. "/setup.py") == 1 then return "python" end
|
||||
|
||||
return "unknown"
|
||||
end
|
||||
|
||||
--- 构建
|
||||
function M.build()
|
||||
local project = M.detect()
|
||||
|
||||
if project == "rust" then
|
||||
vim.cmd("RustLsp runnables")
|
||||
elseif project == "cmake" then
|
||||
vim.cmd("CMakeBuild")
|
||||
elseif project == "go" then
|
||||
local result = vim.fn.system("go build ./...")
|
||||
if vim.v.shell_error == 0 then
|
||||
vim.notify("go build 成功", vim.log.levels.INFO)
|
||||
else
|
||||
vim.notify("go build 失败:\n" .. result, vim.log.levels.ERROR)
|
||||
end
|
||||
else
|
||||
vim.notify("未识别的项目类型,无法自动构建", vim.log.levels.WARN)
|
||||
end
|
||||
end
|
||||
|
||||
--- 运行
|
||||
function M.run()
|
||||
local project = M.detect()
|
||||
|
||||
if project == "rust" then
|
||||
vim.cmd("RustLsp runnables")
|
||||
elseif project == "cmake" then
|
||||
vim.cmd("CMakeRun")
|
||||
elseif project == "go" then
|
||||
vim.cmd("terminal go run .")
|
||||
elseif project == "python" then
|
||||
vim.cmd("terminal python " .. vim.fn.expand("%"))
|
||||
else
|
||||
vim.notify("未识别的项目类型,无法自动运行", vim.log.levels.WARN)
|
||||
end
|
||||
end
|
||||
|
||||
--- 调试
|
||||
function M.debug()
|
||||
local project = M.detect()
|
||||
|
||||
if project == "rust" then
|
||||
vim.cmd("RustLsp debuggables")
|
||||
elseif project == "cmake" then
|
||||
vim.cmd("CMakeDebug")
|
||||
else
|
||||
-- 通用 DAP 启动
|
||||
require("dap").continue()
|
||||
end
|
||||
end
|
||||
|
||||
--- 测试
|
||||
function M.test()
|
||||
local project = M.detect()
|
||||
|
||||
if project == "rust" then
|
||||
vim.cmd("RustLsp testables")
|
||||
elseif project == "cmake" then
|
||||
vim.cmd("CMakeBuild")
|
||||
vim.cmd("terminal ctest --test-dir build")
|
||||
elseif project == "go" then
|
||||
vim.cmd("terminal go test ./...")
|
||||
elseif project == "python" then
|
||||
vim.cmd("terminal pytest")
|
||||
else
|
||||
vim.notify("未识别的项目类型,无法自动测试", vim.log.levels.WARN)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user