withSpans method

Stream<Tuple2<String, SourceSpanWithContext>> withSpans({
  1. Uri? sourceUrl,
  2. String? sourcePath,
})

Returns a stream of lines along with FileSpans indicating the position of those lines within the original source (assuming that this stream is the entirety of the source).

The sourceUrl argument provides the URL of the resulting spans. The sourcePath argument is a shorthand for converting the path to a URL and passing it to sourceUrl. The sourceUrl and sourcePath arguments may not both be passed at once.

See LineAndSpanStreamExtensions.

Implementation

Stream<Tuple2<String, SourceSpanWithContext>> withSpans(
    {Uri? sourceUrl, String? sourcePath}) {
  if (sourcePath != null) {
    if (sourceUrl != null) {
      throw ArgumentError("Only one of url and path may be passed.");
    }
    sourceUrl = p.toUri(sourcePath);
  }

  var lineNumber = 0;
  var offset = 0;
  return map((line) {
    var span = SourceSpanWithContext(
        SourceLocation(offset,
            sourceUrl: sourceUrl, line: lineNumber, column: 0),
        SourceLocation(offset + line.length,
            sourceUrl: sourceUrl, line: lineNumber, column: line.length),
        line,
        line);
    lineNumber++;
    offset += line.length + 1;
    return Tuple2(line, span);
  });
}