insertBlankLine function

bool insertBlankLine(
  1. StateCommandTarget target
)

Create a blank, indented line below the current line.

Implementation

bool insertBlankLine(StateCommandTarget target) {
  if (target.state.isReadOnly) return false;

  final state = target.state;

  final result = state.changeByRange((range) {
    var to = range.to;
    final line = state.doc.lineAt(to);

    // Move to end of line
    to = line.to;

    // Create indent context
    final cx = IndentContext(
      state,
      options: IndentContextOptions(simulateBreak: to),
    );

    // Get indentation
    var indent = getIndentation(cx, to);
    if (indent == null) {
      final lineIndent = RegExp(r'^\s*').firstMatch(line.text);
      indent = countColumn(lineIndent?.group(0) ?? '', state.tabSize);
    }

    final indentStr = indentString(state, indent);
    final insertText = '${state.lineBreak}$indentStr';

    return ChangeByRangeResult(
      changes: [ChangeSpec(from: to, insert: insertText)],
      range: EditorSelection.cursor(to + insertText.length),
    );
  });

  target.dispatch(state.update([
    TransactionSpec(
      changes: result.changes,
      selection: result.selection,
      scrollIntoView: true,
      userEvent: 'input',
    ),
  ]));
  return true;
}