Skip to content

Commit

Permalink
Fix incorrect align when underlining lines with tab...
Browse files Browse the repository at this point in the history
...character

Fixes #407
  • Loading branch information
Nam Jeonghyun committed Aug 19, 2019
1 parent 8d47f90 commit 1aa8cdc
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions pest/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,13 @@ impl<R: RuleType> Error<R> {
_ => 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 {
Expand Down Expand Up @@ -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<u32> = 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")
);
}
}

0 comments on commit 1aa8cdc

Please sign in to comment.