selectSelectionMatches function
Select all instances of the currently selected text.
Implementation
bool selectSelectionMatches(
EditorState state,
void Function(txn.Transaction) dispatch,
) {
final sel = state.selection;
if (sel.ranges.length > 1 || sel.main.empty) return false;
final from = sel.main.from;
final to = sel.main.to;
final searchText = state.sliceDoc(from, to);
final ranges = <SelectionRange>[];
var main = 0;
final cursor = SearchCursor(state.doc, searchText);
while (true) {
final match = cursor.next();
if (match == null) break;
if (ranges.length > 1000) return false;
if (match.from == from) main = ranges.length;
ranges.add(EditorSelection.range(match.from, match.to));
}
dispatch(state.update([
TransactionSpec(
selection: EditorSelection.create(ranges, main),
userEvent: 'select.search.matches',
),
]));
return true;
}