From c696dcf8dae0c34161d467b7fbc32b0d7b01bb61 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 2 May 2016 20:54:25 +1200 Subject: [PATCH] Handle attributes on modules (#968) * Handle attributes (including doc comments) on inline modules Closes #22 Closes #684 * Tweak the rules for changing indentation in comments (to do it less often). --- src/comment.rs | 10 +++++----- src/visitor.rs | 31 ++++++++++++++++++++++--------- tests/target/attrib.rs | 4 ++-- tests/target/structs.rs | 3 ++- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/comment.rs b/src/comment.rs index 991faa3c9c8..83ea21b91e8 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -82,7 +82,7 @@ pub fn rewrite_comment(orig: &str, }) .map(left_trim_comment_line) .map(|line| { - if line_breaks == 0 { + if orig.starts_with("/*") && line_breaks == 0 { line.trim_left() } else { line @@ -529,10 +529,10 @@ pub fn recover_comment_removed(new: String, /// Return true if the two strings of code have the same payload of comments. /// The payload of comments is everything in the string except: -/// - actual code (not comments) -/// - comment start/end marks -/// - whitespace -/// - '*' at the beginning of lines in block comments +/// - actual code (not comments) +/// - comment start/end marks +/// - whitespace +/// - '*' at the beginning of lines in block comments fn changed_comment_content(orig: &str, new: &str) -> bool { // Cannot write this as a fn since we cannot return types containing closures let code_comment_content = |code| { diff --git a/src/visitor.rs b/src/visitor.rs index 8fba02de2d1..260303f32ae 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -177,14 +177,19 @@ impl<'a> FmtVisitor<'a> { } fn visit_item(&mut self, item: &ast::Item) { - // Don't look at attributes for modules (except for rustfmt_skip). - // We want to avoid looking at attributes in another file, which the AST - // doesn't distinguish. - // FIXME This is overly conservative and means we miss attributes on - // inline modules. + // Only look at attributes for modules (except for rustfmt_skip) if the + // module is inline. We want to avoid looking at attributes in another + // file, which the AST doesn't distinguish. match item.node { - ast::ItemKind::Mod(_) => { - if utils::contains_skip(&item.attrs) { + ast::ItemKind::Mod(ref m) => { + let outer_file = self.codemap.lookup_char_pos(item.span.lo).file; + let inner_file = self.codemap.lookup_char_pos(m.inner.lo).file; + if outer_file.name == inner_file.name { + if self.visit_attrs(&item.attrs) { + self.push_rewrite(item.span, None); + return; + } + } else if utils::contains_skip(&item.attrs) { return; } } @@ -551,7 +556,7 @@ impl<'a> Rewrite for [ast::Attribute] { let indent = offset.to_string(context.config); for (i, a) in self.iter().enumerate() { - let a_str = context.snippet(a.span); + let mut a_str = context.snippet(a.span); // Write comments and blank lines between attributes. if i > 0 { @@ -564,7 +569,7 @@ impl<'a> Rewrite for [ast::Attribute] { if !comment.is_empty() { let comment = try_opt!(rewrite_comment(comment, false, - context.config.max_width - + context.config.ideal_width - offset.width(), offset, context.config)); @@ -577,6 +582,14 @@ impl<'a> Rewrite for [ast::Attribute] { result.push_str(&indent); } + if a_str.starts_with("//") { + a_str = try_opt!(rewrite_comment(&a_str, + false, + context.config.ideal_width - offset.width(), + offset, + context.config)); + } + // Write the attribute itself. result.push_str(&a_str); diff --git a/tests/target/attrib.rs b/tests/target/attrib.rs index 9317a837065..b861b971d9b 100644 --- a/tests/target/attrib.rs +++ b/tests/target/attrib.rs @@ -33,8 +33,8 @@ impl Bar { #[attrib1] /// Blah blah bing. #[attrib2] - // Another comment that needs rewrite because it's tooooooooooooooooooooooooooooooo - // loooooooooooong. + // Another comment that needs rewrite because it's + // tooooooooooooooooooooooooooooooo loooooooooooong. /// Blah blah bing. fn f4(self) -> Cat {} } diff --git a/tests/target/structs.rs b/tests/target/structs.rs index 428258c4f76..0af68686318 100644 --- a/tests/target/structs.rs +++ b/tests/target/structs.rs @@ -96,7 +96,8 @@ pub struct State { } struct Palette { - /// A map of indizes in the palette to a count of pixels in approximately that color + /// A map of indizes in the palette to a count of pixels in approximately + /// that color foo: i32, }