Fix padding around certain line numbers Resolves dart-lang/source_span#38.
diff --git a/pkgs/source_span/CHANGELOG.md b/pkgs/source_span/CHANGELOG.md index ddb4ff0..0dfe483 100644 --- a/pkgs/source_span/CHANGELOG.md +++ b/pkgs/source_span/CHANGELOG.md
@@ -1,3 +1,8 @@ +# 1.5.6 + +* Fix padding around line numbers that are powers of 10 in + `FileSpan.highlight()`. + # 1.5.5 * Fix a bug where `FileSpan.highlight()` would crash for spans that covered a
diff --git a/pkgs/source_span/lib/src/highlighter.dart b/pkgs/source_span/lib/src/highlighter.dart index 17a47bc..0714acd 100644 --- a/pkgs/source_span/lib/src/highlighter.dart +++ b/pkgs/source_span/lib/src/highlighter.dart
@@ -181,7 +181,7 @@ // In a purely mathematical world, floor(log10(n)) would give the number of // digits in n, but floating point errors render that unreliable in // practice. - _paddingBeforeSidebar = _span.end.line.toString().length + 1; + _paddingBeforeSidebar = (_span.end.line + 1).toString().length + 1; /// Returns the highlighted span text. ///
diff --git a/pkgs/source_span/test/highlight_test.dart b/pkgs/source_span/test/highlight_test.dart index 5fdf622..521f748 100644 --- a/pkgs/source_span/test/highlight_test.dart +++ b/pkgs/source_span/test/highlight_test.dart
@@ -530,4 +530,30 @@ ${colors.BLUE} '${colors.NONE}""")); }); }); + + group("line numbers have appropriate padding", () { + test("with line number 9", () { + expect( + SourceFile.fromString("\n" * 8 + "foo bar baz\n") + .span(8, 11) + .highlight(), + equals(""" + , +9 | foo bar baz + | ^^^ + '""")); + }); + + test("with line number 10", () { + expect( + SourceFile.fromString("\n" * 9 + "foo bar baz\n") + .span(9, 12) + .highlight(), + equals(""" + , +10 | foo bar baz + | ^^^ + '""")); + }); + }); }