Skip to content

Commit 3478d3a

Browse files
authored
Fix tab highlighting. (flutter#14)
1 parent 5466b15 commit 3478d3a

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.3.1
2+
3+
* Properly highlight spans for lines that include tabs with
4+
`SourceSpan.highlight()` and `SourceSpan.message()`.
5+
16
# 1.3.0
27

38
* Add `SourceSpan.highlight()`, which returns just the highlighted text that

lib/src/span_mixin.dart

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:math' as math;
6+
7+
import 'package:charcode/charcode.dart';
68
import 'package:path/path.dart' as p;
79

810
import 'colors.dart' as colors;
@@ -96,7 +98,15 @@ abstract class SourceSpanMixin implements SourceSpan {
9698
buffer.write(textLine);
9799
}
98100
if (!textLine.endsWith('\n')) buffer.write('\n');
99-
buffer.write(' ' * column);
101+
102+
for (var i = 0; i < column; i++) {
103+
if (textLine.codeUnitAt(i) == $tab) {
104+
buffer.writeCharCode($tab);
105+
} else {
106+
buffer.writeCharCode($space);
107+
}
108+
}
109+
100110
if (color != null) buffer.write(color);
101111
buffer.write('^' * math.max(toColumn - column, 1));
102112
if (color != null) buffer.write(colors.NONE);

pubspec.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name: source_span
2-
version: 1.3.0
2+
version: 1.3.1
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
66
dependencies:
7+
charcode: '^1.0.0'
78
path: '>=1.2.0 <2.0.0'
89
environment:
910
sdk: '>=0.8.10+6 <2.0.0'

test/highlight_test.dart

+7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ foo bar
7575
^^^^^^^"""));
7676
});
7777

78+
test("emits tabs for tabs", () {
79+
expect(new SourceFile(" \t \t\tfoo bar").span(5, 8).highlight(),
80+
equals("""
81+
\t \t\tfoo bar
82+
\t \t\t^^^"""));
83+
});
84+
7885
test("supports lines of preceding context", () {
7986
var span = new SourceSpanWithContext(
8087
new SourceLocation(5, line: 3, column: 5, sourceUrl: "foo.dart"),

0 commit comments

Comments
 (0)