Skip to content

Fix TAB failing to re-expand collapsed tool blocks (#166)#168

Merged
dnouri merged 1 commit intomasterfrom
fix/tab-toggle-overlay-boundary
Mar 17, 2026
Merged

Fix TAB failing to re-expand collapsed tool blocks (#166)#168
dnouri merged 1 commit intomasterfrom
fix/tab-toggle-overlay-boundary

Conversation

@dnouri
Copy link
Copy Markdown
Owner

@dnouri dnouri commented Mar 17, 2026

Fixes #166.

When a user expands a collapsed tool block, reads the output, then presses TAB to collapse it again, the next TAB to re-expand silently does nothing — Emacs jumps to an unrelated heading instead.

Root cause: Collapsing places the cursor at the exact end position of the tool overlay. Emacs overlays use half-open intervals: overlays-at considers a position inside [start, end) but not at end itself. The subsequent TAB finds no tool overlay at point and falls through to outline-cycle, which navigates to the nearest markdown heading.

Trigger: Collapsing from the [-] button or any line in the lower portion of the expanded output. The clamping expression (min original-pos (cdr new-bounds)) yields exactly the overlay boundary when the cursor was deep in the expanded content.

Fix: Clamp to (1- end) rather than end, keeping the cursor on the last character inside the overlay.

Tests: Unit test for the exact boundary condition, plus a GUI test exercising the full expand → collapse → re-expand cycle with tree-sitter and font-lock active.

When a user expands a collapsed tool block, reads the output, then
presses TAB to collapse it again, the next TAB to re-expand silently
does nothing.  Instead of toggling, Emacs jumps to an unrelated
heading somewhere else in the buffer.

The problem is that collapsing places the cursor at the exact end
position of the tool overlay.  Emacs overlays use half-open intervals:
overlays-at considers a position inside [start, end) but not at end
itself.  So the subsequent TAB finds no tool overlay at point and
falls through to outline-cycle, which navigates to the nearest
markdown heading---appearing to the user as a random jump.

The trigger condition is that original-pos (where TAB was pressed in
the expanded view) exceeds the collapsed overlay's end, so the
clamping expression (min original-pos (cdr new-bounds)) yields exactly
the overlay boundary.  This happens reliably when collapsing from the
[-] button or from any line in the lower portion of the expanded
output.

Fix this by clamping to (1- end) rather than end, keeping the cursor
on the last character inside the overlay.  One character of difference,
but it preserves the invariant that point remains inside the tool block
after every toggle.
@dnouri dnouri merged commit fe2baf0 into master Mar 17, 2026
11 checks passed
@dnouri dnouri deleted the fix/tab-toggle-overlay-boundary branch March 17, 2026 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TAB no longer expands '... (N more lines)' tool blocks in Pi-Chat buffer

1 participant