toPrettier method

String toPrettier({
  1. String indent = ' ',
  2. List<String> separators = const <String>[','],
  3. Map<String, String> brackets = const {'{' : '}', '[' : ']', '(' : ')'},
})

This prettily prints all strings.

  • Before
Dog(true, 10, [Dog(true, 100, [], null), Dog(true, 100, [], null)], Dog(true, 100, [], null))
  • After
Dog(
  true,
  10,
  [
    Dog(
      true,
      100,
      [],
      null
    ),
    Dog(
      true,
      100,
      [],
      null
    )
  ],
  Dog(
    true,
    100,
    [],
    null
  )
)

Implementation

String toPrettier({
  String indent = '  ',
  List<String> separators = const <String>[','],
  Map<String, String> brackets = const {
    '{': '}',
    '[': ']',
    '(': ')',
  },
}) {
  final buff = StringBuffer();
  final stack = <String>[];

  bool isOpenings(String? char) => brackets.keys.contains(char);
  bool isClosings(String? char) => brackets.values.contains(char);
  bool isSeparator(String? char) => separators.contains(char);

  var withLineBreak = false;
  var formerLineBreak = false;

  for (var idx = 0; idx < length; idx++) {
    final prevChar = _tryGet(idx - 1);
    final currChar = this[idx];
    final nextChar = _tryGet(idx + 1);
    withLineBreak = false;
    formerLineBreak = false;

    if (isOpenings(currChar)) {
      stack.add(currChar);
      withLineBreak = !isClosings(nextChar);
    } else if (isClosings(currChar)) {
      stack.removeLast();
      if (!isOpenings(prevChar)) {
        formerLineBreak = true;
        withLineBreak = true;
      }
    } else if (isSeparator(currChar)) {
      withLineBreak = true;
      if (nextChar ==
          ' ' /** , Ignore a whitespace if its with separator */) {
        idx++;
      }
    } else {
      withLineBreak = false;
    }

    if (withLineBreak) {
      if (formerLineBreak) {
        buff
          ..writeln()
          ..write(indent * stack.length)
          ..write(currChar);
      } else {
        buff
          ..writeln(currChar)
          ..write(indent * stack.length);
      }
    } else {
      buff.write(currChar);
    }
  }

  return buff.toString();
}