Skip to content

Refactoring: clean up source code #1983

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 7 commits into from
Sep 19, 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
2 changes: 1 addition & 1 deletion src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
/// .qux
/// ```

use Shape;
use shape::Shape;
use config::IndentStyle;
use expr::rewrite_call;
use macros::convert_try_mac;
Expand Down
4 changes: 2 additions & 2 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use std::{self, iter};

use syntax::codemap::Span;

use {Indent, Shape};
use config::Config;
use rewrite::RewriteContext;
use shape::{Indent, Shape};
use string::{rewrite_string, StringFormat};
use utils::{first_line_width, last_line_width};

Expand Down Expand Up @@ -928,7 +928,7 @@ fn remove_comment_header(comment: &str) -> &str {
mod test {
use super::{contains_comment, rewrite_comment, CharClasses, CodeCharKind, CommentCodeSlices,
FindUncommented, FullCodeCharKind};
use {Indent, Shape};
use shape::{Indent, Shape};

#[test]
fn char_classes() {
Expand Down
83 changes: 37 additions & 46 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use syntax::{ast, ptr};
use syntax::codemap::{BytePos, CodeMap, Span};
use syntax::parse::classify;

use {Indent, Shape, Spanned};
use spanned::Spanned;
use chains::rewrite_chain;
use codemap::{LineRangeUtils, SpanUtils};
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
Expand All @@ -30,12 +30,13 @@ use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_format
use macros::{rewrite_macro, MacroArg, MacroPosition};
use patterns::{can_be_overflowed_pat, TuplePatField};
use rewrite::{Rewrite, RewriteContext};
use shape::{Indent, Shape};
use string::{rewrite_string, StringFormat};
use types::{can_be_overflowed_type, rewrite_path, PathContext};
use utils::{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, ptr_vec_to_ref_vec, semicolon_for_stmt, stmt_expr,
trimmed_last_line_width, wrap_str};
trimmed_last_line_width};
use vertical::rewrite_with_alignment;
use visitor::FmtVisitor;

Expand Down Expand Up @@ -75,11 +76,7 @@ pub fn format_expr(
ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
rewrite_string_lit(context, l.span, shape)
}
_ => wrap_str(
context.snippet(expr.span),
context.config.max_width(),
shape,
),
_ => Some(context.snippet(expr.span)),
},
ast::ExprKind::Call(ref callee, ref args) => {
let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
Expand Down Expand Up @@ -152,11 +149,7 @@ pub fn format_expr(
Some(ident) => format!(" {}", ident.node),
None => String::new(),
};
wrap_str(
format!("continue{}", id_str),
context.config.max_width(),
shape,
)
Some(format!("continue{}", id_str))
}
ast::ExprKind::Break(ref opt_ident, ref opt_expr) => {
let id_str = match *opt_ident {
Expand All @@ -167,17 +160,13 @@ pub fn format_expr(
if let Some(ref expr) = *opt_expr {
rewrite_unary_prefix(context, &format!("break{} ", id_str), &**expr, shape)
} else {
wrap_str(
format!("break{}", id_str),
context.config.max_width(),
shape,
)
Some(format!("break{}", id_str))
}
}
ast::ExprKind::Yield(ref opt_expr) => if let Some(ref expr) = *opt_expr {
rewrite_unary_prefix(context, "yield ", &**expr, shape)
} else {
wrap_str("yield".to_string(), context.config.max_width(), shape)
Some("yield".to_string())
},
ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
rewrite_closure(capture, fn_decl, body, expr.span, context, shape)
Expand All @@ -189,17 +178,10 @@ pub fn format_expr(
ast::ExprKind::Mac(ref mac) => {
// Failure to rewrite a marco should not imply failure to
// rewrite the expression.
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
wrap_str(
context.snippet(expr.span),
context.config.max_width(),
shape,
)
})
}
ast::ExprKind::Ret(None) => {
wrap_str("return".to_owned(), context.config.max_width(), shape)
rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
.or_else(|| Some(context.snippet(expr.span)))
}
ast::ExprKind::Ret(None) => Some("return".to_owned()),
ast::ExprKind::Ret(Some(ref expr)) => {
rewrite_unary_prefix(context, "return ", &**expr, shape)
}
Expand Down Expand Up @@ -301,16 +283,14 @@ pub fn format_expr(
};
rewrite_unary_suffix(context, &sp_delim, &*lhs, shape)
}
(None, None) => wrap_str(delim.into(), context.config.max_width(), shape),
(None, None) => Some(delim.into()),
}
}
// We do not format these expressions yet, but they should still
// satisfy our width restrictions.
ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => wrap_str(
context.snippet(expr.span),
context.config.max_width(),
shape,
),
ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => {
Some(context.snippet(expr.span))
}
ast::ExprKind::Catch(ref block) => {
if let rw @ Some(_) = rewrite_single_line_block(context, "do catch ", block, shape) {
rw
Expand Down Expand Up @@ -382,7 +362,11 @@ where
.map(|first_line| first_line.ends_with('{'))
.unwrap_or(false);
if !rhs_result.contains('\n') || allow_same_line {
return Some(format!("{}{}{}{}", lhs_result, infix, rhs_result, suffix));
let one_line_width = last_line_width(&lhs_result) + infix.len()
+ first_line_width(&rhs_result) + suffix.len();
if one_line_width <= shape.width {
return Some(format!("{}{}{}{}", lhs_result, infix, rhs_result, suffix));
}
}
}

Expand Down Expand Up @@ -2231,12 +2215,23 @@ where
_ if args.len() >= 1 => {
item_vec[args.len() - 1].item = args.last()
.and_then(|last_arg| last_arg.rewrite(context, shape));
tactic = definitive_tactic(
&*item_vec,
ListTactic::LimitedHorizontalVertical(args_max_width),
Separator::Comma,
one_line_width,
);
// Use horizontal layout for a function with a single argument as long as
// everything fits in a single line.
if args.len() == 1
&& args_max_width != 0 // Vertical layout is forced.
&& !item_vec[0].has_comment()
&& !item_vec[0].inner_as_ref().contains('\n')
&& ::lists::total_item_width(&item_vec[0]) <= one_line_width
{
tactic = DefinitiveListTactic::Horizontal;
} else {
tactic = definitive_tactic(
&*item_vec,
ListTactic::LimitedHorizontalVertical(args_max_width),
Separator::Comma,
one_line_width,
);
}
}
_ => (),
}
Expand Down Expand Up @@ -2664,11 +2659,7 @@ pub fn rewrite_field(
prefix_max_width: usize,
) -> Option<String> {
if contains_skip(&field.attrs) {
return wrap_str(
context.snippet(field.span()),
context.config.max_width(),
shape,
);
return Some(context.snippet(field.span()));
}
let name = &field.ident.node.to_string();
if field.is_shorthand {
Expand Down
4 changes: 1 addition & 3 deletions src/file_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ impl FileLines {
}

/// `FileLines` files iterator.
pub struct Files<'a>(
Option<::std::collections::hash_map::Keys<'a, String, Vec<Range>>>,
);
pub struct Files<'a>(Option<::std::collections::hash_map::Keys<'a, String, Vec<Range>>>);

