textIndentLines function

TextLineCommandResult textIndentLines({
  1. required List<String> lines,
  2. required TextLineStateSnapshot state,
  3. int width = 2,
})

Implementation

TextLineCommandResult textIndentLines({
  required List<String> lines,
  required TextLineStateSnapshot state,
  int width = 2,
}) {
  final nextLines = lines.isEmpty ? <String>[''] : List<String>.from(lines);
  final indentWidth = width < 1 ? 1 : width;
  final clampedState = _clampLineStateSnapshot(state, nextLines);
  final span = _selectedLineSpan(clampedState);
  final editorState = EditorState();
  syncEditorStateFromLineSnapshot(
    editorState,
    clampedState,
    lineCount: nextLines.length,
    lineLength: (line) => nextLines[line].length,
  );

  final deltas = <int, int>{};
  final indent = ' ' * indentWidth;
  for (var lineIndex = span.startLine; lineIndex <= span.endLine; lineIndex++) {
    nextLines[lineIndex] = '$indent${nextLines[lineIndex]}';
    deltas[lineIndex] = indentWidth;
  }

  editorState.applyColumnDeltas(
    deltas,
    lineLength: (line) => nextLines[line].length,
  );
  return _lineResultFromSnapshot(
    nextLines,
    lineSnapshotFromEditorState(
      editorState,
      lineCount: nextLines.length,
      lineLength: (line) => nextLines[line].length,
      preserveCollapsedSelection: true,
    ),
  );
}