Repository: microsoft/terminal
File: src/cascadia/TerminalControl/ControlCore.cpp (C++)

Code before:
    }

    winrt::fire_and_forget ControlCore::_terminalCompletionsChanged(std::wstring_view menuJson,
                                                                    unsigned int replaceLength)
    {
        auto args = winrt::make_self<CompletionsChangedEventArgs>(winrt::hstring{ menuJson },
                                                                  replaceLength);

        co_await winrt::resume_background();

        CompletionsChanged.raise(*this, *args);
    }

    // Select the region of text between [s.start, s.end), in buffer space
    void ControlCore::_selectSpan(til::point_span s)
    {
        // s.end is an _exclusive_ point. We need an inclusive one. But
        // decrement in bounds wants an inclusive one. If you pass an exclusive
        // one, then it might assert at you for being out of bounds. So we also
        // take care of the case that the end point is outside the viewport
        // manually.
        const auto bufferSize{ _terminal->GetTextBuffer().GetSize() };
        til::point inclusiveEnd = s.end;
        if (s.end.x == bufferSize.Width())
        {
            inclusiveEnd = til::point{ std::max(0, s.end.x - 1), s.end.y };
        }
        else
        {
            bufferSize.DecrementInBounds(inclusiveEnd);
        }

        _terminal->SelectNewRegion(s.start, inclusiveEnd);
        _renderer->TriggerSelection();
    }

    void ControlCore::SelectCommand(const bool goUp)
    {
        const auto lock = _terminal->LockForWriting();

        const til::point start = _terminal->IsSelectionActive() ? (goUp ? _terminal->GetSelectionAnchor() : _terminal->GetSelectionEnd()) :
                                                                  _terminal->GetTextBuffer().GetCursor().GetPosition();

        std::optional<::MarkExtents> nearest{ std::nullopt };
        const auto& marks{ _terminal->GetMarkExtents() };

        // Early return so we don't have to check for the validity of `nearest` below after the loop exits.
        if (marks.empty())
        {
            return;
        }

Review comment (suggestion):
`DecrementInBounds` has a 2nd parameter `bool allowEndExclusive` which you can set to `true` to fix this.

Code after:
            {
                UserScrollViewport(0);
                _terminalScrollPositionChanged(0, viewHeight, bufferSize);
            }
        }
    }

    winrt::fire_and_forget ControlCore::_terminalCompletionsChanged(std::wstring_view menuJson,
                                                                    unsigned int replaceLength)
    {
        auto args = winrt::make_self<CompletionsChangedEventArgs>(winrt::hstring{ menuJson },
                                                                  replaceLength);

        co_await winrt::resume_background();

        CompletionsChanged.raise(*this, *args);
    }

    // Select the region of text between [s.start, s.end), in buffer space
    void ControlCore::_selectSpan(til::point_span s)
    {
        // s.end is an _exclusive_ point. We need an inclusive one. But
        // decrement in bounds wants an inclusive one. If you pass an exclusive
        // one, then it might assert at you for being out of bounds. So we also
        // take care of the case that the end point is outside the viewport
        // manually.
        const auto bufferSize{ _terminal->GetTextBuffer().GetSize() };
        til::point inclusiveEnd = s.end;
        if (s.end.x == bufferSize.Width())
        {
            inclusiveEnd = til::point{ std::max(0, s.end.x - 1), s.end.y };
        }
        else
        {
            bufferSize.DecrementInBounds(inclusiveEnd);
        }

        _terminal->SelectNewRegion(s.start, inclusiveEnd);
        _renderer->TriggerSelection();
    }

    void ControlCore::SelectCommand(const bool goUp)
    {
        const auto lock = _terminal->LockForWriting();

        const til::point start = _terminal->IsSelectionActive() ? (goUp ? _terminal->GetSelectionAnchor() : _terminal->GetSelectionEnd()) :
                                                                  _terminal->GetTextBuffer().GetCursor().GetPosition();

        std::optional<::MarkExtents> nearest{ std::nullopt };
        const auto& marks{ _terminal->GetMarkExtents() };