add TSamFile and TSamAll command with default keymaps
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -12,6 +12,14 @@ function M.test_nearest()
|
||||
core.run_nearest()
|
||||
end
|
||||
|
||||
function M.test_file()
|
||||
core.run_file()
|
||||
end
|
||||
|
||||
function M.test_all()
|
||||
core.run_all()
|
||||
end
|
||||
|
||||
function M.show_output()
|
||||
core.show_output()
|
||||
end
|
||||
|
||||
@@ -172,4 +172,38 @@ function runner.build_command(spec)
|
||||
}
|
||||
end
|
||||
|
||||
function runner.build_file_command(bufnr)
|
||||
local path = util.get_buf_path(bufnr)
|
||||
if not path or path == "" then
|
||||
return nil
|
||||
end
|
||||
local root = util.find_root(path, { "go.mod", ".git" })
|
||||
if not root or root == "" then
|
||||
root = vim.loop.cwd()
|
||||
end
|
||||
local spec = { file = path, cwd = root }
|
||||
local pkg = build_pkg_arg(spec)
|
||||
local cmd = { "go", "test", "-v", pkg }
|
||||
return {
|
||||
cmd = cmd,
|
||||
cwd = root,
|
||||
}
|
||||
end
|
||||
|
||||
function runner.build_all_command(bufnr)
|
||||
local path = util.get_buf_path(bufnr)
|
||||
local root
|
||||
if path and path ~= "" then
|
||||
root = util.find_root(path, { "go.mod", ".git" })
|
||||
end
|
||||
if not root or root == "" then
|
||||
root = vim.loop.cwd()
|
||||
end
|
||||
local cmd = { "go", "test", "-v", "./..." }
|
||||
return {
|
||||
cmd = cmd,
|
||||
cwd = root,
|
||||
}
|
||||
end
|
||||
|
||||
return runner
|
||||
|
||||
@@ -4,4 +4,5 @@ return js.new({
|
||||
name = "js-mocha",
|
||||
framework = "mocha",
|
||||
command = { "npx", "mocha" },
|
||||
all_glob = "test/**/*.test.js",
|
||||
})
|
||||
|
||||
@@ -220,6 +220,7 @@ function M.new(opts)
|
||||
runner.name = cfg.name or "js"
|
||||
runner.framework = cfg.framework or "jest"
|
||||
runner.command = cfg.command or { "npx", runner.framework }
|
||||
runner.all_glob = cfg.all_glob
|
||||
|
||||
runner.filetypes = {}
|
||||
if cfg.filetypes then
|
||||
@@ -312,6 +313,62 @@ function M.new(opts)
|
||||
}
|
||||
end
|
||||
|
||||
function runner.build_file_command(bufnr)
|
||||
local path = util.get_buf_path(bufnr)
|
||||
if not path or path == "" then
|
||||
return nil
|
||||
end
|
||||
local root
|
||||
if runner.framework == "jest" then
|
||||
root = find_jest_root(path)
|
||||
else
|
||||
root = util.find_root(path, {
|
||||
"jest.config.js",
|
||||
"jest.config.ts",
|
||||
"vitest.config.ts",
|
||||
"vitest.config.js",
|
||||
"package.json",
|
||||
"node_modules",
|
||||
})
|
||||
end
|
||||
local cmd = vim.deepcopy(runner.command)
|
||||
table.insert(cmd, path)
|
||||
return {
|
||||
cmd = cmd,
|
||||
cwd = root,
|
||||
}
|
||||
end
|
||||
|
||||
function runner.build_all_command(bufnr)
|
||||
local path = util.get_buf_path(bufnr)
|
||||
local root
|
||||
if path and path ~= "" then
|
||||
if runner.framework == "jest" then
|
||||
root = find_jest_root(path)
|
||||
else
|
||||
root = util.find_root(path, {
|
||||
"jest.config.js",
|
||||
"jest.config.ts",
|
||||
"vitest.config.ts",
|
||||
"vitest.config.js",
|
||||
"package.json",
|
||||
"node_modules",
|
||||
})
|
||||
end
|
||||
end
|
||||
if not root or root == "" then
|
||||
root = vim.loop.cwd()
|
||||
end
|
||||
local cmd = vim.deepcopy(runner.command)
|
||||
if runner.framework == "mocha" and runner.all_glob then
|
||||
table.insert(cmd, runner.all_glob)
|
||||
end
|
||||
return {
|
||||
cmd = cmd,
|
||||
cwd = root,
|
||||
}
|
||||
end
|
||||
|
||||
return runner
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user