Fix tab highlighting. (#14)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index afcc493..0176b9f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+# 1.3.1
+
+* Properly highlight spans for lines that include tabs with
+ `SourceSpan.highlight()` and `SourceSpan.message()`.
+
# 1.3.0
* Add `SourceSpan.highlight()`, which returns just the highlighted text that
diff --git a/lib/src/span_mixin.dart b/lib/src/span_mixin.dart
index 8d84cea..06e2024 100644
--- a/lib/src/span_mixin.dart
+++ b/lib/src/span_mixin.dart
@@ -3,6 +3,8 @@
// BSD-style license that can be found in the LICENSE file.
import 'dart:math' as math;
+
+import 'package:charcode/charcode.dart';
import 'package:path/path.dart' as p;
import 'colors.dart' as colors;
@@ -96,7 +98,15 @@
buffer.write(textLine);
}
if (!textLine.endsWith('\n')) buffer.write('\n');
- buffer.write(' ' * column);
+
+ for (var i = 0; i < column; i++) {
+ if (textLine.codeUnitAt(i) == $tab) {
+ buffer.writeCharCode($tab);
+ } else {
+ buffer.writeCharCode($space);
+ }
+ }
+
if (color != null) buffer.write(color);
buffer.write('^' * math.max(toColumn - column, 1));
if (color != null) buffer.write(colors.NONE);
diff --git a/pubspec.yaml b/pubspec.yaml
index 8fa8711..d340d85 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,9 +1,10 @@
name: source_span
-version: 1.3.0
+version: 1.3.1
author: Dart Team <misc@dartlang.org>
description: A library for identifying source spans and locations.
homepage: https://github.com/dart-lang/source_span
dependencies:
+ charcode: '^1.0.0'
path: '>=1.2.0 <2.0.0'
environment:
sdk: '>=0.8.10+6 <2.0.0'
diff --git a/test/highlight_test.dart b/test/highlight_test.dart
index 32b09ff..74faed5 100644
--- a/test/highlight_test.dart
+++ b/test/highlight_test.dart
@@ -75,6 +75,13 @@
^^^^^^^"""));
});
+ test("emits tabs for tabs", () {
+ expect(new SourceFile(" \t \t\tfoo bar").span(5, 8).highlight(),
+ equals("""
+ \t \t\tfoo bar
+ \t \t\t^^^"""));
+ });
+
test("supports lines of preceding context", () {
var span = new SourceSpanWithContext(
new SourceLocation(5, line: 3, column: 5, sourceUrl: "foo.dart"),