Don't crash on spans that end on empty lines
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 67ba8d5..afba8b7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,9 @@
* `FileSpan.context` now includes the full line on which the span appears for
empty spans at the beginning and end of lines.
+* Fix an edge case where `FileSpan.highlight()` could crash when highlighting a
+ span that ended with an empty line.
+
# 1.5.1
* Produce better source span highlights for multi-line spans that cover the
diff --git a/lib/src/highlighter.dart b/lib/src/highlighter.dart
index 3ed1749..f29a89d 100644
--- a/lib/src/highlighter.dart
+++ b/lib/src/highlighter.dart
@@ -202,12 +202,14 @@
var lines = context.split("\n");
- // Trim a trailing newline so we don't add an empty line to the end of the
- // highlight.
- if (lines.last.isEmpty && lines.length > 1) lines.removeLast();
+ var lastLineIndex = _span.end.line - _span.start.line;
+ if (lines.last.isEmpty && lines.length > lastLineIndex + 1) {
+ // Trim a trailing newline so we don't add an empty line to the end of the
+ // highlight.
+ lines.removeLast();
+ }
_writeFirstLine(lines.first);
- var lastLineIndex = _span.end.line - _span.start.line;
if (_multiline) {
_writeIntermediateLines(lines.skip(1).take(lastLineIndex - 1));
_writeLastLine(lines[lastLineIndex]);
diff --git a/pubspec.yaml b/pubspec.yaml
index ffd9df3..fd80bc8 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: source_span
-version: 1.5.2-dev
+version: 1.5.2
description: A library for identifying source spans and locations.
author: Dart Team <misc@dartlang.org>
diff --git a/test/highlight_test.dart b/test/highlight_test.dart
index b9e7ded..8418367 100644
--- a/test/highlight_test.dart
+++ b/test/highlight_test.dart
@@ -157,6 +157,20 @@
'"""));
});
+ test("highlights the full last line if it's empty", () {
+ var file = new SourceFile.fromString("""
+foo
+
+bar
+""");
+
+ expect(file.span(4, 9).highlight(), equals("""
+ ,
+2 | /
+3 | \\ bar
+ '"""));
+ });
+
test("highlights the full last line", () {
expect(file.span(4, 27).highlight(), equals("""
,
@@ -216,6 +230,20 @@
3 | \\ zip zap zop
'"""));
});
+
+ test("highlights the full last line if it's empty", () {
+ var file = new SourceFile.fromString("""
+foo
+
+bar
+""");
+
+ expect(file.span(0, 5).highlight(), equals("""
+ ,
+1 | / foo
+2 | \\
+ '"""));
+ });
});
group("prints tabs as spaces", () {