Skip to content

Commit 71825d9

Browse files
committed
chains: treat some string lits as blocks
1 parent 428d1ac commit 71825d9

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

src/chains.rs

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ use macros::convert_try_mac;
7171
use rewrite::{Rewrite, RewriteContext};
7272
use shape::Shape;
7373
use spanned::Spanned;
74-
use utils::{first_line_width, last_line_extendable, last_line_width, mk_sp, wrap_str};
74+
use utils::{
75+
first_line_width, last_line_extendable, last_line_width, mk_sp, trimmed_last_line_width,
76+
wrap_str,
77+
};
7578

7679
use std::borrow::Cow;
7780
use std::cmp::min;
@@ -395,10 +398,11 @@ impl<'a> ChainFormatterShared<'a> {
395398
let last = &self.children[0];
396399
let extendable =
397400
may_extend && last_line_extendable(&self.rewrites[self.rewrites.len() - 1]);
401+
let prev_last_line_width = last_line_width(&self.rewrites[self.rewrites.len() - 1]);
398402

399403
// Total of all items excluding the last.
400404
let almost_total = if extendable {
401-
last_line_width(&self.rewrites[self.rewrites.len() - 1])
405+
prev_last_line_width
402406
} else {
403407
self.rewrites.iter().fold(0, |a, b| a + b.len())
404408
} + last.tries;
@@ -410,7 +414,7 @@ impl<'a> ChainFormatterShared<'a> {
410414

411415
let all_in_one_line =
412416
self.rewrites.iter().all(|s| !s.contains('\n')) && one_line_budget > 0;
413-
let last_shape = if all_in_one_line {
417+
let last_shape = if all_in_one_line || extendable {
414418
shape.sub_width(last.tries)?
415419
} else {
416420
child_shape.sub_width(shape.rhs_overhead(context.config) + last.tries)?
@@ -508,6 +512,37 @@ impl<'a> ChainFormatterBlock<'a> {
508512
is_block_like: Vec::with_capacity(chain.children.len() + 1),
509513
}
510514
}
515+
516+
// States whether an expression's last line exclusively consists of closing
517+
// parens, braces, and brackets in its idiomatic formatting.
518+
fn is_block_expr(context: &RewriteContext, expr: &ast::Expr, repr: &str) -> bool {
519+
match expr.node {
520+
ast::ExprKind::Mac(..)
521+
| ast::ExprKind::Call(..)
522+
| ast::ExprKind::MethodCall(..)
523+
| ast::ExprKind::Struct(..)
524+
| ast::ExprKind::While(..)
525+
| ast::ExprKind::WhileLet(..)
526+
| ast::ExprKind::If(..)
527+
| ast::ExprKind::IfLet(..)
528+
| ast::ExprKind::Block(..)
529+
| ast::ExprKind::Loop(..)
530+
| ast::ExprKind::ForLoop(..)
531+
| ast::ExprKind::Match(..) => repr.contains('\n'),
532+
ast::ExprKind::Paren(ref expr)
533+
| ast::ExprKind::Binary(_, _, ref expr)
534+
| ast::ExprKind::Index(_, ref expr)
535+
| ast::ExprKind::Unary(_, ref expr)
536+
| ast::ExprKind::Closure(_, _, _, _, ref expr, _)
537+
| ast::ExprKind::Try(ref expr)
538+
| ast::ExprKind::Yield(Some(ref expr)) => Self::is_block_expr(context, expr, repr),
539+
// This can only be a string lit
540+
ast::ExprKind::Lit(_) => {
541+
repr.contains('\n') && trimmed_last_line_width(repr) <= context.config.tab_spaces()
542+
}
543+
_ => false,
544+
}
545+
}
511546
}
512547

513548
impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
@@ -519,7 +554,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
519554
) -> Option<()> {
520555
let mut root_rewrite: String = parent.rewrite(context, shape)?;
521556

522-
let mut root_ends_with_block = is_block_expr(context, &parent.expr, &root_rewrite);
557+
let mut root_ends_with_block = Self::is_block_expr(context, &parent.expr, &root_rewrite);
523558
let tab_width = context.config.tab_spaces().saturating_sub(shape.offset);
524559

525560
while root_rewrite.len() <= tab_width && !root_rewrite.contains('\n') {
@@ -530,7 +565,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
530565
None => break,
531566
}
532567

533-
root_ends_with_block = is_block_expr(context, &item.expr, &root_rewrite);
568+
root_ends_with_block = Self::is_block_expr(context, &item.expr, &root_rewrite);
534569

535570
self.shared.children = &self.shared.children[..self.shared.children.len() - 1];
536571
if self.shared.children.is_empty() {
@@ -554,7 +589,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
554589
for item in self.shared.children[1..].iter().rev() {
555590
let rewrite = item.rewrite_postfix(context, child_shape)?;
556591
self.is_block_like
557-
.push(is_block_expr(context, &item.expr, &rewrite));
592+
.push(Self::is_block_expr(context, &item.expr, &rewrite));
558593
self.shared.rewrites.push(rewrite);
559594
}
560595
Some(())
@@ -608,12 +643,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
608643
}
609644
}
610645

611-
// Parent is the first item in the chain, e.g., `foo` in `foo.bar.baz()`.
612-
let parent_shape = if is_block_expr(context, &parent.expr, "\n") {
613-
shape.visual_indent(0)
614-
} else {
615-
shape
616-
};
646+
let parent_shape = shape.visual_indent(0);
617647
let mut root_rewrite = parent.rewrite(context, parent_shape)?;
618648

619649
if !root_rewrite.contains('\n') && is_continuable(&parent.expr) {
@@ -661,30 +691,3 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
661691
self.shared.pure_root()
662692
}
663693
}
664-
665-
// States whether an expression's last line exclusively consists of closing
666-
// parens, braces, and brackets in its idiomatic formatting.
667-
fn is_block_expr(context: &RewriteContext, expr: &ast::Expr, repr: &str) -> bool {
668-
match expr.node {
669-
ast::ExprKind::Mac(..) | ast::ExprKind::Call(..) | ast::ExprKind::MethodCall(..) => {
670-
context.use_block_indent() && repr.contains('\n')
671-
}
672-
ast::ExprKind::Struct(..)
673-
| ast::ExprKind::While(..)
674-
| ast::ExprKind::WhileLet(..)
675-
| ast::ExprKind::If(..)
676-
| ast::ExprKind::IfLet(..)
677-
| ast::ExprKind::Block(..)
678-
| ast::ExprKind::Loop(..)
679-
| ast::ExprKind::ForLoop(..)
680-
| ast::ExprKind::Match(..) => repr.contains('\n'),
681-
ast::ExprKind::Paren(ref expr)
682-
| ast::ExprKind::Binary(_, _, ref expr)
683-
| ast::ExprKind::Index(_, ref expr)
684-
| ast::ExprKind::Unary(_, ref expr)
685-
| ast::ExprKind::Closure(_, _, _, _, ref expr, _)
686-
| ast::ExprKind::Try(ref expr)
687-
| ast::ExprKind::Yield(Some(ref expr)) => is_block_expr(context, expr, repr),
688-
_ => false,
689-
}
690-
}

0 commit comments

Comments
 (0)