Format source codes
This commit is contained in:
parent
c06d487712
commit
b8f11a4e3c
14 changed files with 79 additions and 78 deletions
|
@ -319,8 +319,8 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle) -> &'a str {
|
||||||
} else {
|
} else {
|
||||||
&line[opener.trim_right().len()..]
|
&line[opener.trim_right().len()..]
|
||||||
}
|
}
|
||||||
} else if 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("/*!") ||
|
line.starts_with("** ") || line.starts_with("/*!") ||
|
||||||
(line.starts_with("/**") && !line.starts_with("/**/"))
|
(line.starts_with("/**") && !line.starts_with("/**/"))
|
||||||
{
|
{
|
||||||
|
|
32
src/expr.rs
32
src/expr.rs
|
@ -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> {
|
fn nop_block_collapse(block_str: Option<String>, budget: usize) -> Option<String> {
|
||||||
debug!("nop_block_collapse {:?} {}", block_str, budget);
|
debug!("nop_block_collapse {:?} {}", block_str, budget);
|
||||||
block_str.map(|block_str| if block_str.starts_with('{') &&
|
block_str.map(|block_str| if block_str.starts_with('{') && budget >= 2 &&
|
||||||
budget >= 2 &&
|
(block_str[1..]
|
||||||
(block_str[1..].find(|c: char| !c.is_whitespace()).unwrap() == block_str.len() - 2)
|
.find(|c: char| !c.is_whitespace())
|
||||||
|
.unwrap() == block_str.len() - 2)
|
||||||
{
|
{
|
||||||
"{}".to_owned()
|
"{}".to_owned()
|
||||||
} else {
|
} else {
|
||||||
|
@ -1509,9 +1510,9 @@ impl Rewrite for ast::Arm {
|
||||||
let pats_str = format!("{}{}", pats_str, guard_str);
|
let pats_str = format!("{}{}", pats_str, guard_str);
|
||||||
|
|
||||||
let (mut extend, body) = match body.node {
|
let (mut extend, body) = match body.node {
|
||||||
ast::ExprKind::Block(ref block) if !is_unsafe_block(block) &&
|
ast::ExprKind::Block(ref block)
|
||||||
is_simple_block(block, context.codemap) &&
|
if !is_unsafe_block(block) && is_simple_block(block, context.codemap) &&
|
||||||
context.config.wrap_match_arms() => {
|
context.config.wrap_match_arms() => {
|
||||||
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
|
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
|
||||||
(false, &**expr)
|
(false, &**expr)
|
||||||
} else {
|
} else {
|
||||||
|
@ -2539,21 +2540,10 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
|
||||||
|
|
||||||
// FIXME: DRY!
|
// FIXME: DRY!
|
||||||
match (rhs, new_rhs) {
|
match (rhs, new_rhs) {
|
||||||
(Some(ref orig_rhs), Some(ref replacement_rhs)) if count_line_breaks(
|
(Some(ref orig_rhs), Some(ref replacement_rhs))
|
||||||
orig_rhs,
|
if count_line_breaks(orig_rhs) > count_line_breaks(replacement_rhs) + 1 ||
|
||||||
) >
|
(orig_rhs.rewrite(context, shape).is_none() &&
|
||||||
count_line_breaks(
|
replacement_rhs.rewrite(context, new_shape).is_some()) => {
|
||||||
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(&format!("\n{}", new_shape.indent.to_string(context.config)));
|
||||||
result.push_str(replacement_rhs);
|
result.push_str(replacement_rhs);
|
||||||
}
|
}
|
||||||
|
|
25
src/items.rs
25
src/items.rs
|
@ -685,12 +685,12 @@ fn format_impl_ref_and_type(
|
||||||
offset: Indent,
|
offset: Indent,
|
||||||
) -> Option<String> {
|
) -> Option<String> {
|
||||||
if let ast::ItemKind::Impl(unsafety,
|
if let ast::ItemKind::Impl(unsafety,
|
||||||
polarity,
|
polarity,
|
||||||
_,
|
_,
|
||||||
ref generics,
|
ref generics,
|
||||||
ref trait_ref,
|
ref trait_ref,
|
||||||
ref self_ty,
|
ref self_ty,
|
||||||
_) = item.node
|
_) = item.node
|
||||||
{
|
{
|
||||||
let mut result = String::new();
|
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 has_body = !trait_items.is_empty();
|
||||||
|
|
||||||
let where_density = if (context.config.where_density() == Density::Compressed &&
|
let where_density = if (context.config.where_density() == Density::Compressed &&
|
||||||
(!result.contains('\n') ||
|
(!result.contains('\n') ||
|
||||||
context.config.fn_args_layout() == IndentStyle::Block)) ||
|
context.config.fn_args_layout() == IndentStyle::Block)) ||
|
||||||
(context.config.fn_args_layout() == IndentStyle::Block && result.is_empty()) ||
|
(context.config.fn_args_layout() == IndentStyle::Block && result.is_empty()) ||
|
||||||
(context.config.where_density() == Density::CompressedIfEmpty && !has_body &&
|
(context.config.where_density() == Density::CompressedIfEmpty && !has_body &&
|
||||||
!result.contains('\n'))
|
!result.contains('\n'))
|
||||||
|
@ -1468,9 +1468,9 @@ impl Rewrite for ast::StructField {
|
||||||
Some(ref ty) if ty.contains('\n') => {
|
Some(ref ty) if ty.contains('\n') => {
|
||||||
let new_ty = rewrite_type_in_next_line();
|
let new_ty = rewrite_type_in_next_line();
|
||||||
match new_ty {
|
match new_ty {
|
||||||
Some(ref new_ty) if !new_ty.contains('\n') &&
|
Some(ref new_ty)
|
||||||
new_ty.len() + type_offset.width() <=
|
if !new_ty.contains('\n') &&
|
||||||
context.config.max_width() => {
|
new_ty.len() + type_offset.width() <= context.config.max_width() => {
|
||||||
Some(format!(
|
Some(format!(
|
||||||
"{}\n{}{}",
|
"{}\n{}{}",
|
||||||
result,
|
result,
|
||||||
|
@ -2688,7 +2688,8 @@ fn format_generics(
|
||||||
let same_line_brace = force_same_line_brace ||
|
let same_line_brace = force_same_line_brace ||
|
||||||
(generics.where_clause.predicates.is_empty() && trimmed_last_line_width(&result) == 1);
|
(generics.where_clause.predicates.is_empty() && trimmed_last_line_width(&result) == 1);
|
||||||
if !same_line_brace &&
|
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('\n');
|
||||||
result.push_str(&offset.block_only().to_string(context.config));
|
result.push_str(&offset.block_only().to_string(context.config));
|
||||||
|
|
|
@ -81,7 +81,7 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String>
|
||||||
if cur_end < cur_start + MIN_STRING {
|
if cur_end < cur_start + MIN_STRING {
|
||||||
cur_end = cur_start + max_chars;
|
cur_end = cur_start + max_chars;
|
||||||
while !(punctuation.contains(graphemes[cur_end - 1]) ||
|
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() {
|
if cur_end >= graphemes.len() {
|
||||||
let line = &graphemes[cur_start..].join("");
|
let line = &graphemes[cur_start..].join("");
|
||||||
|
|
|
@ -206,9 +206,9 @@ fn rewrite_segment(
|
||||||
|
|
||||||
let params = if let Some(ref params) = segment.parameters {
|
let params = if let Some(ref params) = segment.parameters {
|
||||||
match **params {
|
match **params {
|
||||||
ast::PathParameters::AngleBracketed(ref data) if !data.lifetimes.is_empty() ||
|
ast::PathParameters::AngleBracketed(ref data)
|
||||||
!data.types.is_empty() ||
|
if !data.lifetimes.is_empty() || !data.types.is_empty() ||
|
||||||
!data.bindings.is_empty() => {
|
!data.bindings.is_empty() => {
|
||||||
let param_list = data.lifetimes
|
let param_list = data.lifetimes
|
||||||
.iter()
|
.iter()
|
||||||
.map(SegmentParam::LifeTime)
|
.map(SegmentParam::LifeTime)
|
||||||
|
|
|
@ -546,10 +546,11 @@ impl<'a> FmtVisitor<'a> {
|
||||||
fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
|
fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
|
||||||
self.format_missing_with_indent(source!(self, span).lo);
|
self.format_missing_with_indent(source!(self, span).lo);
|
||||||
self.failed = match rewrite {
|
self.failed = match rewrite {
|
||||||
Some(ref s) if s.rewrite(
|
Some(ref s)
|
||||||
&self.get_context(),
|
if s.rewrite(
|
||||||
Shape::indented(self.block_indent, self.config),
|
&self.get_context(),
|
||||||
).is_none() => true,
|
Shape::indented(self.block_indent, self.config),
|
||||||
|
).is_none() => true,
|
||||||
None => true,
|
None => true,
|
||||||
_ => self.failed,
|
_ => self.failed,
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,7 +5,7 @@ fn main() {
|
||||||
loop {
|
loop {
|
||||||
if foo {
|
if foo {
|
||||||
if ((right_paddle_speed < 0.) &&
|
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_speed > 0.) &&
|
||||||
(right_paddle.position().y + paddle_size.y / 2. < game_height as f32 - 5.))
|
(right_paddle.position().y + paddle_size.y / 2. < game_height as f32 - 5.))
|
||||||
{
|
{
|
||||||
|
|
|
@ -14,10 +14,10 @@ fn main() {
|
||||||
|
|
||||||
// #1544
|
// #1544
|
||||||
if let VrMsg::ClientReply {
|
if let VrMsg::ClientReply {
|
||||||
request_num: reply_req_num,
|
request_num: reply_req_num,
|
||||||
value,
|
value,
|
||||||
..
|
..
|
||||||
} = msg
|
} = msg
|
||||||
{
|
{
|
||||||
let _ = safe_assert_eq!(reply_req_num, request_num, op);
|
let _ = safe_assert_eq!(reply_req_num, request_num, op);
|
||||||
return Ok((request_num, op, value));
|
return Ok((request_num, op, value));
|
||||||
|
|
|
@ -7,8 +7,8 @@ fn foo() -> bool {
|
||||||
let referenced = &5;
|
let referenced = &5;
|
||||||
|
|
||||||
let very_long_variable_name = (a + first + simple + test);
|
let very_long_variable_name = (a + first + simple + test);
|
||||||
let very_long_variable_name =
|
let very_long_variable_name = (a + first + simple + test + AAAAAAAAAAAAA +
|
||||||
(a + first + simple + test + AAAAAAAAAAAAA + BBBBBBBBBBBBBBBBB + b + c);
|
BBBBBBBBBBBBBBBBB + b + c);
|
||||||
|
|
||||||
let is_internalxxxx = self.codemap.span_to_filename(s) ==
|
let is_internalxxxx = self.codemap.span_to_filename(s) ==
|
||||||
self.codemap.span_to_filename(m.inner);
|
self.codemap.span_to_filename(m.inner);
|
||||||
|
@ -20,8 +20,10 @@ fn foo() -> bool {
|
||||||
10000 * 30000000000 + 40000 / 1002200000000 - 50000 * sqrt(-1),
|
10000 * 30000000000 + 40000 / 1002200000000 - 50000 * sqrt(-1),
|
||||||
trivial_value,
|
trivial_value,
|
||||||
);
|
);
|
||||||
(((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + a +
|
(((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
|
||||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaa))))))))) ;
|
a +
|
||||||
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
|
||||||
|
aaaaa)))))))));
|
||||||
|
|
||||||
{
|
{
|
||||||
for _ in 0..10 {}
|
for _ in 0..10 {}
|
||||||
|
@ -47,21 +49,21 @@ fn foo() -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(x) = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
|
if let Some(x) = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
|
||||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
if let (some_very_large,
|
if let (some_very_large,
|
||||||
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
|
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
if let (some_very_large,
|
if let (some_very_large,
|
||||||
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) =
|
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) =
|
||||||
1111 + 2222
|
1111 + 2222
|
||||||
{}
|
{}
|
||||||
|
|
||||||
if let (some_very_large,
|
if let (some_very_large,
|
||||||
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
|
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
|
||||||
{}
|
{}
|
||||||
|
|
||||||
let test = if true { 5 } else { 3 };
|
let test = if true { 5 } else { 3 };
|
||||||
|
|
|
@ -96,7 +96,12 @@ fn foo<g: G>() {
|
||||||
foo();
|
foo();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn foo<L: Loooooooooooooooooooooong, G: Geeeeeeeeeeeneric, I: iiiiiiiiis, L: Looooooooooooooooong>() {
|
fn foo<
|
||||||
|
L: Loooooooooooooooooooooong,
|
||||||
|
G: Geeeeeeeeeeeneric,
|
||||||
|
I: iiiiiiiiis,
|
||||||
|
L: Looooooooooooooooong,
|
||||||
|
>() {
|
||||||
foo();
|
foo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ fn main() {
|
||||||
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAa";
|
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAa";
|
||||||
|
|
||||||
if let (some_very_large,
|
if let (some_very_large,
|
||||||
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
|
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3
|
||||||
{}
|
{}
|
||||||
|
|
||||||
if cond() {
|
if cond() {
|
||||||
|
|
|
@ -12,8 +12,8 @@ fn baz(p: Packet) {
|
||||||
loop {
|
loop {
|
||||||
loop {
|
loop {
|
||||||
if let Packet::Transaction {
|
if let Packet::Transaction {
|
||||||
state: TransactionState::Committed(ts, ..), ..
|
state: TransactionState::Committed(ts, ..), ..
|
||||||
} = p
|
} = p
|
||||||
{
|
{
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,13 +67,18 @@ fn main() {
|
||||||
vec![a, b; c];
|
vec![a, b; c];
|
||||||
vec![a; b, c];
|
vec![a; b, c];
|
||||||
|
|
||||||
vec![a;
|
vec![
|
||||||
(|x| {
|
a;
|
||||||
let y = x + 1;
|
(|x| {
|
||||||
let z = y + 1;
|
let y = x + 1;
|
||||||
z
|
let z = y + 1;
|
||||||
})(2)];
|
z
|
||||||
vec![a; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx];
|
})(2)
|
||||||
|
];
|
||||||
|
vec![
|
||||||
|
a;
|
||||||
|
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||||
|
];
|
||||||
vec![a; unsafe { x + 1 }];
|
vec![a; unsafe { x + 1 }];
|
||||||
|
|
||||||
unknown_bracket_macro__comma_should_not_be_stripped![
|
unknown_bracket_macro__comma_should_not_be_stripped![
|
||||||
|
|
|
@ -37,10 +37,8 @@ fn foo() {
|
||||||
Patternnnnnnnnnnnnnnnnnnnnnnnnn if loooooooooooooooooooooooooooooooooooooooooong_guard => {}
|
Patternnnnnnnnnnnnnnnnnnnnnnnnn if loooooooooooooooooooooooooooooooooooooooooong_guard => {}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
ast::PathParameters::AngleBracketedParameters(ref data) if data.lifetimes.len() >
|
ast::PathParameters::AngleBracketedParameters(ref data)
|
||||||
0 ||
|
if data.lifetimes.len() > 0 || data.types.len() > 0 || data.bindings.len() > 0 => {}
|
||||||
data.types.len() > 0 ||
|
|
||||||
data.bindings.len() > 0 => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let whatever = match something {
|
let whatever = match something {
|
||||||
|
@ -316,16 +314,15 @@ fn issue386() {
|
||||||
|
|
||||||
fn guards() {
|
fn guards() {
|
||||||
match foo {
|
match foo {
|
||||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo &&
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||||
barrrrrrrrrrrr => {}
|
if foooooooooooooo && barrrrrrrrrrrr => {}
|
||||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
|
||||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo &&
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||||
barrrrrrrrrrrr => {}
|
if foooooooooooooo && barrrrrrrrrrrr => {}
|
||||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||||
if fooooooooooooooooooooo &&
|
if fooooooooooooooooooooo &&
|
||||||
(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || cccccccccccccccccccccccccccccccccccccccc) => {
|
(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ||
|
||||||
{}
|
cccccccccccccccccccccccccccccccccccccccc) => {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue