toNodes method
Converts a Line list to a AST Node list.
Set trimLeft
to true
to remove leading whitespaces of the each line.
Set trimLeading
to true
to remove leading whitespaces of the first
line.
Set trimTrailing
to true
to remove trailing whitespaces of the last
line(The line ending will be kept, set popLineEnding
to true
to
remove the final line ending).
Set popLineEnding
to true
to remove the line ending of the last Line
and return this lineEnding in SourceFromLineList.
Implementation
SourceFromLineList toNodes(
Node Function(SourceSpan span) transfer, {
bool trimLeft = false,
bool trimLeading = false,
bool trimTrailing = false,
bool popLineEnding = false,
}) {
if (isEmpty) {
return SourceFromLineList(<Node>[], null);
}
var spans = toSourceSpans();
// Pop the line ending from `spans` if there is one, no matter
// `popLineEnding` parameter is `true` or `false`.
// Otherwise the trimRight() later will trim only the line ending span if
// the last span is a line ending span.
final lineEnding = spans.last.isLineEnding ? spans.removeLast() : null;
if (spans.isNotEmpty) {
if (trimLeft) {
spans = spans
.map((span) => span.isLineEnding ? span : span.trimLeft())
.toList();
} else if (trimLeading) {
spans.first = spans.first.trimLeft();
}
if (trimTrailing) {
spans.last = spans.last.trimRight();
}
}
// Put the line ending span back if `popLineEnding` is `false`.
if (!popLineEnding && lineEnding != null) {
spans.add(lineEnding);
}
final nodes = spans.concatWhilePossible().map<Node>(transfer).toList();
return SourceFromLineList(nodes, popLineEnding ? lineEnding : null);
}