Format source codes

This commit is contained in:
topecongiro 2017-05-03 11:26:25 +09:00
parent e91d498b8f
commit f5da9d779f
4 changed files with 11 additions and 24 deletions

View file

@ -123,10 +123,8 @@ impl FileLines {
Some(ref map) => map,
};
match canonicalize_path_string(range.file_name()).and_then(|canonical| {
map.get_vec(&canonical)
.ok_or(())
}) {
match canonicalize_path_string(range.file_name())
.and_then(|canonical| map.get_vec(&canonical).ok_or(())) {
Ok(ranges) => ranges.iter().any(|r| r.contains(Range::from(range))),
Err(_) => false,
}
@ -140,10 +138,8 @@ impl FileLines {
Some(ref map) => map,
};
match canonicalize_path_string(range.file_name()).and_then(|canonical| {
map.get_vec(&canonical)
.ok_or(())
}) {
match canonicalize_path_string(range.file_name())
.and_then(|canonical| map.get_vec(&canonical).ok_or(())) {
Ok(ranges) => ranges.iter().any(|r| r.intersects(Range::from(range))),
Err(_) => false,
}

View file

@ -278,8 +278,7 @@ impl<'a> FmtVisitor<'a> {
result.push(' ');
}
self.single_line_fn(&result, block)
.or_else(|| Some(result))
self.single_line_fn(&result, block).or_else(|| Some(result))
}
pub fn rewrite_required_fn(&mut self,
@ -912,9 +911,7 @@ fn format_struct_struct(context: &RewriteContext,
let snippet = context.snippet(mk_sp(body_lo, span.hi - BytePos(1)));
if snippet.trim().is_empty() {
// `struct S {}`
} else if snippet
.trim_right_matches(&[' ', '\t'][..])
.ends_with('\n') {
} else if snippet.trim_right_matches(&[' ', '\t'][..]).ends_with('\n') {
// fix indent
result.push_str(&snippet.trim_right());
result.push('\n');
@ -1030,9 +1027,7 @@ fn format_tuple_struct(context: &RewriteContext,
let snippet = context.snippet(mk_sp(body_lo, context.codemap.span_before(span, ")")));
if snippet.is_empty() {
// `struct S ()`
} else if snippet
.trim_right_matches(&[' ', '\t'][..])
.ends_with('\n') {
} else if snippet.trim_right_matches(&[' ', '\t'][..]).ends_with('\n') {
result.push_str(&snippet.trim_right());
result.push('\n');
result.push_str(&offset.to_string(context.config));
@ -1229,8 +1224,7 @@ impl Rewrite for ast::StructField {
let type_offset = shape.indent.block_indent(context.config);
let rewrite_type_in_next_line = || {
let budget = try_opt!(context.config.max_width.checked_sub(type_offset.width()));
self.ty
.rewrite(context, Shape::legacy(budget, type_offset))
self.ty.rewrite(context, Shape::legacy(budget, type_offset))
};
let last_line_width = last_line_width(&result) + type_annotation_spacing.1.len();

View file

@ -51,10 +51,7 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String>
let ender_length = fmt.line_end.len();
// If we cannot put at least a single character per line, the rewrite won't
// succeed.
let max_chars = try_opt!(shape
.width
.checked_sub(fmt.opener.len() + ender_length + 1)) +
1;
let max_chars = try_opt!(shape.width.checked_sub(fmt.opener.len() + ender_length + 1)) + 1;
// Snip a line at a time from `orig` until it is used up. Push the snippet
// onto result.

View file

@ -704,9 +704,9 @@ fn rewrite_bare_fn(bare_fn: &ast::BareFnTy,
.lifetimes
.iter()
.map(|l| {
l.rewrite(context,
l.rewrite(context,
Shape::legacy(try_opt!(shape.width.checked_sub(6)), shape.indent + 4))
})
})
.intersperse(Some(", ".to_string()))
.collect::<Option<String>>()));
result.push_str("> ");