From 1aa8cdca65fb32ac6bc60a2bddf0d4b83d7a74d1 Mon Sep 17 00:00:00 2001 From: Nam Jeonghyun Date: Mon, 19 Aug 2019 23:39:29 +0900 Subject: [PATCH] Fix incorrect align when underlining lines with tab... ...character Fixes #407 --- pest/src/error.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/pest/src/error.rs b/pest/src/error.rs index 81cedd1a..d6b76a82 100644 --- a/pest/src/error.rs +++ b/pest/src/error.rs @@ -287,9 +287,13 @@ impl Error { _ => None, }; let offset = start - 1; + let line_chars = self.line.chars(); - for _ in 0..offset { - underline.push(' '); + for c in line_chars.take(offset) { + match c { + '\t' => underline.push('\t'), + _ => underline.push(' '), + } } if let Some(end) = end { @@ -773,4 +777,31 @@ mod tests { .join("\n") ); } + + #[test] + fn underline_with_tabs() { + let input = "a\txbc"; + let pos = position::Position::new(input, 2).unwrap(); + let error: Error = Error::new_from_pos( + ErrorVariant::ParsingError { + positives: vec![1, 2, 3], + negatives: vec![4, 5, 6], + }, + pos, + ) + .with_path("file.rs"); + + assert_eq!( + format!("{}", error), + vec![ + " --> file.rs:1:3", + " |", + "1 | a xbc", + " | ^---", + " |", + " = unexpected 4, 5, or 6; expected 1, 2, or 3", + ] + .join("\n") + ); + } }