mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-20 19:04:21 +00:00
Format non-statement if-else expressions on a single line
This commit is contained in:
parent
66cac1f3e9
commit
98c0570a28
@ -354,27 +354,13 @@ fn rewrite_chain_subexpr(expr: &ast::Expr,
|
||||
}
|
||||
ast::ExprKind::Field(_, ref field) => {
|
||||
let s = format!(".{}", field.node);
|
||||
if s.len() <= width {
|
||||
Some(s)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if s.len() <= width { Some(s) } else { None }
|
||||
}
|
||||
ast::ExprKind::TupField(_, ref field) => {
|
||||
let s = format!(".{}", field.node);
|
||||
if s.len() <= width {
|
||||
Some(s)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
ast::ExprKind::Try(_) => {
|
||||
if width >= 1 {
|
||||
Some("?".into())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if s.len() <= width { Some(s) } else { None }
|
||||
}
|
||||
ast::ExprKind::Try(_) => if width >= 1 { Some("?".into()) } else { None },
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
@ -381,7 +381,9 @@ create_config! {
|
||||
chain_indent: BlockIndentStyle, BlockIndentStyle::Tabbed, "Indentation of chain";
|
||||
chains_overflow_last: bool, true, "Allow last call in method chain to break the line";
|
||||
reorder_imports: bool, false, "Reorder import statements alphabetically";
|
||||
single_line_if_else: bool, false, "Put else on same line as closing brace for if statements";
|
||||
single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else \
|
||||
expressions. A value of zero means always break \
|
||||
if-else expressions.";
|
||||
format_strings: bool, true, "Format string literals where necessary";
|
||||
force_format_strings: bool, false, "Always format string literals";
|
||||
take_source_hints: bool, true, "Retain some formatting characteristics from the source code";
|
||||
|
406
src/expr.rs
406
src/expr.rs
@ -36,193 +36,210 @@ use syntax::parse::classify;
|
||||
|
||||
impl Rewrite for ast::Expr {
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
let result = match self.node {
|
||||
ast::ExprKind::Vec(ref expr_vec) => {
|
||||
rewrite_array(expr_vec.iter().map(|e| &**e),
|
||||
mk_sp(context.codemap.span_after(self.span, "["), self.span.hi),
|
||||
context,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::Lit(ref l) => {
|
||||
match l.node {
|
||||
ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
|
||||
rewrite_string_lit(context, l.span, width, offset)
|
||||
}
|
||||
_ => {
|
||||
wrap_str(context.snippet(self.span),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
format_expr(self, ExprType::SubExpression, context, width, offset)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum ExprType {
|
||||
Statement,
|
||||
SubExpression,
|
||||
}
|
||||
|
||||
fn format_expr(expr: &ast::Expr,
|
||||
expr_type: ExprType,
|
||||
context: &RewriteContext,
|
||||
width: usize,
|
||||
offset: Indent)
|
||||
-> Option<String> {
|
||||
let result = match expr.node {
|
||||
ast::ExprKind::Vec(ref expr_vec) => {
|
||||
rewrite_array(expr_vec.iter().map(|e| &**e),
|
||||
mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi),
|
||||
context,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::Lit(ref l) => {
|
||||
match l.node {
|
||||
ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
|
||||
rewrite_string_lit(context, l.span, width, offset)
|
||||
}
|
||||
}
|
||||
ast::ExprKind::Call(ref callee, ref args) => {
|
||||
let inner_span = mk_sp(callee.span.hi, self.span.hi);
|
||||
rewrite_call(context, &**callee, args, inner_span, width, offset)
|
||||
}
|
||||
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, width, offset),
|
||||
ast::ExprKind::Binary(ref op, ref lhs, ref rhs) => {
|
||||
rewrite_binary_op(context, op, lhs, rhs, width, offset)
|
||||
}
|
||||
ast::ExprKind::Unary(ref op, ref subexpr) => {
|
||||
rewrite_unary_op(context, op, subexpr, width, offset)
|
||||
}
|
||||
ast::ExprKind::Struct(ref path, ref fields, ref base) => {
|
||||
rewrite_struct_lit(context,
|
||||
path,
|
||||
fields,
|
||||
base.as_ref().map(|e| &**e),
|
||||
self.span,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::Tup(ref items) => {
|
||||
rewrite_tuple(context,
|
||||
items.iter().map(|x| &**x),
|
||||
self.span,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::While(ref cond, ref block, label) => {
|
||||
Loop::new_while(None, cond, block, label).rewrite(context, width, offset)
|
||||
}
|
||||
ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => {
|
||||
Loop::new_while(Some(pat), cond, block, label).rewrite(context, width, offset)
|
||||
}
|
||||
ast::ExprKind::ForLoop(ref pat, ref cond, ref block, label) => {
|
||||
Loop::new_for(pat, cond, block, label).rewrite(context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Loop(ref block, label) => {
|
||||
Loop::new_loop(block, label).rewrite(context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Block(ref block) => block.rewrite(context, width, offset),
|
||||
ast::ExprKind::If(ref cond, ref if_block, ref else_block) => {
|
||||
rewrite_if_else(context,
|
||||
cond,
|
||||
if_block,
|
||||
else_block.as_ref().map(|e| &**e),
|
||||
self.span,
|
||||
None,
|
||||
width,
|
||||
offset,
|
||||
true)
|
||||
}
|
||||
ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref else_block) => {
|
||||
rewrite_if_else(context,
|
||||
cond,
|
||||
if_block,
|
||||
else_block.as_ref().map(|e| &**e),
|
||||
self.span,
|
||||
Some(pat),
|
||||
width,
|
||||
offset,
|
||||
true)
|
||||
}
|
||||
ast::ExprKind::Match(ref cond, ref arms) => {
|
||||
rewrite_match(context, cond, arms, width, offset, self.span)
|
||||
}
|
||||
ast::ExprKind::Path(ref qself, ref path) => {
|
||||
rewrite_path(context, true, qself.as_ref(), path, width, offset)
|
||||
}
|
||||
ast::ExprKind::Assign(ref lhs, ref rhs) => {
|
||||
rewrite_assignment(context, lhs, rhs, None, width, offset)
|
||||
}
|
||||
ast::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => {
|
||||
rewrite_assignment(context, lhs, rhs, Some(op), width, offset)
|
||||
}
|
||||
ast::ExprKind::Again(ref opt_ident) => {
|
||||
let id_str = match *opt_ident {
|
||||
Some(ident) => format!(" {}", ident.node),
|
||||
None => String::new(),
|
||||
};
|
||||
wrap_str(format!("continue{}", id_str),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::Break(ref opt_ident) => {
|
||||
let id_str = match *opt_ident {
|
||||
Some(ident) => format!(" {}", ident.node),
|
||||
None => String::new(),
|
||||
};
|
||||
wrap_str(format!("break{}", id_str),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
|
||||
rewrite_closure(capture, fn_decl, body, self.span, context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Try(..) |
|
||||
ast::ExprKind::Field(..) |
|
||||
ast::ExprKind::TupField(..) |
|
||||
ast::ExprKind::MethodCall(..) => rewrite_chain(self, context, width, offset),
|
||||
ast::ExprKind::Mac(ref mac) => {
|
||||
// Failure to rewrite a marco should not imply failure to
|
||||
// rewrite the expression.
|
||||
rewrite_macro(mac, None, context, width, offset).or_else(|| {
|
||||
wrap_str(context.snippet(self.span),
|
||||
_ => {
|
||||
wrap_str(context.snippet(expr.span),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
})
|
||||
}
|
||||
ast::ExprKind::Ret(None) => {
|
||||
wrap_str("return".to_owned(), context.config.max_width, width, offset)
|
||||
}
|
||||
ast::ExprKind::Ret(Some(ref expr)) => {
|
||||
rewrite_unary_prefix(context, "return ", &**expr, width, offset)
|
||||
}
|
||||
ast::ExprKind::Box(ref expr) => {
|
||||
rewrite_unary_prefix(context, "box ", &**expr, width, offset)
|
||||
}
|
||||
ast::ExprKind::AddrOf(mutability, ref expr) => {
|
||||
rewrite_expr_addrof(context, mutability, expr, width, offset)
|
||||
}
|
||||
ast::ExprKind::Cast(ref expr, ref ty) => {
|
||||
rewrite_pair(&**expr, &**ty, "", " as ", "", context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Type(ref expr, ref ty) => {
|
||||
rewrite_pair(&**expr, &**ty, "", ": ", "", context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Index(ref expr, ref index) => {
|
||||
rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Repeat(ref expr, ref repeats) => {
|
||||
rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
|
||||
let delim = match limits {
|
||||
ast::RangeLimits::HalfOpen => "..",
|
||||
ast::RangeLimits::Closed => "...",
|
||||
};
|
||||
|
||||
match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) {
|
||||
(Some(ref lhs), Some(ref rhs)) => {
|
||||
rewrite_pair(&**lhs, &**rhs, "", delim, "", context, width, offset)
|
||||
}
|
||||
(None, Some(ref rhs)) => {
|
||||
rewrite_unary_prefix(context, delim, &**rhs, width, offset)
|
||||
}
|
||||
(Some(ref lhs), None) => {
|
||||
rewrite_unary_suffix(context, delim, &**lhs, width, offset)
|
||||
}
|
||||
(None, None) => wrap_str(delim.into(), context.config.max_width, width, offset),
|
||||
}
|
||||
}
|
||||
// We do not format these expressions yet, but they should still
|
||||
// satisfy our width restrictions.
|
||||
ast::ExprKind::InPlace(..) |
|
||||
ast::ExprKind::InlineAsm(..) => {
|
||||
wrap_str(context.snippet(self.span),
|
||||
}
|
||||
ast::ExprKind::Call(ref callee, ref args) => {
|
||||
let inner_span = mk_sp(callee.span.hi, expr.span.hi);
|
||||
rewrite_call(context, &**callee, args, inner_span, width, offset)
|
||||
}
|
||||
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, width, offset),
|
||||
ast::ExprKind::Binary(ref op, ref lhs, ref rhs) => {
|
||||
rewrite_binary_op(context, op, lhs, rhs, width, offset)
|
||||
}
|
||||
ast::ExprKind::Unary(ref op, ref subexpr) => {
|
||||
rewrite_unary_op(context, op, subexpr, width, offset)
|
||||
}
|
||||
ast::ExprKind::Struct(ref path, ref fields, ref base) => {
|
||||
rewrite_struct_lit(context,
|
||||
path,
|
||||
fields,
|
||||
base.as_ref().map(|e| &**e),
|
||||
expr.span,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::Tup(ref items) => {
|
||||
rewrite_tuple(context,
|
||||
items.iter().map(|x| &**x),
|
||||
expr.span,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::While(ref cond, ref block, label) => {
|
||||
Loop::new_while(None, cond, block, label).rewrite(context, width, offset)
|
||||
}
|
||||
ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => {
|
||||
Loop::new_while(Some(pat), cond, block, label).rewrite(context, width, offset)
|
||||
}
|
||||
ast::ExprKind::ForLoop(ref pat, ref cond, ref block, label) => {
|
||||
Loop::new_for(pat, cond, block, label).rewrite(context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Loop(ref block, label) => {
|
||||
Loop::new_loop(block, label).rewrite(context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Block(ref block) => block.rewrite(context, width, offset),
|
||||
ast::ExprKind::If(ref cond, ref if_block, ref else_block) => {
|
||||
rewrite_if_else(context,
|
||||
cond,
|
||||
expr_type,
|
||||
if_block,
|
||||
else_block.as_ref().map(|e| &**e),
|
||||
expr.span,
|
||||
None,
|
||||
width,
|
||||
offset,
|
||||
true)
|
||||
}
|
||||
ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref else_block) => {
|
||||
rewrite_if_else(context,
|
||||
cond,
|
||||
expr_type,
|
||||
if_block,
|
||||
else_block.as_ref().map(|e| &**e),
|
||||
expr.span,
|
||||
Some(pat),
|
||||
width,
|
||||
offset,
|
||||
true)
|
||||
}
|
||||
ast::ExprKind::Match(ref cond, ref arms) => {
|
||||
rewrite_match(context, cond, arms, width, offset, expr.span)
|
||||
}
|
||||
ast::ExprKind::Path(ref qself, ref path) => {
|
||||
rewrite_path(context, true, qself.as_ref(), path, width, offset)
|
||||
}
|
||||
ast::ExprKind::Assign(ref lhs, ref rhs) => {
|
||||
rewrite_assignment(context, lhs, rhs, None, width, offset)
|
||||
}
|
||||
ast::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => {
|
||||
rewrite_assignment(context, lhs, rhs, Some(op), width, offset)
|
||||
}
|
||||
ast::ExprKind::Again(ref opt_ident) => {
|
||||
let id_str = match *opt_ident {
|
||||
Some(ident) => format!(" {}", ident.node),
|
||||
None => String::new(),
|
||||
};
|
||||
wrap_str(format!("continue{}", id_str),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::Break(ref opt_ident) => {
|
||||
let id_str = match *opt_ident {
|
||||
Some(ident) => format!(" {}", ident.node),
|
||||
None => String::new(),
|
||||
};
|
||||
wrap_str(format!("break{}", id_str),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
|
||||
rewrite_closure(capture, fn_decl, body, expr.span, context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Try(..) |
|
||||
ast::ExprKind::Field(..) |
|
||||
ast::ExprKind::TupField(..) |
|
||||
ast::ExprKind::MethodCall(..) => rewrite_chain(expr, context, width, offset),
|
||||
ast::ExprKind::Mac(ref mac) => {
|
||||
// Failure to rewrite a marco should not imply failure to
|
||||
// rewrite the expression.
|
||||
rewrite_macro(mac, None, context, width, offset).or_else(|| {
|
||||
wrap_str(context.snippet(expr.span),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
})
|
||||
}
|
||||
ast::ExprKind::Ret(None) => {
|
||||
wrap_str("return".to_owned(), context.config.max_width, width, offset)
|
||||
}
|
||||
ast::ExprKind::Ret(Some(ref expr)) => {
|
||||
rewrite_unary_prefix(context, "return ", &**expr, width, offset)
|
||||
}
|
||||
ast::ExprKind::Box(ref expr) => {
|
||||
rewrite_unary_prefix(context, "box ", &**expr, width, offset)
|
||||
}
|
||||
ast::ExprKind::AddrOf(mutability, ref expr) => {
|
||||
rewrite_expr_addrof(context, mutability, expr, width, offset)
|
||||
}
|
||||
ast::ExprKind::Cast(ref expr, ref ty) => {
|
||||
rewrite_pair(&**expr, &**ty, "", " as ", "", context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Type(ref expr, ref ty) => {
|
||||
rewrite_pair(&**expr, &**ty, "", ": ", "", context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Index(ref expr, ref index) => {
|
||||
rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Repeat(ref expr, ref repeats) => {
|
||||
rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset)
|
||||
}
|
||||
ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
|
||||
let delim = match limits {
|
||||
ast::RangeLimits::HalfOpen => "..",
|
||||
ast::RangeLimits::Closed => "...",
|
||||
};
|
||||
|
||||
match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) {
|
||||
(Some(ref lhs), Some(ref rhs)) => {
|
||||
rewrite_pair(&**lhs, &**rhs, "", delim, "", context, width, offset)
|
||||
}
|
||||
(None, Some(ref rhs)) => {
|
||||
rewrite_unary_prefix(context, delim, &**rhs, width, offset)
|
||||
}
|
||||
(Some(ref lhs), None) => {
|
||||
rewrite_unary_suffix(context, delim, &**lhs, width, offset)
|
||||
}
|
||||
(None, None) => wrap_str(delim.into(), context.config.max_width, width, offset),
|
||||
}
|
||||
};
|
||||
result.and_then(|res| recover_comment_removed(res, self.span, context, width, offset))
|
||||
}
|
||||
}
|
||||
// We do not format these expressions yet, but they should still
|
||||
// satisfy our width restrictions.
|
||||
ast::ExprKind::InPlace(..) |
|
||||
ast::ExprKind::InlineAsm(..) => {
|
||||
wrap_str(context.snippet(expr.span),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
};
|
||||
result.and_then(|res| recover_comment_removed(res, expr.span, context, width, offset))
|
||||
}
|
||||
|
||||
pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,
|
||||
@ -470,11 +487,7 @@ fn rewrite_closure(capture: ast::CaptureBy,
|
||||
}
|
||||
|
||||
fn and_one_line(x: Option<String>) -> Option<String> {
|
||||
x.and_then(|x| if x.contains('\n') {
|
||||
None
|
||||
} else {
|
||||
Some(x)
|
||||
})
|
||||
x.and_then(|x| if x.contains('\n') { None } else { Some(x) })
|
||||
}
|
||||
|
||||
fn nop_block_collapse(block_str: Option<String>, budget: usize) -> Option<String> {
|
||||
@ -560,15 +573,13 @@ impl Rewrite for ast::Stmt {
|
||||
}
|
||||
ast::StmtKind::Expr(ref ex, _) |
|
||||
ast::StmtKind::Semi(ref ex, _) => {
|
||||
let suffix = if semicolon_for_stmt(self) {
|
||||
";"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let suffix = if semicolon_for_stmt(self) { ";" } else { "" };
|
||||
|
||||
ex.rewrite(context,
|
||||
context.config.max_width - offset.width() - suffix.len(),
|
||||
offset)
|
||||
format_expr(ex,
|
||||
ExprType::Statement,
|
||||
context,
|
||||
context.config.max_width - offset.width() - suffix.len(),
|
||||
offset)
|
||||
.map(|s| s + suffix)
|
||||
}
|
||||
ast::StmtKind::Mac(..) => None,
|
||||
@ -704,6 +715,7 @@ fn extract_comment(span: Span,
|
||||
// treated as an if-let-else expression.
|
||||
fn rewrite_if_else(context: &RewriteContext,
|
||||
cond: &ast::Expr,
|
||||
expr_type: ExprType,
|
||||
if_block: &ast::Block,
|
||||
else_block_opt: Option<&ast::Expr>,
|
||||
span: Span,
|
||||
@ -726,10 +738,12 @@ fn rewrite_if_else(context: &RewriteContext,
|
||||
offset + 3));
|
||||
|
||||
// Try to format if-else on single line.
|
||||
if allow_single_line && context.config.single_line_if_else {
|
||||
if expr_type == ExprType::SubExpression && allow_single_line &&
|
||||
context.config.single_line_if_else_max_width > 0 {
|
||||
let trial = single_line_if_else(context, &pat_expr_string, if_block, else_block_opt, width);
|
||||
|
||||
if trial.is_some() {
|
||||
if trial.is_some() &&
|
||||
trial.as_ref().unwrap().len() <= context.config.single_line_if_else_max_width {
|
||||
return trial;
|
||||
}
|
||||
}
|
||||
@ -766,6 +780,7 @@ fn rewrite_if_else(context: &RewriteContext,
|
||||
ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref next_else_block) => {
|
||||
rewrite_if_else(context,
|
||||
cond,
|
||||
expr_type,
|
||||
if_block,
|
||||
next_else_block.as_ref().map(|e| &**e),
|
||||
mk_sp(else_block.span.lo, span.hi),
|
||||
@ -777,6 +792,7 @@ fn rewrite_if_else(context: &RewriteContext,
|
||||
ast::ExprKind::If(ref cond, ref if_block, ref next_else_block) => {
|
||||
rewrite_if_else(context,
|
||||
cond,
|
||||
expr_type,
|
||||
if_block,
|
||||
next_else_block.as_ref().map(|e| &**e),
|
||||
mk_sp(else_block.span.lo, span.hi),
|
||||
@ -1248,11 +1264,7 @@ fn rewrite_pat_expr(context: &RewriteContext,
|
||||
|
||||
// The expression may (partionally) fit on the current line.
|
||||
if width > extra_offset + 1 {
|
||||
let spacer = if pat.is_some() {
|
||||
" "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let spacer = if pat.is_some() { " " } else { "" };
|
||||
|
||||
let expr_rewrite = expr.rewrite(context,
|
||||
width - extra_offset - spacer.len(),
|
||||
|
@ -111,11 +111,7 @@ pub fn rewrite_use_list(width: usize,
|
||||
}
|
||||
|
||||
// 2 = ::
|
||||
let path_separation_w = if !path_str.is_empty() {
|
||||
2
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let path_separation_w = if !path_str.is_empty() { 2 } else { 0 };
|
||||
// 1 = {
|
||||
let supp_indent = path_str.len() + path_separation_w + 1;
|
||||
// 1 = }
|
||||
@ -140,11 +136,7 @@ pub fn rewrite_use_list(width: usize,
|
||||
// potentially move "self" to the front of the vector without touching
|
||||
// the rest of the items.
|
||||
let has_self = move_self_to_front(&mut items);
|
||||
let first_index = if has_self {
|
||||
0
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let first_index = if has_self { 0 } else { 1 };
|
||||
|
||||
if context.config.reorder_imports {
|
||||
items[1..].sort_by(|a, b| a.item.cmp(&b.item));
|
||||
|
18
src/items.rs
18
src/items.rs
@ -140,11 +140,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
// FIXME(#21): we're dropping potential comments in between the
|
||||
// function keywords here.
|
||||
let vis = format_visibility(&item.vis);
|
||||
let mut_str = if is_mutable {
|
||||
"mut "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let mut_str = if is_mutable { "mut " } else { "" };
|
||||
let prefix = format!("{}static {}{}: ", vis, mut_str, item.ident);
|
||||
let offset = self.block_indent + prefix.len();
|
||||
// 1 = ;
|
||||
@ -265,11 +261,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
if self.config.fn_single_line && is_simple_block_stmt(block, codemap) {
|
||||
let rewrite = {
|
||||
if let Some(ref e) = block.expr {
|
||||
let suffix = if semicolon_for_expr(e) {
|
||||
";"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let suffix = if semicolon_for_expr(e) { ";" } else { "" };
|
||||
|
||||
e.rewrite(&self.get_context(),
|
||||
self.config.max_width - self.block_indent.width(),
|
||||
@ -1280,11 +1272,7 @@ fn rewrite_fn_base(context: &RewriteContext,
|
||||
.rewrite(&context, context.config.max_width - indent.width(), indent));
|
||||
|
||||
let multi_line_ret_str = ret_str.contains('\n');
|
||||
let ret_str_len = if multi_line_ret_str {
|
||||
0
|
||||
} else {
|
||||
ret_str.len()
|
||||
};
|
||||
let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
|
||||
|
||||
// Args.
|
||||
let (mut one_line_budget, mut multi_line_budget, mut arg_indent) =
|
||||
|
@ -208,11 +208,7 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
|
||||
let first = i == 0;
|
||||
let last = iter.peek().is_none();
|
||||
let separate = !last || trailing_separator;
|
||||
let item_sep_len = if separate {
|
||||
sep_len
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let item_sep_len = if separate { sep_len } else { 0 };
|
||||
|
||||
// Item string may be multi-line. Its length (used for block comment alignment)
|
||||
// Should be only the length of the last line.
|
||||
|
@ -93,13 +93,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
|
||||
fn replace_chars(string: &str) -> String {
|
||||
string.chars()
|
||||
.map(|ch| {
|
||||
if ch.is_whitespace() {
|
||||
ch
|
||||
} else {
|
||||
'X'
|
||||
}
|
||||
})
|
||||
.map(|ch| { if ch.is_whitespace() { ch } else { 'X' } })
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
@ -121,11 +121,7 @@ impl Rewrite for Pat {
|
||||
PatKind::Struct(ref path, ref fields, elipses) => {
|
||||
let path = try_opt!(rewrite_path(context, true, None, path, width, offset));
|
||||
|
||||
let (elipses_str, terminator) = if elipses {
|
||||
(", ..", "..")
|
||||
} else {
|
||||
("", "}")
|
||||
};
|
||||
let (elipses_str, terminator) = if elipses { (", ..", "..") } else { ("", "}") };
|
||||
|
||||
// 5 = `{` plus space before and after plus `}` plus space before.
|
||||
let budget = try_opt!(width.checked_sub(path.len() + 5 + elipses_str.len()));
|
||||
|
@ -187,11 +187,7 @@ fn rewrite_segment(expr_context: bool,
|
||||
|
||||
let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1);
|
||||
let list_lo = context.codemap.span_after(codemap::mk_sp(*span_lo, span_hi), "<");
|
||||
let separator = if expr_context {
|
||||
"::"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let separator = if expr_context { "::" } else { "" };
|
||||
|
||||
// 1 for <
|
||||
let extra_offset = 1 + separator.len();
|
||||
|
@ -77,11 +77,7 @@ pub fn format_visibility(vis: &Visibility) -> Cow<'static, str> {
|
||||
Visibility::Crate(_) => Cow::from("pub(crate) "),
|
||||
Visibility::Restricted { ref path, .. } => {
|
||||
let Path { global, ref segments, .. } = **path;
|
||||
let prefix = if global {
|
||||
"::"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let prefix = if global { "::" } else { "" };
|
||||
let mut segments_iter = segments.iter().map(|seg| seg.identifier.name.as_str());
|
||||
|
||||
Cow::from(format!("pub({}{}) ", prefix, segments_iter.join("::")))
|
||||
|
@ -77,11 +77,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
// Check if this block has braces.
|
||||
let snippet = self.snippet(b.span);
|
||||
let has_braces = snippet.starts_with("{") || snippet.starts_with("unsafe");
|
||||
let brace_compensation = if has_braces {
|
||||
BytePos(1)
|
||||
} else {
|
||||
BytePos(0)
|
||||
};
|
||||
let brace_compensation = if has_braces { BytePos(1) } else { BytePos(0) };
|
||||
|
||||
self.last_pos = self.last_pos + brace_compensation;
|
||||
self.block_indent = self.block_indent.block_indent(self.config);
|
||||
|
@ -21,7 +21,7 @@ enum_trailing_comma = true
|
||||
report_todo = "Always"
|
||||
report_fixme = "Never"
|
||||
reorder_imports = false
|
||||
single_line_if_else = false
|
||||
single_line_if_else_max_width = 0
|
||||
format_strings = true
|
||||
chains_overflow_last = true
|
||||
take_source_hints = true
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-chain_base_indent: Inherit
|
||||
// Test chain formatting with block indented base
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-chains_overflow_last: false
|
||||
// Test chain formatting without overflowing the last item.
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-chain_indent: Visual
|
||||
// rustfmt-chain_base_indent: Visual
|
||||
// Test chain formatting.
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// Test chain formatting.
|
||||
|
||||
fn main() {
|
||||
|
@ -11,21 +11,19 @@ fn main() {
|
||||
};
|
||||
|
||||
let loooooooooooooong_name = |field| {
|
||||
// TODO(#27): format comments.
|
||||
// format comments.
|
||||
if field.node.attrs.len() > 0 { field.node.attrs[0].span.lo
|
||||
} else {
|
||||
field.span.lo
|
||||
}};
|
||||
|
||||
let block_me = |field| if true_story() { 1 } else { 2 };
|
||||
|
||||
let unblock_me = |trivial| {
|
||||
closure()
|
||||
};
|
||||
|
||||
let empty = |arg| {};
|
||||
|
||||
let simple = |arg| { /* TODO(#27): comment formatting */ foo(arg) };
|
||||
let simple = |arg| { /* comment formatting */ foo(arg) };
|
||||
|
||||
let test = | | { do_something(); do_something_else(); };
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-else_if_brace_style: AlwaysNextLine
|
||||
|
||||
fn main() {
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-else_if_brace_style: AlwaysSameLine
|
||||
|
||||
fn main() {
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-else_if_brace_style: ClosingNextLine
|
||||
|
||||
fn main() {
|
||||
|
@ -255,3 +255,20 @@ fn ranges() {
|
||||
// the expr below won't compile for some reason...
|
||||
// let a = 0 ... ;
|
||||
}
|
||||
|
||||
fn if_else() {
|
||||
let exact = diff /
|
||||
(if size == 0 {
|
||||
1
|
||||
} else {
|
||||
size
|
||||
});
|
||||
|
||||
let cx = tp1.x +
|
||||
any * radius *
|
||||
if anticlockwise {
|
||||
1.0
|
||||
} else {
|
||||
-1.0
|
||||
};
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-wrap_comments: true
|
||||
// rustfmt-hard_tabs: true
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// rustfmt-single_line_if_else: true
|
||||
// rustfmt-single_line_if_else_max_width: 100
|
||||
|
||||
// Format if-else expressions on a single line, when possible.
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-chain_base_indent: Inherit
|
||||
// Test chain formatting with block indented base
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-chains_overflow_last: false
|
||||
// Test chain formatting without overflowing the last item.
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-chain_indent: Visual
|
||||
// rustfmt-chain_base_indent: Visual
|
||||
// Test chain formatting.
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// Test chain formatting.
|
||||
|
||||
fn main() {
|
||||
|
@ -18,7 +18,7 @@ fn main() {
|
||||
};
|
||||
|
||||
let loooooooooooooong_name = |field| {
|
||||
// TODO(#27): format comments.
|
||||
// format comments.
|
||||
if field.node.attrs.len() > 0 {
|
||||
field.node.attrs[0].span.lo
|
||||
} else {
|
||||
@ -26,18 +26,12 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
let block_me = |field| if true_story() {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
};
|
||||
|
||||
let unblock_me = |trivial| closure();
|
||||
|
||||
let empty = |arg| {};
|
||||
|
||||
let simple = |arg| {
|
||||
// TODO(#27): comment formatting
|
||||
// comment formatting
|
||||
foo(arg)
|
||||
};
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-else_if_brace_style: AlwaysNextLine
|
||||
|
||||
fn main() {
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-else_if_brace_style: AlwaysSameLine
|
||||
|
||||
fn main() {
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-else_if_brace_style: ClosingNextLine
|
||||
|
||||
fn main() {
|
||||
|
@ -62,11 +62,7 @@ fn foo() -> bool {
|
||||
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3 {
|
||||
}
|
||||
|
||||
let test = if true {
|
||||
5
|
||||
} else {
|
||||
3
|
||||
};
|
||||
let test = if true { 5 } else { 3 };
|
||||
|
||||
if cond() {
|
||||
something();
|
||||
@ -92,11 +88,7 @@ fn bar() {
|
||||
syntactically_correct(loop {
|
||||
sup('?');
|
||||
},
|
||||
if cond {
|
||||
0
|
||||
} else {
|
||||
1
|
||||
});
|
||||
if cond { 0 } else { 1 });
|
||||
|
||||
let third = ..10;
|
||||
let infi_range = ..;
|
||||
@ -277,3 +269,9 @@ fn ranges() {
|
||||
// the expr below won't compile for some reason...
|
||||
// let a = 0 ... ;
|
||||
}
|
||||
|
||||
fn if_else() {
|
||||
let exact = diff / (if size == 0 { 1 } else { size });
|
||||
|
||||
let cx = tp1.x + any * radius * if anticlockwise { 1.0 } else { -1.0 };
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-single_line_if_else_max_width: 0
|
||||
// rustfmt-wrap_comments: true
|
||||
// rustfmt-hard_tabs: true
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// rustfmt-single_line_if_else: true
|
||||
// rustfmt-single_line_if_else_max_width: 100
|
||||
|
||||
// Format if-else expressions on a single line, when possible.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user