dedent method

String dedent({
  1. Pattern whitespace = const CharMatcher.whitespace(),
  2. bool ignoreEmpty = true,
})

Removes a common leading prefix from every line.

  • whitespace matches the leading prefix.
  • ignoreEmpty is used to skip over empty lines.

Implementation

String dedent({
  Pattern whitespace = const CharMatcher.whitespace(),
  bool ignoreEmpty = true,
}) {
  final lines = split('\n');
  final prefix = findLongestPrefix(whitespace,
      ignoreEmpty ? lines.where((line) => line.trim().isNotEmpty) : lines);
  if (prefix.isEmpty) {
    return this;
  }
  return lines.map((line) => line.removePrefix(prefix)).join('\n');
}