Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make selection an exclusive range #18106

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
aa95790
[AtlasEngine] Render selection as exclusive range
carlos-zamora Oct 15, 2024
4697f49
Fix any side-effects of making GetText exclusive
carlos-zamora Oct 15, 2024
4364643
Update selection markers appropriately
carlos-zamora Oct 15, 2024
014f338
[Mark Mode] Move by character (includes wide-glyph handling)
carlos-zamora Oct 16, 2024
ba673a4
[Mark Mode] Move by viewport
carlos-zamora Oct 16, 2024
cd2da0e
[Mark Mode] Move by buffer
carlos-zamora Oct 16, 2024
ff45eb9
[Mark Mode] Move by word
carlos-zamora Oct 17, 2024
659cd02
[Mark Mode] Handle move up/down onto middle of emoji
carlos-zamora Oct 17, 2024
e7ca807
Bugfix: renderer highlights lines when at x-boundary
carlos-zamora Oct 18, 2024
7ff4838
Bugfix: move to next word would move past mutable bottom
carlos-zamora Oct 18, 2024
928e9a9
Fix some more rendering issues
carlos-zamora Oct 21, 2024
9d361d2
Fix more rendering issues w/ Leonard
carlos-zamora Oct 21, 2024
845e43d
Fix highlighting in AtlasEngine
lhecker Oct 22, 2024
8b996a9
fix session restore
carlos-zamora Oct 22, 2024
df89ad0
Ensure mouse selection scenarios work right
carlos-zamora Oct 23, 2024
9e9db42
Fix UIA
carlos-zamora Oct 23, 2024
eea8399
Fix hyperlinks
carlos-zamora Oct 23, 2024
7bfd647
fix tests
carlos-zamora Oct 24, 2024
52f3bc0
fix other tests
carlos-zamora Nov 5, 2024
408d149
spell
carlos-zamora Nov 5, 2024
57b82cb
[UIA] allow end exclusive
carlos-zamora Nov 6, 2024
e98033e
audit
carlos-zamora Nov 6, 2024
e621c1c
fix some more tests
carlos-zamora Nov 7, 2024
83d2bff
apply feedback
carlos-zamora Nov 8, 2024
6aed5f9
Fix shift+tab to select hyperlink
carlos-zamora Nov 15, 2024
7a8d448
Fix ConHost
carlos-zamora Nov 15, 2024
6b99816
Fix 'off by 1' hyperlink detection
carlos-zamora Nov 15, 2024
d179e9c
bugfix: don't select trailing whitespace with mouse word selection
carlos-zamora Nov 26, 2024
d2ed09e
bugfix: click+drag on cell should select the cell we're on
carlos-zamora Nov 26, 2024
37ad74f
bugfix: double-click + drag would prematurely expand
carlos-zamora Dec 3, 2024
dbfdf81
fix tests; fix conhost block selection
carlos-zamora Dec 3, 2024
bee9371
fix terminal core selection tests
carlos-zamora Dec 3, 2024
44a1e42
fix tests and add more comments to preserve sanity
carlos-zamora Dec 14, 2024
4764653
simplify and add comment
carlos-zamora Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/buffer/out/Row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,20 @@ constexpr OutIt copy_n_small(InIt first, Diff count, OutIt dest)
return dest;
}

CharToColumnMapper::CharToColumnMapper(const wchar_t* chars, const uint16_t* charOffsets, ptrdiff_t lastCharOffset, til::CoordType currentColumn) noexcept :
CharToColumnMapper::CharToColumnMapper(const wchar_t* chars, const uint16_t* charOffsets, ptrdiff_t charsLength, til::CoordType currentColumn, til::CoordType columnCount) noexcept :
_chars{ chars },
_charOffsets{ charOffsets },
_lastCharOffset{ lastCharOffset },
_currentColumn{ currentColumn }
_charsLength{ charsLength },
_currentColumn{ currentColumn },
_columnCount{ columnCount }
{
}

