- line
- })
- .map(|s| left_trim_comment_line(s, &style))
- .map(|(line, has_leading_whitespace)| {
- if orig.starts_with("/*") && line_breaks == 0 {
- (
- line.trim_left(),
- has_leading_whitespace || config.normalize_comments(),
- )
- } else {
- (line, has_leading_whitespace || config.normalize_comments())
- }
- });
+ /// 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.
+ /// If it is, then it is added to the internal lines list.
+ fn add_line(&mut self, line: &str) -> bool {
+ if !ItemizedBlock::is_itemized_line(line)
+ && self.indent <= line.chars().take_while(|c| c.is_whitespace()).count()
+ {
+ self.lines.push(line.to_string());
+ return true;
+ }
+ false
+ }
+
+ /// Returns the block as a string, with each line trimmed at the start.
+ fn trimmed_block_as_string(&self) -> String {
+ self.lines
+ .iter()
+ .map(|line| format!("{} ", line.trim_start()))
+ .collect::<String>()
+ }
+
+ /// Returns the block as a string under its original form.
+ fn original_block_as_string(&self) -> String {
+ self.lines.join("\n")
+ }
+}
+
+struct CommentRewrite<'a> {
+ result: String,
+ code_block_buffer: String,
+ is_prev_line_multi_line: bool,
+ code_block_attr: Option<CodeBlockAttribute>,
+ item_block: Option<ItemizedBlock>,
+ comment_line_separator: String,
+ indent_str: String,
+ max_width: usize,
+ fmt_indent: Indent,
+ fmt: StringFormat<'a>,
+
+ opener: String,
+ closer: String,
+ line_start: String,
+}
+
+impl<'a> CommentRewrite<'a> {
+ fn new(
+ orig: &'a str,
+ block_style: bool,
+ shape: Shape,
+ config: &'a Config,
+ ) -> CommentRewrite<'a> {
+ let (opener, closer, line_start) = if block_style {
+ CommentStyle::SingleBullet.to_str_tuplet()
+ } else {
+ comment_style(orig, config.normalize_comments()).to_str_tuplet()
+ };