Compare code block line indentation with config whitespace (#4166)

Previously the indetation of a line was compared with the configured
number of spaces per tab, which could cause lines that were formatted
with hard tabs not to be recognized as indented ("\t".len() < "    ".len()).

Closes #4152
This commit is contained in:
hafiz 2020-05-10 06:54:46 -05:00 committed by Caleb Cartwright
parent e7ecdc1664
commit c77c6a405d
2 changed files with 20 additions and 2 deletions

View File

@ -371,6 +371,7 @@ fn enclose_in_main_block(s: &str, config: &Config) -> String {
.rfind('}')
.unwrap_or_else(|| formatted.snippet.len());
let mut is_indented = true;
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
if !is_first {
result.push('\n');
@ -385,9 +386,8 @@ fn enclose_in_main_block(s: &str, config: &Config) -> String {
// are too long, or we have failed to format code block. We will be
// conservative and just return `None` in this case.
return None;
} else if line.len() > config.tab_spaces() {
} else if line.len() > indent_str.len() {
// Make sure that the line has leading whitespaces.
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
if line.starts_with(indent_str.as_ref()) {
let offset = if config.hard_tabs() {
1

View File

@ -0,0 +1,18 @@
// rustfmt-hard_tabs: true
macro_rules! bit {
($bool:expr) => {
if $bool {
1;
1
} else {
0;
0
}
};
}
macro_rules! add_one {
($vec:expr) => {{
$vec.push(1);
}};
}