[PR #19259] [Conhost] Fix off-by-1 error when copying and coloring selections #31769

Open
opened 2026-01-31 09:49:25 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/microsoft/terminal/pull/19259

State: closed
Merged: Yes


Summary of the Pull Request

Fixes a bug where copying and coloring selected text would be off by one. This was introduced in #18106 when selection was updated to be stored as an exclusive range. Selection::_RegenerateSelectionSpans() was updated then, but copying text and coloring selection didn't rely on selection spans.

Copying text relies on GetSelectionAnchors(). This function has now been updated to increment the bottom-right point of the selection. This way, GetTextSpans() operates on the expected exclusive range.

Coloring selection relies on TextBuffer::SearchText(), TextBuffer::GetTextRects and GetSelectionSpans(). Both Selection::ColorSelection() were updated to use rect over inclusive_rect to emphasize that they are exclusive ranges. Converting between the two improves clarity and fixes the bug.

References and Relevant Issues

Introduced in #18106

Validation Steps Performed

Copying text works in the following scenarios:
single line, left-to-right and right-to-left
multi-line, diagonal directions
block selection

Coloring text works in the following scenarios:
ctrl+# --> color instance
ctrl+shift+# --> color all instances

Closes #19053

**Original Pull Request:** https://github.com/microsoft/terminal/pull/19259 **State:** closed **Merged:** Yes --- ## Summary of the Pull Request Fixes a bug where copying and coloring selected text would be off by one. This was introduced in #18106 when selection was updated to be stored as an exclusive range. `Selection::_RegenerateSelectionSpans()` was updated then, but copying text and coloring selection didn't rely on selection spans. Copying text relies on `GetSelectionAnchors()`. This function has now been updated to increment the bottom-right point of the selection. This way, `GetTextSpans()` operates on the expected _exclusive_ range. Coloring selection relies on `TextBuffer::SearchText()`, `TextBuffer::GetTextRects` and `GetSelectionSpans()`. Both `Selection::ColorSelection()` were updated to use `rect` over `inclusive_rect` to emphasize that they are exclusive ranges. Converting between the two improves clarity and fixes the bug. ## References and Relevant Issues Introduced in #18106 ## Validation Steps Performed Copying text works in the following scenarios: ✅ single line, left-to-right and right-to-left ✅ multi-line, diagonal directions ✅ block selection Coloring text works in the following scenarios: ✅ctrl+# --> color instance ✅ctrl+shift+# --> color all instances Closes #19053
claunia added the pull-request label 2026-01-31 09:49:25 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#31769