about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNick Cameron <nrc@ncameron.org>2018-09-19 11:05:27 +1200
committerGitHub <noreply@github.com>2018-09-19 11:05:27 +1200
commit1739041f0372b336ca727585fb7c2ecda07e889c (patch)
tree395f0e045b691dbbc727d3c97a64aa0b482ddb6b /src
parent2267c2cddcdd70b7bf55d70d4771355637c2077f (diff)
parent0c73b9414bdfdc1f8d4d910bf2276e5ea9d2e9de (diff)
Merge pull request #3002 from lqd/normalize-doc-attributes
normalize_doc_attributes option: convert doc attributes to comments
Diffstat (limited to 'src')
-rw-r--r--src/attr.rs37
-rw-r--r--src/config/mod.rs1
2 files changed, 30 insertions, 8 deletions
diff --git a/src/attr.rs b/src/attr.rs
index fc29c69b498..f8f6cd1c376 100644
--- a/src/attr.rs
+++ b/src/attr.rs
@@ -10,7 +10,7 @@
 
 //! Format attributes and meta items.
 
-use comment::{contains_comment, rewrite_doc_comment};
+use comment::{contains_comment, rewrite_doc_comment, CommentStyle};
 use config::lists::*;
 use config::IndentStyle;
 use expr::rewrite_literal;
@@ -350,13 +350,34 @@ impl Rewrite for ast::Attribute {
             if contains_comment(snippet) {
                 return Some(snippet.to_owned());
             }
-            // 1 = `[`
-            let shape = shape.offset_left(prefix.len() + 1)?;
-            Some(
-                self.meta()
-                    .and_then(|meta| meta.rewrite(context, shape))
-                    .map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)),
-            )
+
+            if let Some(ref meta) = self.meta() {
+                // This attribute is possibly a doc attribute needing normalization to a doc comment
+                if context.config.normalize_doc_attributes() && meta.check_name("doc") {
+                    if let Some(ref literal) = meta.value_str() {
+                        let comment_style = match self.style {
+                            ast::AttrStyle::Inner => CommentStyle::Doc,
+                            ast::AttrStyle::Outer => CommentStyle::TripleSlash,
+                        };
+
+                        let doc_comment = format!("{}{}", comment_style.opener(), literal);
+                        return rewrite_doc_comment(
+                            &doc_comment,
+                            shape.comment(context.config),
+                            context.config,
+                        );
+                    }
+                }
+
+                // 1 = `[`
+                let shape = shape.offset_left(prefix.len() + 1)?;
+                Some(
+                    meta.rewrite(context, shape)
+                        .map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)),
+                )
+            } else {
+                Some(snippet.to_owned())
+            }
         }
     }
 }
diff --git a/src/config/mod.rs b/src/config/mod.rs
index f240c7b13c6..52401a18d92 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -49,6 +49,7 @@ create_config! {
     comment_width: usize, 80, false,
         "Maximum length of comments. No effect unless wrap_comments = true";
     normalize_comments: bool, false, false, "Convert /* */ comments to // comments where possible";
+    normalize_doc_attributes: bool, false, false, "Normalize doc attributes as doc comments";
     license_template_path: String, String::default(), false,
         "Beginning of file must match license template";
     format_strings: bool, false, false, "Format string literals where necessary";