diagnostic formatting: respect tab characters

This commit is contained in:
Michael Sippel 2024-08-14 00:27:21 +02:00
parent 184c8f3d50
commit 6c6f283352
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -28,7 +28,6 @@ pub fn print_diagnostic(
println!("\n{}:", path.green());
for (i, l) in lines.enumerate() {
line_region.end += l.chars().count();
last_lines.push((i+1, l.clone()));
@ -44,12 +43,27 @@ pub fn print_diagnostic(
let column_begin = region.begin - line_region.begin;
let column_end = region.end - line_region.begin;
let tab_width = 4;
let column_begin_c = column_begin + (tab_width-1)*l.chars().take(column_begin).filter(|&c|c=='\t').count();
let column_end_c = column_end + (tab_width-1)*l.chars().filter(|&c|c=='\t').count();
// display the source line
for (j,ll) in last_lines.iter() {
print!("{}\t{}{}",
format!("{}",j).to_string().bright_black(),
"|".bright_black().bold(),
ll.bright_white());
ll.chars().map(|c| {
if c == '\t' {
std::iter::repeat(' ').take(tab_width)
} else {
std::iter::repeat(c).take(1)
}
})
.flatten()
.collect::<String>()
.bright_white());
}
print!("\t{}", "|".bright_magenta());