diagnostic formatting: respect tab characters

This commit is contained in:
Michael Sippel 2024-08-14 00:27:21 +02:00
parent 4e9a4d1204
commit cf2ea4df78
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -28,7 +28,6 @@ pub fn print_diagnostic(
println!("\n{}:", path.green()); println!("\n{}:", path.green());
for (i, l) in lines.enumerate() { for (i, l) in lines.enumerate() {
line_region.end += l.chars().count(); line_region.end += l.chars().count();
last_lines.push((i+1, l.clone())); 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_begin = region.begin - line_region.begin;
let column_end = region.end - 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 // display the source line
for (j,ll) in last_lines.iter() { for (j,ll) in last_lines.iter() {
print!("{}\t{}{}", print!("{}\t{}{}",
format!("{}",j).to_string().bright_black(), format!("{}",j).to_string().bright_black(),
"|".bright_black().bold(), "|".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()); print!("\t{}", "|".bright_magenta());