diff --git a/src/expr.rs b/src/expr.rs index 699e8091d81..e06c928044a 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -36,7 +36,7 @@ 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; @@ -76,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()); @@ -153,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 { @@ -168,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) @@ -190,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) } @@ -302,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 @@ -383,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)); + } } } @@ -2665,11 +2648,7 @@ pub fn rewrite_field( prefix_max_width: usize, ) -> Option { 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 { diff --git a/src/items.rs b/src/items.rs index 7f2bbe906da..9d35c83501f 100644 --- a/src/items.rs +++ b/src/items.rs @@ -32,7 +32,7 @@ use utils::{colon_spaces, contains_skip, end_typaram, first_line_width, format_a 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; @@ -1361,8 +1361,7 @@ pub fn rewrite_struct_field( lhs_max_width: usize, ) -> Option { 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); diff --git a/src/lib.rs b/src/lib.rs index c78aa050984..14c50563baa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,6 @@ extern crate syntax; extern crate term; extern crate unicode_segmentation; -use std::borrow::Cow; use std::collections::HashMap; use std::fmt; use std::io::{self, stdout, Write}; diff --git a/src/patterns.rs b/src/patterns.rs index d1b080f4a38..c58a98627ff 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -22,7 +22,7 @@ use lists::{itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_sh use rewrite::{Rewrite, RewriteContext}; use shape::Shape; use types::{rewrite_path, PathContext}; -use utils::{format_mutability, mk_sp, wrap_str}; +use utils::{format_mutability, mk_sp}; impl Rewrite for Pat { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { @@ -51,8 +51,7 @@ impl Rewrite for Pat { None => "".to_owned(), }; - let result = format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat); - wrap_str(result, context.config.max_width(), shape) + Some(format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat)) } PatKind::Wild => if 1 <= shape.width { Some("_".to_owned()) @@ -125,17 +124,13 @@ impl Rewrite for Pat { } else { format!("[{}]", pats.join(", ")) }; - wrap_str(result, context.config.max_width(), shape) + Some(result) } PatKind::Struct(ref path, ref fields, elipses) => { rewrite_struct_pat(path, fields, elipses, self.span, context, shape) } // FIXME(#819) format pattern macros. - PatKind::Mac(..) => wrap_str( - context.snippet(self.span), - context.config.max_width(), - shape, - ), + PatKind::Mac(..) => Some(context.snippet(self.span)), } } } @@ -225,11 +220,21 @@ impl Rewrite for FieldPat { if self.is_shorthand { pat } else { - wrap_str( - format!("{}: {}", self.ident.to_string(), try_opt!(pat)), - context.config.max_width(), - shape, - ) + let pat_str = try_opt!(pat); + let id_str = self.ident.to_string(); + let one_line_width = id_str.len() + 2 + pat_str.len(); + if one_line_width <= shape.width { + Some(format!("{}: {}", id_str, pat_str)) + } else { + let nested_shape = shape.block_indent(context.config.tab_spaces()); + let pat_str = try_opt!(self.pat.rewrite(context, nested_shape)); + Some(format!( + "{}:\n{}{}", + id_str, + nested_shape.indent.to_string(context.config), + pat_str, + )) + } } } } diff --git a/src/types.rs b/src/types.rs index 64f5ba93555..311192889d3 100644 --- a/src/types.rs +++ b/src/types.rs @@ -26,7 +26,7 @@ use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListTac SeparatorPlace, SeparatorTactic}; use rewrite::{Rewrite, RewriteContext}; use shape::Shape; -use utils::{colon_spaces, extra_offset, format_mutability, last_line_width, mk_sp, wrap_str}; +use utils::{colon_spaces, extra_offset, format_mutability, last_line_width, mk_sp}; #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum PathContext { @@ -504,7 +504,7 @@ impl Rewrite for ast::WherePredicate { } }; - wrap_str(result, context.config.max_width(), shape) + Some(result) } } @@ -542,7 +542,7 @@ where colon, join_bounds(context, try_opt!(shape.sub_width(overhead)), &appendix) ); - wrap_str(result, context.config.max_width(), shape) + Some(result) } } @@ -565,12 +565,8 @@ impl Rewrite for ast::TyParamBound { } impl Rewrite for ast::Lifetime { - fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - wrap_str( - pprust::lifetime_to_string(self), - context.config.max_width(), - shape, - ) + fn rewrite(&self, _: &RewriteContext, _: Shape) -> Option { + Some(pprust::lifetime_to_string(self)) } } @@ -612,7 +608,7 @@ impl Rewrite for ast::TyParam { result.push_str(&rewrite); } - wrap_str(result, context.config.max_width(), shape) + Some(result) } }