Format source codes
This commit is contained in:
parent
cb4a6a2419
commit
ec33121aaf
@ -158,15 +158,14 @@ fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option<
|
||||
// `*` in `/*`.
|
||||
let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
|
||||
if let Some(fnw) = first_non_whitespace {
|
||||
if l.as_bytes()[fnw] == '*' as u8 && fnw > 0 {
|
||||
&l[fnw - 1..]
|
||||
} else {
|
||||
&l[fnw..]
|
||||
}
|
||||
if l.as_bytes()[fnw] == '*' as u8 && fnw > 0 {
|
||||
&l[fnw - 1..]
|
||||
} else {
|
||||
""
|
||||
&l[fnw..]
|
||||
}
|
||||
.trim_right()
|
||||
} else {
|
||||
""
|
||||
}.trim_right()
|
||||
})
|
||||
.collect();
|
||||
Some(lines.join(&format!("\n{}", offset.to_string(config))))
|
||||
|
29
src/expr.rs
29
src/expr.rs
@ -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,14 +952,13 @@ 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()),
|
||||
self.pat
|
||||
.map_or(cond_span.lo, |p| if self.matcher.is_empty() {
|
||||
p.span.lo
|
||||
} else {
|
||||
context.codemap.span_before(self.span, self.matcher.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
|
||||
} else {
|
||||
context.codemap.span_before(self.span, self.matcher.trim())
|
||||
}));
|
||||
|
||||
let between_kwd_cond_comment = extract_comment(between_kwd_cond, context, shape);
|
||||
|
||||
@ -1042,9 +1040,10 @@ 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),
|
||||
"else"),
|
||||
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);
|
||||
|
||||
|
58
src/items.rs
58
src/items.rs
@ -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
|
||||
.config
|
||||
.max_width()
|
||||
.checked_sub(offset.block_only().width() + result.len() +
|
||||
3));
|
||||
let body_budget =
|
||||
try_opt!(context
|
||||
.config
|
||||
.max_width()
|
||||
.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,28 +1309,30 @@ 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 joiner = match context.config.type_punctuation_density() {
|
||||
TypeDensity::Compressed => "+",
|
||||
TypeDensity::Wide => " + ",
|
||||
};
|
||||
let bounds: &[_] = ty_param_bounds;
|
||||
let bound_str = try_opt!(bounds
|
||||
.iter()
|
||||
.map(|ty_bound| {
|
||||
ty_bound.rewrite(context, Shape::legacy(context.config.max_width(), indent))
|
||||
})
|
||||
.intersperse(Some(joiner.to_string()))
|
||||
.collect::<Option<String>>());
|
||||
if bounds.len() > 0 {
|
||||
format!(": {}", bound_str)
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
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
|
||||
.iter()
|
||||
.map(|ty_bound| {
|
||||
ty_bound.rewrite(context,
|
||||
Shape::legacy(context.config.max_width(),
|
||||
indent))
|
||||
})
|
||||
.intersperse(Some(joiner.to_string()))
|
||||
.collect::<Option<String>>());
|
||||
if bounds.len() > 0 {
|
||||
format!(": {}", bound_str)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
if let Some(ty) = ty_opt {
|
||||
let ty_str = try_opt!(ty.rewrite(context,
|
||||
@ -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));
|
||||
}
|
||||
|
61
src/types.rs
61
src/types.rs
@ -364,12 +364,11 @@ impl Rewrite for ast::WherePredicate {
|
||||
let colon = type_bound_colon(context);
|
||||
|
||||
if !bound_lifetimes.is_empty() {
|
||||
let lifetime_str: String =
|
||||
try_opt!(bound_lifetimes
|
||||
.iter()
|
||||
.map(|lt| lt.rewrite(context, shape))
|
||||
.intersperse(Some(", ".to_string()))
|
||||
.collect());
|
||||
let lifetime_str: String = try_opt!(bound_lifetimes
|
||||
.iter()
|
||||
.map(|lt| lt.rewrite(context, shape))
|
||||
.intersperse(Some(", ".to_string()))
|
||||
.collect());
|
||||
|
||||
let joiner = match context.config.type_punctuation_density() {
|
||||
TypeDensity::Compressed => "+",
|
||||
@ -378,17 +377,13 @@ 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
|
||||
.iter()
|
||||
.map(|ty_bound| {
|
||||
ty_bound.rewrite(context,
|
||||
Shape::legacy(budget,
|
||||
shape.indent +
|
||||
used_width))
|
||||
})
|
||||
.intersperse(Some(joiner.to_string()))
|
||||
.collect());
|
||||
let bounds_str: String = try_opt!(bounds
|
||||
.iter()
|
||||
.map(|ty_bound| {
|
||||
ty_bound.rewrite(context, Shape::legacy(budget, shape.indent + used_width))
|
||||
})
|
||||
.intersperse(Some(joiner.to_string()))
|
||||
.collect());
|
||||
|
||||
if context.config.spaces_within_angle_brackets() && lifetime_str.len() > 0 {
|
||||
format!("for< {} > {}{}{}",
|
||||
@ -406,17 +401,13 @@ 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
|
||||
.iter()
|
||||
.map(|ty_bound| {
|
||||
ty_bound.rewrite(context,
|
||||
Shape::legacy(budget,
|
||||
shape.indent +
|
||||
used_width))
|
||||
})
|
||||
.intersperse(Some(joiner.to_string()))
|
||||
.collect());
|
||||
let bounds_str: String = try_opt!(bounds
|
||||
.iter()
|
||||
.map(|ty_bound| {
|
||||
ty_bound.rewrite(context, Shape::legacy(budget, shape.indent + used_width))
|
||||
})
|
||||
.intersperse(Some(joiner.to_string()))
|
||||
.collect());
|
||||
|
||||
format!("{}{}{}", type_str, colon, bounds_str)
|
||||
}
|
||||
@ -532,12 +523,11 @@ impl Rewrite for ast::TyParam {
|
||||
TypeDensity::Compressed => "+",
|
||||
TypeDensity::Wide => " + ",
|
||||
};
|
||||
let bounds: String =
|
||||
try_opt!(self.bounds
|
||||
.iter()
|
||||
.map(|ty_bound| ty_bound.rewrite(context, shape))
|
||||
.intersperse(Some(joiner.to_string()))
|
||||
.collect());
|
||||
let bounds: String = try_opt!(self.bounds
|
||||
.iter()
|
||||
.map(|ty_bound| ty_bound.rewrite(context, shape))
|
||||
.intersperse(Some(joiner.to_string()))
|
||||
.collect());
|
||||
|
||||
result.push_str(&bounds);
|
||||
}
|
||||
@ -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));
|
||||
|
@ -66,38 +66,34 @@ fn floaters() {
|
||||
};
|
||||
|
||||
let x = Foo {
|
||||
field1: val1,
|
||||
field2: val2,
|
||||
}
|
||||
.method_call()
|
||||
field1: val1,
|
||||
field2: val2,
|
||||
}.method_call()
|
||||
.method_call();
|
||||
|
||||
let y = if cond {
|
||||
val1
|
||||
} else {
|
||||
val2
|
||||
}
|
||||
.method_call();
|
||||
val1
|
||||
} else {
|
||||
val2
|
||||
}.method_call();
|
||||
|
||||
{
|
||||
match x {
|
||||
PushParam => {
|
||||
// params are 1-indexed
|
||||
stack.push(mparams[match cur.to_digit(10) {
|
||||
Some(d) => d as usize - 1,
|
||||
None => return Err("bad param number".to_owned()),
|
||||
}]
|
||||
.clone());
|
||||
Some(d) => d as usize - 1,
|
||||
None => return Err("bad param number".to_owned()),
|
||||
}].clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cond {
|
||||
some();
|
||||
} else {
|
||||
none();
|
||||
}
|
||||
.bar()
|
||||
some();
|
||||
} else {
|
||||
none();
|
||||
}.bar()
|
||||
.baz();
|
||||
|
||||
Foo { x: val }
|
||||
@ -108,21 +104,19 @@ fn floaters() {
|
||||
.quux();
|
||||
|
||||
Foo {
|
||||
y: i_am_multi_line,
|
||||
z: ok,
|
||||
}
|
||||
.baz(|| {
|
||||
force();
|
||||
multiline();
|
||||
})
|
||||
y: i_am_multi_line,
|
||||
z: ok,
|
||||
}.baz(|| {
|
||||
force();
|
||||
multiline();
|
||||
})
|
||||
.quux();
|
||||
|
||||
a +
|
||||
match x {
|
||||
true => "yay!",
|
||||
false => "boo!",
|
||||
}
|
||||
.bar()
|
||||
true => "yay!",
|
||||
false => "boo!",
|
||||
}.bar()
|
||||
}
|
||||
|
||||
fn is_replaced_content() -> bool {
|
||||
|
@ -37,8 +37,7 @@ fn issue1420() {
|
||||
# Getting started
|
||||
...
|
||||
"#,
|
||||
)
|
||||
.running(waltz)
|
||||
).running(waltz)
|
||||
}
|
||||
|
||||
// #1563
|
||||
|
@ -2,10 +2,9 @@
|
||||
|
||||
fn floaters() {
|
||||
let x = Foo {
|
||||
field1: val1,
|
||||
field2: val2,
|
||||
}
|
||||
.method_call()
|
||||
field1: val1,
|
||||
field2: val2,
|
||||
}.method_call()
|
||||
.method_call();
|
||||
|
||||
let y = if cond {
|
||||
|
@ -3,10 +3,9 @@
|
||||
|
||||
fn floaters() {
|
||||
let x = Foo {
|
||||
field1: val1,
|
||||
field2: val2,
|
||||
}
|
||||
.method_call()
|
||||
field1: val1,
|
||||
field2: val2,
|
||||
}.method_call()
|
||||
.method_call();
|
||||
|
||||
let y = if cond { val1 } else { val2 }.method_call();
|
||||
|
Loading…
x
Reference in New Issue
Block a user