Skip to content

feat: Improve selection handling for buffer trims and scrolling#4

Merged
tomlm merged 1 commit into
tomlm:mainfrom
IvanJosipovic:dev
May 13, 2026
Merged

feat: Improve selection handling for buffer trims and scrolling#4
tomlm merged 1 commit into
tomlm:mainfrom
IvanJosipovic:dev

Conversation

@IvanJosipovic
Copy link
Copy Markdown
Contributor

Summary

Update selection handling in XTerm.NET to better match xterm.js.

This changes selection from a viewport-relative model to a buffer-anchored model so selection stays attached to the underlying buffer content when the viewport scrolls.

What changed

  • Store selection rows in absolute buffer coordinates inside SelectionManager
  • Translate viewport-relative row input to absolute buffer rows in:
    • StartSelection
    • UpdateSelection
    • IsCellSelected
  • Update GetSelectionText() to read directly from absolute buffer lines instead of buffer.YDisp + row
  • Update SelectAll() to cover the full buffer, including scrollback, not just the visible viewport
  • Add trim handling so selection adjusts or clears when old scrollback lines are recycled
  • Add TerminalBuffer.Trimmed so selection can respond when buffer lines are dropped

Why

The previous selection implementation was tied to viewport rows:

  • scrolling changed which buffer lines those stored rows referred to
  • selected text could appear to move with the viewport
  • selection text and rendering did not remain stably anchored to the original buffer content

xterm.js behaves differently. Its selection model is buffer-oriented and adjusts selection when scrollback is trimmed. This change moves XTerm.NET closer to that behavior.

Tests

Added focused tests covering:

  • selected text remains anchored when the viewport scrolls
  • IsCellSelected follows the selected buffer line across viewport movement
  • SelectAll includes scrollback, not just the visible rows
  • selection clears correctly when trim removes the selected lines

Validation:

  • dotnet test src/XTerm.NET.Tests/XTerm.NET.Tests.csproj
  • passed 584/584

- Add Trimmed event to TerminalBuffer for line trimming notifications
- Update SelectionManager to track and adjust selection on trims
- Refactor selection logic to use absolute buffer coordinates
- Ensure SelectAll covers full buffer, not just viewport
- Enhance GetSelectionText and IsCellSelected for accuracy
- Add tests for selection anchoring, scrolling, and trim behavior
@tomlm tomlm merged commit b33bb48 into tomlm:main May 13, 2026
1 check passed
@tomlm
Copy link
Copy Markdown
Owner

tomlm commented May 13, 2026

Thanks!

@tomlm
Copy link
Copy Markdown
Owner

tomlm commented May 13, 2026

I pushed a new package

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.

2 participants