]> git.lizzy.rs Git - rust.git/blobdiff - src/comment.rs
handle lines prefixed with a # inside code blocks
[rust.git] / src / comment.rs
index 41301527653132279f745f2c1eec9eba6e23a085..c946cc6e1c013e0a534b2408186a8cf84e7ca8ce 100644 (file)
@@ -333,7 +333,10 @@ fn consume_same_line_comments(
     let rewritten_first_group =
         if !config.normalize_comments() && has_bare_lines && style.is_block_comment() {
             light_rewrite_block_comment_with_bare_lines(first_group, shape, config)?
-        } else if !config.normalize_comments() && !config.wrap_comments() {
+        } else if !config.normalize_comments()
+            && !config.wrap_comments()
+            && !config.format_doc_comments()
+        {
             light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)?
         } else {
             rewrite_comment_inner(
@@ -593,7 +596,7 @@ fn rewrite_comment_inner(
                     _ if code_block_buffer.is_empty() => String::new(),
                     _ => {
                         let mut config = config.clone();
-                        config.set().wrap_comments(false);
+                        config.set().format_doc_comments(false);
                         match ::format_code_block(&code_block_buffer, &config) {
                             Some(ref s) => trim_custom_comment_prefix(s),
                             None => trim_custom_comment_prefix(&code_block_buffer),
@@ -749,9 +752,16 @@ fn trim_custom_comment_prefix(s: &str) -> String {
         .map(|line| {
             let left_trimmed = line.trim_left();
             if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) {
-                left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
+                let orig = left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX);
+                // due to comment wrapping, a line that was originaly behind `#` is split over
+                // multiple lines, which needs then to be prefixed with a `#`
+                if !orig.trim_left().starts_with("# ") {
+                    format!("# {}", orig)
+                } else {
+                    orig.to_string()
+                }
             } else {
-                line
+                line.to_string()
             }
         })
         .collect::<Vec<_>>()
@@ -1622,7 +1632,7 @@ fn comment_code_slices_three() {
 
     #[test]
     #[rustfmt::skip]
-    fn format_comments() {
+    fn format_doc_comments() {
         let mut wrap_normalize_config: ::config::Config = Default::default();
         wrap_normalize_config.set().wrap_comments(true);
         wrap_normalize_config.set().normalize_comments(true);