]> git.lizzy.rs Git - rust.git/blobdiff - src/comment.rs
0.99.7
[rust.git] / src / comment.rs
index 358c18e1b954d3ad8e803b83dfc378ad6438ab89..4ba95bda053867caaacee25a799a5fd2901f7790 100644 (file)
@@ -19,7 +19,7 @@
 use rewrite::RewriteContext;
 use shape::{Indent, Shape};
 use string::{rewrite_string, StringFormat};
-use utils::{count_newlines, first_line_width, last_line_width};
+use utils::{count_newlines, first_line_width, last_line_width, trim_left_preserve_layout};
 use {ErrorKind, FormattingError};
 
 fn is_custom_comment(comment: &str) -> bool {
@@ -332,12 +332,12 @@ fn consume_same_line_comments(
     let (first_group, rest) = orig.split_at(first_group_ending);
     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)?
+            trim_left_preserve_layout(first_group, shape.indent, config)?
         } else if !config.normalize_comments()
             && !config.wrap_comments()
             && !config.format_doc_comments()
         {
-            light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)?
+            light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)
         } else {
             rewrite_comment_inner(
                 first_group,
@@ -370,47 +370,6 @@ fn consume_same_line_comments(
     }
 }
 
-/// 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 {
@@ -439,7 +398,7 @@ fn new(attribute: &str) -> CodeBlockAttribute {
 /// Block that is formatted as an item.
 ///
 /// An item starts with either a star `*` or a dash `-`. Different level of indentation are
-/// handled.
+/// handled by shrinking the shape accordingly.
 struct ItemizedBlock {
     /// the number of whitespaces up to the item sigil
     indent: usize,
@@ -626,7 +585,7 @@ fn handle_line(
 
         if let Some(ref ib) = self.item_block {
             if ib.in_block(&line) {
-                self.item_block_buffer.push_str(&line);
+                self.item_block_buffer.push_str(line.trim_start());
                 self.item_block_buffer.push('\n');
                 return false;
             }
@@ -661,7 +620,7 @@ fn handle_line(
                         let mut config = self.fmt.config.clone();
                         config.set().format_doc_comments(false);
                         match ::format_code_block(&self.code_block_buffer, &config) {
-                            Some(ref s) => trim_custom_comment_prefix(s),
+                            Some(ref s) => trim_custom_comment_prefix(&s.snippet),
                             None => trim_custom_comment_prefix(&self.code_block_buffer),
                         }
                     }
@@ -912,7 +871,7 @@ fn light_rewrite_comment(
     offset: Indent,
     config: &Config,
     is_doc_comment: bool,
-) -> Option<String> {
+) -> String {
     let lines: Vec<&str> = orig
         .lines()
         .map(|l| {
@@ -933,7 +892,7 @@ fn light_rewrite_comment(
             trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment)
         })
         .collect();
-    Some(lines.join(&format!("\n{}", offset.to_string(config))))
+    lines.join(&format!("\n{}", offset.to_string(config)))
 }
 
 /// Trims comment characters and possibly a single space from the left of a string.