Skip to content

Commit ef2875e

Browse files
authored
Merge pull request #446 from epage/simd
fix(simd): Fix indentation with trailing newline
2 parents 75df9d7 + a8cd5b7 commit ef2875e

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

src/renderer/render.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,7 +2736,10 @@ fn pre_process<'a>(
27362736
fn newline_count(body: &str) -> usize {
27372737
#[cfg(feature = "simd")]
27382738
{
2739-
memchr::memchr_iter(b'\n', body.as_bytes()).count()
2739+
// Trailing newlines do not count towards the number of lines
2740+
// (this is based into `str::lines`)
2741+
let trailing_newline = body.ends_with('\n');
2742+
memchr::memchr_iter(b'\n', body.as_bytes()).count() - usize::from(trailing_newline)
27402743
}
27412744
#[cfg(not(feature = "simd"))]
27422745
{
@@ -2782,9 +2785,20 @@ mod test {
27822785
[dependencies]
27832786
bar = { base = '^^not-valid^^', path = 'bar' }
27842787
"#;
2785-
let actual_count = newline_count(source);
2786-
let expected_count = 10;
2788+
assert_eq!(newline_count(source), 10);
27872789

2788-
assert_eq!(expected_count, actual_count);
2790+
assert_eq!(newline_count(""), 0);
2791+
2792+
assert_eq!(newline_count("one"), 0);
2793+
2794+
assert_eq!(newline_count("one\n"), 0);
2795+
2796+
assert_eq!(newline_count("one\ntwo"), 1);
2797+
2798+
assert_eq!(newline_count("one\ntwo\n"), 1);
2799+
2800+
assert_eq!(newline_count("one\n\n"), 1);
2801+
2802+
assert_eq!(newline_count("one\r\ntwo\r\n"), 1);
27892803
}
27902804
}

tests/formatter.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5234,3 +5234,43 @@ error[test-diagnostics]: main diagnostic message
52345234
let renderer = renderer.decor_style(DecorStyle::Unicode);
52355235
assert_data_eq!(renderer.render(input), expected_unicode);
52365236
}
5237+
5238+
#[test]
5239+
fn consistent_indentation_with_trailing_newline() {
5240+
let input = &[Level::ERROR
5241+
.primary_title("main diagnostic message")
5242+
.id("test-diagnostic")
5243+
.element(
5244+
Snippet::source("dog\nelephant\nfinch\n")
5245+
.path("spacey-animals")
5246+
.fold(false)
5247+
.line_start(7)
5248+
.annotation(AnnotationKind::Primary.span(4..12)),
5249+
)];
5250+
let expected_ascii = str![[r#"
5251+
error[test-diagnostic]: main diagnostic message
5252+
--> spacey-animals:8:1
5253+
|
5254+
7 | dog
5255+
8 | elephant
5256+
| ^^^^^^^^
5257+
9 | finch
5258+
|
5259+
"#]];
5260+
5261+
let renderer = Renderer::plain();
5262+
assert_data_eq!(renderer.render(input), expected_ascii);
5263+
5264+
let expected_unicode = str![[r#"
5265+
error[test-diagnostic]: main diagnostic message
5266+
╭▸ spacey-animals:8:1
5267+
5268+
7 │ dog
5269+
8 │ elephant
5270+
│ ━━━━━━━━
5271+
9 │ finch
5272+
╰╴
5273+
"#]];
5274+
let renderer = renderer.decor_style(DecorStyle::Unicode);
5275+
assert_data_eq!(renderer.render(input), expected_unicode);
5276+
}

0 commit comments

Comments
 (0)