// If given a position (`offset`) inside the ROW's text, this function will return the corresponding column.
// This function in particular returns the glyph's first column.
til::CoordType CharToColumnMapper::GetLeadingColumnAt(ptrdiff_t targetOffset) noexcept
{
targetOffset = clamp(targetOffset, 0, _lastCharOffset);
targetOffset = clamp(targetOffset, 0, _charsLength);
Comment on lines +83 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's all this?

Copy link
Member

@lhecker lhecker Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are necessary so that both accessor functions work reliably with an exclusive target char offset. I suggested to rename the member to make its usage a bit easier to understand, now that it refers to the length and not length-1 anymore. I did this in collaboration with Carlos. 🙂


// This code needs to fulfill two conditions on top of the obvious (a forward/backward search):
// A: We never want to stop on a column that is marked with CharOffsetsTrailer (= "GetLeadingColumn").
Expand Down Expand Up @@ -130,10 +131,14 @@ til::CoordType CharToColumnMapper::GetLeadingColumnAt(ptrdiff_t targetOffset) no
til::CoordType CharToColumnMapper::GetTrailingColumnAt(ptrdiff_t offset) noexcept
{
auto col = GetLeadingColumnAt(offset);
// This loop is a little redundant with the forward search loop in GetLeadingColumnAt()
// but it's realistically not worth caring about this. This code is not a bottleneck.
for (; WI_IsFlagSet(_charOffsets[col + 1], CharOffsetsTrailer); ++col)

if (col < _columnCount)
{
// This loop is a little redundant with the forward search loop in GetLeadingColumnAt()
// but it's realistically not worth caring about this. This code is not a bottleneck.
for (; WI_IsFlagSet(_charOffsets[col + 1], CharOffsetsTrailer); ++col)
{
}
}
return col;
}
Expand Down Expand Up @@ -1222,15 +1227,15 @@ T ROW::_adjustForward(T column) const noexcept
}

// Creates a CharToColumnMapper given an offset into _chars.data().
// In other words, for a 120 column ROW with just ASCII text, the offset should be [0,120).
// In other words, for a 120 column ROW with just ASCII text, the offset should be [0,120].
CharToColumnMapper ROW::_createCharToColumnMapper(ptrdiff_t offset) const noexcept
{
const auto charsSize = _charSize();
const auto lastChar = gsl::narrow_cast<ptrdiff_t>(charsSize - 1);
const auto lastChar = gsl::narrow_cast<ptrdiff_t>(charsSize);
// We can sort of guess what column belongs to what offset because BMP glyphs are very common and
// UTF-16 stores them in 1 char. In other words, usually a ROW will have N chars for N columns.
const auto guessedColumn = gsl::narrow_cast<til::CoordType>(clamp(offset, 0, _columnCount));
return CharToColumnMapper{ _chars.data(), _charOffsets.data(), lastChar, guessedColumn };
return CharToColumnMapper{ _chars.data(), _charOffsets.data(), lastChar, guessedColumn, _columnCount };
}

const std::optional<ScrollbarData>& ROW::GetScrollbarData() const noexcept
Expand Down
5 changes: 3 additions & 2 deletions src/buffer/out/Row.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct RowCopyTextFromState
// into a ROW's text this class can tell you what cell that pointer belongs to.
struct CharToColumnMapper
{
CharToColumnMapper(const wchar_t* chars, const uint16_t* charOffsets, ptrdiff_t lastCharOffset, til::CoordType currentColumn) noexcept;
CharToColumnMapper(const wchar_t* chars, const uint16_t* charOffsets, ptrdiff_t lastCharOffset, til::CoordType currentColumn, til::CoordType columnCount) noexcept;

til::CoordType GetLeadingColumnAt(ptrdiff_t targetOffset) noexcept;
til::CoordType GetTrailingColumnAt(ptrdiff_t offset) noexcept;
Expand All @@ -85,8 +85,9 @@ struct CharToColumnMapper

const wchar_t* _chars;
const uint16_t* _charOffsets;
ptrdiff_t _lastCharOffset;
ptrdiff_t _charsLength;
til::CoordType _currentColumn;
til::CoordType _columnCount;
};

