Fix TAB failing to re-expand collapsed tool blocks (#166)#168
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-atconsiders a position inside[start, end)but not atenditself. The subsequent TAB finds no tool overlay at point and falls through tooutline-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 thanend, 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.