Skip to content

Reorder use items inside blocks #1968

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

Merged
merged 3 commits into from
Sep 18, 2017
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
15 changes: 6 additions & 9 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use string::{rewrite_string, StringFormat};
use types::{can_be_overflowed_type, rewrite_path, PathContext};
use utils::{binary_search, colon_spaces, contains_skip, extra_offset, first_line_width,
inner_attributes, last_line_extendable, last_line_width, left_most_sub_expr, mk_sp,
outer_attributes, paren_overhead, semicolon_for_stmt, stmt_expr,
outer_attributes, paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, stmt_expr,
trimmed_last_line_width, wrap_str};
use vertical::rewrite_with_alignment;
use visitor::FmtVisitor;
Expand Down Expand Up @@ -85,7 +85,7 @@ pub fn format_expr(
rewrite_call_with_binary_search(
context,
&**callee,
&args.iter().map(|x| &**x).collect::<Vec<_>>()[..],
&ptr_vec_to_ref_vec(&args),
inner_span,
shape,
)
Expand All @@ -112,12 +112,9 @@ pub fn format_expr(
expr.span,
shape,
),
ast::ExprKind::Tup(ref items) => rewrite_tuple(
context,
&items.iter().map(|x| &**x).collect::<Vec<_>>()[..],
expr.span,
shape,
),
ast::ExprKind::Tup(ref items) => {
rewrite_tuple(context, &ptr_vec_to_ref_vec(&items), expr.span, shape)
}
ast::ExprKind::If(..) |
ast::ExprKind::IfLet(..) |
ast::ExprKind::ForLoop(..) |
Expand Down Expand Up @@ -2037,7 +2034,7 @@ pub fn rewrite_call(
rewrite_call_inner(
context,
callee,
&args.iter().map(|x| &**x).collect::<Vec<_>>(),
&ptr_vec_to_ref_vec(&args),
span,
shape,
context.config.fn_call_width(),
Expand Down
6 changes: 3 additions & 3 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use std::cmp::Ordering;

use syntax::{ast, ptr};
use syntax::ast;
use syntax::codemap::{BytePos, Span};

use {Shape, Spanned};
Expand Down Expand Up @@ -216,7 +216,7 @@ fn rewrite_import(

fn rewrite_imports(
context: &RewriteContext,
use_items: &[ptr::P<ast::Item>],
use_items: &[&ast::Item],
shape: Shape,
span: Span,
) -> Option<String> {
Expand Down Expand Up @@ -275,7 +275,7 @@ fn rewrite_imports(
}

impl<'a> FmtVisitor<'a> {
pub fn format_imports(&mut self, use_items: &[ptr::P<ast::Item>]) {
pub fn format_imports(&mut self, use_items: &[&ast::Item]) {
if use_items.is_empty() {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ impl Rewrite for ast::Ty {
}
ast::TyKind::Tup(ref items) => rewrite_tuple(
context,
&items.iter().map(|x| &**x).collect::<Vec<_>>()[..],
&::utils::ptr_vec_to_ref_vec(&items),
self.span,
shape,
),
Expand Down
8 changes: 7 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use std::borrow::Cow;
use std::cmp::Ordering;

use syntax::abi;
use syntax::{abi, ptr};
use syntax::ast::{self, Attribute, MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind,
Path, Visibility};
use syntax::codemap::{BytePos, Span, NO_EXPANSION};
Expand Down Expand Up @@ -97,6 +97,12 @@ pub fn format_abi(abi: abi::Abi, explicit_abi: bool) -> String {
}
}

#[inline]
// Transform `Vec<syntax::ptr::P<T>>` into `Vec<&T>`
pub fn ptr_vec_to_ref_vec<T>(vec: &[ptr::P<T>]) -> Vec<&T> {
vec.iter().map(|x| &**x).collect::<Vec<_>>()
}

#[inline]
pub fn filter_attributes(attrs: &[ast::Attribute], style: ast::AttrStyle) -> Vec<ast::Attribute> {
attrs
Expand Down
55 changes: 42 additions & 13 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use std::cmp;

use strings::string_buffer::StringBuffer;
use syntax::{ast, ptr, visit};
use syntax::{ast, visit};
use syntax::attr::HasAttrs;
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
use syntax::parse::ParseSess;
Expand All @@ -30,7 +30,7 @@ use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, Sepa
use macros::{rewrite_macro, MacroPosition};
use regex::Regex;
use rewrite::{Rewrite, RewriteContext};
use utils::{self, contains_skip, inner_attributes, mk_sp};
use utils::{self, contains_skip, inner_attributes, mk_sp, ptr_vec_to_ref_vec};

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

for stmt in &b.stmts {
self.visit_stmt(stmt)
}
self.walk_block_stmts(b);

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

fn reorder_items<F>(
&mut self,
items_left: &[ptr::P<ast::Item>],
is_item: &F,
in_group: bool,
) -> usize
fn reorder_items<F>(&mut self, items_left: &[&ast::Item], is_item: &F, in_group: bool) -> usize
where
F: Fn(&ast::Item) -> bool,
{
Expand Down Expand Up @@ -679,8 +672,7 @@ impl<'a> FmtVisitor<'a> {
item_length
}

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

fn walk_mod_items(&mut self, m: &ast::Mod) {
self.walk_items(&ptr_vec_to_ref_vec(&m.items));
}

fn walk_stmts(&mut self, stmts: &[ast::Stmt]) {
fn to_stmt_item(stmt: &ast::Stmt) -> Option<&ast::Item> {
match stmt.node {
ast::StmtKind::Item(ref item) => Some(&**item),
_ => None,
}
}

if stmts.is_empty() {
return;
}

// Extract leading `use ...;`.
let items: Vec<_> = stmts
.iter()
.take_while(|stmt| to_stmt_item(stmt).is_some())
.filter_map(|stmt| to_stmt_item(stmt))
.take_while(|item| is_use_item(item))
.collect();

if items.is_empty() {
self.visit_stmt(&stmts[0]);
self.walk_stmts(&stmts[1..]);
} else {
self.walk_items(&items);
self.walk_stmts(&stmts[items.len()..]);
}
}

fn walk_block_stmts(&mut self, b: &ast::Block) {
self.walk_stmts(&b.stmts)
}

fn format_mod(
&mut self,
m: &ast::Mod,
Expand Down
12 changes: 12 additions & 0 deletions tests/source/configs-reorder_imports-true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@ use lorem;
use ipsum;
use dolor;
use sit;

fn foo() {
use C;
use B;
use A;

bar();

use F;
use E;
use D;
}
12 changes: 12 additions & 0 deletions tests/target/configs-reorder_imports-true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@ use dolor;
use ipsum;
use lorem;
use sit;

fn foo() {
use A;
use B;
use C;

bar();

use D;
use E;
use F;
}