```rust
let lorem = Lorem { ipsum: dolor,
- sit: amet, };
+ sit: amet, };
```
See also: [`struct_lit_multiline_style`](#struct_lit_multiline_style), [`struct_lit_style`](#struct_lit_style).
nested_shape,
one_line_width,
force_trailing_comma)
+ .or_else(|| if context.use_block_indent() {
+ rewrite_call_args(context,
+ args,
+ args_span,
+ Shape::indented(shape
+ .block()
+ .indent
+ .block_indent(context.config),
+ context.config),
+ 0,
+ force_trailing_comma)
+ } else {
+ None
+ })
.ok_or(Ordering::Less)?;
if !context.use_block_indent() && need_block_indent(&list_str, nested_shape) && !extendable {
force_trailing_comma);
}
+ let args_shape = shape
+ .sub_width(last_line_width(&callee_str))
+ .ok_or(Ordering::Less)?;
Ok(format!("{}{}",
callee_str,
- wrap_args_with_parens(context, &list_str, extendable, shape, nested_shape)))
+ wrap_args_with_parens(context, &list_str, extendable, args_shape, nested_shape)))
}
fn need_block_indent(s: &str, shape: Shape) -> bool {
}
}
+fn paren_overhead(context: &RewriteContext) -> usize {
+ if context.config.spaces_within_parens() {
+ 4
+ } else {
+ 2
+ }
+}
+
fn wrap_args_with_parens(context: &RewriteContext,
args_str: &str,
is_extendable: bool,
shape: Shape,
nested_shape: Shape)
-> String {
- if !context.use_block_indent() || (context.inside_macro && !args_str.contains('\n')) ||
- is_extendable {
+ if !context.use_block_indent() ||
+ (context.inside_macro && !args_str.contains('\n') &&
+ args_str.len() + paren_overhead(context) <= shape.width) || is_extendable {
if context.config.spaces_within_parens() && args_str.len() > 0 {
format!("( {} )", args_str)
} else {
result.push_str(&where_clause_str);
+ force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
return Some((result, force_new_line_for_brace));
}
}
result.push_str(&where_clause_str);
- Some((result, force_new_line_for_brace))
+ force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
+ return Some((result, force_new_line_for_brace));
+}
+
+fn last_line_contains_single_line_comment(s: &str) -> bool {
+ s.lines().last().map_or(false, |l| l.contains("//"))
}
fn rewrite_args(context: &RewriteContext,
// Formatting which depends on the AST.
fn format_ast<F>(krate: &ast::Crate,
- parse_session: &ParseSess,
+ mut parse_session: &mut ParseSess,
main_file: &Path,
config: &Config,
+ codemap: &Rc<CodeMap>,
mut after_file: F)
-> Result<(FileMap, bool), io::Error>
where F: FnMut(&str, &mut StringBuffer) -> Result<bool, io::Error>
if config.verbose() {
println!("Formatting {}", path);
}
- let mut visitor = FmtVisitor::from_codemap(parse_session, config);
- visitor.format_separate_mod(module);
+ {
+ let mut visitor = FmtVisitor::from_codemap(parse_session, config);
+ visitor.format_separate_mod(module);
- has_diff |= after_file(path, &mut visitor.buffer)?;
+ has_diff |= after_file(path, &mut visitor.buffer)?;
- result.push((path.to_owned(), visitor.buffer));
+ result.push((path.to_owned(), visitor.buffer));
+ }
+ // Reset the error count.
+ if parse_session.span_diagnostic.has_errors() {
+ parse_session.span_diagnostic =
+ Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(codemap.clone()));
+ }
}
Ok((result, has_diff))
match format_ast(
&krate,
- &parse_session,
+ &mut parse_session,
&main_file,
config,
+ &codemap,
|file_name, file| {
// For some reason, the codemap does not include terminating
// newlines so we must add one on for each file. This is sad.
-> Option<(Option<Shape>, Shape)> {
let v_shape = match context.config.struct_lit_style() {
IndentStyle::Visual => {
- try_opt!(try_opt!(shape.shrink_left(prefix_width)).sub_width(suffix_width))
+ try_opt!(try_opt!(shape.visual_indent(0).shrink_left(prefix_width))
+ .sub_width(suffix_width))
}
IndentStyle::Block => {
let shape = shape.block_indent(context.config.tab_spaces());
},
);
}
+
+fn issue1651() {
+ {
+ let type_list: Vec<_> = try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect());
+ }
+}
fn bar<F /* comment on F */, G /* comment on G */>() {}
fn baz() -> Baz /* Comment after return type */ {}
+
+fn some_fn<T>() where T: Eq // some comment
+{
+}
// rustfmt should not add trailing comma when rewriting macro. See #1528.
fn a() {
- panic!("this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:");
+ panic!(
+ "this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:"
+ );
foo(
oooptoptoptoptptooptoptoptoptptooptoptoptoptptoptoptoptoptpt(),
);
}
});
}
+
+fn issue1651() {
+ {
+ let type_list: Vec<_> =
+ try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect());
+ }
+}
fn main() {
let lorem = Lorem { ipsum: dolor,
- sit: amet, };
+ sit: amet, };
}