Skip to content

Commit abee1df

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] #6442
1 parent ee329d3 commit abee1df

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/formatting.rs

+10-1
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

tests/source/issue-6442.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-hard_tabs: true
2+
3+
fn main() {
4+
// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
5+
}

tests/target/issue-6442.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-hard_tabs: true
2+
3+
fn main() {
4+
// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
5+
}

0 commit comments

Comments
 (0)