-
Notifications
You must be signed in to change notification settings - Fork 889
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
Merge imports #2603
Merge imports #2603
Conversation
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.
Looks good, thank you! A couple of small questions inline.
src/imports.rs
Outdated
|
||
impl fmt::Debug for UseTree { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}", self) |
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.
Nit: think writing <Self as Display>::fmt(self, f)
is clearer (and more efficient) than using write!
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.
It can be Display::fmt(self, f)
too which is less verbose.
src/imports.rs
Outdated
impl UseTree { | ||
// Rewrite use tree with `use ` and a trailing `;`. | ||
pub fn rewrite_top_level(&self, context: &RewriteContext, shape: Shape) -> Option<String> { | ||
let mut result = String::with_capacity(256); | ||
if let Some(ref attrs) = self.attrs { | ||
result.push_str(&attrs.rewrite(context, shape)?); | ||
result.push_str(&attrs.rewrite(context, shape).expect("rewrite attr")); |
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.
Why change this? It seems that we could crash if the attribute can't be rewritten.
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.
Ah, I changed this for debugging purpose, but I forgot to bring this back.
@@ -168,6 +241,17 @@ impl UseTree { | |||
Some(result) | |||
} | |||
|
|||
// FIXME: Use correct span? |
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.
Why is the span incorrect?
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.
The span could be wrong when we are using the span from list imports. It should not matter, though, since we have already extracted comments around use items at this point.
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.
Could you add this detail to the comment please?
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.
Sure 😄
src/imports.rs
Outdated
break; | ||
} | ||
} | ||
if let Some(merged) = merge_rest(&self.path, &other.path, len) { |
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.
len
is always at new_path.len()
, right?
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.
Yes, thanks!
5155f1e
to
8820a59
Compare
Ping @nrc 😄 |
Thanks for the changes! |
Is it intended for this to put all std:: imports in a single block? e.g.
|
This PR adds a
merge_imports
config option, which merges multiple imports with the same prefix into a single nested import.Thoughts:
usize
(0 means no merging, 1 means merging the top level segment, and so on), but it turned out to be kind of counter-intuitive IMO, so came back to true/false.Closes #1383. Closes #2452. Closes #2544.