-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Fix infinite looping in notebook addFindMatchToSelection (Cmd+D) #251157
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
Conversation
Co-authored-by: Yoyokrazy <12552271+Yoyokrazy@users.noreply.github.com>
notebook.addFindMatchToSelection
keeps looping// Check if all matches are already covered by selections to avoid infinite looping | ||
const allMatches = notebookTextModel.findMatches(this.word, false, true, USUAL_WORD_SEPARATORS); | ||
const totalMatches = allMatches.reduce((sum, cellMatch) => sum + cellMatch.matches.length, 0); | ||
const totalSelections = this.trackedCells.reduce((sum, trackedCell) => sum + trackedCell.matchSelections.length, 0); | ||
|
||
if (totalSelections >= totalMatches) { | ||
// All matches are already selected, make this a no-op like in regular editors | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't optimal, since we don't want to do an entire extra findMatches call each time we check this and same with the iteration overTracked cells for totalSelections. Can we just record allMatches
at the beginning of this whole process? ie when we're going from idle into selecting? the first case? Then each time we successfully add another match we can just incremement and then its a lot better, perf-wise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! I've optimized the performance by recording allMatches
count once during the idle→selecting transition and using a cached counter instead of recalculating on every Cmd+D press. This eliminates the expensive findMatches()
call and tracked cells iteration. See commit 2dda55d.
…ulating Co-authored-by: Yoyokrazy <12552271+Yoyokrazy@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
built and verified fix
Problem
When using
Cmd+D
(Add Selection to Next Find Match) in notebooks, the operation keeps looping indefinitely instead of becoming a no-op once all find results are covered by selections. This differs from regular editors where pressingCmd+D
stops being effective when all matches are already selected.Root Cause
The issue was in the
findAndTrackNextSelection
method innotebookMulticursor.ts
. When in theSelecting
state, the method callsnotebookTextModel.findNextMatch()
which cycles through matches infinitely, unlike regular editors wherefindNextMatch
returnsnull
when no more unique matches are available.Solution
Added a check before calling
findNextMatch
that compares:totalMatches
: Count of all available matches in the notebooktotalSelections
: Count of all current selections across tracked cellsWhen
totalSelections >= totalMatches
, the method returns early (no-op), preventing the infinite loop and matching the behavior of regular editors.Testing
The fix ensures that pressing
Cmd+D
in notebooks will stop being effective once all find results are covered by selections, exactly like in regular text editors. The operation becomes a no-op when all matches are already selected, preventing the infinite cycling behavior.Fixes #235117.