impl<'a> iter::Iterator for Files<'a> {
type Item = &'a String;
Expand Down
3 changes: 2 additions & 1 deletion src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ use std::cmp::Ordering;
use syntax::ast;
use syntax::codemap::{BytePos, Span};

use {Shape, Spanned};
use spanned::Spanned;
use codemap::SpanUtils;
use comment::combine_strs_with_missing_comments;
use config::IndentStyle;
use lists::{definitive_tactic, itemize_list, write_list, DefinitiveListTactic, ListFormatting,
ListItem, Separator, SeparatorPlace, SeparatorTactic};
use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
use types::{rewrite_path, PathContext};
use utils::{format_visibility, mk_sp};
use visitor::{rewrite_extern_crate, FmtVisitor};
Expand Down
8 changes: 4 additions & 4 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use syntax::{abi, ast, ptr, symbol};
use syntax::ast::ImplItem;
use syntax::codemap::{BytePos, Span};

use {Indent, Shape, Spanned};
use spanned::Spanned;
use codemap::{LineRangeUtils, SpanUtils};
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented};
Expand All @@ -26,12 +26,13 @@ use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs
use lists::{definitive_tactic, itemize_list, write_list, DefinitiveListTactic, ListFormatting,
ListItem, ListTactic, Separator, SeparatorPlace, SeparatorTactic};
use rewrite::{Rewrite, RewriteContext};
use shape::{Indent, Shape};
use types::join_bounds;
use utils::{colon_spaces, contains_skip, end_typaram, first_line_width, format_abi,
format_constness, format_defaultness, format_mutability, format_unsafety,
format_visibility, is_attributes_extendable, last_line_contains_single_line_comment,
last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, stmt_expr,
trim_newlines, trimmed_last_line_width, wrap_str};
trim_newlines, trimmed_last_line_width};
use vertical::rewrite_with_alignment;
use visitor::FmtVisitor;

Expand Down Expand Up @@ -1360,8 +1361,7 @@ pub fn rewrite_struct_field(
lhs_max_width: usize,
) -> Option<String> {
if contains_skip(&field.attrs) {
let span = context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi()));
return wrap_str(span, context.config.max_width(), shape);
return Some(context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi())));
}

let type_annotation_spacing = type_annotation_spacing(context.config);
Expand Down
Loading