Format source codes

This commit is contained in:
topecongiro 2017-06-14 20:39:07 +09:00
parent c06d487712
commit b8f11a4e3c
14 changed files with 79 additions and 78 deletions

View File

@ -319,8 +319,8 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle) -> &'a str {
} else {
&line[opener.trim_right().len()..]
}
} else if line.starts_with("/* ") || line.starts_with("// ") ||
line.starts_with("//!") || line.starts_with("///") ||
} else if line.starts_with("/* ") || line.starts_with("// ") || line.starts_with("//!") ||
line.starts_with("///") ||
line.starts_with("** ") || line.starts_with("/*!") ||
(line.starts_with("/**") && !line.starts_with("/**/"))
{

View File

@ -729,9 +729,10 @@ fn and_one_line(x: Option<String>) -> Option<String> {
fn nop_block_collapse(block_str: Option<String>, budget: usize) -> Option<String> {
debug!("nop_block_collapse {:?} {}", block_str, budget);
block_str.map(|block_str| if block_str.starts_with('{') &&
budget >= 2 &&
(block_str[1..].find(|c: char| !c.is_whitespace()).unwrap() == block_str.len() - 2)
block_str.map(|block_str| if block_str.starts_with('{') && budget >= 2 &&
(block_str[1..]
.find(|c: char| !c.is_whitespace())
.unwrap() == block_str.len() - 2)
{
"{}".to_owned()
} else {
@ -1509,9 +1510,9 @@ impl Rewrite for ast::Arm {
let pats_str = format!("{}{}", pats_str, guard_str);
let (mut extend, body) = match body.node {
ast::ExprKind::Block(ref block) if !is_unsafe_block(block) &&
is_simple_block(block, context.codemap) &&
context.config.wrap_match_arms() => {
ast::ExprKind::Block(ref block)
if !is_unsafe_block(block) && is_simple_block(block, context.codemap) &&
context.config.wrap_match_arms() => {
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
(false, &**expr)
} else {
@ -2539,21 +2540,10 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
// FIXME: DRY!
match (rhs, new_rhs) {
(Some(ref orig_rhs), Some(ref replacement_rhs)) if count_line_breaks(
orig_rhs,
) >
count_line_breaks(
replacement_rhs,
) + 1 ||
(orig_rhs
.rewrite(context, shape)
.is_none() &&
replacement_rhs
.rewrite(
context,
new_shape,
)
.is_some()) => {
(Some(ref orig_rhs), Some(ref replacement_rhs))
if count_line_breaks(orig_rhs) > count_line_breaks(replacement_rhs) + 1 ||
(orig_rhs.rewrite(context, shape).is_none() &&
replacement_rhs.rewrite(context, new_shape).is_some()) => {
result.push_str(&format!("\n{}", new_shape.indent.to_string(context.config)));
result.push_str(replacement_rhs);
}

View File

@ -685,12 +685,12 @@ fn format_impl_ref_and_type(
offset: Indent,
) -> Option<String> {
if let ast::ItemKind::Impl(unsafety,
polarity,
_,
ref generics,
ref trait_ref,
ref self_ty,
_) = item.node
polarity,
_,
ref generics,
ref trait_ref,
ref self_ty,
_) = item.node
{
let mut result = String::new();
@ -942,8 +942,8 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
let has_body = !trait_items.is_empty();
let where_density = if (context.config.where_density() == Density::Compressed &&
(!result.contains('\n') ||
context.config.fn_args_layout() == IndentStyle::Block)) ||
(!result.contains('\n') ||
context.config.fn_args_layout() == IndentStyle::Block)) ||
(context.config.fn_args_layout() == IndentStyle::Block && result.is_empty()) ||
(context.config.where_density() == Density::CompressedIfEmpty && !has_body &&
!result.contains('\n'))
@ -1468,9 +1468,9 @@ impl Rewrite for ast::StructField {
Some(ref ty) if ty.contains('\n') => {
let new_ty = rewrite_type_in_next_line();
match new_ty {
Some(ref new_ty) if !new_ty.contains('\n') &&
new_ty.len() + type_offset.width() <=
context.config.max_width() => {
Some(ref new_ty)
if !new_ty.contains('\n') &&
new_ty.len() + type_offset.width() <= context.config.max_width() => {
Some(format!(
"{}\n{}{}",
result,
@ -2688,7 +2688,8 @@ fn format_generics(
let same_line_brace = force_same_line_brace ||
(generics.where_clause.predicates.is_empty() && trimmed_last_line_width(&result) == 1);
if !same_line_brace &&
(brace_style == BraceStyle::SameLineWhere || brace_style == BraceStyle::AlwaysNextLine)
(brace_style == BraceStyle::SameLineWhere ||
brace_style == BraceStyle::AlwaysNextLine)
{
result.push('\n');
result.push_str(&offset.block_only().to_string(context.config));

View File

@ -81,7 +81,7 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String>
if cur_end < cur_start + MIN_STRING {
cur_end = cur_start + max_chars;
while !(punctuation.contains(graphemes[cur_end - 1]) ||
graphemes[cur_end - 1].trim().is_empty())
graphemes[cur_end - 1].trim().is_empty())
{
if cur_end >= graphemes.len() {
let line = &graphemes[cur_start..].join("");

View File

@ -206,9 +206,9 @@ fn rewrite_segment(
let params = if let Some(ref params) = segment.parameters {
match **params {
ast::PathParameters::AngleBracketed(ref data) if !data.lifetimes.is_empty() ||
!data.types.is_empty() ||
!data.bindings.is_empty() => {
ast::PathParameters::AngleBracketed(ref data)
if !data.lifetimes.is_empty() || !data.types.is_empty() ||
!data.bindings.is_empty() => {
let param_list = data.lifetimes
.iter()
.map(SegmentParam::LifeTime)

View File

@ -546,10 +546,11 @@ impl<'a> FmtVisitor<'a> {
fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
self.format_missing_with_indent(source!(self, span).lo);
self.failed = match rewrite {
Some(ref s) if s.rewrite(
&self.get_context(),
Shape::indented(self.block_indent, self.config),
).is_none() => true,
Some(ref s)
if s.rewrite(
&self.get_context(),
Shape::indented(self.block_indent, self.config),
).is_none() => true,
None => true,
_ => self.failed,
};

View File

@ -5,7 +5,7 @@ fn main() {
loop {
if foo {
if ((right_paddle_speed < 0.) &&
(right_paddle.position().y - paddle_size.y / 2. > 5.)) ||
(right_paddle.position().y - paddle_size.y / 2. > 5.)) ||
((right_paddle_speed > 0.) &&
(right_paddle.position().y + paddle_size.y / 2. < game_height as f32 - 5.))
{

View File

@ -14,10 +14,10 @@ fn main() {
// #1544
if let VrMsg::ClientReply {
request_num: reply_req_num,
value,
..
} = msg
request_num: reply_req_num,
value,
..
} = msg
{
let _ = safe_assert_eq!(reply_req_num, request_num, op);
return Ok((request_num, op, value));

View File

@ -7,8 +7,8 @@ fn foo() -> bool {
let referenced = &5;
let very_long_variable_name = (a + first + simple + test);
let very_long_variable_name =
(a + first + simple + test + AAAAAAAAAAAAA + BBBBBBBBBBBBBBBBB + b + c);
let very_long_variable_name = (a + first + simple + test + AAAAAAAAAAAAA +
BBBBBBBBBBBBBBBBB + b + c);
let is_internalxxxx = self.codemap.span_to_filename(s) ==
self.codemap.span_to_filename(m.inner);
@ -20,8 +20,10 @@ fn foo() -> bool {
10000 * 30000000000 + 40000 / 1002200000000 - 50000 * sqrt(-1),
trivial_value,
);
(((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + a +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaa))))))))) ;
(((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
a +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaa)))))))));
{
for _ in 0..10 {}
@ -47,21 +49,21 @@ fn foo() -> bool {
}
if let Some(x) = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
{}
if let (some_very_large,
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
{
}
if let (some_very_large,
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) =
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) =
1111 + 2222
{}
if let (some_very_large,
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
{}
let test = if true { 5 } else { 3 };

View File

@ -96,7 +96,12 @@ fn foo<g: G>() {
foo();
}
fn foo<L: Loooooooooooooooooooooong, G: Geeeeeeeeeeeneric, I: iiiiiiiiis, L: Looooooooooooooooong>() {
fn foo<
L: Loooooooooooooooooooooong,
G: Geeeeeeeeeeeneric,
I: iiiiiiiiis,
L: Looooooooooooooooong,
>() {
foo();
}

View File

@ -22,7 +22,7 @@ fn main() {
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAa";
if let (some_very_large,
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
{}
if cond() {

View File

@ -12,8 +12,8 @@ fn baz(p: Packet) {
loop {
loop {
if let Packet::Transaction {
state: TransactionState::Committed(ts, ..), ..
} = p
state: TransactionState::Committed(ts, ..), ..
} = p
{
unreachable!()
}

View File

@ -67,13 +67,18 @@ fn main() {
vec![a, b; c];
vec![a; b, c];
vec![a;
(|x| {
let y = x + 1;
let z = y + 1;
z
})(2)];
vec![a; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx];
vec![
a;
(|x| {
let y = x + 1;
let z = y + 1;
z
})(2)
];
vec![
a;
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
];
vec![a; unsafe { x + 1 }];
unknown_bracket_macro__comma_should_not_be_stripped![

View File

@ -37,10 +37,8 @@ fn foo() {
Patternnnnnnnnnnnnnnnnnnnnnnnnn if loooooooooooooooooooooooooooooooooooooooooong_guard => {}
_ => {}
ast::PathParameters::AngleBracketedParameters(ref data) if data.lifetimes.len() >
0 ||
data.types.len() > 0 ||
data.bindings.len() > 0 => {}
ast::PathParameters::AngleBracketedParameters(ref data)
if data.lifetimes.len() > 0 || data.types.len() > 0 || data.bindings.len() > 0 => {}
}
let whatever = match something {
@ -316,16 +314,15 @@ fn issue386() {
fn guards() {
match foo {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo &&
barrrrrrrrrrrr => {}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
if foooooooooooooo && barrrrrrrrrrrr => {}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo &&
barrrrrrrrrrrr => {}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
if foooooooooooooo && barrrrrrrrrrrr => {}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
if fooooooooooooooooooooo &&
(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || cccccccccccccccccccccccccccccccccccccccc) => {
{}
}
(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ||
cccccccccccccccccccccccccccccccccccccccc) => {}
}
}