message method

String message({
  1. bool? color,
})

Creates a human-friendly representation of the analysis errors.

Implementation

String message({bool? color}) {
  var buffer = StringBuffer();
  buffer.writeln('Could not format because the source could not be parsed:');

  // In case we get a huge series of cascaded errors, just show the first few.
  var shownErrors = errors;
  if (errors.length > 10) shownErrors = errors.take(10).toList();

  for (var error in shownErrors) {
    var source = error.source.contents.data;

    // If the parse error is for something missing from the end of the file,
    // the error position will go past the end of the source. In that case,
    // just pad the source with spaces so we can report it nicely.
    if (error.offset + error.length > source.length) {
      source += ' ' * (error.offset + error.length - source.length);
    }

    var file = SourceFile.fromString(source, url: error.source.fullName);
    var span = file.span(error.offset, error.offset + error.length);
    if (buffer.isNotEmpty) buffer.writeln();
    buffer.write(span.message(error.message, color: color));
  }

  if (shownErrors.length != errors.length) {
    buffer.writeln();
    buffer.write('(${errors.length - shownErrors.length} more errors...)');
  }

  return buffer.toString();
}