dotfiles bugabingas dorkfiles
owned by bugabinga
README Source History Refs Compare Notes Search fix(dbg): correct event indices, restart watcher, preserve highlights, fix fd caching and sanitize_src e92a76f3174b4892aec307109076510bb7c063f7
Oliver Jan Krylow <oliver@bugabinga.net > · 15 Apr 2026
unsigned
Browse files Compare with HEAD
- Fix jump_to_source targeting wrong event after auto-refresh prepend
(prepend_new_events used local loop index instead of actual events array index)
- Fix watcher dying after first auto-refresh (was stopped but never restarted)
- Fix highlights lost on every auto-refresh (store highlights in state, shift+merge on prepend)
- Fix ensure_fd not caching (added fs_stat check for stale fd)
- Fix sanitize_src regex (was matching literal "[string \",]\" not "[string ...]")
- Add table-driven filter matching (FILTER_SPECS, FILTER_WINBAR_SPECS)
- Add TDD tests for each bug fix
- Inline clear_filters_notify into keymap
Changed files (4) neovim/lua/std/dbg/file.lua Modified 39 39 local function ensure_fd ()
40 40 local dir
= vim.fs.dirname(DBG_FILE)
41 41 vim.fn.mkdir(dir,
'p' )
42 - local fd
= vim.uv.fs_open(DBG_FILE,
'a' ,
tonumber (
'644' ,
8 ))
43 - if not fd
or fd
== 0 then 44 - return false ,
'Failed to open debug file: ' .. DBG_FILE
42 + local stat
= vim.uv.fs_stat(DBG_FILE)
43 + if H.fd
and stat
then 47 47 vim.uv.fs_close(H.fd)
50 + local fd
= vim.uv.fs_open(DBG_FILE,
'a' ,
tonumber (
'644' ,
8 ))
51 + if not fd
or fd
== 0 then 52 + return false ,
'Failed to open debug file: ' .. DBG_FILE
neovim/lua/std/dbg/viewer.lua Modified 126 127 local WINBAR_FORMAT
= '%%#%s#%%{v:lua.require("std.dbg.viewer").get_winbar()}%%#Normal#' 129 + local FILTER_WINBAR_SPECS
= {
130 + {
key =
'all' ,
prefix =
' all:"' ,
suffix =
'"' },
131 + {
key =
'kind' ,
prefix =
' kind:' ,
suffix =
'' },
132 + {
key =
'message' ,
prefix =
' msg:"' ,
suffix =
'"' },
133 + {
key =
'filename' ,
prefix =
' file:' ,
suffix =
'' },
128 136 function M.
get_winbar ()
129 137 if not H.state
or not H.state.win_id
then return '' end 130 138 local s
= H.state
132 140 local has_filter
= false 134 - if s.filters.all
and s.filters.all
~= '' then 135 - table .
insert (parts,
' all:"' .. s.filters.all
.. '"' )
138 - if s.filters.kind
and s.filters.kind
~= '' then 139 - table .
insert (parts,
' kind:' .. s.filters.kind)
142 - if s.filters.message
and s.filters.message
~= '' then 143 - table .
insert (parts,
' msg:"' .. s.filters.message
.. '"' )
146 - if s.filters.filename
and s.filters.filename
~= '' then 147 - table .
insert (parts,
' file:' .. s.filters.filename)
142 + for _, spec
in ipairs (FILTER_WINBAR_SPECS)
do 143 + local val
= s.filters[spec.key]
144 + if val
and val
~= '' then 145 + parts[
# parts
+ 1 ]
= spec.prefix
.. val
.. spec.suffix
151 150 local filter_str
= has_filter
and table .
concat (parts,
'' )
or '' 167 166 local function sanitize_src (src)
168 167 if not src
or src
== '' then return 'unknown' end 169 - return (src:gsub(
'^@' ,
'' ):gsub(
'^%[string ",%]$' ,
'unknown' ))
168 + return (src:gsub(
'^@' ,
'' ):gsub(
'^%[string .+%]$' ,
'unknown' ))
172 171 local function format_event_for_display (event)
288 287 map.normal {
keys =
'r' ,
command = M.refresh,
buffer = buf_id,
desc =
'Refresh events from disk' }
289 288 map.normal {
keys =
'<CR>' ,
command =
function () M.jump_to_source(win_id)
end ,
buffer = buf_id,
desc =
'Jump to source location' }
290 289 map.normal {
keys =
'ff' ,
command =
function () M.filter_by_all()
end ,
buffer = buf_id,
desc =
'Filter combined (all)' }
291 - map.normal {
keys =
'fc' ,
command =
function () M.clear_filters_notify()
end ,
buffer = buf_id,
desc =
'Clear filters + notify' }
290 + map.normal {
keys =
'fc' ,
command =
function () M.clear_filters() vim.notify(
'Filters cleared' )
end ,
buffer = buf_id,
desc =
'Clear filters + notify' }
292 291 map.normal {
keys =
'fk' ,
command =
function () M.filter_by_kind()
end ,
buffer = buf_id,
desc =
'Filter by kind' }
293 292 map.normal {
keys =
'fm' ,
command =
function () M.filter_by_message()
end ,
buffer = buf_id,
desc =
'Filter by message' }
294 293 map.normal {
keys =
'fl' ,
command =
function () M.filter_by_location()
end ,
buffer = buf_id,
desc =
'Filter by location' }
296 + local FILTER_SPECS
= {
299 + extract =
function (ev)
return ev.kind
.. ' ' .. ev.message
.. ' ' .. sanitize_src(ev.filename)
end ,
303 + extract =
function (ev)
return ev.kind
end ,
307 + extract =
function (ev)
return ev.message
end ,
311 + extract =
function (ev)
return sanitize_src(ev.filename)
end ,
297 315 local function event_passes_filters (event, filters)
298 - if filters.all
and filters.all
~= '' then 299 - local combined
= event.kind
.. ' ' .. event.message
.. ' ' .. sanitize_src(event.filename)
300 - if # vim.fn.matchfuzzy({ combined }, filters.all)
== 0 then 304 - if filters.kind
and filters.kind
~= '' then 305 - if # vim.fn.matchfuzzy({ event.kind }, filters.kind)
== 0 then 309 - if filters.message
and filters.message
~= '' then 310 - if # vim.fn.matchfuzzy({ event.message }, filters.message)
== 0 then 314 - if filters.filename
and filters.filename
~= '' then 315 - local src
= sanitize_src(event.filename)
316 - if # vim.fn.matchfuzzy({ src }, filters.filename)
== 0 then 316 + for _, spec
in ipairs (FILTER_SPECS)
do 317 + local fval
= filters[spec.key]
318 + if fval
and fval
~= '' then 319 + local ftext
= spec.extract(event)
320 + if # vim.fn.matchfuzzy({ ftext }, fval)
== 0 then 323 328 local function compute_filtered_indices (events, filters)
324 329 if not filters
then return nil end 326 - local has_any_filter
= (filters.all
and filters.all
~= '' )
327 - or (filters.kind
and filters.kind
~= '' )
328 - or (filters.message
and filters.message
~= '' )
329 - or (filters.filename
and filters.filename
~= '' )
331 + local has_any_filter
= false 332 + for _, spec
in ipairs (FILTER_SPECS)
do 333 + local fval
= filters[spec.key]
334 + if fval
and fval
~= '' then 335 + has_any_filter
= true 331 340 if not has_any_filter
then return nil end 353 362 local lines, line_events, highlights
= build_lines_and_hl(display_events)
354 363 H.state.line_events
= line_events
364 + H.state.highlights
= highlights
355 365 H.state.filtered_count
= # display_events
357 367 local was_modifiable
= vim.bo[H.state.buf_id].modifiable
383 393 local new_highlights
= {}
384 394 local new_line_events
= {}
396 + local base_idx
= H.state.event_count
- # new_display_events
+ 1 386 397 for ei, event
in ipairs (new_display_events)
do 387 398 local d
= format_event_for_display(event)
393 404 local header_line_idx
= # new_lines
+ 1 394 405 new_lines[
# new_lines
+ 1 ]
= header
395 - new_line_events[header_line_idx]
= ei
406 + new_line_events[header_line_idx]
= base_idx
+ ei
- 1 396 407 new_highlights[
# new_highlights
+ 1 ]
= {
397 408 line = header_line_idx,
398 409 kind = event.kind,
407 418 local msg_line_idx
= # new_lines
+ 1 408 419 new_lines[
# new_lines
+ 1 ]
= msg_line
409 - new_line_events[msg_line_idx]
= ei
420 + new_line_events[msg_line_idx]
= base_idx
+ ei
- 1 411 422 local loc_line_idx
= # new_lines
+ 1 412 423 new_lines[
# new_lines
+ 1 ]
= loc_line
413 - new_line_events[loc_line_idx]
= ei
424 + new_line_events[loc_line_idx]
= base_idx
+ ei
- 1 414 425 new_highlights[
# new_highlights
+ 1 ]
= {
415 426 line = loc_line_idx,
437 448 H.state.line_events
= shifted_line_events
439 - local shifted_highlights
= {}
440 - for _, hl
in ipairs (H.state.line_events)
do 450 + local all_highlights
= {}
451 + for _, hl
in ipairs (H.state.highlights
or {})
do 452 + all_highlights[
# all_highlights
+ 1 ]
= {
453 + line = hl.line
+ shift,
455 + location = hl.location,
456 + icon_start = hl.icon_start,
457 + icon_end = hl.icon_end,
458 + kind_name_start = hl.kind_name_start,
459 + kind_name_end = hl.kind_name_end,
460 + time_start = hl.time_start,
461 + time_end = hl.time_end,
443 464 for _, hl
in ipairs (new_highlights)
do 444 - shifted_highlights[
# shifted_highlights
+ 1 ]
= hl
465 + all_highlights[
# all_highlights
+ 1 ]
= hl
467 + H.state.highlights
= all_highlights
447 469 vim.api.nvim_buf_clear_highlight(H.state.buf_id, H.ns_id,
0 ,
- 1 )
448 - for _, hl
in ipairs (new_highlights)
do 470 + for _, hl
in ipairs (all_highlights)
do 449 471 local lnum
= hl.line
- 1 450 472 if hl.location
then 451 473 vim.api.nvim_buf_add_highlight(H.state.buf_id, H.ns_id, NS_HL.location, lnum,
0 ,
- 1 )
490 + local function start_watcher ()
491 + local path
= file.get_path()
492 + local callback = function (err)
493 + if err
then return end 496 + local watcher
= vim.uv.new_fs_event()
497 + watcher:start(path, {}, callback)
468 501 local function on_file_change ()
469 502 if not H.state
then return end 477 510 local new_events, new_total
= file.read_new(H.state.event_count)
478 - if # new_events
== 0 and new_total
== H.state.event_count
then return end 511 + if # new_events
== 0 and new_total
== H.state.event_count
then 512 + H.state.watcher
= start_watcher()
516 + local reversed
= {}
517 + for i
= # new_events,
1 ,
- 1 do 518 + reversed[
# reversed
+ 1 ]
= new_events[i]
520 + new_events
= reversed
480 522 for _, ev
in ipairs (new_events)
do 481 523 H.state.events[
# H.state.events
+ 1 ]
= ev
483 525 H.state.event_count
= new_total
485 527 prepend_new_events(new_events)
488 - local function start_watcher ()
489 - local path
= file.get_path()
490 - local callback = function (err)
491 - if err
then return end 494 - local watcher
= vim.uv.new_fs_event()
495 - watcher:start(path, {}, callback)
529 + H.state.watcher
= start_watcher()
499 532 function M.
open ()
597 - function M.
clear_filters_notify ()
599 - vim.notify(
'Filters cleared' )
602 632 function M.
filter_by_all ()
603 633 vim.ui.input({
prompt =
'Filter (all): ' },
function (value)
631 661 do_filter(
'filename' ,
'Filter location' )
664 + M._on_file_change
= on_file_change
665 + M.
_get_state = function ()
return H.state
end neovim/tests/std/dbg/file_spec.lua Modified 107 107 assert .equals(
0 ,
# events)
110 + it(
'should reopen fd when file is externally recreated' ,
function ()
112 + vim.fn.delete(get_path())
113 + local event1
= {
kind =
'DEBUG' ,
date =
os .
time (),
message =
'e1' ,
data = {},
filename =
'/a.lua' ,
fn =
'f' ,
line =
1 }
114 + local event2
= {
kind =
'ERROR' ,
date =
os .
time (),
message =
'e2' ,
data = {},
filename =
'/b.lua' ,
fn =
'g' ,
line =
2 }
115 + write_event(event1)
116 + vim.fn.delete(get_path())
117 + write_event(event2)
118 + local events
= read_all(get_path())
119 + assert .equals(
1 ,
# events,
'new writes go to newly created file after deletion' )
120 + assert .equals(
'e2' , events[
1 ].message)
110 123 it(
'should handle events with nested data' ,
function ()
neovim/tests/std/dbg/viewer_spec.lua Modified 352 352 assert .is_true(ok)
353 353 assert .is_true(viewer.is_open())
356 + it(
'jump_to_source jumps to correct file after auto-refresh prepend' ,
function ()
357 + local path1
= vim.fn.fnamemodify(vim.fn.getcwd(),
':p' )
.. 'jump_test_1.lua' 358 + local path2
= vim.fn.fnamemodify(vim.fn.getcwd(),
':p' )
.. 'jump_test_2.lua' 359 + local path3
= vim.fn.fnamemodify(vim.fn.getcwd(),
':p' )
.. 'jump_test_3.lua' 360 + local path4
= vim.fn.fnamemodify(vim.fn.getcwd(),
':p' )
.. 'jump_test_4.lua' 361 + for _, p
in ipairs ({ path1, path2, path3, path4 })
do 362 + local f
= io .
open (p,
'w' )
363 + f:write(
'line1\nline2\nline3\nline4\nline5\n' )
366 + write_event(make_event({
filename = path1,
line =
2 ,
message =
'event1' }))
367 + write_event(make_event({
filename = path2,
line =
3 ,
message =
'event2' }))
369 + write_event(make_event({
filename = path3,
line =
4 ,
message =
'event3' }))
370 + write_event(make_event({
filename = path4,
line =
5 ,
message =
'event4' }))
371 + viewer._on_file_change()
372 + local viewer_win
= vim.api.nvim_get_current_win()
373 + vim.api.nvim_win_set_cursor(viewer_win, {
1 ,
0 })
374 + vim.api.nvim_win_call(viewer_win,
function ()
375 + viewer.jump_to_source(viewer_win)
377 + local curbuf
= vim.api.nvim_get_current_buf()
378 + local name
= vim.api.nvim_buf_get_name(curbuf)
379 + assert .is_not_nil(name:match
'jump_test_4' ,
'should jump to newest event (path4), got: ' .. tostring (name))
380 + for _, p
in ipairs ({ path1, path2, path3, path4 })
do 385 + it(
'watcher remains alive after auto-refresh' ,
function ()
386 + write_event(make_event({
message =
'e1' }))
388 + assert .is_not_nil(viewer._get_state().watcher,
'watcher should exist after open' )
389 + write_event(make_event({
message =
'e2' }))
390 + viewer._on_file_change()
391 + assert .is_not_nil(viewer._get_state().watcher,
'watcher should still exist after file change' )
394 + it(
'original event highlights survive auto-refresh' ,
function ()
395 + write_event(make_event({
kind =
'ERROR' ,
message =
'event1' }))
396 + write_event(make_event({
kind =
'DEBUG' ,
message =
'event2' }))
398 + local buf_id
= vim.api.nvim_win_get_buf(vim.api.nvim_get_current_win())
399 + local extmarks_before
= vim.api.nvim_buf_get_extmarks(buf_id,
- 1 ,
0 ,
- 1 , {
details =
true })
400 + assert .is_true(
# extmarks_before
> 0 ,
'should have highlights on initial render' )
401 + write_event(make_event({
kind =
'TRACE' ,
message =
'event3' }))
402 + viewer._on_file_change()
403 + local extmarks_after
= vim.api.nvim_buf_get_extmarks(buf_id,
- 1 ,
0 ,
- 1 , {
details =
true })
404 + assert .is_true(
# extmarks_after
>= # extmarks_before,
'should retain original highlights after prepend' )
407 + it(
'sanitizes [string "..."] source to unknown' ,
function ()
408 + local chunk_event
= {
411 + message =
'chunk test' ,
413 + filename =
'[string "chunk:123"]' ,
417 + write_event(chunk_event)
419 + local lines
= vim.api.nvim_buf_get_lines(vim.api.nvim_win_get_buf(vim.api.nvim_get_current_win()),
0 ,
- 1 ,
false )
420 + local found_unknown
= false 421 + for _, l
in ipairs (lines)
do 422 + if l:match
'unknown' then found_unknown
= true break end 424 + assert .is_true(found_unknown,
'should show "unknown" for [string "..."] source' )
427 + it(
'filtered_indices stay consistent after auto-refresh' ,
function ()
428 + write_event(make_event({
kind =
'DEBUG' ,
message =
'debug1' }))
430 + viewer.clear_filters()
431 + local state_before
= viewer._get_state()
432 + local filtered_count_before
= state_before.filtered_count
433 + write_event(make_event({
kind =
'DEBUG' ,
message =
'debug2' }))
434 + viewer._on_file_change()
435 + local state_after
= viewer._get_state()
436 + assert .equals(filtered_count_before
+ 1 , state_after.filtered_count,
'filtered_count should increment for new matching event' )