Skip to content
Closed

CLOSED #6804

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2754,6 +2754,44 @@ fn main() {
}
```

## `strict_line_comment_wrap`

Wrap long line comments (`//`) that exceed the maximum width, even if they are not markdown headers. This option works in combination with `wrap_comments`.

- **Default value**: `false`
- **Possible values**: `true`, `false`
- **Stable**: No (tracking issue: [#6726](https://github.com/rust-lang/rustfmt/issues/6726))

#### `false` (default):

Long line comments may remain unwrapped:

```rust
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
fn foo() {
println!("Lorem");
}
```
#### `true`:

Long line comments are automatically wrapped to fit within the configured maximum width, preserving indentation and // prefixes, without introducing blank lines between segments:

```rust
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
// tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
// veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
// ea commodo consequat.
fn main() {
println!("Hello, world!");
}
```

- Works with `wrap_comments = true`.
- Inline comments attached to code (`println!("Hello"); // comment`) are also wrapped correctly.
- Markdown header doc comments (`/// # Header`) are **not wrapped**.

See also: [`wrap_comments`](#wrap_comments).

## `struct_field_align_threshold`

The maximum diff of width between struct fields to be aligned with each other.
Expand Down
3 changes: 3 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ error_on_line_overflow = true
error_on_unformatted = true
style_edition = "2024"
overflow_delimited_expr = false
wrap_comments = true
strict_line_comment_wrap = true
comment_width = 40
43 changes: 43 additions & 0 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,49 @@ impl<'a> CommentRewrite<'a> {
&& !has_url(line)
&& !is_table_item(line);

if self.fmt.config.wrap_comments()
&& self.fmt.config.strict_line_comment_wrap()
&& matches!(self.style, CommentStyle::DoubleSlash)
&& should_wrap_comment
{
println!("condition passed");
let available_width = self.max_width;
let indent_str = &self.indent_str;

let mut wrapped = String::new();
let mut current = String::new();

for word in line.split_whitespace() {
if !current.is_empty()
&& current.len() + 1 + word.len() > available_width
{
if !wrapped.is_empty() {
wrapped.push_str(indent_str);
wrapped.push_str(&self.line_start);
}
wrapped.push_str(current.trim_end());
current.clear();
}

if !current.is_empty() {
current.push(' ');
}
current.push_str(word);
}

if !current.is_empty() {
if !wrapped.is_empty() {
wrapped.push_str(indent_str);
wrapped.push_str(&self.line_start);
}
wrapped.push_str(current.trim_end());
}

self.result.push_str(&wrapped);
self.is_prev_line_multi_line = wrapped.contains('\n');
return false;
}

if should_wrap_comment {
match rewrite_string(line, &self.fmt, self.max_width) {
Some(ref s) => {
Expand Down
2 changes: 2 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ create_config! {
snippets in doc comments. No effect unless format_code_in_doc_comments = true";
comment_width: CommentWidth, false,
"Maximum length of comments. No effect unless wrap_comments = true";
strict_line_comment_wrap: StrictLineCommentWrap, false,
"Strict wrapping for plain // comments preserving original indentation";
normalize_comments: NormalizeComments, false, "Convert /* */ comments to // comments where \
possible";
normalize_doc_attributes: NormalizeDocAttributes, false, "Normalize doc attributes as doc \
Expand Down
1 change: 1 addition & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ config_option_with_style_edition_default!(
FormatCodeInDocComments, bool, _ => false;
DocCommentCodeBlockWidth, usize, _ => 100;
CommentWidth, usize, _ => 80;
StrictLineCommentWrap, bool, _ => false;
NormalizeComments, bool, _ => false;
NormalizeDocAttributes, bool, _ => false;
FormatStrings, bool, _ => false;
Expand Down
Loading