-
Notifications
You must be signed in to change notification settings - Fork 971
handle field attributes when aligning a struct's fields #3513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@topecongiro Thanks for the comments ;o) |
|
Following previous comment, |
|
I agree about essentially ignoring attributes. It's not likely to be useful to consider them as "group separators" because (for example with pub struct Output {
// things shared / derived
time: DateTime<Utc>,
cs_method: Intern<String>,
cs_uri_stem: Intern<String>,
#[serde(skip_serializing_if = "Option::is_none")]
cs_uri_query: Option<Intern<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
cs_user_agent: Option<Intern<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
cs_referer: Option<Intern<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
cs_host: Option<Intern<String>>,
// things from IIS
#[serde(skip_serializing_if = "Option::is_none")]
iis_time: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
iis_ip: Option<Ipv4Addr>,
#[serde(skip_serializing_if = "Option::is_none")]
iis_port: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
iis_username: Option<Intern<String>>,
.... // many more
}Comments, especially doc-comments, likewise - if you're documenting your fields, you should document them all. Blank lines should separate alignment groups. Comments and attributes should be ignored. |
|
@topecongiro Are you ok with the comments ? If so, I will fix the alignment when there is a comment #3513 (comment) |
|
@scampi Sorry for the late review. IMHO I do not see the benefit of aligning fields when they are not consecutive. Also, it does not make sense to align fields when the comment or attributes between them have multiple lines (FYI gofmt doesn't align fields that are separated by comment or empty lines). |
|
@topecongiro Thanks for sharing ;o) If we align fields only when they are together, then there is very little practical use to the config option I think: most of the time, I expect fields to have a doc comment. The concept of grouping could be handled by a separate config option:
BTW, this grouping option would fit nicely in the structured configuration you proposed once #3487: [struct.field]
struct_field_align_threshold = 100
[struct.field.group]
# whether to group by blank lines
blank_lines = "yes"
# whether to group by comments
comment = "yes"
# whether to group by attributes
attribute = "no" |
|
Taking a struct from rustfmt codebase: struct ChainFormatterShared<'a> {
// The current working set of child items.
children: &'a [ChainItem],
// The current rewrites of items (includes trailing `?`s, but not any way to
// connect the rewrites together).
rewrites: Vec<String>,
// Whether the chain can fit on one line.
fits_single_line: bool,
// The number of children in the chain. This is not equal to `self.children.len()`
// because `self.children` will change size as we process the chain.
child_count: usize,
}
struct ChainFormatterShared<'a> {
// The current working set of child items.
children: &'a [ChainItem],
// The current rewrites of items (includes trailing `?`s, but not any way to
// connect the rewrites together).
rewrites: Vec<String>,
// Whether the chain can fit on one line.
fits_single_line: bool,
// The number of children in the chain. This is not equal to `self.children.len()`
// because `self.children` will change size as we process the chain.
child_count: usize,
}I thought as well aligned fields would look strange because of the multiline comments inbetween, but it is more readable when aligned as it looks less packed together. It's a matter of taste. |
|
Hmm, looking at your example, I think it's fine to align fields even when there are a multi-line comment or attributes. I personally don't use this feature, so you should have a better idea of what is good or not.
Sorry that it took a long time to reach the design direction, but could you update this PR according to this comment?
This is cool, but probably too nitch to provide as an option, I think. |
the `items::rewrite_struct_field` method was already handling multiline field names via `last_line_width`, which was rendered moot by the previous logic in `vertical::AlignedItem::struct_field_prefix_max_min_width`. Close rust-lang#2869
|
@topecongiro sorry for the delay in coming back to this PR. What do you think of the changes? |
topecongiro
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@scampi Well, this is my turn to say sorry about the delay 🙉 The code looks good to me. Thanks!
|
@topecongiro I don't mind ;o) thanks for taking time reviewing this! |
the
items::rewrite_struct_fieldmethod was already handling multilinefield names via
last_line_width, which was rendered moot by theprevious logic in
vertical::AlignedItem::struct_field_prefix_max_min_width.Close #2869