Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit e56cd40

Browse files
committed
Add SourceSpan.highlight().
This is useful for constructing a message with a non-standard file/line/column display.
1 parent 202b0ef commit e56cd40

File tree

6 files changed

+65
-69
lines changed

6 files changed

+65
-69
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.3.0
2+
3+
* Add `SourceSpan.highlight()`, which returns just the highlighted text that
4+
would be included in `SourceSpan.message()`.
5+
16
# 1.2.3
27

38
* Fix a bug where a point span at the end of a file without a trailing newline

lib/src/span.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ abstract class SourceSpan implements Comparable<SourceSpan> {
5353
/// should be highlighted using the default color. If it's `false` or `null`,
5454
/// it indicates that the text shouldn't be highlighted.
5555
String message(String message, {color});
56+
57+
/// Prints the text associated with this span in a user-friendly way.
58+
///
59+
/// This is identical to [message], except that it doesn't print the file
60+
/// name, line number, column number, or message. If [length] is 0 and this
61+
/// isn't a [SourceSpanWithContext], returns an empty string.
62+
///
63+
/// [color] may either be a [String], a [bool], or `null`. If it's a string,
64+
/// it indicates an ANSII terminal color escape that should be used to
65+
/// highlight the span's text. If it's `true`, it indicates that the text
66+
/// should be highlighted using the default color. If it's `false` or `null`,
67+
/// it indicates that the text shouldn't be highlighted.
68+
String highlight({color});
5669
}
5770

5871
/// A base class for source spans with [start], [end], and [text] known at

lib/src/span_mixin.dart

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,26 @@ abstract class SourceSpanMixin implements SourceSpan {
4646
}
4747

4848
String message(String message, {color}) {
49-
if (color == true) color = colors.RED;
50-
if (color == false) color = null;
51-
52-
var line = start.line;
53-
var column = start.column;
54-
5549
var buffer = new StringBuffer();
56-
buffer.write('line ${line + 1}, column ${column + 1}');
50+
buffer.write('line ${start.line + 1}, column ${start.column + 1}');
5751
if (sourceUrl != null) buffer.write(' of ${p.prettyUri(sourceUrl)}');
5852
buffer.write(': $message');
5953

60-
if (length == 0 && this is! SourceSpanWithContext) return buffer.toString();
61-
buffer.write("\n");
54+
var highlight = this.highlight(color: color);
55+
if (!highlight.isEmpty) {
56+
buffer.writeln();
57+
buffer.write(highlight);
58+
}
59+
60+
return buffer.toString();
61+
}
6262

