From 984ac100a4072b7f64898913afa70e71c65c79da Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 22 Dec 2017 12:00:24 +0900 Subject: [PATCH] Fix indent width bug when recovering comments Using last_line_width() ignores the width of tab character ('\t'). --- src/missed_spans.rs | 7 ++++--- tests/source/hard-tabs.rs | 11 +++++++++++ tests/target/hard-tabs.rs | 11 +++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 718ac6bfaab..dff6b94bd75 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -210,21 +210,22 @@ impl<'a> FmtVisitor<'a> { let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c)); - if fix_indent { + let comment_indent = if fix_indent { if let Some('{') = last_char { self.push_str("\n"); } let indent_str = self.block_indent.to_string(self.config); self.push_str(&indent_str); + self.block_indent } else { self.push_str(" "); - } + Indent::from_width(self.config, last_line_width(&self.buffer)) + }; let comment_width = ::std::cmp::min( self.config.comment_width(), self.config.max_width() - self.block_indent.width(), ); - let comment_indent = Indent::from_width(self.config, last_line_width(&self.buffer)); let comment_shape = Shape::legacy(comment_width, comment_indent); let comment_str = rewrite_comment(subslice, false, comment_shape, self.config) .unwrap_or_else(|| String::from(subslice)); diff --git a/tests/source/hard-tabs.rs b/tests/source/hard-tabs.rs index f598ad881d0..b8fd09f8792 100644 --- a/tests/source/hard-tabs.rs +++ b/tests/source/hard-tabs.rs @@ -72,3 +72,14 @@ fn generic(arg: T) -> &SomeType } }); } + +// #2296 +impl Foo { + // a comment + // on multiple lines + fn foo() { + // another comment + // on multiple lines + let x = true; + } +} diff --git a/tests/target/hard-tabs.rs b/tests/target/hard-tabs.rs index 72d366b1fa1..90be4d6b367 100644 --- a/tests/target/hard-tabs.rs +++ b/tests/target/hard-tabs.rs @@ -86,3 +86,14 @@ fn main() { false => (), }); } + +// #2296 +impl Foo { + // a comment + // on multiple lines + fn foo() { + // another comment + // on multiple lines + let x = true; + } +}