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 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 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
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
2 changes: 2 additions & 0 deletions .github/actions/spelling/expect/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ CBN
cbt
Ccc
CCCBB
CCCDDD
cch
CCHAR
CCmd
Expand Down Expand Up @@ -365,6 +366,7 @@ dcommon
dcompiler
DComposition
dde
DDDCCC
DDESHARE
DDevice
DEADCHAR
Expand Down
28 changes: 18 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 @@ -1114,6 +1119,9 @@ std::wstring_view ROW::GetText() const noexcept
return { _chars.data(), width };
}

// Arguments:
// - columnBegin: inclusive
// - columnEnd: exclusive
std::wstring_view ROW::GetText(til::CoordType columnBegin, til::CoordType columnEnd) const noexcept
{
const auto columns = GetReadableColumnCount();
Expand Down Expand Up @@ -1219,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
9 changes: 3 additions & 6 deletions src/buffer/out/UTextAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,16 +411,13 @@ Microsoft::Console::ICU::unique_uregex Microsoft::Console::ICU::CreateRegex(cons
return unique_uregex{ re };
}

// Returns an inclusive point range given a text start and end position.
// Returns a half-open [beg,end) range given a text start and end position.
// This function is designed to be used with uregex_start64/uregex_end64.
til::point_span Microsoft::Console::ICU::BufferRangeFromMatch(UText* ut, URegularExpression* re)
{
UErrorCode status = U_ZERO_ERROR;
const auto nativeIndexBeg = uregex_start64(re, 0, &status);
auto nativeIndexEnd = uregex_end64(re, 0, &status);

// The parameters are given as a half-open [beg,end) range, but the point_span we return in closed [beg,end].
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
nativeIndexEnd--;
const auto nativeIndexEnd = uregex_end64(re, 0, &status);

const auto& textBuffer = *static_cast<const TextBuffer*>(ut->context);
til::point_span ret;
Expand All @@ -439,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
Loading
Loading