Skip to content
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

Pretty print ItemKind::Use in rustfmt style #93759

Merged
merged 3 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_ast_pretty/src/pp/convenience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ impl Printer {
}

pub fn trailing_comma(&mut self) {
self.scan_break(BreakToken { pre_break: Some(','), ..BreakToken::default() });
}

pub fn trailing_comma_or_space(&mut self) {
self.scan_break(BreakToken {
blank_space: 1,
pre_break: Some(','),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<'a> State<'a> {
if !field.is_last || has_rest {
self.word_space(",");
} else {
self.trailing_comma();
self.trailing_comma_or_space();
}
}
if has_rest {
Expand Down
48 changes: 34 additions & 14 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::pp::Breaks::Inconsistent;
use crate::pprust::state::{AnnNode, PrintState, State};
use crate::pprust::state::delimited::IterDelimited;
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};

use rustc_ast as ast;
use rustc_ast::GenericBound;
Expand Down Expand Up @@ -138,11 +139,10 @@ impl<'a> State<'a> {
self.end(); // end outer head-block
}
ast::ItemKind::Use(ref tree) => {
self.head(visibility_qualified(&item.vis, "use"));
self.print_visibility(&item.vis);
self.word_nbsp("use");
self.print_use_tree(tree);
self.word(";");
self.end(); // end inner head-block
self.end(); // end outer head-block
}
ast::ItemKind::Static(ref ty, mutbl, ref body) => {
let def = ast::Defaultness::Final;
Expand Down Expand Up @@ -615,8 +615,8 @@ impl<'a> State<'a> {
ast::UseTreeKind::Simple(rename, ..) => {
self.print_path(&tree.prefix, false, 0);
if let Some(rename) = rename {
self.space();
self.word_space("as");
self.nbsp();
self.word_nbsp("as");
self.print_ident(rename);
}
}
Expand All @@ -628,16 +628,36 @@ impl<'a> State<'a> {
self.word("*");
}
ast::UseTreeKind::Nested(ref items) => {
if tree.prefix.segments.is_empty() {
self.word("{");
} else {
if !tree.prefix.segments.is_empty() {
self.print_path(&tree.prefix, false, 0);
self.word("::{");
self.word("::");
}
if items.is_empty() {
self.word("{}");
} else if items.len() == 1 {
self.print_use_tree(&items[0].0);
} else {
self.cbox(INDENT_UNIT);
self.word("{");
self.zerobreak();
self.ibox(0);
for use_tree in items.iter().delimited() {
self.print_use_tree(&use_tree.0);
if !use_tree.is_last {
self.word(",");
if let ast::UseTreeKind::Nested(_) = use_tree.0.kind {
self.hardbreak();
} else {
self.space();
}
}
}
self.end();
self.trailing_comma();
self.offset(-INDENT_UNIT);
self.word("}");
self.end();
}
self.commasep(Inconsistent, &items, |this, &(ref tree, _)| {
this.print_use_tree(tree)
});
self.word("}");
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/pretty/use-tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// pp-exact
// edition:2021

#![allow(unused_imports)]

use ::std::fmt::{self, Debug, Display, Write as _};

use core::option::Option::*;

use core::{
cmp::{Eq, Ord, PartialEq, PartialOrd},
convert::{AsMut, AsRef, From, Into},
iter::{
DoubleEndedIterator, ExactSizeIterator, Extend, FromIterator,
IntoIterator, Iterator,
},
marker::{
Copy as Copy, Send as Send, Sized as Sized, Sync as Sync, Unpin as U,
},
ops::{*, Drop, Fn, FnMut, FnOnce},
};

fn main() {}