Skip to content

Commit d9131c6

Browse files
authored
Stop scrolling on output when search is open (#17885)
* Don't reset the position entirely when changing the needle * Don't change the scroll position when output arrives * Don't interfere with the search when output arrives constantly Closes #17301 ## Validation Steps Performed * In pwsh, run `10000..20000 | % { sleep 0.25; $_ }` * You can search for e.g. `1004` and it'll find 10 results. ✅ * You can scroll up and down past it and it won't snap back when new output arrives. ✅ * `while ($true) { Write-Host -NoNewline "`e[Ha"; sleep 0.0001; }` * You can cycle between the hits effortlessly. ✅ (This tests that the constantly reset `OutputIdle` event won't interfere.) * On input change, the focused result is near the previous one. ✅
1 parent 0ce654e commit d9131c6

File tree

9 files changed

+32
-48
lines changed

9 files changed

+32
-48
lines changed

src/buffer/out/search.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ bool Search::IsStale(const Microsoft::Console::Render::IRenderData& renderData,
1616
_lastMutationId != renderData.GetTextBuffer().GetLastMutationId();
1717
}
1818

19-
bool Search::Reset(Microsoft::Console::Render::IRenderData& renderData, const std::wstring_view& needle, SearchFlag flags, bool reverse)
19+
void Search::Reset(Microsoft::Console::Render::IRenderData& renderData, const std::wstring_view& needle, SearchFlag flags, bool reverse)
2020
{
2121
const auto& textBuffer = renderData.GetTextBuffer();
2222

@@ -30,15 +30,15 @@ bool Search::Reset(Microsoft::Console::Render::IRenderData& renderData, const st
3030
_results = std::move(result).value_or(std::vector<til::point_span>{});
3131
_index = reverse ? gsl::narrow_cast<ptrdiff_t>(_results.size()) - 1 : 0;
3232
_step = reverse ? -1 : 1;
33-
return true;
34-
}
3533

36-
void Search::MoveToCurrentSelection()
37-
{
3834
if (_renderData->IsSelectionActive())
3935
{
4036
MoveToPoint(_renderData->GetTextBuffer().ScreenToBufferPosition(_renderData->GetSelectionAnchor()));
4137
}
38+
else if (const auto span = _renderData->GetSearchHighlightFocused())
39+
{
40+
MoveToPoint(_step > 0 ? span->start : span->end);
41+
}
4242
}
4343

4444
void Search::MoveToPoint(const til::point anchor) noexcept

src/buffer/out/search.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ class Search final
3636
Search() = default;
3737

3838
bool IsStale(const Microsoft::Console::Render::IRenderData& renderData, const std::wstring_view& needle, SearchFlag flags) const noexcept;
39-
bool Reset(Microsoft::Console::Render::IRenderData& renderData, const std::wstring_view& needle, SearchFlag flags, bool reverse);
39+
void Reset(Microsoft::Console::Render::IRenderData& renderData, const std::wstring_view& needle, SearchFlag flags, bool reverse);
4040

41-
void MoveToCurrentSelection();
4241
void MoveToPoint(til::point anchor) noexcept;
4342
void MovePastPoint(til::point anchor) noexcept;
4443
void FindNext(bool reverse) noexcept;

src/cascadia/TerminalControl/ControlCore.cpp

+16-29
Original file line numberDiff line numberDiff line change
@@ -1697,38 +1697,41 @@ namespace winrt::Microsoft::Terminal::Control::implementation
16971697
SearchResults ControlCore::Search(SearchRequest request)
16981698
{
16991699
const auto lock = _terminal->LockForWriting();
1700+
17001701
SearchFlag flags{};
17011702
WI_SetFlagIf(flags, SearchFlag::CaseInsensitive, !request.CaseSensitive);
17021703
WI_SetFlagIf(flags, SearchFlag::RegularExpression, request.RegularExpression);
17031704
const auto searchInvalidated = _searcher.IsStale(*_terminal.get(), request.Text, flags);
17041705

1705-
if (searchInvalidated || !request.Reset)
1706+
if (searchInvalidated || !request.ResetOnly)
17061707
{
17071708
std::vector<til::point_span> oldResults;
1709+
til::point_span oldFocused;
1710+
1711+
if (const auto focused = _terminal->GetSearchHighlightFocused())
1712+
{
1713+
oldFocused = *focused;
1714+
}
17081715

17091716
if (searchInvalidated)
17101717
{
17111718
oldResults = _searcher.ExtractResults();
17121719
_searcher.Reset(*_terminal.get(), request.Text, flags, !request.GoForward);
1713-
1714-
if (SnapSearchResultToSelection())
1715-
{
1716-
_searcher.MoveToCurrentSelection();
1717-
SnapSearchResultToSelection(false);
1718-
}
1719-
17201720
_terminal->SetSearchHighlights(_searcher.Results());
17211721
}
1722-
else
1722+
1723+
if (!request.ResetOnly)
17231724
{
17241725
_searcher.FindNext(!request.GoForward);
17251726
}
17261727

1727-
if (const auto idx = _searcher.CurrentMatch(); idx >= 0)
1728+
_terminal->SetSearchHighlightFocused(gsl::narrow<size_t>(std::max<ptrdiff_t>(0, _searcher.CurrentMatch())));
1729+
_renderer->TriggerSearchHighlight(oldResults);
1730+
1731+
if (const auto focused = _terminal->GetSearchHighlightFocused(); focused && *focused != oldFocused)
17281732
{
1729-
_terminal->SetSearchHighlightFocused(gsl::narrow<size_t>(idx), request.ScrollOffset);
1733+
_terminal->ScrollToSearchHighlight(request.ScrollOffset);
17301734
}
1731-
_renderer->TriggerSearchHighlight(oldResults);
17321735
}
17331736

17341737
int32_t totalMatches = 0;
@@ -1756,27 +1759,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
17561759
{
17571760
const auto lock = _terminal->LockForWriting();
17581761
_terminal->SetSearchHighlights({});
1759-
_terminal->SetSearchHighlightFocused({}, 0);
1762+
_terminal->SetSearchHighlightFocused(0);
17601763
_renderer->TriggerSearchHighlight(_searcher.Results());
17611764
_searcher = {};
17621765
}
17631766

1764-
// Method Description:
1765-
// - Tells ControlCore to snap the current search result index to currently
1766-
// selected text if the search was started using it.
1767-
void ControlCore::SnapSearchResultToSelection(bool shouldSnap) noexcept
1768-
{
1769-
_snapSearchResultToSelection = shouldSnap;
1770-
}
1771-
1772-
// Method Description:
1773-
// - Returns true, if we should snap the current search result index to
1774-
// the currently selected text after a new search is started, else false.
1775-
bool ControlCore::SnapSearchResultToSelection() const noexcept
1776-
{
1777-
return _snapSearchResultToSelection;
1778-
}
1779-
17801767
void ControlCore::Close()
17811768
{
17821769
if (!_IsClosing())

src/cascadia/TerminalControl/ControlCore.h

-2
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
228228
SearchResults Search(SearchRequest request);
229229
const std::vector<til::point_span>& SearchResultRows() const noexcept;
230230
void ClearSearch();
231-
void SnapSearchResultToSelection(bool snap) noexcept;
232-
bool SnapSearchResultToSelection() const noexcept;
233231

234232
void LeftClickOnTerminal(const til::point terminalPosition,
235233
const int numberOfClicks,

src/cascadia/TerminalControl/ControlCore.idl

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace Microsoft.Terminal.Control
5555
Boolean GoForward;
5656
Boolean CaseSensitive;
5757
Boolean RegularExpression;
58-
Boolean Reset;
58+
Boolean ResetOnly;
5959
Int32 ScrollOffset;
6060
};
6161

@@ -148,7 +148,6 @@ namespace Microsoft.Terminal.Control
148148

149149
SearchResults Search(SearchRequest request);
150150
void ClearSearch();
151-
Boolean SnapSearchResultToSelection;
152151

153152
Microsoft.Terminal.Core.Color ForegroundColor { get; };
154153
Microsoft.Terminal.Core.Color BackgroundColor { get; };

src/cascadia/TerminalControl/TermControl.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
576576
// but since code paths differ, extra work is required to ensure correctness.
577577
if (!_core.HasMultiLineSelection())
578578
{
579-
_core.SnapSearchResultToSelection(true);
580579
const auto selectedLine{ _core.SelectedText(true) };
581580
_searchBox->PopulateTextbox(selectedLine);
582581
}
@@ -3861,7 +3860,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
38613860
const auto goForward = _searchBox->GoForward();
38623861
const auto caseSensitive = _searchBox->CaseSensitive();
38633862
const auto regularExpression = _searchBox->RegularExpression();
3864-
const auto request = SearchRequest{ text, goForward, caseSensitive, regularExpression, true, _calculateSearchScrollOffset() };
3863+
const auto request = SearchRequest{ text, goForward, caseSensitive, regularExpression, true, _searchScrollOffset };
38653864
_handleSearchResults(_core.Search(request));
38663865
}
38673866

src/cascadia/TerminalCore/Terminal.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -1262,15 +1262,17 @@ void Terminal::SetSearchHighlights(const std::vector<til::point_span>& highlight
12621262
// Method Description:
12631263
// - Stores the focused search highlighted region in the terminal
12641264
// - If the region isn't empty, it will be brought into view
1265-
void Terminal::SetSearchHighlightFocused(const size_t focusedIdx, til::CoordType searchScrollOffset)
1265+
void Terminal::SetSearchHighlightFocused(const size_t focusedIdx) noexcept
12661266
{
12671267
_assertLocked();
12681268
_searchHighlightFocused = focusedIdx;
1269+
}
12691270

1270-
// bring the focused region into the view if the index is in valid range
1271-
if (focusedIdx < _searchHighlights.size())
1271+
void Terminal::ScrollToSearchHighlight(til::CoordType searchScrollOffset)
1272+
{
1273+
if (_searchHighlightFocused < _searchHighlights.size())
12721274
{
1273-
const auto focused = til::at(_searchHighlights, focusedIdx);
1275+
const auto focused = til::at(_searchHighlights, _searchHighlightFocused);
12741276
const auto adjustedStart = til::point{ focused.start.x, std::max(0, focused.start.y - searchScrollOffset) };
12751277
const auto adjustedEnd = til::point{ focused.end.x, std::max(0, focused.end.y - searchScrollOffset) };
12761278
_ScrollToPoints(adjustedStart, adjustedEnd);

src/cascadia/TerminalCore/Terminal.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ class Microsoft::Terminal::Core::Terminal final :
234234
void SetClearQuickFixCallback(std::function<void()> pfn) noexcept;
235235
void SetWindowSizeChangedCallback(std::function<void(int32_t, int32_t)> pfn) noexcept;
236236
void SetSearchHighlights(const std::vector<til::point_span>& highlights) noexcept;
237-
void SetSearchHighlightFocused(const size_t focusedIdx, til::CoordType searchScrollOffset);
237+
void SetSearchHighlightFocused(size_t focusedIdx) noexcept;
238+
void ScrollToSearchHighlight(til::CoordType searchScrollOffset);
238239

239240
void BlinkCursor() noexcept;
240241
void SetCursorOn(const bool isOn) noexcept;

src/interactivity/win32/find.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ INT_PTR CALLBACK FindDialogProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM l
5757
if (searcher.IsStale(gci.renderData, lastFindString, flags))
5858
{
5959
searcher.Reset(gci.renderData, lastFindString, flags, reverse);
60-
searcher.MoveToCurrentSelection();
6160
}
6261
else
6362
{

0 commit comments

Comments
 (0)