replaceNext function

bool replaceNext(
  1. EditorViewState view
)

Replace the current match with the replacement text.

Implementation

bool replaceNext(EditorViewState view) {
  return _searchCommand(view, (v, state) {
    if (v.state.isReadOnly) return false;

    final from = v.state.selection.main.from;
    final to = v.state.selection.main.to;
    var match = state.query.nextMatch(v.state, from, from);
    if (match == null) return false;

    final changes = <Object>[];
    EditorSelection? selection;
    final effects = <StateEffect<dynamic>>[];

    if (match.from == from && match.to == to) {
      final replacement = state.query.getReplacement(match);
      changes.add(ChangeSpec(from: match.from, to: match.to, insert: replacement));
      match = state.query.nextMatch(v.state, match.from, match.to);
    }

    if (match != null) {
      final changeSet = v.state.changes(changes);
      selection = EditorSelection.single(match.from, match.to).map(changeSet);
      final config = v.state.facet(searchConfigFacet);
      final scrollEffect = config.scrollToMatch?.call(selection.main, v) ??
          EditorView.scrollIntoView.of(ScrollTarget(selection));
      effects.add(scrollEffect);
    }

    v.dispatch([
      TransactionSpec(
        changes: changes.isEmpty ? null : changes,
        selection: selection,
        effects: effects.isEmpty ? null : effects,
        userEvent: 'input.replace',
      ),
    ]);
    return true;
  });
}