add TSamFile and TSamAll command with default keymaps

This commit is contained in:
2025-12-23 23:35:40 +01:00
parent 463d0ae2f7
commit 61f9a7dcb4
8 changed files with 281 additions and 41 deletions

View File

@@ -250,6 +250,11 @@ local function append_lines(buf, new_lines)
end
local existing = vim.api.nvim_buf_line_count(buf)
vim.api.nvim_buf_set_lines(buf, existing, existing, false, new_lines)
if state.last_win and vim.api.nvim_win_is_valid(state.last_win) then
local total = vim.api.nvim_buf_line_count(buf)
vim.api.nvim_win_set_cursor(state.last_win, { total, 0 })
end
end
local function run_cmd(cmd, cwd, handlers)
@@ -292,40 +297,7 @@ local function run_cmd(cmd, cwd, handlers)
})
end
function M.run_nearest()
local bufnr = vim.api.nvim_get_current_buf()
local pos = vim.api.nvim_win_get_cursor(0)
local row = pos[1] - 1
local col = pos[2]
local runner = M.get_runner_for_buf(bufnr)
if not runner then
vim.notify("[test-samurai] No runner for this file", vim.log.levels.WARN)
return
end
if type(runner.find_nearest) ~= "function" or type(runner.build_command) ~= "function" then
vim.notify("[test-samurai] Runner missing methods", vim.log.levels.ERROR)
return
end
local ok, spec_or_err = pcall(runner.find_nearest, bufnr, row, col)
if not ok or not spec_or_err then
local msg = "[test-samurai] No test found"
if type(spec_or_err) == "string" then
msg = "[test-samurai] " .. spec_or_err
end
vim.notify(msg, vim.log.levels.WARN)
return
end
local spec = spec_or_err
local ok_cmd, command = pcall(runner.build_command, spec)
if not ok_cmd or not command or type(command.cmd) ~= "table" or #command.cmd == 0 then
vim.notify("[test-samurai] Runner failed to build command", vim.log.levels.ERROR)
return
end
local function run_command(command)
local cmd = command.cmd
local cwd = command.cwd or vim.loop.cwd()
@@ -378,6 +350,89 @@ function M.run_nearest()
})
end
function M.run_nearest()
local bufnr = vim.api.nvim_get_current_buf()
local pos = vim.api.nvim_win_get_cursor(0)
local row = pos[1] - 1
local col = pos[2]
local runner = M.get_runner_for_buf(bufnr)
if not runner then
vim.notify("[test-samurai] No runner for this file", vim.log.levels.WARN)
return
end
if type(runner.find_nearest) ~= "function" or type(runner.build_command) ~= "function" then
vim.notify("[test-samurai] Runner missing methods", vim.log.levels.ERROR)
return
end
local ok, spec_or_err = pcall(runner.find_nearest, bufnr, row, col)
if not ok or not spec_or_err then
local msg = "[test-samurai] No test found"
if type(spec_or_err) == "string" then
msg = "[test-samurai] " .. spec_or_err
end
vim.notify(msg, vim.log.levels.WARN)
return
end
local spec = spec_or_err
local ok_cmd, command = pcall(runner.build_command, spec)
if not ok_cmd or not command or type(command.cmd) ~= "table" or #command.cmd == 0 then
vim.notify("[test-samurai] Runner failed to build command", vim.log.levels.ERROR)
return
end
run_command(command)
end
function M.run_file()
local bufnr = vim.api.nvim_get_current_buf()
local runner = M.get_runner_for_buf(bufnr)
if not runner then
vim.notify("[test-samurai] No runner for this file", vim.log.levels.WARN)
return
end
if type(runner.build_file_command) ~= "function" then
vim.notify("[test-samurai] Runner does not support file-level execution", vim.log.levels.WARN)
return
end
local ok_cmd, command = pcall(runner.build_file_command, bufnr)
if not ok_cmd or not command or type(command.cmd) ~= "table" or #command.cmd == 0 then
vim.notify("[test-samurai] Runner failed to build file command", vim.log.levels.ERROR)
return
end
run_command(command)
end
function M.run_all()
local bufnr = vim.api.nvim_get_current_buf()
local runner = M.get_runner_for_buf(bufnr)
if not runner then
vim.notify("[test-samurai] No runner for this file", vim.log.levels.WARN)
return
end
if type(runner.build_all_command) ~= "function" then
vim.notify("[test-samurai] Runner does not support project-level execution", vim.log.levels.WARN)
return
end
local ok_cmd, command = pcall(runner.build_all_command, bufnr)
if not ok_cmd or not command or type(command.cmd) ~= "table" or #command.cmd == 0 then
vim.notify("[test-samurai] Runner failed to build all-tests command", vim.log.levels.ERROR)
return
end
run_command(command)
end
function M.show_output()
if not (state.last_buf and vim.api.nvim_buf_is_valid(state.last_buf)) then
vim.notify("[test-samurai] No previous output", vim.log.levels.WARN)