insertIndentedNewline function
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,
);
}