Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7665017

Browse files
committed
Combine chain items only when the item will get orphaned
1 parent 594774b commit 7665017

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

src/chains.rs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ use utils::{
8080

8181
use std::borrow::Cow;
8282
use std::cmp::min;
83-
use std::iter;
8483

8584
use syntax::source_map::{BytePos, Span};
8685
use syntax::{ast, ptr};
@@ -132,8 +131,8 @@ impl ChainItemKind {
132131
fn is_block_like(&self, context: &RewriteContext, reps: &str) -> bool {
133132
match self {
134133
ChainItemKind::Parent(ref expr) => is_block_expr(context, expr, reps),
135-
ChainItemKind::MethodCall(..) => reps.contains('\n'),
136-
ChainItemKind::StructField(..)
134+
ChainItemKind::MethodCall(..)
135+
| ChainItemKind::StructField(..)
137136
| ChainItemKind::TupleField(..)
138137
| ChainItemKind::Comment(..) => false,
139138
}
@@ -625,12 +624,7 @@ impl<'a> ChainFormatterShared<'a> {
625624
Some(())
626625
}
627626

628-
fn join_rewrites(
629-
&self,
630-
context: &RewriteContext,
631-
child_shape: Shape,
632-
block_like_iter: impl Iterator<Item = bool>,
633-
) -> Option<String> {
627+
fn join_rewrites(&self, context: &RewriteContext, child_shape: Shape) -> Option<String> {
634628
let connector = if self.fits_single_line {
635629
// Yay, we can put everything on one line.
636630
Cow::from("")
@@ -645,17 +639,13 @@ impl<'a> ChainFormatterShared<'a> {
645639
let mut rewrite_iter = self.rewrites.iter();
646640
let mut result = rewrite_iter.next().unwrap().clone();
647641
let children_iter = self.children.iter();
648-
let iter = rewrite_iter.zip(block_like_iter).zip(children_iter);
642+
let iter = rewrite_iter.zip(children_iter);
649643

650-
for ((rewrite, prev_is_block_like), chain_item) in iter {
644+
for (rewrite, chain_item) in iter {
651645
match chain_item.kind {
652646
ChainItemKind::Comment(_, CommentPosition::Back) => result.push(' '),
653647
ChainItemKind::Comment(_, CommentPosition::Top) => result.push_str(&connector),
654-
_ => {
655-
if !prev_is_block_like {
656-
result.push_str(&connector);
657-
}
658-
}
648+
_ => result.push_str(&connector),
659649
}
660650
result.push_str(&rewrite);
661651
}
@@ -667,15 +657,14 @@ impl<'a> ChainFormatterShared<'a> {
667657
// Formats a chain using block indent.
668658
struct ChainFormatterBlock<'a> {
669659
shared: ChainFormatterShared<'a>,
670-
// For each rewrite, whether the corresponding item is block-like.
671-
is_block_like: Vec<bool>,
660+
root_ends_with_block: bool,
672661
}
673662

674663
impl<'a> ChainFormatterBlock<'a> {
675664
fn new(chain: &'a Chain) -> ChainFormatterBlock<'a> {
676665
ChainFormatterBlock {
677666
shared: ChainFormatterShared::new(chain),
678-
is_block_like: Vec::with_capacity(chain.children.len() + 1),
667+
root_ends_with_block: false,
679668
}
680669
}
681670
}
@@ -703,21 +692,21 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
703692
None => break,
704693
}
705694

706-
root_ends_with_block = item.kind.is_block_like(context, &root_rewrite);
695+
root_ends_with_block = last_line_extendable(&root_rewrite);
707696

708697
self.shared.children = &self.shared.children[1..];
709698
if self.shared.children.is_empty() {
710699
break;
711700
}
712701
}
713-
self.is_block_like.push(root_ends_with_block);
714702
self.shared.rewrites.push(root_rewrite);
703+
self.root_ends_with_block = root_ends_with_block;
715704
Some(())
716705
}
717706

718707
fn child_shape(&self, context: &RewriteContext, shape: Shape) -> Option<Shape> {
719708
Some(
720-
if self.is_block_like[0] {
709+
if self.root_ends_with_block {
721710
shape.block_indent(0)
722711
} else {
723712
shape.block_indent(context.config.tab_spaces())
@@ -728,8 +717,6 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
728717
fn format_children(&mut self, context: &RewriteContext, child_shape: Shape) -> Option<()> {
729718
for item in &self.shared.children[..self.shared.children.len() - 1] {
730719
let rewrite = item.rewrite(context, child_shape)?;
731-
self.is_block_like
732-
.push(item.kind.is_block_like(context, &rewrite));
733720
self.shared.rewrites.push(rewrite);
734721
}
735722
Some(())
@@ -746,8 +733,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
746733
}
747734

748735
fn join_rewrites(&self, context: &RewriteContext, child_shape: Shape) -> Option<String> {
749-
self.shared
750-
.join_rewrites(context, child_shape, self.is_block_like.iter().cloned())
736+
self.shared.join_rewrites(context, child_shape)
751737
}
752738

753739
fn pure_root(&mut self) -> Option<String> {
@@ -841,8 +827,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
841827
}
842828

843829
fn join_rewrites(&self, context: &RewriteContext, child_shape: Shape) -> Option<String> {
844-
self.shared
845-
.join_rewrites(context, child_shape, iter::repeat(false))
830+
self.shared.join_rewrites(context, child_shape)
846831
}
847832

848833
fn pure_root(&mut self) -> Option<String> {

0 commit comments

Comments
 (0)