Skip to content

Commit b8266a1

Browse files
author
Naseschwarz
committed
Convert error line buffer to space indentation
The type of indentation is not important for error reporting. However, keeping the indentation as tabs has two issues: 1. rustfmt keeps track of widths, not character offsets. For space indentation, these two are the same. For tab indentation, this leads to issues like [1]. 2. annotate-snippet-rs always format leading tabs as four spaces. Thus, formatter error reporting is not faithful and ranges will not be marked correctly (or need another transformation). Replacing tabs with spaces for error reporting as early as possible solves these issues. [1] rust-lang#6442
1 parent c6c8159 commit b8266a1

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/formatting.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,22 @@ impl<'a> FormatLines<'a> {
593593
}
594594
}
595595

596+
fn line_buffer_with_leading_spaces(&self) -> String {
597+
if self.config.hard_tabs() {
598+
let leading_tabs = self.line_buffer.chars().take_while(|&c| c == '\t').count();
599+
" ".repeat(self.config.tab_spaces() * leading_tabs) + &self.line_buffer[leading_tabs..]
600+
} else {
601+
self.line_buffer.clone()
602+
}
603+
}
604+
596605
fn push_err(&mut self, kind: ErrorKind, is_comment: bool, is_string: bool) {
597606
self.errors.push(FormattingError {
598607
line: self.cur_line,
599608
kind,
600609
is_comment,
601610
is_string,
602-
line_buffer: self.line_buffer.clone(),
611+
line_buffer: self.line_buffer_with_leading_spaces(),
603612
});
604613
}
605614

0 commit comments

Comments
 (0)