-
Notifications
You must be signed in to change notification settings - Fork 925
- formatting of import long lines #5612
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
base: master
Are you sure you want to change the base?
- formatting of import long lines #5612
Conversation
src/imports.rs
Outdated
while let Some(segment) = iter.next() { | ||
let segment_str = segment.rewrite(context, shape)?; | ||
let mut added_len = first_line_width(&segment_str); | ||
if iter.peek().is_some() { | ||
added_len += 2; // 3 == "::" |
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.
should this be
added_len += 2; // 3 == "::" | |
added_len += 2; // 2 == "::" |
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.
Changed (the 3 was because originally I also added {
)
tests/target/issue-4746/crate.rs
Outdated
use abaadfsasdfdsfdfas::aasdffjsioejr::abc::sdsdf::sdfsdfsdf::sdfsdfdsf:: | ||
asdfasdefasdasdfsdfdfasdf::asdfasdasedfafasdfasdf; |
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.
I don't think this is formatting that's permitted based on the formatting rules for large imports. I think we'd need to add braces in order to introduce this line break.
As mentioned in #4746 (comment) I think these changes need to be version gated. I can also see cases where users are fine with their imports exceeding the |
The For example, the use a::b::c::d::e;
use a::b::c::
d::e;
use a::b::c::{
d::e
}; |
@ytmimi, I improved the formatting, so when an import path is broken between lines, nested imports will always be in a new line, even if they could be in the same line. This may be more in line of the formatting rules. I also prepared a private version that always adds braces when import path is broken between lines. It produces the following Crate and Preserve target outputs for the source test cases. I hope this can help with the decision whether braces should or should not be added whenever the import line is broken between lines. |
The first two are the same because the rustc_parser doesn't treat whitespace as significant so both of those get parsed as use a::b::c::d::e;
use a::b::c::d::e;
use a::b::c::{d::e}; I don't expect us to tackle this in this PR. It's unclear whether this is a bug in the |
Just so we're on the same page are you referring to the
Thanks for continuing to work on this. I think including the braces are more inline with the current rules in the style guide, however, I think it might be best to try and get an explicit rule added to the style guide for long imports that can't be formatted on a single line. There are some good edge cases illustrated by your test cases, for example, use z123::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::{
z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::{
z1234567::z1234567::z1234567::{
baz, foo
}
}
};
// VS
use z123::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::
z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::
z1234567::z1234567::z1234567::{baz, foo}; @calebcartwright what are your thoughts on this? |
Yes, practically... Actually I assumed that from the Large list imports rules, but I meant exactly what is written in Nested imports rules.
With the last change referred above (when not adding braces for each line break) this is now formatted as: use z123::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::
z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::z1234567::
z1234567::z1234567::{
baz, foo
};
I see that setting |
Thanks for clarifying!
If I had to guess it's because we're not doing any normalization of the imports when |
Issue was mainly related to indentation. Now fixed. Added related test cases. During the issue evaluation, I found that use-trees merge does not take
See the discussion about the " |
Fixes #4746
Suggested fix to format long import lines. The main issue was that there was no check whether the last import segment in the formatted output will not exceed the maximum width. In case the import line breaks the next line starts with the original next line indentation.
No braces are added when breaking an import line, and it seems that this is not required.
The two test files are the same, except that one is for
imports_granularity
Crate and the other is for Preserve.The fix changes
issue_3033.rs
test, as the target test file included an import line with length of 106 bytes. To keep the original target file it is now used as the test source file (there was no source test file for this issue).