tests and terminal
This commit is contained in:
65
lua/plugins/config/neotest.lua
Normal file
65
lua/plugins/config/neotest.lua
Normal file
@@ -0,0 +1,65 @@
|
||||
local lib = require("neotest.lib")
|
||||
|
||||
local has_cpp_parser = false
|
||||
local ok_parsers, parsers = pcall(require, "nvim-treesitter.parsers")
|
||||
if ok_parsers and parsers.has_parser then
|
||||
has_cpp_parser = parsers.has_parser("cpp")
|
||||
end
|
||||
|
||||
if not has_cpp_parser then
|
||||
vim.schedule(function()
|
||||
vim.notify(
|
||||
"neotest-gtest needs Treesitter cpp parser. Run :TSInstall cpp",
|
||||
vim.log.levels.WARN
|
||||
)
|
||||
end)
|
||||
return
|
||||
end
|
||||
|
||||
require("neotest").setup({
|
||||
adapters = {
|
||||
require("neotest-gtest").setup({
|
||||
root = lib.files.match_root_pattern(
|
||||
"CMakeLists.txt",
|
||||
".git"
|
||||
),
|
||||
debug_adapter = "codelldb",
|
||||
is_test_file = function(file)
|
||||
local normalized = file:gsub("\\", "/")
|
||||
local is_in_tests_dir = normalized:match("/tests/") ~= nil
|
||||
local has_test_suffix = normalized:match("_test%.cpp$")
|
||||
or normalized:match("_test%.cc$")
|
||||
or normalized:match("_test%.cxx$")
|
||||
or normalized:match("_test%.c%+%+$")
|
||||
|
||||
return is_in_tests_dir and has_test_suffix ~= nil
|
||||
end,
|
||||
filter_dir = function(name, rel_path)
|
||||
if rel_path == "" then
|
||||
return true
|
||||
end
|
||||
|
||||
local blocked = {
|
||||
[".git"] = true,
|
||||
[".cache"] = true,
|
||||
["build"] = true,
|
||||
["out"] = true,
|
||||
["_deps"] = true,
|
||||
}
|
||||
|
||||
if blocked[name] then
|
||||
return false
|
||||
end
|
||||
|
||||
if rel_path:match("^build/") or rel_path:match("^out/") then
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end,
|
||||
mappings = {
|
||||
configure = "C",
|
||||
},
|
||||
}),
|
||||
},
|
||||
})
|
||||
117
lua/plugins/config/toggleterm.lua
Normal file
117
lua/plugins/config/toggleterm.lua
Normal file
@@ -0,0 +1,117 @@
|
||||
local toggleterm = require("toggleterm")
|
||||
local Terminal = require("toggleterm.terminal").Terminal
|
||||
|
||||
toggleterm.setup({
|
||||
start_in_insert = true,
|
||||
insert_mappings = true,
|
||||
terminal_mappings = true,
|
||||
persist_size = true,
|
||||
persist_mode = true,
|
||||
close_on_exit = false,
|
||||
direction = "horizontal",
|
||||
size = 14,
|
||||
})
|
||||
|
||||
local M = {}
|
||||
|
||||
local runners = {
|
||||
build = {
|
||||
cmd = "cmake --build --preset debug-with-tidy",
|
||||
title = "Build",
|
||||
},
|
||||
test = {
|
||||
cmd = "ctest --test-dir build --output-on-failure",
|
||||
title = "Tests",
|
||||
},
|
||||
}
|
||||
|
||||
local terminals = {
|
||||
shell = Terminal:new({
|
||||
hidden = true,
|
||||
close_on_exit = false,
|
||||
direction = "horizontal",
|
||||
}),
|
||||
}
|
||||
|
||||
local last_runner = nil
|
||||
|
||||
local function run_in_terminal(term, cmd)
|
||||
term:open()
|
||||
vim.defer_fn(function()
|
||||
term:send(cmd .. "\r", false)
|
||||
end, 20)
|
||||
end
|
||||
|
||||
local function project_root()
|
||||
local current_file = vim.api.nvim_buf_get_name(0)
|
||||
local start_path = current_file ~= "" and vim.fs.dirname(current_file) or vim.loop.cwd()
|
||||
local markers = { "CMakePresets.json", "CMakeLists.txt", ".git" }
|
||||
local found = vim.fs.find(markers, { upward = true, path = start_path })[1]
|
||||
|
||||
if found then
|
||||
return vim.fs.dirname(found)
|
||||
end
|
||||
|
||||
return vim.loop.cwd()
|
||||
end
|
||||
|
||||
local function get_runner_term(name)
|
||||
if terminals[name] then
|
||||
return terminals[name]
|
||||
end
|
||||
|
||||
terminals[name] = Terminal:new({
|
||||
hidden = true,
|
||||
close_on_exit = false,
|
||||
direction = "horizontal",
|
||||
display_name = runners[name].title,
|
||||
})
|
||||
|
||||
return terminals[name]
|
||||
end
|
||||
|
||||
function M.toggle_shell()
|
||||
terminals.shell.dir = project_root()
|
||||
terminals.shell:toggle()
|
||||
end
|
||||
|
||||
function M.run_build()
|
||||
local term = get_runner_term("build")
|
||||
term.dir = project_root()
|
||||
run_in_terminal(term, runners.build.cmd)
|
||||
last_runner = "build"
|
||||
end
|
||||
|
||||
function M.run_tests()
|
||||
local term = get_runner_term("test")
|
||||
term.dir = project_root()
|
||||
run_in_terminal(term, runners.test.cmd)
|
||||
last_runner = "test"
|
||||
end
|
||||
|
||||
function M.rerun_last()
|
||||
if not last_runner then
|
||||
vim.notify("No build/test command has been run yet", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
if last_runner == "build" then
|
||||
M.run_build()
|
||||
else
|
||||
M.run_tests()
|
||||
end
|
||||
end
|
||||
|
||||
function M.toggle_build_terminal()
|
||||
local term = get_runner_term("build")
|
||||
term.dir = project_root()
|
||||
term:toggle()
|
||||
end
|
||||
|
||||
function M.toggle_test_terminal()
|
||||
local term = get_runner_term("test")
|
||||
term.dir = project_root()
|
||||
term:toggle()
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,6 +1,6 @@
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
-- A list of parser names, or "all" (the listed parsers MUST always be installed)
|
||||
ensure_installed = { "c", "lua", "vim", "vimdoc", "query", "markdown", "markdown_inline", "zig" },
|
||||
ensure_installed = { "c", "cpp", "lua", "vim", "vimdoc", "query", "markdown", "markdown_inline", "zig" },
|
||||
|
||||
-- Install parsers synchronously (only applied to `ensure_installed`)
|
||||
sync_install = false,
|
||||
|
||||
@@ -22,7 +22,9 @@ if wk.add then
|
||||
{ "<leader>c", group = "CMake" },
|
||||
{ "<leader>d", group = "Debug" },
|
||||
{ "<leader>f", group = "Find" },
|
||||
{ "<leader>m", group = "Build" },
|
||||
{ "<leader>o", group = "OpenCode" },
|
||||
{ "<leader>t", group = "Tests" },
|
||||
{ "<leader>x", group = "Problems" },
|
||||
})
|
||||
else
|
||||
@@ -30,7 +32,9 @@ else
|
||||
c = { name = "+CMake" },
|
||||
d = { name = "+Debug" },
|
||||
f = { name = "+Find" },
|
||||
m = { name = "+Build" },
|
||||
o = { name = "+OpenCode" },
|
||||
t = { name = "+Tests" },
|
||||
x = { name = "+Problems" },
|
||||
}, { prefix = "<leader>" })
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user