mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-26 14:43:24 +00:00
Rollup merge of #93759 - dtolnay:usetree, r=nagisa
Pretty print ItemKind::Use in rustfmt style This PR backports the formatting for `use` items from https://github.com/dtolnay/prettyplease into rustc_ast_pretty. Before: ```rust 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}}; ``` After: ```rust 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}, }; ```
This commit is contained in:
commit
f30f6def0f
@ -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(','),
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
@ -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;
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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("}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
23
src/test/pretty/use-tree.rs
Normal file
23
src/test/pretty/use-tree.rs
Normal file
@ -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() {}
|
Loading…
Reference in New Issue
Block a user