Format source codes
This commit is contained in:
parent
92b54d6490
commit
04bb5d8929
36
src/expr.rs
36
src/expr.rs
@ -98,16 +98,20 @@ fn format_expr(expr: &ast::Expr,
|
|||||||
}
|
}
|
||||||
ast::ExprKind::Tup(ref items) => rewrite_tuple(context, items, expr.span, shape),
|
ast::ExprKind::Tup(ref items) => rewrite_tuple(context, items, expr.span, shape),
|
||||||
ast::ExprKind::While(ref cond, ref block, label) => {
|
ast::ExprKind::While(ref cond, ref block, label) => {
|
||||||
ControlFlow::new_while(None, cond, block, label, expr.span).rewrite(context, shape)
|
ControlFlow::new_while(None, cond, block, label, expr.span)
|
||||||
|
.rewrite(context, shape)
|
||||||
}
|
}
|
||||||
ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => {
|
ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => {
|
||||||
ControlFlow::new_while(Some(pat), cond, block, label, expr.span).rewrite(context, shape)
|
ControlFlow::new_while(Some(pat), cond, block, label, expr.span)
|
||||||
|
.rewrite(context, shape)
|
||||||
}
|
}
|
||||||
ast::ExprKind::ForLoop(ref pat, ref cond, ref block, label) => {
|
ast::ExprKind::ForLoop(ref pat, ref cond, ref block, label) => {
|
||||||
ControlFlow::new_for(pat, cond, block, label, expr.span).rewrite(context, shape)
|
ControlFlow::new_for(pat, cond, block, label, expr.span)
|
||||||
|
.rewrite(context, shape)
|
||||||
}
|
}
|
||||||
ast::ExprKind::Loop(ref block, label) => {
|
ast::ExprKind::Loop(ref block, label) => {
|
||||||
ControlFlow::new_loop(block, label, expr.span).rewrite(context, shape)
|
ControlFlow::new_loop(block, label, expr.span)
|
||||||
|
.rewrite(context, shape)
|
||||||
}
|
}
|
||||||
ast::ExprKind::Block(ref block) => block.rewrite(context, shape),
|
ast::ExprKind::Block(ref block) => block.rewrite(context, shape),
|
||||||
ast::ExprKind::If(ref cond, ref if_block, ref else_block) => {
|
ast::ExprKind::If(ref cond, ref if_block, ref else_block) => {
|
||||||
@ -175,11 +179,12 @@ fn format_expr(expr: &ast::Expr,
|
|||||||
ast::ExprKind::Mac(ref mac) => {
|
ast::ExprKind::Mac(ref mac) => {
|
||||||
// Failure to rewrite a marco should not imply failure to
|
// Failure to rewrite a marco should not imply failure to
|
||||||
// rewrite the expression.
|
// rewrite the expression.
|
||||||
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
|
rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
|
||||||
wrap_str(context.snippet(expr.span),
|
.or_else(|| {
|
||||||
context.config.max_width(),
|
wrap_str(context.snippet(expr.span),
|
||||||
shape)
|
context.config.max_width(),
|
||||||
})
|
shape)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
ast::ExprKind::Ret(None) => {
|
ast::ExprKind::Ret(None) => {
|
||||||
wrap_str("return".to_owned(), context.config.max_width(), shape)
|
wrap_str("return".to_owned(), context.config.max_width(), shape)
|
||||||
@ -319,7 +324,8 @@ pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,
|
|||||||
.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() {
|
let rhs_shape = match context.config.control_style() {
|
||||||
Style::Default => {
|
Style::Default => {
|
||||||
try_opt!(shape.sub_width(suffix.len() + prefix.len())).visual_indent(prefix.len())
|
try_opt!(shape.sub_width(suffix.len() + prefix.len()))
|
||||||
|
.visual_indent(prefix.len())
|
||||||
}
|
}
|
||||||
Style::Rfc => try_opt!(shape.block_left(context.config.tab_spaces())),
|
Style::Rfc => try_opt!(shape.block_left(context.config.tab_spaces())),
|
||||||
};
|
};
|
||||||
@ -510,7 +516,8 @@ fn rewrite_closure(capture: ast::CaptureBy,
|
|||||||
|
|
||||||
// 1 = space between `|...|` and body.
|
// 1 = space between `|...|` and body.
|
||||||
let extra_offset = extra_offset(&prefix, shape) + 1;
|
let extra_offset = extra_offset(&prefix, shape) + 1;
|
||||||
let body_shape = try_opt!(shape.sub_width(extra_offset)).add_offset(extra_offset);
|
let body_shape = try_opt!(shape.sub_width(extra_offset))
|
||||||
|
.add_offset(extra_offset);
|
||||||
|
|
||||||
if let ast::ExprKind::Block(ref block) = body.node {
|
if let ast::ExprKind::Block(ref block) = body.node {
|
||||||
// The body of the closure is an empty block.
|
// The body of the closure is an empty block.
|
||||||
@ -852,8 +859,8 @@ impl<'a> ControlFlow<'a> {
|
|||||||
|
|
||||||
let new_width = try_opt!(new_width.checked_sub(if_str.len()));
|
let new_width = try_opt!(new_width.checked_sub(if_str.len()));
|
||||||
let else_expr = &else_node.stmts[0];
|
let else_expr = &else_node.stmts[0];
|
||||||
let else_str =
|
let else_str = try_opt!(else_expr.rewrite(context,
|
||||||
try_opt!(else_expr.rewrite(context, Shape::legacy(new_width, Indent::empty())));
|
Shape::legacy(new_width, Indent::empty())));
|
||||||
|
|
||||||
if if_str.contains('\n') || else_str.contains('\n') {
|
if if_str.contains('\n') || else_str.contains('\n') {
|
||||||
return None;
|
return None;
|
||||||
@ -1790,11 +1797,11 @@ fn rewrite_call_args(context: &RewriteContext,
|
|||||||
|
|
||||||
fn can_be_overflowed(context: &RewriteContext, args: &[ptr::P<ast::Expr>]) -> bool {
|
fn can_be_overflowed(context: &RewriteContext, args: &[ptr::P<ast::Expr>]) -> bool {
|
||||||
match args.last().map(|x| &x.node) {
|
match args.last().map(|x| &x.node) {
|
||||||
Some(&ast::ExprKind::Block(..)) |
|
|
||||||
Some(&ast::ExprKind::Match(..)) => {
|
Some(&ast::ExprKind::Match(..)) => {
|
||||||
(context.config.fn_call_style() == IndentStyle::Block && args.len() == 1) ||
|
(context.config.fn_call_style() == IndentStyle::Block && args.len() == 1) ||
|
||||||
(context.config.fn_call_style() == IndentStyle::Visual && args.len() > 1)
|
(context.config.fn_call_style() == IndentStyle::Visual && args.len() > 1)
|
||||||
}
|
}
|
||||||
|
Some(&ast::ExprKind::Block(..)) |
|
||||||
Some(&ast::ExprKind::Closure(..)) => {
|
Some(&ast::ExprKind::Closure(..)) => {
|
||||||
context.config.fn_call_style() == IndentStyle::Block ||
|
context.config.fn_call_style() == IndentStyle::Block ||
|
||||||
context.config.fn_call_style() == IndentStyle::Visual && args.len() > 1
|
context.config.fn_call_style() == IndentStyle::Visual && args.len() > 1
|
||||||
@ -1821,6 +1828,7 @@ fn is_extendable(args: &[ptr::P<ast::Expr>]) -> bool {
|
|||||||
}
|
}
|
||||||
} else if args.len() > 1 {
|
} else if args.len() > 1 {
|
||||||
match args[args.len() - 1].node {
|
match args[args.len() - 1].node {
|
||||||
|
ast::ExprKind::Block(..) |
|
||||||
ast::ExprKind::Closure(..) |
|
ast::ExprKind::Closure(..) |
|
||||||
ast::ExprKind::Tup(..) => true,
|
ast::ExprKind::Tup(..) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -126,7 +126,8 @@ impl FileLines {
|
|||||||
Some(ref map) => map,
|
Some(ref map) => map,
|
||||||
};
|
};
|
||||||
|
|
||||||
match canonicalize_path_string(file_name).and_then(|file| map.get_vec(&file).ok_or(())) {
|
match canonicalize_path_string(file_name)
|
||||||
|
.and_then(|file| map.get_vec(&file).ok_or(())) {
|
||||||
Ok(ranges) => ranges.iter().any(f),
|
Ok(ranges) => ranges.iter().any(f),
|
||||||
Err(_) => false,
|
Err(_) => false,
|
||||||
}
|
}
|
||||||
|
@ -1126,7 +1126,8 @@ pub fn rewrite_type_alias(context: &RewriteContext,
|
|||||||
|
|
||||||
let generics_indent = indent + result.len();
|
let generics_indent = indent + result.len();
|
||||||
let generics_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo);
|
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));
|
let generics_str = try_opt!(rewrite_generics(context, generics, shape, generics_span));
|
||||||
|
|
||||||
result.push_str(&generics_str);
|
result.push_str(&generics_str);
|
||||||
|
@ -430,7 +430,8 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
|
|||||||
let post_snippet_trimmed = if post_snippet.starts_with(',') {
|
let post_snippet_trimmed = if post_snippet.starts_with(',') {
|
||||||
post_snippet[1..].trim_matches(white_space)
|
post_snippet[1..].trim_matches(white_space)
|
||||||
} else if post_snippet.ends_with(',') {
|
} else if post_snippet.ends_with(',') {
|
||||||
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
|
post_snippet[..(post_snippet.len() - 1)]
|
||||||
|
.trim_matches(white_space)
|
||||||
} else {
|
} else {
|
||||||
post_snippet
|
post_snippet
|
||||||
};
|
};
|
||||||
@ -528,7 +529,8 @@ pub fn struct_lit_shape(shape: Shape,
|
|||||||
-> Option<(Option<Shape>, Shape)> {
|
-> Option<(Option<Shape>, Shape)> {
|
||||||
let v_shape = match context.config.struct_lit_style() {
|
let v_shape = match context.config.struct_lit_style() {
|
||||||
IndentStyle::Visual => {
|
IndentStyle::Visual => {
|
||||||
try_opt!(try_opt!(shape.shrink_left(prefix_width)).sub_width(suffix_width))
|
try_opt!(try_opt!(shape.shrink_left(prefix_width))
|
||||||
|
.sub_width(suffix_width))
|
||||||
}
|
}
|
||||||
IndentStyle::Block => {
|
IndentStyle::Block => {
|
||||||
let shape = shape.block_indent(context.config.tab_spaces());
|
let shape = shape.block_indent(context.config.tab_spaces());
|
||||||
|
@ -172,12 +172,11 @@ pub fn rewrite_macro(mac: &ast::Mac,
|
|||||||
MacroStyle::Parens => {
|
MacroStyle::Parens => {
|
||||||
// Format macro invocation as function call, forcing no trailing
|
// Format macro invocation as function call, forcing no trailing
|
||||||
// comma because not all macros support them.
|
// comma because not all macros support them.
|
||||||
rewrite_call(context, ¯o_name, &expr_vec, mac.span, shape).map(|rw| {
|
rewrite_call(context, ¯o_name, &expr_vec, mac.span, shape)
|
||||||
match position {
|
.map(|rw| match position {
|
||||||
MacroPosition::Item => format!("{};", rw),
|
MacroPosition::Item => format!("{};", rw),
|
||||||
_ => rw,
|
_ => rw,
|
||||||
}
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
MacroStyle::Brackets => {
|
MacroStyle::Brackets => {
|
||||||
let mac_shape = try_opt!(shape.shrink_left(macro_name.len()));
|
let mac_shape = try_opt!(shape.shrink_left(macro_name.len()));
|
||||||
|
@ -68,7 +68,8 @@ fn module_file(id: ast::Ident,
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
match parser::Parser::default_submod_path(id, dir_path, codemap).result {
|
match parser::Parser::default_submod_path(id, dir_path, codemap)
|
||||||
|
.result {
|
||||||
Ok(parser::ModulePathSuccess { path, .. }) => path,
|
Ok(parser::ModulePathSuccess { path, .. }) => path,
|
||||||
Err(_) => panic!("Couldn't find module {}", id),
|
Err(_) => panic!("Couldn't find module {}", id),
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,8 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String>
|
|||||||
let re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap();
|
let re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap();
|
||||||
let stripped_str = re.replace_all(orig, "$1");
|
let stripped_str = re.replace_all(orig, "$1");
|
||||||
|
|
||||||
let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
|
let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false)
|
||||||
|
.collect::<Vec<&str>>();
|
||||||
let shape = fmt.shape.visual_indent(0);
|
let shape = fmt.shape.visual_indent(0);
|
||||||
let indent = shape.indent.to_string(fmt.config);
|
let indent = shape.indent.to_string(fmt.config);
|
||||||
let punctuation = ":,;.";
|
let punctuation = ":,;.";
|
||||||
|
@ -334,7 +334,8 @@ pub fn wrap_str<S: AsRef<str>>(s: S, max_width: usize, shape: Shape) -> Option<S
|
|||||||
|
|
||||||
impl Rewrite for String {
|
impl Rewrite for String {
|
||||||
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
||||||
wrap_str(self, context.config.max_width(), shape).map(ToOwned::to_owned)
|
wrap_str(self, context.config.max_width(), shape)
|
||||||
|
.map(ToOwned::to_owned)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,8 @@ fn system_tests() {
|
|||||||
// the only difference is the coverage mode
|
// the only difference is the coverage mode
|
||||||
#[test]
|
#[test]
|
||||||
fn coverage_tests() {
|
fn coverage_tests() {
|
||||||
let files = fs::read_dir("tests/coverage/source").expect("Couldn't read source dir");
|
let files = fs::read_dir("tests/coverage/source")
|
||||||
|
.expect("Couldn't read source dir");
|
||||||
let files = files.map(get_path_string);
|
let files = files.map(get_path_string);
|
||||||
let (_reports, count, fails) = check_files(files);
|
let (_reports, count, fails) = check_files(files);
|
||||||
|
|
||||||
@ -82,7 +83,8 @@ fn assert_output(source: &str, expected_filename: &str) {
|
|||||||
let _ = filemap::write_all_files(&file_map, &mut out, &config);
|
let _ = filemap::write_all_files(&file_map, &mut out, &config);
|
||||||
let output = String::from_utf8(out).unwrap();
|
let output = String::from_utf8(out).unwrap();
|
||||||
|
|
||||||
let mut expected_file = fs::File::open(&expected_filename).expect("Couldn't open target");
|
let mut expected_file = fs::File::open(&expected_filename)
|
||||||
|
.expect("Couldn't open target");
|
||||||
let mut expected_text = String::new();
|
let mut expected_text = String::new();
|
||||||
expected_file
|
expected_file
|
||||||
.read_to_string(&mut expected_text)
|
.read_to_string(&mut expected_text)
|
||||||
@ -277,7 +279,8 @@ fn get_config(config_file: Option<&str>) -> Config {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut def_config_file = fs::File::open(config_file_name).expect("Couldn't open config");
|
let mut def_config_file = fs::File::open(config_file_name)
|
||||||
|
.expect("Couldn't open config");
|
||||||
let mut def_config = String::new();
|
let mut def_config = String::new();
|
||||||
def_config_file
|
def_config_file
|
||||||
.read_to_string(&mut def_config)
|
.read_to_string(&mut def_config)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user