|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 | 11 | /// Formatting of chained expressions, i.e. expressions which are chained by
|
12 |
| -/// dots: struct and enum field access and method calls. |
| 12 | +/// dots: struct and enum field access, method calls, and try shorthand (?). |
13 | 13 | ///
|
14 | 14 | /// Instead of walking these subexpressions one-by-one, as is our usual strategy
|
15 | 15 | /// for expression formatting, we collect maximal sequences of these expressions
|
|
81 | 81 | /// true, then we allow the last method call to spill over multiple lines without
|
82 | 82 | /// forcing the rest of the chain to be split.
|
83 | 83 |
|
84 |
| - |
85 | 84 | use Indent;
|
86 | 85 | use rewrite::{Rewrite, RewriteContext};
|
87 | 86 | use utils::{wrap_str, first_line_width};
|
@@ -109,8 +108,16 @@ pub fn rewrite_chain(expr: &ast::Expr,
|
109 | 108 | // put the first non-parent item on the same line as the parent.
|
110 | 109 | let (indent, extend) = if !parent_rewrite.contains('\n') && is_continuable(parent) ||
|
111 | 110 | parent_rewrite.len() <= context.config.tab_spaces {
|
112 |
| - // Try and put at least the first two items on the same line. |
113 |
| - (chain_indent(context, offset + Indent::new(0, parent_rewrite.len())), true) |
| 111 | +// <<<<<<< HEAD |
| 112 | +// // Try and put at least the first two items on the same line. |
| 113 | +// (chain_indent(context, offset + Indent::new(0, parent_rewrite.len())), true) |
| 114 | +// ======= |
| 115 | + let indent = if let ast::ExprKind::Try(..) = subexpr_list.last().unwrap().node { |
| 116 | + parent_block_indent.block_indent(context.config) |
| 117 | + } else { |
| 118 | + offset + Indent::new(0, parent_rewrite.len()) |
| 119 | + }; |
| 120 | + (indent, true) |
114 | 121 | } else if is_block_expr(parent, &parent_rewrite) {
|
115 | 122 | // The parent is a block, so align the rest of the chain with the closing
|
116 | 123 | // brace.
|
@@ -184,12 +191,27 @@ pub fn rewrite_chain(expr: &ast::Expr,
|
184 | 191 | wrap_str(format!("{}{}{}",
|
185 | 192 | parent_rewrite,
|
186 | 193 | first_connector,
|
187 |
| - rewrites.join(&connector)), |
| 194 | + join_rewrites(&rewrites, &subexpr_list, &connector)), |
188 | 195 | context.config.max_width,
|
189 | 196 | width,
|
190 | 197 | offset)
|
191 | 198 | }
|
192 | 199 |
|
| 200 | +fn join_rewrites(rewrites: &[String], subexps: &[&ast::Expr], connector: &str) -> String { |
| 201 | + let mut rewrite_iter = rewrites.iter(); |
| 202 | + let mut result = rewrite_iter.next().unwrap().clone(); |
| 203 | + |
| 204 | + for (rewrite, expr) in rewrite_iter.zip(subexps.iter()) { |
| 205 | + match expr.node { |
| 206 | + ast::ExprKind::Try(_) => (), |
| 207 | + _ => result.push_str(connector), |
| 208 | + }; |
| 209 | + result.push_str(&rewrite[..]); |
| 210 | + } |
| 211 | + |
| 212 | + result |
| 213 | +} |
| 214 | + |
193 | 215 | // States whether an expression's last line exclusively consists of closing
|
194 | 216 | // parens, braces, and brackets in its idiomatic formatting.
|
195 | 217 | fn is_block_expr(expr: &ast::Expr, repr: &str) -> bool {
|
@@ -293,6 +315,16 @@ fn rewrite_method_call_with_overflow(expr_kind: &ast::ExprKind,
|
293 | 315 | }
|
294 | 316 | }
|
295 | 317 |
|
| 318 | +fn pop_expr_chain(expr: &ast::Expr) -> Option<&ast::Expr> { |
| 319 | + match expr.node { |
| 320 | + ast::ExprKind::MethodCall(_, _, ref expressions) => Some(&expressions[0]), |
| 321 | + ast::ExprKind::TupField(ref subexpr, _) | |
| 322 | + ast::ExprKind::Field(ref subexpr, _) | |
| 323 | + ast::ExprKind::Try(ref subexpr) => Some(subexpr), |
| 324 | + _ => None, |
| 325 | + } |
| 326 | +} |
| 327 | + |
296 | 328 | // Rewrite the last element in the chain `expr`. E.g., given `a.b.c` we rewrite
|
297 | 329 | // `.c`.
|
298 | 330 | fn rewrite_chain_subexpr(expr: &ast::Expr,
|
@@ -328,6 +360,13 @@ fn rewrite_chain_subexpr(expr: &ast::Expr,
|
328 | 360 | None
|
329 | 361 | }
|
330 | 362 | }
|
| 363 | + ast::ExprKind::Try(_) => { |
| 364 | + if width >= 1 { |
| 365 | + Some("?".into()) |
| 366 | + } else { |
| 367 | + None |
| 368 | + } |
| 369 | + } |
331 | 370 | _ => unreachable!(),
|
332 | 371 | }
|
333 | 372 | }
|
|
0 commit comments