class ROW final
Expand Down
2 changes: 1 addition & 1 deletion src/buffer/out/UTextAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ til::point_span Microsoft::Console::ICU::BufferRangeFromMatch(UText* ut, URegula
if (utextAccess(ut, nativeIndexEnd, true))
{
const auto y = accessCurrentRow(ut);
ret.end.x = textBuffer.GetRowByOffset(y).GetTrailingColumnAtCharOffset(ut->chunkOffset);
ret.end.x = textBuffer.GetRowByOffset(y).GetLeadingColumnAtCharOffset(ut->chunkOffset);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@lhecker lhecker Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The offsets are exclusive now. This means the chunkOffset now refers to the first character after the matched range. In turn, this means that if there's a wide glyph after the matched range, we want its leading half, because the exclusive end should be exactly 1 past the end (and not 2).

ret.end.y = y;
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/buffer/out/textBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@ const std::vector<til::inclusive_rect> TextBuffer::GetTextRects(til::point start
// - Else if a blockSelection, returns spans corresponding to each line in the block selection
// Arguments:
// - start: beginning of the text region of interest (inclusive)
// - end: the other end of the text region of interest (inclusive)
// - end: the other end of the text region of interest (exclusive)
// - blockSelection: when enabled, get spans for each line covered by the block
// - bufferCoordinates: when enabled, treat the coordinates as relative to
// the buffer rather than the screen.
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalCore/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ std::wstring Terminal::GetHyperlinkAtBufferPosition(const til::point bufferPos)
if (result.has_value())
{
result->start = _ConvertToBufferCell(result->start, false);
result->stop = _ConvertToBufferCell(result->stop, false);
result->stop = _ConvertToBufferCell(result->stop, true);
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ class Microsoft::Terminal::Core::Terminal final :
std::vector<til::point_span> _GetSelectionSpans() const noexcept;
std::pair<til::point, til::point> _PivotSelection(const til::point targetPos, bool& targetStart) const noexcept;
std::pair<til::point, til::point> _ExpandSelectionAnchors(std::pair<til::point, til::point> anchors) const;
til::point _ConvertToBufferCell(const til::point viewportPos, bool allowRightExclusive = false) const;
til::point _ConvertToBufferCell(const til::point viewportPos, bool allowRightExclusive) const;
void _ScrollToPoint(const til::point pos);
void _MoveByChar(SelectionDirection direction, til::point& pos);
void _MoveByWord(SelectionDirection direction, til::point& pos);
Expand Down
1 change: 0 additions & 1 deletion src/cascadia/TerminalCore/TerminalSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ void Terminal::SelectHyperlink(const SearchDirection dir)
selection->start = result->first;
selection->pivot = result->first;
selection->end = result->second;
bufferSize.DecrementInBounds(selection->end);
_selectionIsTargetingUrl = true;
_selectionEndpoint = SelectionEndpoint::End;
}
Expand Down
15 changes: 11 additions & 4 deletions src/host/selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ void Selection::_RegenerateSelectionSpans() const
endSelectionAnchor.x = (_d->coordSelectionAnchor.x == _d->srSelectionRect.left) ? _d->srSelectionRect.right : _d->srSelectionRect.left;
endSelectionAnchor.y = (_d->coordSelectionAnchor.y == _d->srSelectionRect.top) ? _d->srSelectionRect.bottom : _d->srSelectionRect.top;

// GH #18106: Conhost uses an inclusive range for selection. GetTextSpans() needs an exclusive range.
// Increment the "end" point (bottom-right-most) to make it exclusive.
// NOTE: if start is past end, increment start instead
const auto& buffer = screenInfo.GetTextBuffer();
auto startSelectionAnchor = _d->coordSelectionAnchor;
buffer.GetSize().IncrementInExclusiveBounds(startSelectionAnchor <= endSelectionAnchor ? endSelectionAnchor : startSelectionAnchor);

const auto blockSelection = !IsLineSelection();
_lastSelectionSpans = screenInfo.GetTextBuffer().GetTextSpans(_d->coordSelectionAnchor,
endSelectionAnchor,
blockSelection,
false);
_lastSelectionSpans = buffer.GetTextSpans(startSelectionAnchor,
endSelectionAnchor,
blockSelection,
false);
_lastSelectionGeneration = _d.generation();
}

Expand Down
Loading