]> git.lizzy.rs Git - rust.git/blobdiff - src/comment.rs
handle lines prefixed with a # inside code blocks
[rust.git] / src / comment.rs
index e79b9011cbf44cae3aa437b20d80e2909bac5bdf..c946cc6e1c013e0a534b2408186a8cf84e7ca8ce 100644 (file)
@@ -52,6 +52,28 @@ fn custom_opener(s: &str) -> &str {
 }
 
 impl<'a> CommentStyle<'a> {
+    /// Returns true if the commenting style covers a line only.
+    pub fn is_line_comment(&self) -> bool {
+        match *self {
+            CommentStyle::DoubleSlash
+            | CommentStyle::TripleSlash
+            | CommentStyle::Doc
+            | CommentStyle::Custom(_) => true,
+            _ => false,
+        }
+    }
+
+    /// Returns true if the commenting style can span over multiple lines.
+    pub fn is_block_comment(&self) -> bool {
+        match *self {
+            CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
+                true
+            }
+            _ => false,
+        }
+    }
+
+    /// Returns true if the commenting style is for documentation.
     pub fn is_doc_comment(&self) -> bool {
         match *self {
             CommentStyle::TripleSlash | CommentStyle::Doc => true,
@@ -213,7 +235,7 @@ pub fn combine_strs_with_missing_comments(
 }
 
 pub fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
-    _rewrite_comment(orig, false, shape, config, true)
+    identify_comment(orig, false, shape, config, true)
 }
 
 pub fn rewrite_comment(
@@ -222,32 +244,7 @@ pub fn rewrite_comment(
     shape: Shape,
     config: &Config,
 ) -> Option<String> {
-    _rewrite_comment(orig, block_style, shape, config, false)
-}
-
-fn _rewrite_comment(
-    orig: &str,
-    block_style: bool,
-    shape: Shape,
-    config: &Config,
-    is_doc_comment: bool,
-) -> Option<String> {
-    // If there are lines without a starting sigil, we won't format them correctly
-    // so in that case we won't even re-align (if !config.normalize_comments()) and
-    // we should stop now.
-    let num_bare_lines = orig
-        .lines()
-        .map(|line| line.trim())
-        .filter(|l| !(l.starts_with('*') || l.starts_with("//") || l.starts_with("/*")))
-        .count();
-    if num_bare_lines > 0 && !config.normalize_comments() {
-        return Some(orig.to_owned());
-    }
-    if !config.normalize_comments() && !config.wrap_comments() {
-        return light_rewrite_comment(orig, shape.indent, config, is_doc_comment);
-    }
-
-    identify_comment(orig, block_style, shape, config, is_doc_comment)
+    identify_comment(orig, block_style, shape, config, false)
 }
 
 fn identify_comment(
@@ -258,8 +255,8 @@ fn identify_comment(
     is_doc_comment: bool,
 ) -> Option<String> {
     let style = comment_style(orig, false);
-    let mut first_group_ending = 0;
 
+    // Computes the len of line taking into account a newline if the line is part of a paragraph.
     fn compute_len(orig: &str, line: &str) -> usize {
         if orig.len() > line.len() {
             if orig.as_bytes()[line.len()] == b'\r' {
@@ -272,59 +269,222 @@ fn compute_len(orig: &str, line: &str) -> usize {
         }
     }
 
-    match style {
+    // Get the first group of line comments having the same commenting style.
+    //
+    // Returns a tuple with:
+    // - a boolean indicating if there is a blank line
+    // - a number indicating the size of the first group of comments
+    fn consume_same_line_comments(
+        style: CommentStyle,
+        orig: &str,
+        line_start: &str,
+    ) -> (bool, usize) {
+        let mut first_group_ending = 0;
+        let mut hbl = false;
+
+        for line in orig.lines() {
+            let trimmed_line = line.trim_left();
+            if trimmed_line.is_empty() {
+                hbl = true;
+                break;
+            } else if trimmed_line.starts_with(line_start)
+                || comment_style(trimmed_line, false) == style
+            {
+                first_group_ending += compute_len(&orig[first_group_ending..], line);
+            } else {
+                break;
+            }
+        }
+        (hbl, first_group_ending)
+    }
+
+    let (has_bare_lines, first_group_ending) = match style {
         CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
             let line_start = style.line_start().trim_left();
-            for line in orig.lines() {
-                if line.trim_left().starts_with(line_start) || comment_style(line, false) == style {
-                    first_group_ending += compute_len(&orig[first_group_ending..], line);
-                } else {
-                    break;
-                }
-            }
+            consume_same_line_comments(style, orig, line_start)
         }
         CommentStyle::Custom(opener) => {
             let trimmed_opener = opener.trim_right();
-            for line in orig.lines() {
-                if line.trim_left().starts_with(trimmed_opener) {
-                    first_group_ending += compute_len(&orig[first_group_ending..], line);
-                } else {
-                    break;
-                }
-            }
+            consume_same_line_comments(style, orig, trimmed_opener)
         }
         // for a block comment, search for the closing symbol
         CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
             let closer = style.closer().trim_left();
+            let mut closing_symbol_offset = 0;
+            let mut hbl = false;
             for line in orig.lines() {
-                first_group_ending += compute_len(&orig[first_group_ending..], line);
-                if line.trim_left().ends_with(closer) {
+                closing_symbol_offset += compute_len(&orig[closing_symbol_offset..], line);
+                let trimmed_line = line.trim_left();
+                if !trimmed_line.starts_with('*')
+                    && !trimmed_line.starts_with("//")
+                    && !trimmed_line.starts_with("/*")
+                {
+                    hbl = true;
+                }
+                if trimmed_line.ends_with(closer) {
                     break;
                 }
             }
+            (hbl, closing_symbol_offset)
         }
-    }
+    };
 
     let (first_group, rest) = orig.split_at(first_group_ending);
-    let first_group_str = rewrite_comment_inner(
-        first_group,
-        block_style,
-        style,
-        shape,
-        config,
-        is_doc_comment || style.is_doc_comment(),
-    )?;
+    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()
+            && !config.format_doc_comments()
+        {
+            light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)?
+        } else {
+            rewrite_comment_inner(
+                first_group,
+                block_style,
+                style,
+                shape,
+                config,
+                is_doc_comment || style.is_doc_comment(),
+            )?
+        };
     if rest.is_empty() {
-        Some(first_group_str)
+        Some(rewritten_first_group)
     } else {
-        identify_comment(rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
-            format!(
-                "{}\n{}{}",
-                first_group_str,
-                shape.indent.to_string(config),
-                rest_str
-            )
+        identify_comment(rest.trim_left(), block_style, shape, config, is_doc_comment).map(
+            |rest_str| {
+                format!(
+                    "{}\n{}{}{}",
+                    rewritten_first_group,
+                    // insert back the blank line
+                    if has_bare_lines && style.is_line_comment() {
+                        "\n"
+                    } else {
+                        ""
+                    },
+                    shape.indent.to_string(config),
+                    rest_str
+                )
+            },
+        )
+    }
+}
+
+/// Trims a minimum of leading whitespaces so that the content layout is kept and aligns to indent.
+fn light_rewrite_block_comment_with_bare_lines(
+    orig: &str,
+    shape: Shape,
+    config: &Config,
+) -> Option<String> {
+    let prefix_whitespace_min = orig
+        .lines()
+        // skip the line with the starting sigil since the leading whitespace is removed
+        // otherwise, the minimum would always be zero
+        .skip(1)
+        .filter(|line| !line.is_empty())
+        .map(|line| {
+            let mut width = 0;
+            for c in line.chars() {
+                match c {
+                    ' ' => width += 1,
+                    '\t' => width += config.tab_spaces(),
+                    _ => break,
+                }
+            }
+            width
+        })
+        .min()?;
+
+    let indent_str = shape.indent.to_string(config);
+    let mut lines = orig.lines();
+    let first_line = lines.next()?;
+    let rest = lines
+        .map(|line| {
+            if line.is_empty() {
+                line
+            } else {
+                &line[prefix_whitespace_min..]
+            }
         })
+        .collect::<Vec<&str>>()
+        .join(&format!("\n{}", indent_str));
+    Some(format!("{}\n{}{}", first_line, indent_str, rest))
+}
+
+/// Attributes for code blocks in rustdoc.
+/// See https://doc.rust-lang.org/rustdoc/print.html#attributes
+enum CodeBlockAttribute {
+    Rust,
+    Ignore,
+    Text,
+    ShouldPanic,
+    NoRun,
+    CompileFail,
+}
+
+impl CodeBlockAttribute {
+    fn new(attribute: &str) -> CodeBlockAttribute {
+        match attribute {
+            "rust" | "" => CodeBlockAttribute::Rust,
+            "ignore" => CodeBlockAttribute::Ignore,
+            "text" => CodeBlockAttribute::Text,
+            "should_panic" => CodeBlockAttribute::ShouldPanic,
+            "no_run" => CodeBlockAttribute::NoRun,
+            "compile_fail" => CodeBlockAttribute::CompileFail,
+            _ => CodeBlockAttribute::Text,
+        }
+    }
+}
+
+/// Block that is formatted as an item.
+///
+/// An item starts with either a star `*` or a dash `-`. Different level of indentation are
+/// handled.
+struct ItemizedBlock {
+    /// the number of whitespaces up to the item sigil
+    indent: usize,
+    /// the string that marks the start of an item
+    opener: String,
+    /// sequence of whitespaces to prefix new lines that are part of the item
+    line_start: String,
+}
+
+impl ItemizedBlock {
+    /// Returns true if the line is formatted as an item
+    fn is_itemized_line(line: &str) -> bool {
+        let trimmed = line.trim_left();
+        trimmed.starts_with("* ") || trimmed.starts_with("- ")
+    }
+
+    /// Creates a new ItemizedBlock described with the given line.
+    /// The `is_itemized_line` needs to be called first.
+    fn new(line: &str) -> ItemizedBlock {
+        let space_to_sigil = line.chars().take_while(|c| c.is_whitespace()).count();
+        let indent = space_to_sigil + 2;
+        ItemizedBlock {
+            indent,
+            opener: line[..indent].to_string(),
+            line_start: " ".repeat(indent),
+        }
+    }
+
+    /// Returns a `StringFormat` used for formatting the content of an item
+    fn create_string_format<'a>(&'a self, fmt: &'a StringFormat) -> StringFormat<'a> {
+        StringFormat {
+            opener: "",
+            closer: "",
+            line_start: "",
+            line_end: "",
+            shape: Shape::legacy(fmt.shape.width.saturating_sub(self.indent), Indent::empty()),
+            trim_end: true,
+            config: fmt.config,
+        }
+    }
+
+    /// Returns true if the line is part of the current itemized block
+    fn in_block(&self, line: &str) -> bool {
+        !ItemizedBlock::is_itemized_line(line)
+            && self.indent <= line.chars().take_while(|c| c.is_whitespace()).count()
     }
 }
 
@@ -387,16 +547,18 @@ fn rewrite_comment_inner(
     result.push_str(opener);
     let mut code_block_buffer = String::with_capacity(128);
     let mut is_prev_line_multi_line = false;
-    let mut inside_code_block = false;
+    let mut code_block_attr = None;
+    let mut item_block_buffer = String::with_capacity(128);
+    let mut item_block: Option<ItemizedBlock> = None;
     let comment_line_separator = format!("{}{}", indent_str, line_start);
-    let join_code_block_with_comment_line_separator = |s: &str| {
+    let join_block = |s: &str, sep: &str| {
         let mut result = String::with_capacity(s.len() + 128);
         let mut iter = s.lines().peekable();
         while let Some(line) = iter.next() {
             result.push_str(line);
             result.push_str(match iter.peek() {
-                Some(next_line) if next_line.is_empty() => comment_line_separator.trim_right(),
-                Some(..) => &comment_line_separator,
+                Some(next_line) if next_line.is_empty() => sep.trim_right(),
+                Some(..) => &sep,
                 None => "",
             });
         }
@@ -406,61 +568,88 @@ fn rewrite_comment_inner(
     for (i, (line, has_leading_whitespace)) in lines.enumerate() {
         let is_last = i == count_newlines(orig);
 
-        if inside_code_block {
+        if let Some(ref ib) = item_block {
+            if ib.in_block(&line) {
+                item_block_buffer.push_str(&line);
+                item_block_buffer.push('\n');
+                continue;
+            }
+            is_prev_line_multi_line = false;
+            fmt.shape = Shape::legacy(max_chars, fmt_indent);
+            let item_fmt = ib.create_string_format(&fmt);
+            result.push_str(&comment_line_separator);
+            result.push_str(&ib.opener);
+            match rewrite_string(&item_block_buffer.replace("\n", " "), &item_fmt) {
+                Some(s) => result.push_str(&join_block(
+                    &s,
+                    &format!("{}{}", &comment_line_separator, ib.line_start),
+                )),
+                None => result.push_str(&join_block(&item_block_buffer, &comment_line_separator)),
+            };
+            item_block_buffer.clear();
+        } else if let Some(ref attr) = code_block_attr {
             if line.starts_with("```") {
-                inside_code_block = false;
-                result.push_str(&comment_line_separator);
-                let code_block = {
-                    let mut config = config.clone();
-                    config.set().wrap_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),
+                let code_block = match attr {
+                    CodeBlockAttribute::Ignore | CodeBlockAttribute::Text => {
+                        trim_custom_comment_prefix(&code_block_buffer)
+                    }
+                    _ if code_block_buffer.is_empty() => String::new(),
+                    _ => {
+                        let mut config = config.clone();
+                        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),
+                        }
                     }
                 };
-                result.push_str(&join_code_block_with_comment_line_separator(&code_block));
+                if !code_block.is_empty() {
+                    result.push_str(&comment_line_separator);
+                    result.push_str(&join_block(&code_block, &comment_line_separator));
+                }
                 code_block_buffer.clear();
                 result.push_str(&comment_line_separator);
                 result.push_str(line);
+                code_block_attr = None;
             } else {
                 code_block_buffer.push_str(&hide_sharp_behind_comment(line));
                 code_block_buffer.push('\n');
-
-                if is_last {
-                    // There is an code block that is not properly enclosed by backticks.
-                    // We will leave them untouched.
-                    result.push_str(&comment_line_separator);
-                    result.push_str(&join_code_block_with_comment_line_separator(
-                        &trim_custom_comment_prefix(&code_block_buffer),
-                    ));
-                }
             }
+            continue;
+        }
 
+        code_block_attr = None;
+        item_block = None;
+        if line.starts_with("```") {
+            code_block_attr = Some(CodeBlockAttribute::new(&line[3..]))
+        } else if config.wrap_comments() && ItemizedBlock::is_itemized_line(&line) {
+            let ib = ItemizedBlock::new(&line);
+            item_block_buffer.push_str(&line[ib.indent..]);
+            item_block_buffer.push('\n');
+            item_block = Some(ib);
             continue;
-        } else {
-            inside_code_block = line.starts_with("```");
+        }
 
-            if result == opener {
-                let force_leading_whitespace = opener == "/* " && count_newlines(orig) == 0;
-                if !has_leading_whitespace && !force_leading_whitespace && result.ends_with(' ') {
-                    result.pop();
-                }
-                if line.is_empty() {
-                    continue;
-                }
-            } else if is_prev_line_multi_line && !line.is_empty() {
-                result.push(' ')
-            } else if is_last && line.is_empty() {
-                // trailing blank lines are unwanted
-                if !closer.is_empty() {
-                    result.push_str(&indent_str);
-                }
-                break;
-            } else {
-                result.push_str(&comment_line_separator);
-                if !has_leading_whitespace && result.ends_with(' ') {
-                    result.pop();
-                }
+        if result == opener {
+            let force_leading_whitespace = opener == "/* " && count_newlines(orig) == 0;
+            if !has_leading_whitespace && !force_leading_whitespace && result.ends_with(' ') {
+                result.pop();
+            }
+            if line.is_empty() {
+                continue;
+            }
+        } else if is_prev_line_multi_line && !line.is_empty() {
+            result.push(' ')
+        } else if is_last && line.is_empty() {
+            // trailing blank lines are unwanted
+            if !closer.is_empty() {
+                result.push_str(&indent_str);
+            }
+            break;
+        } else {
+            result.push_str(&comment_line_separator);
+            if !has_leading_whitespace && result.ends_with(' ') {
+                result.pop();
             }
         }
 
@@ -514,6 +703,30 @@ fn rewrite_comment_inner(
             is_prev_line_multi_line = false;
         }
     }
+    if !code_block_buffer.is_empty() {
+        // There is a code block that is not properly enclosed by backticks.
+        // We will leave them untouched.
+        result.push_str(&comment_line_separator);
+        result.push_str(&join_block(
+            &trim_custom_comment_prefix(&code_block_buffer),
+            &comment_line_separator,
+        ));
+    }
+    if !item_block_buffer.is_empty() {
+        // the last few lines are part of an itemized block
+        let ib = item_block.unwrap();
+        fmt.shape = Shape::legacy(max_chars, fmt_indent);
+        let item_fmt = ib.create_string_format(&fmt);
+        result.push_str(&comment_line_separator);
+        result.push_str(&ib.opener);
+        match rewrite_string(&item_block_buffer.replace("\n", " "), &item_fmt) {
+            Some(s) => result.push_str(&join_block(
+                &s,
+                &format!("{}{}", &comment_line_separator, ib.line_start),
+            )),
+            None => result.push_str(&join_block(&item_block_buffer, &comment_line_separator)),
+        };
+    }
 
     result.push_str(closer);
     if result.ends_with(opener) && opener.ends_with(' ') {
@@ -539,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<_>>()
@@ -1412,27 +1632,30 @@ fn comment_code_slices_three() {
 
     #[test]
     #[rustfmt::skip]
-    fn format_comments() {
-        let mut config: ::config::Config = Default::default();
-        config.set().wrap_comments(true);
-        config.set().normalize_comments(true);
+    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);
+
+        let mut wrap_config: ::config::Config = Default::default();
+        wrap_config.set().wrap_comments(true);
 
         let comment = rewrite_comment(" //test",
                                       true,
                                       Shape::legacy(100, Indent::new(0, 100)),
-                                      &config).unwrap();
+                                      &wrap_normalize_config).unwrap();
         assert_eq!("/* test */", comment);
 
         let comment = rewrite_comment("// comment on a",
                                       false,
                                       Shape::legacy(10, Indent::empty()),
-                                      &config).unwrap();
+                                      &wrap_normalize_config).unwrap();
         assert_eq!("// comment\n// on a", comment);
 
         let comment = rewrite_comment("//  A multi line comment\n             // between args.",
                                       false,
                                       Shape::legacy(60, Indent::new(0, 12)),
-                                      &config).unwrap();
+                                      &wrap_normalize_config).unwrap();
         assert_eq!("//  A multi line comment\n            // between args.", comment);
 
         let input = "// comment";
@@ -1441,14 +1664,55 @@ fn format_comments() {
         let comment = rewrite_comment(input,
                                       true,
                                       Shape::legacy(9, Indent::new(0, 69)),
-                                      &config).unwrap();
+                                      &wrap_normalize_config).unwrap();
         assert_eq!(expected, comment);
 
         let comment = rewrite_comment("/*   trimmed    */",
                                       true,
                                       Shape::legacy(100, Indent::new(0, 100)),
-                                      &config).unwrap();
+                                      &wrap_normalize_config).unwrap();
         assert_eq!("/* trimmed */", comment);
+
+        // check that different comment style are properly recognised
+        let comment = rewrite_comment(r#"/// test1
+                                         /// test2
+                                         /*
+                                          * test3
+                                          */"#,
+                                      false,
+                                      Shape::legacy(100, Indent::new(0, 0)),
+                                      &wrap_normalize_config).unwrap();
+        assert_eq!("/// test1\n/// test2\n// test3", comment);
+
+        // check that the blank line marks the end of a commented paragraph
+        let comment = rewrite_comment(r#"// test1
+
+                                         // test2"#,
+                                      false,
+                                      Shape::legacy(100, Indent::new(0, 0)),
+                                      &wrap_normalize_config).unwrap();
+        assert_eq!("// test1\n\n// test2", comment);
+
+        // check that the blank line marks the end of a custom-commented paragraph
+        let comment = rewrite_comment(r#"//@ test1
+
+                                         //@ test2"#,
+                                      false,
+                                      Shape::legacy(100, Indent::new(0, 0)),
+                                      &wrap_normalize_config).unwrap();
+        assert_eq!("//@ test1\n\n//@ test2", comment);
+
+        // check that bare lines are just indented but left unchanged otherwise
+        let comment = rewrite_comment(r#"// test1
+                                         /*
+                                           a bare line!
+
+                                                another bare line!
+                                          */"#,
+                                      false,
+                                      Shape::legacy(100, Indent::new(0, 0)),
+                                      &wrap_config).unwrap();
+        assert_eq!("// test1\n/*\n a bare line!\n\n      another bare line!\n*/", comment);
     }
 
     // This is probably intended to be a non-test fn, but it is not used. I'm