-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Change struct expr pretty printing to match rustfmt style #93556
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b7e1ba
Add trailing comma support
dtolnay 8bdf08f
Change pp indent to signed to allow negative indents
dtolnay 63406ac
Support offsetting the most recent break
dtolnay 40fcbba
Change struct expr pretty printing to match rustfmt style
dtolnay ca3057f
Bless all pretty printer tests and ui tests
dtolnay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::iter::Peekable; | ||
use std::mem; | ||
use std::ops::Deref; | ||
|
||
pub struct Delimited<I: Iterator> { | ||
is_first: bool, | ||
iter: Peekable<I>, | ||
} | ||
|
||
pub trait IterDelimited: Iterator + Sized { | ||
fn delimited(self) -> Delimited<Self> { | ||
Delimited { is_first: true, iter: self.peekable() } | ||
} | ||
} | ||
|
||
impl<I: Iterator> IterDelimited for I {} | ||
|
||
pub struct IteratorItem<T> { | ||
value: T, | ||
pub is_first: bool, | ||
pub is_last: bool, | ||
} | ||
|
||
impl<I: Iterator> Iterator for Delimited<I> { | ||
type Item = IteratorItem<I::Item>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let value = self.iter.next()?; | ||
let is_first = mem::replace(&mut self.is_first, false); | ||
let is_last = self.iter.peek().is_none(); | ||
Some(IteratorItem { value, is_first, is_last }) | ||
} | ||
} | ||
|
||
impl<T> Deref for IteratorItem<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.run.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 explain what this negative offset does? I have trouble understanding how this code manages to find the correct indentation.
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 top of https://github.com/rust-lang/rust/blob/1.58.1/compiler/rustc_ast_pretty/src/pp.rs has an overview of the printing algorithm. Very briefly, when you make a sequence of cbox string break string break string break string end calls on the printer, either all of those breaks will turn into linebreaks, or none of them will turn into linebreaks. If they turn into linebreaks, the indentation at each one is the sum of the indentation of all the surrounding boxes.
So if we have:
and it does not fit on the line, then you'd expect to get:
That's why every break also holds its own offset which is relative to the sum of the surrounding boxes' indents, so that we can set -4 on the last break and get:
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.
Thank you for the detailed explanation. I'm surprised such negative offset facility was not used before, but that's maybe for another refactoring.