add method

void add(
  1. String str, {
  2. bool projectMarks = false,
})

Add str contents to the output, tracking new lines to track correct positions for span locations. When projectMarks is true, this method adds a source map location on each new line, projecting that every new line in the target file (printed here) corresponds to a new line in the source file.

Implementation

void add(String str, {bool projectMarks = false}) {
  var chars = str.runes.toList();
  var length = chars.length;
  for (var i = 0; i < length; i++) {
    var c = chars[i];
    if (c == lineFeed ||
        (c == carriageReturn &&
            (i + 1 == length || chars[i + 1] != lineFeed))) {
      // Return not followed by line-feed is treated as a new line.
      _line++;
      _column = 0;
      {
        // **Warning**: Any calls to `mark` will change the value of `_loc`,
        // so this local variable is no longer up to date after that point.
        //
        // This is why it has been put inside its own block to limit the
        // scope in which it is available.
        var loc = _loc;
        if (projectMarks && loc != null) {
          if (loc is FileLocation) {
            var file = loc.file;
            mark(file.location(file.getOffset(loc.line + 1)));
          } else {
            mark(SourceLocation(0,
                sourceUrl: loc.sourceUrl, line: loc.line + 1, column: 0));
          }
        }
      }
    } else {
      _column++;
    }
  }
  _buff.write(str);
}