Merge pull request #2826 from nrc/complex-struct
Improve formatting of lists of binops
This commit is contained in:
commit
ddbfefef00
19 changed files with 426 additions and 235 deletions
|
@ -194,7 +194,8 @@ fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
|
|||
.rev()
|
||||
.take_while(|c| c.is_whitespace())
|
||||
.filter(|&c| c == '\n')
|
||||
.count() > 1
|
||||
.count()
|
||||
> 1
|
||||
};
|
||||
(if mlb { "\n" } else { "" }, if mla { "\n" } else { "" })
|
||||
}
|
||||
|
|
|
@ -38,7 +38,8 @@ fn main() {
|
|||
|
||||
let exit_code = match execute(&opts) {
|
||||
Ok((exit_mode, summary)) => {
|
||||
if summary.has_operational_errors() || summary.has_parsing_errors()
|
||||
if summary.has_operational_errors()
|
||||
|| summary.has_parsing_errors()
|
||||
|| ((summary.has_diff || summary.has_check_errors())
|
||||
&& exit_mode == ExitCodeMode::Check)
|
||||
{
|
||||
|
|
|
@ -363,7 +363,8 @@ where
|
|||
})
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.count() > 1
|
||||
.count()
|
||||
> 1
|
||||
}
|
||||
|
||||
fn is_block_closure_forced(context: &RewriteContext, expr: &ast::Expr) -> bool {
|
||||
|
|
191
src/expr.rs
191
src/expr.rs
|
@ -31,6 +31,7 @@ use lists::{
|
|||
use macros::{rewrite_macro, MacroArg, MacroPosition};
|
||||
use matches::rewrite_match;
|
||||
use overflow;
|
||||
use pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
|
||||
use patterns::{can_be_overflowed_pat, is_short_pattern, TuplePatField};
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use shape::{Indent, Shape};
|
||||
|
@ -88,11 +89,11 @@ pub fn format_expr(
|
|||
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape, expr.span),
|
||||
ast::ExprKind::Binary(op, ref lhs, ref rhs) => {
|
||||
// FIXME: format comments between operands and operator
|
||||
rewrite_simple_binaries(context, expr, shape, op).or_else(|| {
|
||||
rewrite_all_pairs(expr, shape, context).or_else(|| {
|
||||
rewrite_pair(
|
||||
&**lhs,
|
||||
&**rhs,
|
||||
PairParts::new("", &format!(" {} ", context.snippet(op.span)), ""),
|
||||
PairParts::infix(&format!(" {} ", context.snippet(op.span))),
|
||||
context,
|
||||
shape,
|
||||
context.config.binop_separator(),
|
||||
|
@ -211,7 +212,7 @@ pub fn format_expr(
|
|||
ast::ExprKind::Cast(ref expr, ref ty) => rewrite_pair(
|
||||
&**expr,
|
||||
&**ty,
|
||||
PairParts::new("", " as ", ""),
|
||||
PairParts::infix(" as "),
|
||||
context,
|
||||
shape,
|
||||
SeparatorPlace::Front,
|
||||
|
@ -219,7 +220,7 @@ pub fn format_expr(
|
|||
ast::ExprKind::Type(ref expr, ref ty) => rewrite_pair(
|
||||
&**expr,
|
||||
&**ty,
|
||||
PairParts::new("", ": ", ""),
|
||||
PairParts::infix(": "),
|
||||
context,
|
||||
shape,
|
||||
SeparatorPlace::Back,
|
||||
|
@ -288,7 +289,7 @@ pub fn format_expr(
|
|||
rewrite_pair(
|
||||
&*lhs,
|
||||
&*rhs,
|
||||
PairParts::new("", &sp_delim, ""),
|
||||
PairParts::infix(&sp_delim),
|
||||
context,
|
||||
shape,
|
||||
context.config.binop_separator(),
|
||||
|
@ -361,172 +362,6 @@ pub fn format_expr(
|
|||
})
|
||||
}
|
||||
|
||||
/// Collect operands that appears in the given binary operator in the opposite order.
|
||||
/// e.g. `collect_binary_items(e, ||)` for `a && b || c || d` returns `[d, c, a && b]`.
|
||||
fn collect_binary_items<'a>(mut expr: &'a ast::Expr, binop: ast::BinOp) -> Vec<&'a ast::Expr> {
|
||||
let mut result = vec![];
|
||||
let mut prev_lhs = None;
|
||||
loop {
|
||||
match expr.node {
|
||||
ast::ExprKind::Binary(inner_binop, ref lhs, ref rhs)
|
||||
if inner_binop.node == binop.node =>
|
||||
{
|
||||
result.push(&**rhs);
|
||||
expr = lhs;
|
||||
prev_lhs = Some(lhs);
|
||||
}
|
||||
_ => {
|
||||
if let Some(lhs) = prev_lhs {
|
||||
result.push(lhs);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Rewrites a binary expression whose operands fits within a single line.
|
||||
fn rewrite_simple_binaries(
|
||||
context: &RewriteContext,
|
||||
expr: &ast::Expr,
|
||||
shape: Shape,
|
||||
op: ast::BinOp,
|
||||
) -> Option<String> {
|
||||
let op_str = context.snippet(op.span);
|
||||
|
||||
// 2 = spaces around a binary operator.
|
||||
let sep_overhead = op_str.len() + 2;
|
||||
let nested_overhead = sep_overhead - 1;
|
||||
|
||||
let nested_shape = (match context.config.indent_style() {
|
||||
IndentStyle::Visual => shape.visual_indent(0),
|
||||
IndentStyle::Block => shape.block_indent(context.config.tab_spaces()),
|
||||
}).with_max_width(context.config);
|
||||
let nested_shape = match context.config.binop_separator() {
|
||||
SeparatorPlace::Back => nested_shape.sub_width(nested_overhead)?,
|
||||
SeparatorPlace::Front => nested_shape.offset_left(nested_overhead)?,
|
||||
};
|
||||
|
||||
let opt_rewrites: Option<Vec<_>> = collect_binary_items(expr, op)
|
||||
.iter()
|
||||
.rev()
|
||||
.map(|e| e.rewrite(context, nested_shape))
|
||||
.collect();
|
||||
if let Some(rewrites) = opt_rewrites {
|
||||
if rewrites.iter().all(|e| ::utils::is_single_line(e)) {
|
||||
let total_width = rewrites.iter().map(|s| s.len()).sum::<usize>()
|
||||
+ sep_overhead * (rewrites.len() - 1);
|
||||
|
||||
let sep_str = if total_width <= shape.width {
|
||||
format!(" {} ", op_str)
|
||||
} else {
|
||||
let indent_str = nested_shape.indent.to_string_with_newline(context.config);
|
||||
match context.config.binop_separator() {
|
||||
SeparatorPlace::Back => format!(" {}{}", op_str.trim_right(), indent_str),
|
||||
SeparatorPlace::Front => format!("{}{} ", indent_str, op_str.trim_left()),
|
||||
}
|
||||
};
|
||||
|
||||
return wrap_str(rewrites.join(&sep_str), context.config.max_width(), shape);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[derive(new, Clone, Copy)]
|
||||
pub struct PairParts<'a> {
|
||||
prefix: &'a str,
|
||||
infix: &'a str,
|
||||
suffix: &'a str,
|
||||
}
|
||||
|
||||
pub fn rewrite_pair<LHS, RHS>(
|
||||
lhs: &LHS,
|
||||
rhs: &RHS,
|
||||
pp: PairParts,
|
||||
context: &RewriteContext,
|
||||
shape: Shape,
|
||||
separator_place: SeparatorPlace,
|
||||
) -> Option<String>
|
||||
where
|
||||
LHS: Rewrite,
|
||||
RHS: Rewrite,
|
||||
{
|
||||
let lhs_overhead = match separator_place {
|
||||
SeparatorPlace::Back => shape.used_width() + pp.prefix.len() + pp.infix.trim_right().len(),
|
||||
SeparatorPlace::Front => shape.used_width(),
|
||||
};
|
||||
let lhs_shape = Shape {
|
||||
width: context.budget(lhs_overhead),
|
||||
..shape
|
||||
};
|
||||
let lhs_result = lhs
|
||||
.rewrite(context, lhs_shape)
|
||||
.map(|lhs_str| format!("{}{}", pp.prefix, lhs_str))?;
|
||||
|
||||
// Try to put both lhs and rhs on the same line.
|
||||
let rhs_orig_result = shape
|
||||
.offset_left(last_line_width(&lhs_result) + pp.infix.len())
|
||||
.and_then(|s| s.sub_width(pp.suffix.len()))
|
||||
.and_then(|rhs_shape| rhs.rewrite(context, rhs_shape));
|
||||
if let Some(ref rhs_result) = rhs_orig_result {
|
||||
// If the length of the lhs is equal to or shorter than the tab width or
|
||||
// the rhs looks like block expression, we put the rhs on the same
|
||||
// line with the lhs even if the rhs is multi-lined.
|
||||
let allow_same_line = lhs_result.len() <= context.config.tab_spaces()
|
||||
|| rhs_result
|
||||
.lines()
|
||||
.next()
|
||||
.map(|first_line| first_line.ends_with('{'))
|
||||
.unwrap_or(false);
|
||||
if !rhs_result.contains('\n') || allow_same_line {
|
||||
let one_line_width = last_line_width(&lhs_result)
|
||||
+ pp.infix.len()
|
||||
+ first_line_width(rhs_result)
|
||||
+ pp.suffix.len();
|
||||
if one_line_width <= shape.width {
|
||||
return Some(format!(
|
||||
"{}{}{}{}",
|
||||
lhs_result, pp.infix, rhs_result, pp.suffix
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We have to use multiple lines.
|
||||
// Re-evaluate the rhs because we have more space now:
|
||||
let mut rhs_shape = match context.config.indent_style() {
|
||||
IndentStyle::Visual => shape
|
||||
.sub_width(pp.suffix.len() + pp.prefix.len())?
|
||||
.visual_indent(pp.prefix.len()),
|
||||
IndentStyle::Block => {
|
||||
// Try to calculate the initial constraint on the right hand side.
|
||||
let rhs_overhead = shape.rhs_overhead(context.config);
|
||||
Shape::indented(shape.indent.block_indent(context.config), context.config)
|
||||
.sub_width(rhs_overhead)?
|
||||
}
|
||||
};
|
||||
let infix = match separator_place {
|
||||
SeparatorPlace::Back => pp.infix.trim_right(),
|
||||
SeparatorPlace::Front => pp.infix.trim_left(),
|
||||
};
|
||||
if separator_place == SeparatorPlace::Front {
|
||||
rhs_shape = rhs_shape.offset_left(infix.len())?;
|
||||
}
|
||||
let rhs_result = rhs.rewrite(context, rhs_shape)?;
|
||||
let indent_str = rhs_shape.indent.to_string_with_newline(context.config);
|
||||
let infix_with_sep = match separator_place {
|
||||
SeparatorPlace::Back => format!("{}{}", infix, indent_str),
|
||||
SeparatorPlace::Front => format!("{}{}", indent_str, infix),
|
||||
};
|
||||
Some(format!(
|
||||
"{}{}{}{}",
|
||||
lhs_result, infix_with_sep, rhs_result, pp.suffix
|
||||
))
|
||||
}
|
||||
|
||||
pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
|
||||
name: &str,
|
||||
exprs: &[&T],
|
||||
|
@ -1028,16 +863,16 @@ impl<'a> ControlFlow<'a> {
|
|||
&& context
|
||||
.config
|
||||
.width_heuristics()
|
||||
.single_line_if_else_max_width > 0
|
||||
.single_line_if_else_max_width
|
||||
> 0
|
||||
{
|
||||
let trial = self.rewrite_single_line(&pat_expr_string, context, shape.width);
|
||||
|
||||
if let Some(cond_str) = trial {
|
||||
if cond_str.len()
|
||||
<= context
|
||||
.config
|
||||
.width_heuristics()
|
||||
.single_line_if_else_max_width
|
||||
if cond_str.len() <= context
|
||||
.config
|
||||
.width_heuristics()
|
||||
.single_line_if_else_max_width
|
||||
{
|
||||
return Some((cond_str, 0));
|
||||
}
|
||||
|
@ -2095,7 +1930,7 @@ fn choose_rhs<R: Rewrite>(
|
|||
}
|
||||
(None, Some(ref new_rhs)) => Some(format!("{}{}", new_indent_str, new_rhs)),
|
||||
(None, None) => None,
|
||||
(Some(ref orig_rhs), _) => Some(format!(" {}", orig_rhs)),
|
||||
(Some(orig_rhs), _) => Some(format!(" {}", orig_rhs)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1382,7 +1382,8 @@ fn format_tuple_struct(
|
|||
)?;
|
||||
}
|
||||
|
||||
if !where_clause_str.is_empty() && !where_clause_str.contains('\n')
|
||||
if !where_clause_str.is_empty()
|
||||
&& !where_clause_str.contains('\n')
|
||||
&& (result.contains('\n')
|
||||
|| offset.block_indent + result.len() + where_clause_str.len() + 1
|
||||
> context.config.max_width())
|
||||
|
@ -2527,7 +2528,8 @@ fn rewrite_where_clause_rfc_style(
|
|||
&& comment_before.is_empty()
|
||||
&& comment_after.is_empty()
|
||||
&& !preds_str.contains('\n')
|
||||
&& 6 + preds_str.len() <= shape.width || where_single_line
|
||||
&& 6 + preds_str.len() <= shape.width
|
||||
|| where_single_line
|
||||
{
|
||||
Cow::from(" ")
|
||||
} else {
|
||||
|
@ -2737,7 +2739,8 @@ fn format_generics(
|
|||
false,
|
||||
)?;
|
||||
result.push_str(&where_clause_str);
|
||||
brace_pos == BracePos::ForceSameLine || brace_style == BraceStyle::PreferSameLine
|
||||
brace_pos == BracePos::ForceSameLine
|
||||
|| brace_style == BraceStyle::PreferSameLine
|
||||
|| (generics.where_clause.predicates.is_empty()
|
||||
&& trimmed_last_line_width(&result) == 1)
|
||||
} else {
|
||||
|
|
|
@ -86,6 +86,7 @@ mod matches;
|
|||
mod missed_spans;
|
||||
pub(crate) mod modules;
|
||||
mod overflow;
|
||||
mod pairs;
|
||||
mod patterns;
|
||||
mod reorder;
|
||||
mod rewrite;
|
||||
|
|
12
src/lists.rs
12
src/lists.rs
|
@ -97,11 +97,10 @@ impl ListItem {
|
|||
}
|
||||
|
||||
pub fn is_different_group(&self) -> bool {
|
||||
self.inner_as_ref().contains('\n') || self.pre_comment.is_some()
|
||||
|| self
|
||||
.post_comment
|
||||
.as_ref()
|
||||
.map_or(false, |s| s.contains('\n'))
|
||||
self.inner_as_ref().contains('\n') || self.pre_comment.is_some() || self
|
||||
.post_comment
|
||||
.as_ref()
|
||||
.map_or(false, |s| s.contains('\n'))
|
||||
}
|
||||
|
||||
pub fn is_multiline(&self) -> bool {
|
||||
|
@ -420,7 +419,8 @@ where
|
|||
if first_line_width(&formatted_comment)
|
||||
+ last_line_width(&result)
|
||||
+ comment_alignment
|
||||
+ 1 > formatting.config.max_width()
|
||||
+ 1
|
||||
> formatting.config.max_width()
|
||||
{
|
||||
item_max_width = None;
|
||||
formatted_comment = rewrite_post_comment(&mut item_max_width)?;
|
||||
|
|
|
@ -1107,23 +1107,22 @@ fn indent_macro_snippet(
|
|||
.min()?;
|
||||
|
||||
Some(
|
||||
first_line + "\n"
|
||||
+ &trimmed_lines
|
||||
.iter()
|
||||
.map(
|
||||
|&(trimmed, ref line, prefix_space_width)| match prefix_space_width {
|
||||
_ if !trimmed => line.to_owned(),
|
||||
Some(original_indent_width) => {
|
||||
let new_indent_width = indent.width()
|
||||
+ original_indent_width.saturating_sub(min_prefix_space_width);
|
||||
let new_indent = Indent::from_width(context.config, new_indent_width);
|
||||
format!("{}{}", new_indent.to_string(context.config), line.trim())
|
||||
}
|
||||
None => String::new(),
|
||||
},
|
||||
)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n"),
|
||||
first_line + "\n" + &trimmed_lines
|
||||
.iter()
|
||||
.map(
|
||||
|&(trimmed, ref line, prefix_space_width)| match prefix_space_width {
|
||||
_ if !trimmed => line.to_owned(),
|
||||
Some(original_indent_width) => {
|
||||
let new_indent_width = indent.width() + original_indent_width
|
||||
.saturating_sub(min_prefix_space_width);
|
||||
let new_indent = Indent::from_width(context.config, new_indent_width);
|
||||
format!("{}{}", new_indent.to_string(context.config), line.trim())
|
||||
}
|
||||
None => String::new(),
|
||||
},
|
||||
)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n"),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -320,15 +320,13 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
|
|||
ListTactic::HorizontalVertical,
|
||||
Separator::Comma,
|
||||
self.nested_shape.width,
|
||||
)
|
||||
== DefinitiveListTactic::Horizontal
|
||||
) == DefinitiveListTactic::Horizontal
|
||||
&& definitive_tactic(
|
||||
&list_items[num_args_before + 1..],
|
||||
ListTactic::HorizontalVertical,
|
||||
Separator::Comma,
|
||||
self.nested_shape.width,
|
||||
)
|
||||
== DefinitiveListTactic::Horizontal;
|
||||
) == DefinitiveListTactic::Horizontal;
|
||||
|
||||
if one_line {
|
||||
tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
|
||||
|
|
331
src/pairs.rs
Normal file
331
src/pairs.rs
Normal file
|
@ -0,0 +1,331 @@
|
|||
// Copyright 2018 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.
|
||||
|
||||
use syntax::ast;
|
||||
|
||||
use config::lists::*;
|
||||
use config::IndentStyle;
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use shape::Shape;
|
||||
use utils::{first_line_width, is_single_line, last_line_width, trimmed_last_line_width, wrap_str};
|
||||
|
||||
/// Sigils that decorate a binop pair.
|
||||
#[derive(new, Clone, Copy)]
|
||||
pub(crate) struct PairParts<'a> {
|
||||
prefix: &'a str,
|
||||
infix: &'a str,
|
||||
suffix: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> PairParts<'a> {
|
||||
pub(crate) fn infix(infix: &'a str) -> PairParts<'a> {
|
||||
PairParts {
|
||||
prefix: "",
|
||||
infix,
|
||||
suffix: "",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Flattens a tree of pairs into a list and tries to rewrite them all at once.
|
||||
// FIXME would be nice to reuse the lists API for this, but because each separator
|
||||
// can be different, we can't.
|
||||
pub(crate) fn rewrite_all_pairs(
|
||||
expr: &ast::Expr,
|
||||
shape: Shape,
|
||||
context: &RewriteContext,
|
||||
) -> Option<String> {
|
||||
// First we try formatting on one line.
|
||||
if let Some(list) = expr.flatten(context, false) {
|
||||
if let Some(r) = rewrite_pairs_one_line(&list, shape, context) {
|
||||
return Some(r);
|
||||
}
|
||||
}
|
||||
|
||||
// We can't format on line, so try many. When we flatten here we make sure
|
||||
// to only flatten pairs with the same operator, that way we don't
|
||||
// necessarily need one line per sub-expression, but we don't do anything
|
||||
// too funny wrt precedence.
|
||||
expr.flatten(context, true)
|
||||
.and_then(|list| rewrite_pairs_multiline(list, shape, context))
|
||||
}
|
||||
|
||||
// This may return a multi-line result since we allow the last expression to go
|
||||
// multiline in a 'single line' formatting.
|
||||
fn rewrite_pairs_one_line<T: Rewrite>(
|
||||
list: &PairList<T>,
|
||||
shape: Shape,
|
||||
context: &RewriteContext,
|
||||
) -> Option<String> {
|
||||
assert!(list.list.len() >= 2, "Not a pair?");
|
||||
|
||||
let mut result = String::new();
|
||||
let base_shape = shape.block();
|
||||
|
||||
for (e, s) in list.list.iter().zip(list.separators.iter()) {
|
||||
let cur_shape = base_shape.offset_left(last_line_width(&result))?;
|
||||
let rewrite = e.rewrite(context, cur_shape)?;
|
||||
|
||||
if !is_single_line(&rewrite) || result.len() > shape.width {
|
||||
return None;
|
||||
}
|
||||
|
||||
result.push_str(&rewrite);
|
||||
result.push(' ');
|
||||
result.push_str(s);
|
||||
result.push(' ');
|
||||
}
|
||||
|
||||
let last = list.list.last().unwrap();
|
||||
let cur_shape = base_shape.offset_left(last_line_width(&result))?;
|
||||
let rewrite = last.rewrite(context, cur_shape)?;
|
||||
result.push_str(&rewrite);
|
||||
|
||||
if first_line_width(&result) > shape.width {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Check the last expression in the list. We let this expression go over
|
||||
// multiple lines, but we check that if this is necessary, then we can't
|
||||
// do better using multi-line formatting.
|
||||
if !is_single_line(&result) {
|
||||
let multiline_shape = shape.offset_left(list.separators.last().unwrap().len() + 1)?;
|
||||
let multiline_list: PairList<T> = PairList {
|
||||
list: vec![last],
|
||||
separators: vec![],
|
||||
separator_place: list.separator_place,
|
||||
};
|
||||
// Format as if we were multi-line.
|
||||
if let Some(rewrite) = rewrite_pairs_multiline(multiline_list, multiline_shape, context) {
|
||||
// Also, don't let expressions surrounded by parens go multi-line,
|
||||
// this looks really bad.
|
||||
if rewrite.starts_with('(') || is_single_line(&rewrite) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wrap_str(result, context.config.max_width(), shape)
|
||||
}
|
||||
|
||||
fn rewrite_pairs_multiline<T: Rewrite>(
|
||||
list: PairList<T>,
|
||||
shape: Shape,
|
||||
context: &RewriteContext,
|
||||
) -> Option<String> {
|
||||
let rhs_offset = shape.rhs_overhead(&context.config);
|
||||
let nested_shape = (match context.config.indent_style() {
|
||||
IndentStyle::Visual => shape.visual_indent(0),
|
||||
IndentStyle::Block => shape.block_indent(context.config.tab_spaces()),
|
||||
}).with_max_width(&context.config)
|
||||
.sub_width(rhs_offset)?;
|
||||
|
||||
let indent_str = nested_shape.indent.to_string_with_newline(context.config);
|
||||
let mut result = String::new();
|
||||
|
||||
let rewrite = list.list[0].rewrite(context, shape)?;
|
||||
result.push_str(&rewrite);
|
||||
|
||||
for (e, s) in list.list[1..].iter().zip(list.separators.iter()) {
|
||||
// The following test checks if we should keep two subexprs on the same
|
||||
// line. We do this if not doing so would create an orphan and there is
|
||||
// enough space to do so.
|
||||
let offset = if result.contains('\n') {
|
||||
0
|
||||
} else {
|
||||
shape.used_width()
|
||||
};
|
||||
if last_line_width(&result) + offset <= nested_shape.used_width() {
|
||||
// We must snuggle the next line onto the previous line to avoid an orphan.
|
||||
if let Some(line_shape) =
|
||||
shape.offset_left(s.len() + 2 + trimmed_last_line_width(&result))
|
||||
{
|
||||
if let Some(rewrite) = e.rewrite(context, line_shape) {
|
||||
result.push(' ');
|
||||
result.push_str(s);
|
||||
result.push(' ');
|
||||
result.push_str(&rewrite);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let nested_overhead = s.len() + 1;
|
||||
let line_shape = match context.config.binop_separator() {
|
||||
SeparatorPlace::Back => {
|
||||
result.push(' ');
|
||||
result.push_str(s);
|
||||
result.push_str(&indent_str);
|
||||
nested_shape.sub_width(nested_overhead)?
|
||||
}
|
||||
SeparatorPlace::Front => {
|
||||
result.push_str(&indent_str);
|
||||
result.push_str(s);
|
||||
result.push(' ');
|
||||
nested_shape.offset_left(nested_overhead)?
|
||||
}
|
||||
};
|
||||
|
||||
let rewrite = e.rewrite(context, line_shape)?;
|
||||
result.push_str(&rewrite);
|
||||
}
|
||||
Some(result)
|
||||
}
|
||||
|
||||
// Rewrites a single pair.
|
||||
pub(crate) fn rewrite_pair<LHS, RHS>(
|
||||
lhs: &LHS,
|
||||
rhs: &RHS,
|
||||
pp: PairParts,
|
||||
context: &RewriteContext,
|
||||
shape: Shape,
|
||||
separator_place: SeparatorPlace,
|
||||
) -> Option<String>
|
||||
where
|
||||
LHS: Rewrite,
|
||||
RHS: Rewrite,
|
||||
{
|
||||
let tab_spaces = context.config.tab_spaces();
|
||||
let lhs_overhead = match separator_place {
|
||||
SeparatorPlace::Back => shape.used_width() + pp.prefix.len() + pp.infix.trim_right().len(),
|
||||
SeparatorPlace::Front => shape.used_width(),
|
||||
};
|
||||
let lhs_shape = Shape {
|
||||
width: context.budget(lhs_overhead),
|
||||
..shape
|
||||
};
|
||||
let lhs_result = lhs
|
||||
.rewrite(context, lhs_shape)
|
||||
.map(|lhs_str| format!("{}{}", pp.prefix, lhs_str))?;
|
||||
|
||||
// Try to put both lhs and rhs on the same line.
|
||||
let rhs_orig_result = shape
|
||||
.offset_left(last_line_width(&lhs_result) + pp.infix.len())
|
||||
.and_then(|s| s.sub_width(pp.suffix.len()))
|
||||
.and_then(|rhs_shape| rhs.rewrite(context, rhs_shape));
|
||||
if let Some(ref rhs_result) = rhs_orig_result {
|
||||
// If the length of the lhs is equal to or shorter than the tab width or
|
||||
// the rhs looks like block expression, we put the rhs on the same
|
||||
// line with the lhs even if the rhs is multi-lined.
|
||||
let allow_same_line = lhs_result.len() <= tab_spaces || rhs_result
|
||||
.lines()
|
||||
.next()
|
||||
.map(|first_line| first_line.ends_with('{'))
|
||||
.unwrap_or(false);
|
||||
if !rhs_result.contains('\n') || allow_same_line {
|
||||
let one_line_width = last_line_width(&lhs_result)
|
||||
+ pp.infix.len()
|
||||
+ first_line_width(rhs_result)
|
||||
+ pp.suffix.len();
|
||||
if one_line_width <= shape.width {
|
||||
return Some(format!(
|
||||
"{}{}{}{}",
|
||||
lhs_result, pp.infix, rhs_result, pp.suffix
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We have to use multiple lines.
|
||||
// Re-evaluate the rhs because we have more space now:
|
||||
let mut rhs_shape = match context.config.indent_style() {
|
||||
IndentStyle::Visual => shape
|
||||
.sub_width(pp.suffix.len() + pp.prefix.len())?
|
||||
.visual_indent(pp.prefix.len()),
|
||||
IndentStyle::Block => {
|
||||
// Try to calculate the initial constraint on the right hand side.
|
||||
let rhs_overhead = shape.rhs_overhead(context.config);
|
||||
Shape::indented(shape.indent.block_indent(context.config), context.config)
|
||||
.sub_width(rhs_overhead)?
|
||||
}
|
||||
};
|
||||
let infix = match separator_place {
|
||||
SeparatorPlace::Back => pp.infix.trim_right(),
|
||||
SeparatorPlace::Front => pp.infix.trim_left(),
|
||||
};
|
||||
if separator_place == SeparatorPlace::Front {
|
||||
rhs_shape = rhs_shape.offset_left(infix.len())?;
|
||||
}
|
||||
let rhs_result = rhs.rewrite(context, rhs_shape)?;
|
||||
let indent_str = rhs_shape.indent.to_string_with_newline(context.config);
|
||||
let infix_with_sep = match separator_place {
|
||||
SeparatorPlace::Back => format!("{}{}", infix, indent_str),
|
||||
SeparatorPlace::Front => format!("{}{}", indent_str, infix),
|
||||
};
|
||||
Some(format!(
|
||||
"{}{}{}{}",
|
||||
lhs_result, infix_with_sep, rhs_result, pp.suffix
|
||||
))
|
||||
}
|
||||
|
||||
// A pair which forms a tree and can be flattened (e.g., binops).
|
||||
trait FlattenPair: Rewrite + Sized {
|
||||
// If `_same_op` is `true`, then we only combine binops with the same
|
||||
// operator into the list. E.g,, if the source is `a * b + c`, if `_same_op`
|
||||
// is true, we make `[(a * b), c]` if `_same_op` is false, we make
|
||||
// `[a, b, c]`
|
||||
fn flatten(&self, _context: &RewriteContext, _same_op: bool) -> Option<PairList<Self>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
struct PairList<'a, 'b, T: Rewrite + 'b> {
|
||||
list: Vec<&'b T>,
|
||||
separators: Vec<&'a str>,
|
||||
separator_place: SeparatorPlace,
|
||||
}
|
||||
|
||||
impl FlattenPair for ast::Expr {
|
||||
fn flatten(&self, context: &RewriteContext, same_op: bool) -> Option<PairList<ast::Expr>> {
|
||||
let top_op = match self.node {
|
||||
ast::ExprKind::Binary(op, _, _) => op.node,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
// Turn a tree of binop expressions into a list using a depth-first,
|
||||
// in-order traversal.
|
||||
let mut stack = vec![];
|
||||
let mut list = vec![];
|
||||
let mut separators = vec![];
|
||||
let mut node = self;
|
||||
loop {
|
||||
match node.node {
|
||||
ast::ExprKind::Binary(op, ref lhs, _) if !same_op || op.node == top_op => {
|
||||
stack.push(node);
|
||||
node = lhs;
|
||||
}
|
||||
_ => {
|
||||
list.push(node);
|
||||
if let Some(pop) = stack.pop() {
|
||||
match pop.node {
|
||||
ast::ExprKind::Binary(op, _, ref rhs) => {
|
||||
separators.push(op.node.to_string());
|
||||
node = rhs;
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(list.len() - 1, separators.len());
|
||||
Some(PairList {
|
||||
list,
|
||||
separators,
|
||||
separator_place: context.config.binop_separator(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl FlattenPair for ast::Ty {}
|
||||
impl FlattenPair for ast::Pat {}
|
|
@ -15,15 +15,14 @@ use syntax::ptr;
|
|||
|
||||
use codemap::SpanUtils;
|
||||
use comment::FindUncommented;
|
||||
use expr::{
|
||||
can_be_overflowed_expr, rewrite_pair, rewrite_unary_prefix, wrap_struct_field, PairParts,
|
||||
};
|
||||
use expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field};
|
||||
use lists::{
|
||||
itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape, struct_lit_tactic,
|
||||
write_list,
|
||||
};
|
||||
use macros::{rewrite_macro, MacroPosition};
|
||||
use overflow;
|
||||
use pairs::{rewrite_pair, PairParts};
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use shape::Shape;
|
||||
use spanned::Spanned;
|
||||
|
@ -112,7 +111,7 @@ impl Rewrite for Pat {
|
|||
rewrite_pair(
|
||||
&**lhs,
|
||||
&**rhs,
|
||||
PairParts::new("", &infix, ""),
|
||||
PairParts::infix(&infix),
|
||||
context,
|
||||
shape,
|
||||
SeparatorPlace::Front,
|
||||
|
|
|
@ -242,12 +242,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||
let item_length = items
|
||||
.iter()
|
||||
.take_while(|ppi| {
|
||||
item_kind.is_same_item_kind(&***ppi) && (!in_group || {
|
||||
let current = self.codemap.lookup_line_range(ppi.span());
|
||||
let in_same_group = current.lo < last.hi + 2;
|
||||
last = current;
|
||||
in_same_group
|
||||
})
|
||||
item_kind.is_same_item_kind(&***ppi)
|
||||
&& (!in_group || {
|
||||
let current = self.codemap.lookup_line_range(ppi.span());
|
||||
let in_same_group = current.lo < last.hi + 2;
|
||||
last = current;
|
||||
in_same_group
|
||||
})
|
||||
})
|
||||
.count();
|
||||
let items = &items[..item_length];
|
||||
|
|
|
@ -72,7 +72,8 @@ pub fn rewrite_string<'a>(
|
|||
// succeed.
|
||||
let mut max_chars = shape
|
||||
.width
|
||||
.checked_sub(fmt.opener.len() + ender_length + 1)? + 1;
|
||||
.checked_sub(fmt.opener.len() + ender_length + 1)?
|
||||
+ 1;
|
||||
|
||||
// Snip a line at a time from `orig` until it is used up. Push the snippet
|
||||
// onto result.
|
||||
|
|
|
@ -703,7 +703,8 @@ impl ConfigCodeBlock {
|
|||
.unwrap()
|
||||
.split('\n')
|
||||
.nth(0)
|
||||
.unwrap_or("") == "#![rustfmt::skip]";
|
||||
.unwrap_or("")
|
||||
== "#![rustfmt::skip]";
|
||||
|
||||
if self.config_name.is_none() && !fmt_skip {
|
||||
write_message(&format!(
|
||||
|
|
|
@ -18,12 +18,11 @@ use syntax::symbol::keywords;
|
|||
|
||||
use codemap::SpanUtils;
|
||||
use config::{IndentStyle, TypeDensity};
|
||||
use expr::{
|
||||
rewrite_assign_rhs, rewrite_pair, rewrite_tuple, rewrite_unary_prefix, PairParts, ToExpr,
|
||||
};
|
||||
use expr::{rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ToExpr};
|
||||
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
|
||||
use macros::{rewrite_macro, MacroPosition};
|
||||
use overflow;
|
||||
use pairs::{rewrite_pair, PairParts};
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use shape::Shape;
|
||||
use spanned::Spanned;
|
||||
|
|
|
@ -295,11 +295,10 @@ pub fn mk_sp(lo: BytePos, hi: BytePos) -> Span {
|
|||
// Return true if the given span does not intersect with file lines.
|
||||
macro_rules! out_of_file_lines_range {
|
||||
($self:ident, $span:expr) => {
|
||||
!$self.config.file_lines().is_all()
|
||||
&& !$self
|
||||
.config
|
||||
.file_lines()
|
||||
.intersects(&$self.codemap.lookup_line_range($span))
|
||||
!$self.config.file_lines().is_all() && !$self
|
||||
.config
|
||||
.file_lines()
|
||||
.intersects(&$self.codemap.lookup_line_range($span))
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -173,11 +173,14 @@ pub fn rewrite_with_alignment<T: AlignedItem>(
|
|||
let rest_span = mk_sp(init_last_pos, span.hi());
|
||||
let rest_str = rewrite_with_alignment(rest, context, shape, rest_span, one_line_width)?;
|
||||
Some(
|
||||
result + spaces + "\n"
|
||||
result
|
||||
+ spaces
|
||||
+ "\n"
|
||||
+ &shape
|
||||
.indent
|
||||
.block_indent(context.config)
|
||||
.to_string(context.config) + &rest_str,
|
||||
.to_string(context.config)
|
||||
+ &rest_str,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -440,3 +440,10 @@ fn issue2704() {
|
|||
fn issue2782() {
|
||||
{let f={let f={{match f{F(f,_)=>{{loop{let f={match f{F(f,_)=>{{match f{F(f,_)=>{{loop{let f={let f={match f{'-'=>F(f,()),}};};}}}}}}}};}}}}}};};}
|
||||
}
|
||||
|
||||
fn issue_2802() {
|
||||
function_to_fill_this_line(some_arg, some_arg, some_arg)
|
||||
* a_very_specific_length(specific_length_arg) * very_specific_length(Foo {
|
||||
a: some_much_much_longer_value,
|
||||
}) * some_value
|
||||
}
|
||||
|
|
|
@ -14,7 +14,8 @@ fn foo() -> bool {
|
|||
self.codemap.span_to_filename(s) == self.codemap.span_to_filename(m.inner);
|
||||
|
||||
let some_val = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa * bbbb
|
||||
/ (bbbbbb - function_call(x, *very_long_pointer, y)) + 1000;
|
||||
/ (bbbbbb - function_call(x, *very_long_pointer, y))
|
||||
+ 1000;
|
||||
|
||||
some_ridiculously_loooooooooooooooooooooong_function(
|
||||
10000 * 30000000000 + 40000 / 1002200000000 - 50000 * sqrt(-1),
|
||||
|
@ -358,7 +359,8 @@ fn issue1749() {
|
|||
{
|
||||
{
|
||||
if self.shape[(r as f32 + self.x_offset) as usize]
|
||||
[(c as f32 + self.y_offset) as usize] != 0
|
||||
[(c as f32 + self.y_offset) as usize]
|
||||
!= 0
|
||||
{
|
||||
// hello
|
||||
}
|
||||
|
@ -507,3 +509,12 @@ fn issue2782() {
|
|||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn issue_2802() {
|
||||
function_to_fill_this_line(some_arg, some_arg, some_arg)
|
||||
* a_very_specific_length(specific_length_arg)
|
||||
* very_specific_length(Foo {
|
||||
a: some_much_much_longer_value,
|
||||
})
|
||||
* some_value
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue