2015-09-11 00:52:16 +02:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-09-09 23:14:09 +02:00
|
|
|
// Formatting of chained expressions, i.e. expressions which are chained by
|
|
|
|
// dots: struct and enum field access and method calls.
|
|
|
|
//
|
|
|
|
// Instead of walking these subexpressions one-by-one, as is our usual strategy
|
|
|
|
// for expression formatting, we collect maximal sequences of these expressions
|
|
|
|
// and handle them simultaneously.
|
|
|
|
//
|
|
|
|
// Whenever possible, the entire chain is put on a single line. If that fails,
|
|
|
|
// we put each subexpression on a separate, much like the (default) function
|
|
|
|
// argument function argument strategy.
|
|
|
|
|
2015-09-11 00:52:16 +02:00
|
|
|
use rewrite::{Rewrite, RewriteContext};
|
2015-09-09 23:14:54 +02:00
|
|
|
use utils::make_indent;
|
2015-09-11 00:52:16 +02:00
|
|
|
use expr::rewrite_call;
|
|
|
|
|
|
|
|
use syntax::{ast, ptr};
|
|
|
|
use syntax::codemap::{mk_sp, Span};
|
|
|
|
use syntax::print::pprust;
|
|
|
|
|
2015-09-09 23:14:54 +02:00
|
|
|
pub fn rewrite_chain(mut expr: &ast::Expr,
|
2015-09-11 00:52:16 +02:00
|
|
|
context: &RewriteContext,
|
|
|
|
width: usize,
|
|
|
|
offset: usize)
|
|
|
|
-> Option<String> {
|
2015-09-09 23:14:54 +02:00
|
|
|
let total_span = expr.span;
|
|
|
|
let mut subexpr_list = vec![expr];
|
2015-09-11 00:52:16 +02:00
|
|
|
|
2015-09-09 23:14:54 +02:00
|
|
|
while let Some(subexpr) = pop_expr_chain(expr) {
|
|
|
|
subexpr_list.push(subexpr);
|
|
|
|
expr = subexpr;
|
2015-09-11 00:52:16 +02:00
|
|
|
}
|
|
|
|
|
2015-09-09 23:14:54 +02:00
|
|
|
let parent = subexpr_list.pop().unwrap();
|
2015-09-11 00:52:16 +02:00
|
|
|
let parent_rewrite = try_opt!(expr.rewrite(context, width, offset));
|
2015-09-09 23:14:54 +02:00
|
|
|
let (extra_indent, extend) = if !parent_rewrite.contains('\n') && is_continuable(parent) ||
|
|
|
|
parent_rewrite.len() <= context.config.tab_spaces {
|
|
|
|
(parent_rewrite.len(), true)
|
|
|
|
} else {
|
|
|
|
(context.config.tab_spaces, false)
|
|
|
|
};
|
|
|
|
let indent = offset + extra_indent;
|
|
|
|
|
|
|
|
let max_width = try_opt!(width.checked_sub(extra_indent));
|
|
|
|
let rewrites = try_opt!(subexpr_list.into_iter()
|
|
|
|
.rev()
|
|
|
|
.map(|e| {
|
|
|
|
rewrite_chain_expr(e,
|
|
|
|
total_span,
|
|
|
|
context,
|
|
|
|
max_width,
|
|
|
|
indent)
|
|
|
|
})
|
|
|
|
.collect::<Option<Vec<_>>>());
|
|
|
|
|
2015-09-09 23:13:37 +02:00
|
|
|
let total_width = rewrites.iter().fold(0, |a, b| a + b.len()) + parent_rewrite.len();
|
|
|
|
let fits_single_line = total_width <= width && rewrites.iter().all(|s| !s.contains('\n'));
|
2015-09-11 00:52:16 +02:00
|
|
|
|
2015-09-09 23:13:37 +02:00
|
|
|
let connector = if fits_single_line {
|
2015-09-11 00:52:16 +02:00
|
|
|
String::new()
|
|
|
|
} else {
|
|
|
|
format!("\n{}", make_indent(indent))
|
|
|
|
};
|
|
|
|
|
2015-09-09 23:14:54 +02:00
|
|
|
let first_connector = if extend {
|
2015-09-11 00:52:16 +02:00
|
|
|
""
|
|
|
|
} else {
|
|
|
|
&connector[..]
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(format!("{}{}{}", parent_rewrite, first_connector, rewrites.join(&connector)))
|
|
|
|
}
|
|
|
|
|
2015-09-09 23:14:54 +02:00
|
|
|
fn pop_expr_chain<'a>(expr: &'a ast::Expr) -> Option<&'a ast::Expr> {
|
|
|
|
match expr.node {
|
|
|
|
ast::Expr_::ExprMethodCall(_, _, ref expressions) => {
|
|
|
|
Some(&expressions[0])
|
|
|
|
}
|
|
|
|
ast::Expr_::ExprTupField(ref subexpr, _) |
|
|
|
|
ast::Expr_::ExprField(ref subexpr, _) => {
|
|
|
|
Some(subexpr)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_chain_expr(expr: &ast::Expr,
|
2015-09-11 00:52:57 +02:00
|
|
|
span: Span,
|
|
|
|
context: &RewriteContext,
|
|
|
|
width: usize,
|
|
|
|
offset: usize)
|
2015-09-09 23:14:54 +02:00
|
|
|
-> Option<String> {
|
2015-09-11 00:52:57 +02:00
|
|
|
match expr.node {
|
|
|
|
ast::Expr_::ExprMethodCall(ref method_name, ref types, ref expressions) => {
|
2015-09-09 23:14:54 +02:00
|
|
|
rewrite_method_call(method_name.node, types, expressions, span, context, width, offset)
|
2015-09-11 00:52:57 +02:00
|
|
|
}
|
2015-09-09 23:14:54 +02:00
|
|
|
ast::Expr_::ExprField(_, ref field) => {
|
|
|
|
Some(format!(".{}", field.node))
|
2015-09-11 00:52:57 +02:00
|
|
|
}
|
2015-09-09 23:14:54 +02:00
|
|
|
ast::Expr_::ExprTupField(_, ref field) => {
|
|
|
|
Some(format!(".{}", field.node))
|
2015-09-11 00:52:57 +02:00
|
|
|
}
|
2015-09-09 23:14:54 +02:00
|
|
|
_ => unreachable!(),
|
2015-09-11 00:52:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determines we can continue formatting a given expression on the same line.
|
|
|
|
fn is_continuable(expr: &ast::Expr) -> bool {
|
|
|
|
match expr.node {
|
|
|
|
ast::Expr_::ExprPath(..) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 00:52:16 +02:00
|
|
|
fn rewrite_method_call(method_name: ast::Ident,
|
|
|
|
types: &[ptr::P<ast::Ty>],
|
|
|
|
args: &[ptr::P<ast::Expr>],
|
|
|
|
span: Span,
|
|
|
|
context: &RewriteContext,
|
|
|
|
width: usize,
|
|
|
|
offset: usize)
|
|
|
|
-> Option<String> {
|
|
|
|
let type_str = if types.is_empty() {
|
|
|
|
String::new()
|
|
|
|
} else {
|
|
|
|
let type_list = types.iter().map(|ty| pprust::ty_to_string(ty)).collect::<Vec<_>>();
|
|
|
|
format!("::<{}>", type_list.join(", "))
|
|
|
|
};
|
|
|
|
|
|
|
|
let callee_str = format!(".{}{}", method_name, type_str);
|
2015-09-09 23:09:39 +02:00
|
|
|
let inner_context = &RewriteContext {
|
|
|
|
block_indent: offset,
|
|
|
|
..*context
|
|
|
|
};
|
2015-09-11 00:52:16 +02:00
|
|
|
|
2015-09-09 23:09:39 +02:00
|
|
|
rewrite_call(inner_context, &callee_str, args, span, width, offset)
|
2015-09-11 00:52:16 +02:00
|
|
|
}
|