add keymaps for quick filtering the test-listings
All checks were successful
tests / test (push) Successful in 10s

This commit is contained in:
2026-01-07 17:58:53 +01:00
parent c538a32307
commit 5d0b4e9dd6
4 changed files with 325 additions and 4 deletions

View File

@@ -23,6 +23,8 @@ local state = {
detail_win = nil,
detail_opening = false,
detail_full = false,
listing_unfiltered_lines = nil,
listing_filtered_kind = nil,
hardtime_refcount = 0,
hardtime_was_enabled = false,
autocmds_set = false,
@@ -36,6 +38,8 @@ local close_container
local restore_listing_full
local close_detail_float
local jump_to_first_quickfix
local apply_summary_highlights
local apply_result_highlights
local function disable_container_maps(buf)
local opts = { buffer = buf, nowait = true, silent = true }
@@ -59,6 +63,9 @@ local function help_lines()
" <leader>qn Close floats + jump to first quickfix entry",
" <leader>nf Next [ FAIL ] in listing",
" <leader>pf Previous [ FAIL ] in listing",
" <leader>sf Filter listing to [ FAIL ] only",
" <leader>ss Filter listing to [ SKIP ] only",
" <leader>sa Show all listing entries (clear filter)",
"",
"Testing-Float (Listing):",
" <cr> Open Detail-Float for selected test",
@@ -80,6 +87,106 @@ local function help_lines()
}
end
local function split_listing_sections(lines)
local summary_start = nil
for i, line in ipairs(lines or {}) do
if line:match("^TOTAL%s+%d+") then
summary_start = i - 1
if summary_start < 2 then
summary_start = 2
end
break
end
end
local header = {}
local body = {}
local summary = {}
if lines and #lines > 0 then
header = { lines[1] }
end
for i, line in ipairs(lines or {}) do
if i == 1 then
elseif summary_start and i >= summary_start then
table.insert(summary, line)
else
table.insert(body, line)
end
end
return header, body, summary
end
local function rebuild_result_line_map(lines)
state.last_result_line_map = {}
for idx, line in ipairs(lines or {}) do
local status = line:match("^%[%s*(%u+)%s*%]%s*%-")
if status == "PASS" or status == "FAIL" or status == "SKIP" then
local name = line:match("^%[%s*[%u]+%s*%]%s*%-%s*(.+)$")
if name and name ~= "" then
state.last_result_line_map[idx] = name
end
end
end
end
local function apply_listing_lines(buf, lines)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.api.nvim_buf_clear_namespace(buf, result_ns, 0, -1)
vim.api.nvim_buf_clear_namespace(buf, summary_ns, 0, -1)
apply_result_highlights(buf, 0, lines)
apply_summary_highlights(buf, 0, lines)
rebuild_result_line_map(lines)
end
local function apply_listing_filter(kind)
if not (state.last_buf and vim.api.nvim_buf_is_valid(state.last_buf)) then
return
end
if kind == "all" then
if not state.listing_filtered_kind or not state.listing_unfiltered_lines then
return
end
apply_listing_lines(state.last_buf, state.listing_unfiltered_lines)
state.listing_filtered_kind = nil
return
end
if state.listing_filtered_kind == kind then
return
end
local base = state.listing_unfiltered_lines
if not base then
base = vim.api.nvim_buf_get_lines(state.last_buf, 0, -1, false)
state.listing_unfiltered_lines = vim.deepcopy(base)
end
local header, body, summary = split_listing_sections(base)
local filtered = {}
for _, line in ipairs(body) do
if kind == "fail" and line:match("^%[ FAIL %] %-") then
table.insert(filtered, line)
elseif kind == "skip" and line:match("^%[ SKIP %] %-") then
table.insert(filtered, line)
end
end
if #filtered == 0 then
return
end
local combined = {}
if #header > 0 then
table.insert(combined, header[1])
table.insert(combined, "")
end
for _, line in ipairs(filtered) do
table.insert(combined, line)
end
for _, line in ipairs(summary) do
table.insert(combined, line)
end
apply_listing_lines(state.last_buf, combined)
state.listing_filtered_kind = kind
end
local function get_hardtime()
local ok, hardtime = pcall(require, "hardtime")
if not ok or type(hardtime) ~= "table" then
@@ -663,6 +770,15 @@ local function create_output_win(initial_lines)
vim.keymap.set("n", "<leader>qn", function()
jump_to_first_quickfix()
end, { buffer = buf, nowait = true, silent = true })
vim.keymap.set("n", "<leader>sf", function()
M.filter_listing_failures()
end, { buffer = buf, nowait = true, silent = true })
vim.keymap.set("n", "<leader>ss", function()
M.filter_listing_skips()
end, { buffer = buf, nowait = true, silent = true })
vim.keymap.set("n", "<leader>sa", function()
M.filter_listing_all()
end, { buffer = buf, nowait = true, silent = true })
vim.keymap.set("n", "?", function()
M.show_help()
end, { buffer = buf, nowait = true, silent = true })
@@ -730,6 +846,15 @@ local function reopen_output_win()
vim.keymap.set("n", "<leader>qn", function()
jump_to_first_quickfix()
end, { buffer = state.last_buf, nowait = true, silent = true })
vim.keymap.set("n", "<leader>sf", function()
M.filter_listing_failures()
end, { buffer = state.last_buf, nowait = true, silent = true })
vim.keymap.set("n", "<leader>ss", function()
M.filter_listing_skips()
end, { buffer = state.last_buf, nowait = true, silent = true })
vim.keymap.set("n", "<leader>sa", function()
M.filter_listing_all()
end, { buffer = state.last_buf, nowait = true, silent = true })
vim.keymap.set("n", "?", function()
M.show_help()
end, { buffer = state.last_buf, nowait = true, silent = true })
@@ -1187,6 +1312,18 @@ function M.show_help()
open_detail_split(help_lines(), "default")
end
function M.filter_listing_failures()
apply_listing_filter("fail")
end
function M.filter_listing_skips()
apply_listing_filter("skip")
end
function M.filter_listing_all()
apply_listing_filter("all")
end
function M.focus_listing()
if state.last_win and vim.api.nvim_win_is_valid(state.last_win) then
if state.detail_win and vim.api.nvim_win_is_valid(state.detail_win) then
@@ -1579,7 +1716,7 @@ local function highlight_label_word(buf, ns, lnum, line, label, hl_group)
vim.api.nvim_buf_add_highlight(buf, ns, hl_group, lnum, label_start - 1, label_start - 1 + #label)
end
local function apply_summary_highlights(buf, start_line, lines)
apply_summary_highlights = function(buf, start_line, lines)
if not (buf and vim.api.nvim_buf_is_valid(buf)) then
return
end
@@ -1601,7 +1738,7 @@ local function apply_summary_highlights(buf, start_line, lines)
end
end
local function apply_result_highlights(buf, start_line, lines)
apply_result_highlights = function(buf, start_line, lines)
if not (buf and vim.api.nvim_buf_is_valid(buf)) then
return
end
@@ -1644,6 +1781,8 @@ local function run_command(command, opts)
state.last_test_outputs = {}
state.last_result_line_map = {}
state.last_raw_output = nil
state.listing_unfiltered_lines = nil
state.listing_filtered_kind = nil
local failures = {}
local failures_seen = {}
if command and type(command.cmd) == "table" and #command.cmd > 0 then
@@ -1858,7 +1997,7 @@ local function run_command(command, opts)
append_lines(buf, summary_lines)
apply_summary_highlights(buf, start_line, summary_lines)
end
append_lines(buf, { "", "[exit code] " .. tostring(code) })
append_lines(buf, { "" })
else
if not has_output then
local cur = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
@@ -1877,7 +2016,7 @@ local function run_command(command, opts)
append_lines(buf, summary_lines)
apply_summary_highlights(buf, start_line, summary_lines)
end
append_lines(buf, { "", "[exit code] " .. tostring(code) })
append_lines(buf, { "" })
end
if options.track_scope then
state.last_scope_exit_code = code