Skip to content

Commit

Permalink
Format source codes with OneLineStyle::Mixed
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro committed Jul 18, 2017
1 parent b26b3cb commit 22a495d
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 511 deletions.
34 changes: 9 additions & 25 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
} else {
chain_indent(context, shape.add_offset(parent_rewrite.len()))
};
(
nested_shape,
context.config.chain_indent() == IndentStyle::Visual || is_small_parent,
)
(nested_shape, context.config.chain_indent() == IndentStyle::Visual || is_small_parent)
} else if is_block_expr(context, &parent, &parent_rewrite) {
match context.config.chain_indent() {
// Try to put the first child on the same line with parent's last line
Expand Down Expand Up @@ -165,20 +162,15 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
} else {
other_child_shape
};
debug!(
"child_shapes {:?} {:?}",
first_child_shape,
other_child_shape
);
debug!("child_shapes {:?} {:?}", first_child_shape, other_child_shape);

let child_shape_iter = Some(first_child_shape).into_iter().chain(
::std::iter::repeat(other_child_shape).take(subexpr_list.len() - 1),
);
let child_shape_iter = Some(first_child_shape)
.into_iter()
.chain(::std::iter::repeat(other_child_shape).take(subexpr_list.len() - 1));
let iter = subexpr_list.iter().rev().zip(child_shape_iter);
let mut rewrites = try_opt!(
iter.map(|(e, shape)| {
rewrite_chain_subexpr(e, total_span, context, shape)
}).collect::<Option<Vec<_>>>()
iter.map(|(e, shape)| { rewrite_chain_subexpr(e, total_span, context, shape) })
.collect::<Option<Vec<_>>>()
);

// Total of all items excluding the last.
Expand Down Expand Up @@ -297,11 +289,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
first_connector,
rewrites[0],
second_connector,
join_rewrites(
&rewrites[1..],
&subexpr_list[0..subexpr_list.len() - 1],
&connector,
)
join_rewrites(&rewrites[1..], &subexpr_list[0..subexpr_list.len() - 1], &connector,)
),
context.config.max_width(),
shape,
Expand Down Expand Up @@ -362,11 +350,7 @@ pub fn rewrite_try(
shape: Shape,
) -> Option<String> {
let sub_expr = try_opt!(expr.rewrite(context, try_opt!(shape.sub_width(try_count))));
Some(format!(
"{}{}",
sub_expr,
iter::repeat("?").take(try_count).collect::<String>()
))
Some(format!("{}{}", sub_expr, iter::repeat("?").take(try_count).collect::<String>()))
}

fn join_rewrites(rewrites: &[String], subexps: &[ast::Expr], connector: &str) -> String {
Expand Down
50 changes: 10 additions & 40 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ pub fn rewrite_comment(
// we should stop now.
let num_bare_lines = orig.lines()
.map(|line| line.trim())
.filter(|l| {
!(l.starts_with('*') || l.starts_with("//") || l.starts_with("/*"))
})
.filter(|l| !(l.starts_with('*') || l.starts_with("//") || l.starts_with("/*")))
.count();
if num_bare_lines > 0 && !config.normalize_comments() {
return Some(orig.to_owned());
Expand Down Expand Up @@ -177,13 +175,8 @@ fn identify_comment(
.collect::<Vec<_>>()
.join("\n");

let first_group_str = try_opt!(rewrite_comment_inner(
&first_group,
block_style,
style,
shape,
config,
));
let first_group_str =
try_opt!(rewrite_comment_inner(&first_group, block_style, style, shape, config,));
if rest.is_empty() {
Some(first_group_str)
} else {
Expand Down Expand Up @@ -684,11 +677,7 @@ impl<'a> Iterator for CommentCodeSlices<'a> {
CodeCharKind::Comment => CodeCharKind::Normal,
CodeCharKind::Normal => CodeCharKind::Comment,
};
let res = (
kind,
self.last_slice_end,
&self.slice[self.last_slice_end..sub_slice_end],
);
let res = (kind, self.last_slice_end, &self.slice[self.last_slice_end..sub_slice_end]);
self.last_slice_end = sub_slice_end;
self.last_slice_kind = kind;

Expand Down Expand Up @@ -799,10 +788,7 @@ fn remove_comment_header(comment: &str) -> &str {
{
&comment[3..comment.len() - 2]
} else {
assert!(
comment.starts_with("/*"),
format!("string '{}' is not a comment", comment)
);
assert!(comment.starts_with("/*"), format!("string '{}' is not a comment", comment));
&comment[2..comment.len() - 2]
}
}
Expand Down Expand Up @@ -830,10 +816,7 @@ mod test {
let mut iter = CommentCodeSlices::new(input);

assert_eq!((CodeCharKind::Normal, 0, "code(); "), iter.next().unwrap());
assert_eq!(
(CodeCharKind::Comment, 8, "/* test */"),
iter.next().unwrap()
);
assert_eq!((CodeCharKind::Comment, 8, "/* test */"), iter.next().unwrap());
assert_eq!((CodeCharKind::Normal, 18, " 1 + 1"), iter.next().unwrap());
assert_eq!(None, iter.next());
}
Expand All @@ -844,14 +827,8 @@ mod test {
let mut iter = CommentCodeSlices::new(input);

assert_eq!((CodeCharKind::Normal, 0, ""), iter.next().unwrap());
assert_eq!(
(CodeCharKind::Comment, 0, "// comment\n"),
iter.next().unwrap()
);
assert_eq!(
(CodeCharKind::Normal, 11, " test();"),
iter.next().unwrap()
);
assert_eq!((CodeCharKind::Comment, 0, "// comment\n"), iter.next().unwrap());
assert_eq!((CodeCharKind::Normal, 11, " test();"), iter.next().unwrap());
assert_eq!(None, iter.next());
}

Expand Down Expand Up @@ -924,10 +901,7 @@ mod test {
#[test]
fn test_uncommented() {
assert_eq!(&uncommented("abc/*...*/"), "abc");
assert_eq!(
&uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"),
"..ac\n"
);
assert_eq!(&uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"), "..ac\n");
assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
}

Expand All @@ -948,11 +922,7 @@ mod test {
check("/*/ */test", "test", Some(6));
check("//test\ntest", "test", Some(7));
check("/* comment only */", "whatever", None);
check(
"/* comment */ some text /* more commentary */ result",
"result",
Some(46),
);
check("/* comment */ some text /* more commentary */ result", "result", Some(46));
check("sup // sup", "p", Some(2));
check("sup", "x", None);
check(r#"π? /**/ π is nice!"#, r#"π is nice"#, Some(9));
Expand Down
5 changes: 1 addition & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,7 @@ mod test {

let used_options = config.used_options();
let toml = used_options.to_toml().unwrap();
assert_eq!(
toml,
format!("verbose = {}\nskip_children = {}\n", verbose, skip_children)
);
assert_eq!(toml, format!("verbose = {}\nskip_children = {}\n", verbose, skip_children));
}

#[test]
Expand Down

0 comments on commit 22a495d

Please sign in to comment.