insertIndentedNewline function

TextCommandResult insertIndentedNewline(
  1. List<String> graphemes, {
  2. required int cursorOffset,
  3. int? selectionBaseOffset,
  4. int? selectionExtentOffset,
  5. required List<String> baseIndent,
  6. List<String> additionalIndent = const <String>[],
  7. List<String> trailingSuffix = const <String>[],
  8. int trailingSuffixReplaceCount = 0,
})

Implementation

TextCommandResult insertIndentedNewline(
  List<String> graphemes, {
  required int cursorOffset,
  int? selectionBaseOffset,
  int? selectionExtentOffset,
  required List<String> baseIndent,
  List<String> additionalIndent = const <String>[],
  List<String> trailingSuffix = const <String>[],
  int trailingSuffixReplaceCount = 0,
}) {
  final selection = normalizedSelectionRange(
    selectionBaseOffset,
    selectionExtentOffset,
  );
  final hasSelection = selection != null && selection.start != selection.end;
  final start = hasSelection ? selection.start : cursorOffset;
  final end = hasSelection ? selection.end : cursorOffset;
  final clampedStart = start.clamp(0, graphemes.length);
  var clampedEnd = end.clamp(clampedStart, graphemes.length);
  if (!hasSelection && trailingSuffixReplaceCount > 0) {
    clampedEnd = (clampedEnd + trailingSuffixReplaceCount).clamp(
      clampedStart,
      graphemes.length,
    );
  }

  final cursorInsertion = <String>['\n', ...baseIndent, ...additionalIndent];
  final replacement = <String>[...cursorInsertion, ...trailingSuffix];
  final result = edit_ops.replaceRange(
    graphemes,
    start: clampedStart,
    end: clampedEnd,
    replacement: replacement,
  );

  return TextCommandResult(
    graphemes: result.graphemes,
    cursorOffset: clampedStart + cursorInsertion.length,
  );
}