Skip to content

Commit d906ea2

Browse files
authored
Merge pull request #1968 from topecongiro/issue-1967
Reorder use items inside blocks
2 parents cccb7f6 + bb4a6bf commit d906ea2

File tree

7 files changed

+83
-27
lines changed

7 files changed

+83
-27
lines changed

src/expr.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use string::{rewrite_string, StringFormat};
3434
use types::{can_be_overflowed_type, rewrite_path, PathContext};
3535
use utils::{binary_search, colon_spaces, contains_skip, extra_offset, first_line_width,
3636
inner_attributes, last_line_extendable, last_line_width, left_most_sub_expr, mk_sp,
37-
outer_attributes, paren_overhead, semicolon_for_stmt, stmt_expr,
37+
outer_attributes, paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, stmt_expr,
3838
trimmed_last_line_width, wrap_str};
3939
use vertical::rewrite_with_alignment;
4040
use visitor::FmtVisitor;
@@ -86,7 +86,7 @@ pub fn format_expr(
8686
rewrite_call_with_binary_search(
8787
context,
8888
&**callee,
89-
&args.iter().map(|x| &**x).collect::<Vec<_>>()[..],
89+
&ptr_vec_to_ref_vec(&args),
9090
inner_span,
9191
shape,
9292
)
@@ -114,12 +114,9 @@ pub fn format_expr(
114114
expr.span,
115115
shape,
116116
),
117-
ast::ExprKind::Tup(ref items) => rewrite_tuple(
118-
context,
119-
&items.iter().map(|x| &**x).collect::<Vec<_>>()[..],
120-
expr.span,
121-
shape,
122-
),
117+
ast::ExprKind::Tup(ref items) => {
118+
rewrite_tuple(context, &ptr_vec_to_ref_vec(&items), expr.span, shape)
119+
}
123120
ast::ExprKind::If(..) |
124121
ast::ExprKind::IfLet(..) |
125122
ast::ExprKind::ForLoop(..) |
@@ -2094,7 +2091,7 @@ pub fn rewrite_call(
20942091
rewrite_call_inner(
20952092
context,
20962093
callee,
2097-
&args.iter().map(|x| &**x).collect::<Vec<_>>(),
2094+
&ptr_vec_to_ref_vec(&args),
20982095
span,
20992096
shape,
21002097
context.config.fn_call_width(),

src/imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use std::cmp::Ordering;
1212

13-
use syntax::{ast, ptr};
13+
use syntax::ast;
1414
use syntax::codemap::{BytePos, Span};
1515

1616
use {Shape, Spanned};
@@ -216,7 +216,7 @@ fn rewrite_import(
216216

217217
fn rewrite_imports(
218218
context: &RewriteContext,
219-
use_items: &[ptr::P<ast::Item>],
219+
use_items: &[&ast::Item],
220220
shape: Shape,
221221
span: Span,
222222
) -> Option<String> {
@@ -275,7 +275,7 @@ fn rewrite_imports(
275275
}
276276

277277
impl<'a> FmtVisitor<'a> {
278-
pub fn format_imports(&mut self, use_items: &[ptr::P<ast::Item>]) {
278+
pub fn format_imports(&mut self, use_items: &[&ast::Item]) {
279279
if use_items.is_empty() {
280280
return;
281281
}

src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ impl Rewrite for ast::Ty {
727727
}
728728
ast::TyKind::Tup(ref items) => rewrite_tuple(
729729
context,
730-
&items.iter().map(|x| &**x).collect::<Vec<_>>()[..],
730+
&::utils::ptr_vec_to_ref_vec(&items),
731731
self.span,
732732
shape,
733733
),

src/utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use std::borrow::Cow;
1212
use std::cmp::Ordering;
1313

14-
use syntax::abi;
14+
use syntax::{abi, ptr};
1515
use syntax::ast::{self, Attribute, MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind,
1616
Path, Visibility};
1717
use syntax::codemap::{BytePos, Span, NO_EXPANSION};
@@ -97,6 +97,12 @@ pub fn format_abi(abi: abi::Abi, explicit_abi: bool) -> Cow<'static, str> {
9797
}
9898
}
9999

100+
#[inline]
101+
// Transform `Vec<syntax::ptr::P<T>>` into `Vec<&T>`
102+
pub fn ptr_vec_to_ref_vec<T>(vec: &[ptr::P<T>]) -> Vec<&T> {
103+
vec.iter().map(|x| &**x).collect::<Vec<_>>()
104+
}
105+
100106
#[inline]
101107
pub fn filter_attributes(attrs: &[ast::Attribute], style: ast::AttrStyle) -> Vec<ast::Attribute> {
102108
attrs

src/visitor.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use std::cmp;
1212

1313
use strings::string_buffer::StringBuffer;
14-
use syntax::{ast, ptr, visit};
14+
use syntax::{ast, visit};
1515
use syntax::attr::HasAttrs;
1616
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
1717
use syntax::parse::ParseSess;
@@ -30,7 +30,7 @@ use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, Sepa
3030
use macros::{rewrite_macro, MacroPosition};
3131
use regex::Regex;
3232
use rewrite::{Rewrite, RewriteContext};
33-
use utils::{self, contains_skip, inner_attributes, mk_sp};
33+
use utils::{self, contains_skip, inner_attributes, mk_sp, ptr_vec_to_ref_vec};
3434

3535
fn is_use_item(item: &ast::Item) -> bool {
3636
match item.node {
@@ -152,9 +152,7 @@ impl<'a> FmtVisitor<'a> {
152152
self.visit_attrs(attrs, ast::AttrStyle::Inner);
153153
}
154154

155-
for stmt in &b.stmts {
156-
self.visit_stmt(stmt)
157-
}
155+
self.walk_block_stmts(b);
158156

159157
if !b.stmts.is_empty() {
160158
if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
@@ -641,12 +639,7 @@ impl<'a> FmtVisitor<'a> {
641639
false
642640
}
643641

644-
fn reorder_items<F>(
645-
&mut self,
646-
items_left: &[ptr::P<ast::Item>],
647-
is_item: &F,
648-
in_group: bool,
649-
) -> usize
642+
fn reorder_items<F>(&mut self, items_left: &[&ast::Item], is_item: &F, in_group: bool) -> usize
650643
where
651644
F: Fn(&ast::Item) -> bool,
652645
{
@@ -679,8 +672,7 @@ impl<'a> FmtVisitor<'a> {
679672
item_length
680673
}
681674

682-
fn walk_mod_items(&mut self, m: &ast::Mod) {
683-
let mut items_left: &[ptr::P<ast::Item>] = &m.items;
675+
fn walk_items(&mut self, mut items_left: &[&ast::Item]) {
684676
while !items_left.is_empty() {
685677
// If the next item is a `use` declaration, then extract it and any subsequent `use`s
686678
// to be potentially reordered within `format_imports`. Otherwise, just format the
@@ -711,6 +703,43 @@ impl<'a> FmtVisitor<'a> {
711703
}
712704
}
713705

706+
fn walk_mod_items(&mut self, m: &ast::Mod) {
707+
self.walk_items(&ptr_vec_to_ref_vec(&m.items));
708+
}
709+
710+
fn walk_stmts(&mut self, stmts: &[ast::Stmt]) {
711+
fn to_stmt_item(stmt: &ast::Stmt) -> Option<&ast::Item> {
712+
match stmt.node {
713+
ast::StmtKind::Item(ref item) => Some(&**item),
714+
_ => None,
715+
}
716+
}
717+
718+
if stmts.is_empty() {
719+
return;
720+
}
721+
722+
// Extract leading `use ...;`.
723+
let items: Vec<_> = stmts
724+
.iter()
725+
.take_while(|stmt| to_stmt_item(stmt).is_some())
726+
.filter_map(|stmt| to_stmt_item(stmt))
727+
.take_while(|item| is_use_item(item))
728+
.collect();
729+
730+
if items.is_empty() {
731+
self.visit_stmt(&stmts[0]);
732+
self.walk_stmts(&stmts[1..]);
733+
} else {
734+
self.walk_items(&items);
735+
self.walk_stmts(&stmts[items.len()..]);
736+
}
737+
}
738+
739+
fn walk_block_stmts(&mut self, b: &ast::Block) {
740+
self.walk_stmts(&b.stmts)
741+
}
742+
714743
fn format_mod(
715744
&mut self,
716745
m: &ast::Mod,

tests/source/configs-reorder_imports-true.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,15 @@ use lorem;
55
use ipsum;
66
use dolor;
77
use sit;
8+
9+
fn foo() {
10+
use C;
11+
use B;
12+
use A;
13+
14+
bar();
15+
16+
use F;
17+
use E;
18+
use D;
19+
}

tests/target/configs-reorder_imports-true.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,15 @@ use dolor;
55
use ipsum;
66
use lorem;
77
use sit;
8+
9+
fn foo() {
10+
use A;
11+
use B;
12+
use C;
13+
14+
bar();
15+
16+
use D;
17+
use E;
18+
use F;
19+
}

0 commit comments

Comments
 (0)