1
Fork 0

Format source codes

This commit is contained in:
topecongiro 2017-05-23 22:13:29 +09:00
parent cb4a6a2419
commit ec33121aaf
8 changed files with 104 additions and 126 deletions

View file

@ -165,8 +165,7 @@ fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option<
}
} else {
""
}
.trim_right()
}.trim_right()
})
.collect();
Some(lines.join(&format!("\n{}", offset.to_string(config))))

View file

@ -316,8 +316,7 @@ pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,
let lhs_budget = try_opt!(context
.config
.max_width()
.checked_sub(shape.used_width() + prefix.len() +
infix.len()));
.checked_sub(shape.used_width() + prefix.len() + infix.len()));
let rhs_shape = match context.config.control_style() {
Style::Default => {
try_opt!(shape.sub_width(suffix.len() + prefix.len())).visual_indent(prefix.len())
@ -853,8 +852,8 @@ impl<'a> ControlFlow<'a> {
let new_width = try_opt!(new_width.checked_sub(if_str.len()));
let else_expr = &else_node.stmts[0];
let else_str = try_opt!(else_expr.rewrite(context,
Shape::legacy(new_width, Indent::empty())));
let else_str =
try_opt!(else_expr.rewrite(context, Shape::legacy(new_width, Indent::empty())));
if if_str.contains('\n') || else_str.contains('\n') {
return None;
@ -953,8 +952,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
};
// for event in event
let between_kwd_cond =
mk_sp(context.codemap.span_after(self.span, self.keyword.trim()),
let between_kwd_cond = mk_sp(context.codemap.span_after(self.span, self.keyword.trim()),
self.pat
.map_or(cond_span.lo, |p| if self.matcher.is_empty() {
p.span.lo
@ -1042,8 +1040,9 @@ impl<'a> Rewrite for ControlFlow<'a> {
let between_kwd_else_block_comment =
extract_comment(between_kwd_else_block, context, shape);
let after_else = mk_sp(context.codemap.span_after(mk_sp(self.block.span.hi,
else_block.span.lo),
let after_else = mk_sp(context
.codemap
.span_after(mk_sp(self.block.span.hi, else_block.span.lo),
"else"),
else_block.span.lo);
let after_else_comment = extract_comment(after_else, context, shape);

View file

@ -1062,11 +1062,11 @@ fn format_tuple_struct(context: &RewriteContext,
|field| field.rewrite(context, Shape::legacy(item_budget, item_indent)),
context.codemap.span_after(span, "("),
span.hi);
let body_budget = try_opt!(context
let body_budget =
try_opt!(context
.config
.max_width()
.checked_sub(offset.block_only().width() + result.len() +
3));
.checked_sub(offset.block_only().width() + result.len() + 3));
let body = try_opt!(list_helper(items,
// TODO budget is wrong in block case
Shape::legacy(body_budget, item_indent),
@ -1126,8 +1126,7 @@ pub fn rewrite_type_alias(context: &RewriteContext,
let generics_indent = indent + result.len();
let generics_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo);
let shape = try_opt!(Shape::indented(generics_indent, context.config)
.sub_width(" =".len()));
let shape = try_opt!(Shape::indented(generics_indent, context.config).sub_width(" =".len()));
let generics_str = try_opt!(rewrite_generics(context, generics, shape, generics_span));
result.push_str(&generics_str);
@ -1310,17 +1309,19 @@ pub fn rewrite_associated_type(ident: ast::Ident,
-> Option<String> {
let prefix = format!("type {}", ident);
let type_bounds_str =
if let Some(ty_param_bounds) = ty_param_bounds_opt {
let type_bounds_str = if let Some(ty_param_bounds) = ty_param_bounds_opt {
let joiner = match context.config.type_punctuation_density() {
TypeDensity::Compressed => "+",
TypeDensity::Wide => " + ",
};
let bounds: &[_] = ty_param_bounds;
let bound_str = try_opt!(bounds
let bound_str =
try_opt!(bounds
.iter()
.map(|ty_bound| {
ty_bound.rewrite(context, Shape::legacy(context.config.max_width(), indent))
ty_bound.rewrite(context,
Shape::legacy(context.config.max_width(),
indent))
})
.intersperse(Some(joiner.to_string()))
.collect::<Option<String>>());
@ -1931,8 +1932,7 @@ fn compute_budgets_for_args(context: &RewriteContext,
let multi_line_budget = try_opt!(context
.config
.max_width()
.checked_sub(indent.width() + result.len() +
4));
.checked_sub(indent.width() + result.len() + 4));
return Some((one_line_budget, multi_line_budget, indent + result.len() + 1));
}

View file

@ -364,8 +364,7 @@ impl Rewrite for ast::WherePredicate {
let colon = type_bound_colon(context);
if !bound_lifetimes.is_empty() {
let lifetime_str: String =
try_opt!(bound_lifetimes
let lifetime_str: String = try_opt!(bound_lifetimes
.iter()
.map(|lt| lt.rewrite(context, shape))
.intersperse(Some(", ".to_string()))
@ -378,14 +377,10 @@ impl Rewrite for ast::WherePredicate {
// 6 = "for<> ".len()
let used_width = lifetime_str.len() + type_str.len() + colon.len() + 6;
let budget = try_opt!(shape.width.checked_sub(used_width));
let bounds_str: String =
try_opt!(bounds
let bounds_str: String = try_opt!(bounds
.iter()
.map(|ty_bound| {
ty_bound.rewrite(context,
Shape::legacy(budget,
shape.indent +
used_width))
ty_bound.rewrite(context, Shape::legacy(budget, shape.indent + used_width))
})
.intersperse(Some(joiner.to_string()))
.collect());
@ -406,14 +401,10 @@ impl Rewrite for ast::WherePredicate {
};
let used_width = type_str.len() + colon.len();
let budget = try_opt!(shape.width.checked_sub(used_width));
let bounds_str: String =
try_opt!(bounds
let bounds_str: String = try_opt!(bounds
.iter()
.map(|ty_bound| {
ty_bound.rewrite(context,
Shape::legacy(budget,
shape.indent +
used_width))
ty_bound.rewrite(context, Shape::legacy(budget, shape.indent + used_width))
})
.intersperse(Some(joiner.to_string()))
.collect());
@ -532,8 +523,7 @@ impl Rewrite for ast::TyParam {
TypeDensity::Compressed => "+",
TypeDensity::Wide => " + ",
};
let bounds: String =
try_opt!(self.bounds
let bounds: String = try_opt!(self.bounds
.iter()
.map(|ty_bound| ty_bound.rewrite(context, shape))
.intersperse(Some(joiner.to_string()))
@ -612,8 +602,7 @@ impl Rewrite for ast::Ty {
let lt_budget = try_opt!(shape.width.checked_sub(2 + mut_len));
let lt_str = try_opt!(lifetime.rewrite(context,
Shape::legacy(lt_budget,
shape.indent +
2 +
shape.indent + 2 +
mut_len)));
let lt_len = lt_str.len();
let budget = try_opt!(shape.width.checked_sub(2 + mut_len + lt_len));

View file

@ -68,16 +68,14 @@ fn floaters() {
let x = Foo {
field1: val1,
field2: val2,
}
.method_call()
}.method_call()
.method_call();
let y = if cond {
val1
} else {
val2
}
.method_call();
}.method_call();
{
match x {
@ -86,8 +84,7 @@ fn floaters() {
stack.push(mparams[match cur.to_digit(10) {
Some(d) => d as usize - 1,
None => return Err("bad param number".to_owned()),
}]
.clone());
}].clone());
}
}
}
@ -96,8 +93,7 @@ fn floaters() {
some();
} else {
none();
}
.bar()
}.bar()
.baz();
Foo { x: val }
@ -110,8 +106,7 @@ fn floaters() {
Foo {
y: i_am_multi_line,
z: ok,
}
.baz(|| {
}.baz(|| {
force();
multiline();
})
@ -121,8 +116,7 @@ fn floaters() {
match x {
true => "yay!",
false => "boo!",
}
.bar()
}.bar()
}
fn is_replaced_content() -> bool {

View file

@ -37,8 +37,7 @@ fn issue1420() {
# Getting started
...
"#,
)
.running(waltz)
).running(waltz)
}
// #1563

View file

@ -4,8 +4,7 @@ fn floaters() {
let x = Foo {
field1: val1,
field2: val2,
}
.method_call()
}.method_call()
.method_call();
let y = if cond {

View file

@ -5,8 +5,7 @@ fn floaters() {
let x = Foo {
field1: val1,
field2: val2,
}
.method_call()
}.method_call()
.method_call();
let y = if cond { val1 } else { val2 }.method_call();