Skip to content

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

Merged
merged 5 commits into from
Jun 13, 2025
Merged
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class NotebookMultiCursorController extends Disposable implements INotebo
position: Position;
} | undefined;
private trackedCells: TrackedCell[];
private totalMatchesCount: number;

private readonly _onDidChangeAnchorCell;
readonly onDidChangeAnchorCell: Event<void>;
Expand Down Expand Up @@ -125,6 +126,7 @@ export class NotebookMultiCursorController extends Disposable implements INotebo
super();
this.word = '';
this.trackedCells = [];
this.totalMatchesCount = 0;
this._onDidChangeAnchorCell = this._register(new Emitter<void>());
this.onDidChangeAnchorCell = this._onDidChangeAnchorCell.event;
this.anchorDisposables = this._register(new DisposableStore());
Expand Down Expand Up @@ -478,6 +480,7 @@ export class NotebookMultiCursorController extends Disposable implements INotebo
this.cursorsDisposables.clear();
this.cursorsControllers.clear();
this.trackedCells = [];
this.totalMatchesCount = 0;
this.startPosition = undefined;
this.word = '';
}
Expand All @@ -496,6 +499,13 @@ export class NotebookMultiCursorController extends Disposable implements INotebo
}
this.word = word.word;

// Record the total number of matches at the beginning of the selection process for performance
const notebookTextModel = this.notebookEditor.textModel;
if (notebookTextModel) {
const allMatches = notebookTextModel.findMatches(this.word, false, true, USUAL_WORD_SEPARATORS);
this.totalMatchesCount = allMatches.reduce((sum, cellMatch) => sum + cellMatch.matches.length, 0);
}

const index = this.notebookEditor.getCellIndex(focusedCell);
if (index === undefined) {
return;
Expand Down Expand Up @@ -545,6 +555,14 @@ export class NotebookMultiCursorController extends Disposable implements INotebo
return; // should not happen
}

// Check if all matches are already covered by selections to avoid infinite looping
const totalSelections = this.trackedCells.reduce((sum, trackedCell) => sum + trackedCell.matchSelections.length, 0);

if (totalSelections >= this.totalMatchesCount) {
// All matches are already selected, make this a no-op like in regular editors
return;
}

const findResult = notebookTextModel.findNextMatch(
this.word,
{ cellIndex: index, position: focusedCell.getSelections()[focusedCell.getSelections().length - 1].getEndPosition() },
Expand Down
Loading