toggleFold function

bool toggleFold(
  1. StateCommandTarget target
)

Toggle folding at cursors.

Unfolds if there is an existing fold starting in that line, tries to find a foldable range around it otherwise.

Implementation

bool toggleFold(StateCommandTarget target) {
  final view = target;
  final effects = <StateEffect<dynamic>>[];

  for (final line in _selectedLines(view)) {
    final folded = _findFold(view.state, line.from, line.to);
    if (folded != null) {
      effects.add(unfoldEffect.of(folded));
    } else {
      final foldRange = _foldableContainer(view, line);
      if (foldRange != null) {
        effects.add(foldEffect.of(foldRange));
      }
    }
  }

  if (effects.isNotEmpty) {
    view.dispatch(view.state.update([
      TransactionSpec(effects: _maybeEnable(view.state, effects)),
    ]));
  }
  return effects.isNotEmpty;
}