replaceSourceRangeIndent method

String replaceSourceRangeIndent(
  1. SourceRange range,
  2. String oldIndent,
  3. String newIndent, {
  4. bool includeLeading = false,
  5. bool ensureTrailingNewline = false,
})

Returns the source of the given SourceRange with indentation changed from oldIndent to newIndent, keeping indentation of lines relative to each other.

Indentation on the first line will only be updated if includeLeading is true.

If ensureTrailingNewline is true, a newline will be added to the end of the returned code if it does not already have one.

Usually includeLeading and ensureTrailingNewline are set together, when indenting a set of statements to go inside a block (as opposed to just wrapping a nested expression that might span multiple lines).

Implementation

String replaceSourceRangeIndent(
  SourceRange range,
  String oldIndent,
  String newIndent, {
  bool includeLeading = false,
  bool ensureTrailingNewline = false,
}) {
  var oldSource = getRangeText(range);
  return replaceSourceIndent(
    oldSource,
    oldIndent,
    newIndent,
    includeLeading: includeLeading,
    ensureTrailingNewline: ensureTrailingNewline,
  );
}