Skip to content

Commit

Permalink
issue(5801): do not change shape before formatting enum comments
Browse files Browse the repository at this point in the history
do not change shape as not formatting doc comments correctly

sub_width has no impact on tests, also removing

add version gate and review comments

remove unused variable

test unexpected wrapping in version one

test version 2 does not wrap

remove unneeded config

remove unneeded config params

adding source test file for version one

fix typo

ensure ordering of config by using .toml files
  • Loading branch information
IVIURRAY committed Jan 7, 2024
1 parent 75e3172 commit ef7e05b
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,9 +656,16 @@ impl<'a> FmtVisitor<'a> {
}

let context = self.get_context();
// 1 = ','
let shape = self.shape().sub_width(1)?;
let attrs_str = field.attrs.rewrite(&context, shape)?;
let shape = self.shape();
let attrs_str = if context.config.version() == Version::Two {
field.attrs.rewrite(&context, shape)?
} else {
// Version::One formatting that was off by 1. See issue #5801
field.attrs.rewrite(&context, shape.sub_width(1)?)?
};
// sub_width(1) to take the trailing comma into account
let shape = shape.sub_width(1)?;

let lo = field
.attrs
.last()
Expand Down
3 changes: 3 additions & 0 deletions tests/config/issue-5801-v1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
max_width = 120
version = "One"
attr_fn_like_width = 120
3 changes: 3 additions & 0 deletions tests/config/issue-5801-v2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
max_width = 120
version = "Two"
attr_fn_like_width = 120
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-config: issue-5801-v1.toml

pub enum Severity {
#[something(AAAAAAAAAAAAA, BBBBBBBBBBBBBB, CCCCCCCCCCCCCCCC, DDDDDDDDDDDDD, EEEEEEEEEEEE, FFFFFFFFFFF, GGGGGGGGGGG)]
AttrsWillWrap,
#[something_else(hhhhhhhhhhhhhhhh, iiiiiiiiiiiiiiii, jjjjjjjjjjjjjjj, kkkkkkkkkkkkk, llllllllllll, mmmmmmmmmmmmmm)]
AttrsWontWrap,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-comment_width: 120
// rustfmt-wrap_comments: true
// rustfmt-max_width: 120
// rustfmt-version: One

/// This function is 120 columns wide and is left alone. This comment is 120 columns wide and the formatter is also fine
fn my_super_cool_function_name(my_very_cool_argument_name: String, my_other_very_cool_argument_name: String) -> String {
unimplemented!()
}

pub enum Severity {
/// In version one, the below line got wrapped prematurely as we subtracted 1 to account for `,`. See issue #5801.
/// But here, this comment is 120 columns wide and the formatter wants to split it up onto two separate lines still.
Error,
/// This comment is 119 columns wide and works perfectly. Lorem ipsum. lorem ipsum. lorem ipsum. lorem ipsum lorem.
Warning,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-config: issue-5801-v2.toml

pub enum Severity {
#[something(AAAAAAAAAAAAA, BBBBBBBBBBBBBB, CCCCCCCCCCCCCCCC, DDDDDDDDDDDDD, EEEEEEEEEEEE, FFFFFFFFFFF, GGGGGGGGGGG)]
AttrsWillWrap,
#[something_else(hhhhhhhhhhhhhhhh, iiiiiiiiiiiiiiii, jjjjjjjjjjjjjjj, kkkkkkkkkkkkk, llllllllllll, mmmmmmmmmmmmmm)]
AttrsWontWrap,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-config: issue-5801-v1.toml

pub enum Severity {
#[something(
AAAAAAAAAAAAA,
BBBBBBBBBBBBBB,
CCCCCCCCCCCCCCCC,
DDDDDDDDDDDDD,
EEEEEEEEEEEE,
FFFFFFFFFFF,
GGGGGGGGGGG
)]
AttrsWillWrap,
#[something_else(hhhhhhhhhhhhhhhh, iiiiiiiiiiiiiiii, jjjjjjjjjjjjjjj, kkkkkkkkkkkkk, llllllllllll, mmmmmmmmmmmmmm)]
AttrsWontWrap,
}
16 changes: 16 additions & 0 deletions tests/target/issue-5801/comment_does_not_wrap_within_max_width.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-comment_width: 120
// rustfmt-wrap_comments: true
// rustfmt-max_width: 120
// rustfmt-version: Two

/// This function is 120 columns wide and is left alone. This comment is 120 columns wide and the formatter is also fine
fn my_super_cool_function_name(my_very_cool_argument_name: String, my_other_very_cool_argument_name: String) -> String {
unimplemented!()
}

pub enum Severity {
/// But here, this comment is 120 columns wide and the formatter wants to split it up onto two separate lines still.
Error,
/// This comment is 119 columns wide and works perfectly. Lorem ipsum. lorem ipsum. lorem ipsum. lorem ipsum lorem.
Warning,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// rustfmt-comment_width: 120
// rustfmt-wrap_comments: true
// rustfmt-max_width: 120
// rustfmt-version: One

/// This function is 120 columns wide and is left alone. This comment is 120 columns wide and the formatter is also fine
fn my_super_cool_function_name(my_very_cool_argument_name: String, my_other_very_cool_argument_name: String) -> String {
unimplemented!()
}

pub enum Severity {
/// In version one, the below line got wrapped prematurely as we subtracted 1 to account for `,`. See issue #5801.
/// But here, this comment is 120 columns wide and the formatter wants to split it up onto two separate lines
/// still.
Error,
/// This comment is 119 columns wide and works perfectly. Lorem ipsum. lorem ipsum. lorem ipsum. lorem ipsum lorem.
Warning,
}

0 comments on commit ef7e05b

Please sign in to comment.