63+
String highlight({color}) {
64+
if (color == true) color = colors.RED;
65+
if (color == false) color = null;
66+
67+
var column = start.column;
68+
var buffer = new StringBuffer();
6369
var textLine;
6470
if (this is SourceSpanWithContext) {
6571
var context = (this as SourceSpanWithContext).context;
@@ -71,6 +77,8 @@ abstract class SourceSpanMixin implements SourceSpan {
7177
var endIndex = context.indexOf('\n');
7278
textLine = endIndex == -1 ? context : context.substring(0, endIndex + 1);
7379
column = math.min(column, textLine.length);
80+
} else if (length == 0) {
81+
return "";
7482
} else {
7583
textLine = text.split("\n").first;
7684
column = 0;

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_span
2-
version: 1.2.3
2+
version: 1.3.0
33
author: Dart Team <[email protected]>
44
description: A library for identifying source spans and locations.
55
homepage: https://github.com/dart-lang/source_span

test/file_message_test.dart renamed to test/highlight_test.dart

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,95 +13,97 @@ main() {
1313
foo bar baz
1414
whiz bang boom
1515
zip zap zop
16-
""", url: "foo.dart");
16+
""");
1717
});
1818

1919
test("points to the span in the source", () {
20-
expect(file.span(4, 7).message("oh no"), equals("""
21-
line 1, column 5 of foo.dart: oh no
20+
expect(file.span(4, 7).highlight(), equals("""
2221
foo bar baz
2322
^^^"""));
2423
});
2524

2625
test("gracefully handles a missing source URL", () {
2726
var span = new SourceFile("foo bar baz").span(4, 7);
28-
expect(span.message("oh no"), equals("""
29-
line 1, column 5: oh no
27+
expect(span.highlight(), equals("""
3028
foo bar baz
3129
^^^"""));
3230
});
3331

3432
test("highlights the first line of a multiline span", () {
35-
expect(file.span(4, 20).message("oh no"), equals("""
36-
line 1, column 5 of foo.dart: oh no
33+
expect(file.span(4, 20).highlight(), equals("""
3734
foo bar baz
3835
^^^^^^^^"""));
3936
});
4037

4138
test("works for a point span", () {
42-
expect(file.location(4).pointSpan().message("oh no"), equals("""
43-
line 1, column 5 of foo.dart: oh no
39+
expect(file.location(4).pointSpan().highlight(), equals("""
4440
foo bar baz
4541
^"""));
4642
});
4743

4844
test("works for a point span at the end of a line", () {
49-
expect(file.location(11).pointSpan().message("oh no"), equals("""
50-
line 1, column 12 of foo.dart: oh no
45+
expect(file.location(11).pointSpan().highlight(), equals("""
5146
foo bar baz
5247
^"""));
5348
});
5449

5550
test("works for a point span at the end of the file", () {
56-
expect(file.location(38).pointSpan().message("oh no"), equals("""
57-
line 3, column 12 of foo.dart: oh no
51+
expect(file.location(38).pointSpan().highlight(), equals("""
5852
zip zap zop
5953
^"""));
6054
});
6155

6256
test("works for a point span at the end of the file with no trailing newline",
6357
() {
6458
file = new SourceFile("zip zap zop");
65-
expect(file.location(11).pointSpan().message("oh no"), equals("""
66-
line 1, column 12: oh no
59+
expect(file.location(11).pointSpan().highlight(), equals("""
6760
zip zap zop
6861
^"""));
6962
});
7063

7164
test("works for a point span in an empty file", () {
72-
expect(new SourceFile("").location(0).pointSpan().message("oh no"),
65+
expect(new SourceFile("").location(0).pointSpan().highlight(),
7366
equals("""
74-
line 1, column 1: oh no
7567
7668
^"""));
7769
});
7870

7971
test("works for a single-line file without a newline", () {
80-
expect(new SourceFile("foo bar").span(0, 7).message("oh no"),
72+
expect(new SourceFile("foo bar").span(0, 7).highlight(),
8173
equals("""
82-
line 1, column 1: oh no
8374
foo bar
8475
^^^^^^^"""));
8576
});
8677

78+
test("supports lines of preceding context", () {
79+
var span = new SourceSpanWithContext(
80+
new SourceLocation(5, line: 3, column: 5, sourceUrl: "foo.dart"),
81+
new SourceLocation(12, line: 3, column: 12, sourceUrl: "foo.dart"),
82+
"foo bar",
83+
"previous\nlines\n-----foo bar-----\nfollowing line\n");
84+
85+
expect(span.highlight(color: colors.YELLOW), equals("""
86+
previous
87+
lines
88+
-----${colors.YELLOW}foo bar${colors.NONE}-----
89+
${colors.YELLOW}^^^^^^^${colors.NONE}"""));
90+
});
91+
8792
group("colors", () {
8893
test("doesn't colorize if color is false", () {
89-
expect(file.span(4, 7).message("oh no", color: false), equals("""
90-
line 1, column 5 of foo.dart: oh no
94+
expect(file.span(4, 7).highlight(color: false), equals("""
9195
foo bar baz
9296
^^^"""));
9397
});
9498

9599
test("colorizes if color is true", () {
96-
expect(file.span(4, 7).message("oh no", color: true), equals("""
97-
line 1, column 5 of foo.dart: oh no
100+
expect(file.span(4, 7).highlight(color: true), equals("""
98101
foo ${colors.RED}bar${colors.NONE} baz
99102
${colors.RED}^^^${colors.NONE}"""));
100103
});
101104

102105
test("uses the given color if it's passed", () {
103-
expect(file.span(4, 7).message("oh no", color: colors.YELLOW), equals("""
104-
line 1, column 5 of foo.dart: oh no
106+
expect(file.span(4, 7).highlight(color: colors.YELLOW), equals("""
105107
foo ${colors.YELLOW}bar${colors.NONE} baz
106108
${colors.YELLOW}^^^${colors.NONE}"""));
107109
});

test/span_test.dart

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -234,48 +234,16 @@ line 1, column 6 of foo.dart: oh no
234234
${colors.YELLOW}foo bar${colors.NONE}
235235
${colors.YELLOW}^^^^^^^${colors.NONE}"""));
236236
});
237-
});
238237

239-
group("message() with context", () {
240-
var spanWithContext;
241-
setUp(() {
242-
spanWithContext = new SourceSpanWithContext(
238+
test("with context, underlines the right column", () {
239+
var spanWithContext = new SourceSpanWithContext(
243240
new SourceLocation(5, sourceUrl: "foo.dart"),
244241
new SourceLocation(12, sourceUrl: "foo.dart"),
245242
"foo bar",
246243
"-----foo bar-----");
247-
});
248244

249-
test("underlines under the right column", () {
250245
expect(spanWithContext.message("oh no", color: colors.YELLOW), equals("""
251246
line 1, column 6 of foo.dart: oh no
252-
-----${colors.YELLOW}foo bar${colors.NONE}-----
253-
${colors.YELLOW}^^^^^^^${colors.NONE}"""));
254-
});
255-
256-
test("underlines correctly when text appears twice", () {
257-
var span = new SourceSpanWithContext(
258-
new SourceLocation(9, column: 9, sourceUrl: "foo.dart"),
259-
new SourceLocation(12, column: 12, sourceUrl: "foo.dart"),
260-
"foo",
261-
"-----foo foo-----");
262-
expect(span.message("oh no", color: colors.YELLOW), equals("""
263-
line 1, column 10 of foo.dart: oh no
264-
-----foo ${colors.YELLOW}foo${colors.NONE}-----
265-
${colors.YELLOW}^^^${colors.NONE}"""));
266-
});
267-
268-
test("supports lines of preceeding context", () {
269-
var span = new SourceSpanWithContext(
270-
new SourceLocation(5, line: 3, column: 5, sourceUrl: "foo.dart"),
271-
new SourceLocation(12, line: 3, column: 12, sourceUrl: "foo.dart"),
272-
"foo bar",
273-
"previous\nlines\n-----foo bar-----\nfollowing line\n");
274-
275-
expect(span.message("oh no", color: colors.YELLOW), equals("""
276-
line 4, column 6 of foo.dart: oh no
277-
previous
278-
lines
279247
-----${colors.YELLOW}foo bar${colors.NONE}-----
280248
${colors.YELLOW}^^^^^^^${colors.NONE}"""));
281249
});

0 commit comments

Comments
 (0)