]> git.lizzy.rs Git - rust.git/commitdiff
Handle attributes on modules (#968)
authorNick Cameron <nrc@ncameron.org>
Mon, 2 May 2016 08:54:25 +0000 (20:54 +1200)
committerMarcus Klaas de Vries <mail@marcusklaas.nl>
Mon, 2 May 2016 08:54:25 +0000 (10:54 +0200)
* 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
src/visitor.rs
tests/target/attrib.rs
tests/target/structs.rs

index 991faa3c9c8ef1be898f7abd3e25088ee5989e68..83ea21b91e8d01761bed80f530673b8c4d571673 100644 (file)
@@ -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| {
index 8fba02de2d1d4a05b43ce449704894464c23af73..260303f32ae65af0fd1e1dcd50e498cb4041bf0c 100644 (file)
@@ -177,14 +177,19 @@ fn visit_fn(&mut self,
     }
 
     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 @@ fn rewrite(&self, context: &RewriteContext, _: usize, offset: Indent) -> Option<
         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 @@ fn rewrite(&self, context: &RewriteContext, _: usize, offset: Indent) -> Option<
                 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 @@ fn rewrite(&self, context: &RewriteContext, _: usize, offset: Indent) -> Option<
                 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);
 
index 9317a83706571ad4f12fb86a9afea19ac6cc8dd3..b861b971d9b77b2bbaf88711fbb5ef237b24b1fb 100644 (file)
@@ -33,8 +33,8 @@ fn f3(self) -> Dog {}
     #[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 {}
 }
index 428258c4f760215c7900906d1cfcea72c3ea7d05..0af68686318f937c7ec2409ceae171a754cab4a9 100644 (file)
@@ -96,7 +96,8 @@ pub struct State<F: FnMut()> {
 }
 
 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,
 }