1 //! Formatting of chained expressions, i.e., expressions that are chained by
2 //! dots: struct and enum field access, method calls, and try shorthand (`?`).
4 //! Instead of walking these subexpressions one-by-one, as is our usual strategy
5 //! for expression formatting, we collect maximal sequences of these expressions
6 //! and handle them simultaneously.
8 //! Whenever possible, the entire chain is put on a single line. If that fails,
9 //! we put each subexpression on a separate, much like the (default) function
10 //! argument function argument strategy.
12 //! Depends on config options: `chain_indent` is the indent to use for
13 //! blocks in the parent/root/base of the chain (and the rest of the chain's
15 //! E.g., `let foo = { aaaa; bbb; ccc }.bar.baz();`, we would layout for the
16 //! following values of `chain_indent`:
40 //! If the first item in the chain is a block expression, we align the dots with
61 use rustc_ast::{ast, ptr};
62 use rustc_span::{symbol, BytePos, Span};
64 use crate::comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar};
65 use crate::config::{IndentStyle, Version};
66 use crate::expr::rewrite_call;
67 use crate::lists::extract_pre_comment;
68 use crate::macros::convert_try_mac;
69 use crate::rewrite::{Rewrite, RewriteContext};
70 use crate::shape::Shape;
71 use crate::source_map::SpanUtils;
73 self, first_line_width, last_line_extendable, last_line_width, mk_sp, rewrite_ident,
74 trimmed_last_line_width, wrap_str,
77 pub(crate) fn rewrite_chain(
79 context: &RewriteContext<'_>,
82 let chain = Chain::from_ast(expr, context);
83 debug!("rewrite_chain {:?} {:?}", chain, shape);
85 // If this is just an expression with some `?`s, then format it trivially and
87 if chain.children.is_empty() {
88 return chain.parent.rewrite(context, shape);
91 chain.rewrite(context, shape)
95 enum CommentPosition {
100 // An expression plus trailing `?`s to be formatted together.
108 // FIXME: we can't use a reference here because to convert `try!` to `?` we
109 // synthesise the AST node. However, I think we could use `Cow` and that
110 // would remove a lot of cloning.
116 Vec<ast::GenericArg>,
117 Vec<ptr::P<ast::Expr>>,
119 StructField(symbol::Ident),
120 TupleField(symbol::Ident, bool),
122 Comment(String, CommentPosition),
126 fn is_block_like(&self, context: &RewriteContext<'_>, reps: &str) -> bool {
128 ChainItemKind::Parent(ref expr) => utils::is_block_expr(context, expr, reps),
129 ChainItemKind::MethodCall(..)
130 | ChainItemKind::StructField(..)
131 | ChainItemKind::TupleField(..)
132 | ChainItemKind::Await
133 | ChainItemKind::Comment(..) => false,
137 fn is_tup_field_access(expr: &ast::Expr) -> bool {
139 ast::ExprKind::Field(_, ref field) => {
140 field.name.to_string().chars().all(|c| c.is_digit(10))
146 fn from_ast(context: &RewriteContext<'_>, expr: &ast::Expr) -> (ChainItemKind, Span) {
147 let (kind, span) = match expr.kind {
148 ast::ExprKind::MethodCall(ref segment, ref receiver, ref expressions, _) => {
149 let types = if let Some(ref generic_args) = segment.args {
150 if let ast::GenericArgs::AngleBracketed(ref data) = **generic_args {
153 .filter_map(|x| match x {
154 ast::AngleBracketedArg::Arg(ref generic_arg) => {
155 Some(generic_arg.clone())
166 let span = mk_sp(receiver.span.hi(), expr.span.hi());
167 let kind = ChainItemKind::MethodCall(segment.clone(), types, expressions.clone());
170 ast::ExprKind::Field(ref nested, field) => {
171 let kind = if Self::is_tup_field_access(expr) {
172 ChainItemKind::TupleField(field, Self::is_tup_field_access(nested))
174 ChainItemKind::StructField(field)
176 let span = mk_sp(nested.span.hi(), field.span.hi());
179 ast::ExprKind::Await(ref nested) => {
180 let span = mk_sp(nested.span.hi(), expr.span.hi());
181 (ChainItemKind::Await, span)
183 _ => return (ChainItemKind::Parent(expr.clone()), expr.span),
186 // Remove comments from the span.
187 let lo = context.snippet_provider.span_before(span, ".");
188 (kind, mk_sp(lo, span.hi()))
192 impl Rewrite for ChainItem {
193 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
194 let shape = shape.sub_width(self.tries)?;
195 let rewrite = match self.kind {
196 ChainItemKind::Parent(ref expr) => expr.rewrite(context, shape)?,
197 ChainItemKind::MethodCall(ref segment, ref types, ref exprs) => {
198 Self::rewrite_method_call(segment.ident, types, exprs, self.span, context, shape)?
200 ChainItemKind::StructField(ident) => format!(".{}", rewrite_ident(context, ident)),
201 ChainItemKind::TupleField(ident, nested) => format!(
203 if nested && context.config.version() == Version::One {
208 rewrite_ident(context, ident)
210 ChainItemKind::Await => ".await".to_owned(),
211 ChainItemKind::Comment(ref comment, _) => {
212 rewrite_comment(comment, false, shape, context.config)?
215 Some(format!("{}{}", rewrite, "?".repeat(self.tries)))
220 fn new(context: &RewriteContext<'_>, expr: &ast::Expr, tries: usize) -> ChainItem {
221 let (kind, span) = ChainItemKind::from_ast(context, expr);
222 ChainItem { kind, tries, span }
225 fn comment(span: Span, comment: String, pos: CommentPosition) -> ChainItem {
227 kind: ChainItemKind::Comment(comment, pos),
233 fn is_comment(&self) -> bool {
234 matches!(self.kind, ChainItemKind::Comment(..))
237 fn rewrite_method_call(
238 method_name: symbol::Ident,
239 types: &[ast::GenericArg],
240 args: &[ptr::P<ast::Expr>],
242 context: &RewriteContext<'_>,
244 ) -> Option<String> {
245 let type_str = if types.is_empty() {
248 let type_list = types
250 .map(|ty| ty.rewrite(context, shape))
251 .collect::<Option<Vec<_>>>()?;
253 format!("::<{}>", type_list.join(", "))
255 let callee_str = format!(".{}{}", rewrite_ident(context, method_name), type_str);
256 rewrite_call(context, &callee_str, &args, span, shape)
263 children: Vec<ChainItem>,
267 fn from_ast(expr: &ast::Expr, context: &RewriteContext<'_>) -> Chain {
268 let subexpr_list = Self::make_subexpr_list(expr, context);
270 // Un-parse the expression tree into ChainItems
271 let mut rev_children = vec![];
272 let mut sub_tries = 0;
273 for subexpr in &subexpr_list {
275 ast::ExprKind::Try(_) => sub_tries += 1,
277 rev_children.push(ChainItem::new(context, subexpr, sub_tries));
283 fn is_tries(s: &str) -> bool {
284 s.chars().all(|c| c == '?')
287 fn is_post_comment(s: &str) -> bool {
288 let comment_start_index = s.chars().position(|c| c == '/');
289 if comment_start_index.is_none() {
293 let newline_index = s.chars().position(|c| c == '\n');
294 if newline_index.is_none() {
298 comment_start_index.unwrap() < newline_index.unwrap()
301 fn handle_post_comment(
302 post_comment_span: Span,
303 post_comment_snippet: &str,
304 prev_span_end: &mut BytePos,
305 children: &mut Vec<ChainItem>,
307 let white_spaces: &[_] = &[' ', '\t'];
308 if post_comment_snippet
309 .trim_matches(white_spaces)
315 let trimmed_snippet = trim_tries(post_comment_snippet);
316 if is_post_comment(&trimmed_snippet) {
317 children.push(ChainItem::comment(
319 trimmed_snippet.trim().to_owned(),
320 CommentPosition::Back,
322 *prev_span_end = post_comment_span.hi();
326 let parent = rev_children.pop().unwrap();
327 let mut children = vec![];
328 let mut prev_span_end = parent.span.hi();
329 let mut iter = rev_children.into_iter().rev().peekable();
330 if let Some(first_chain_item) = iter.peek() {
331 let comment_span = mk_sp(prev_span_end, first_chain_item.span.lo());
332 let comment_snippet = context.snippet(comment_span);
333 if !is_tries(comment_snippet.trim()) {
342 while let Some(chain_item) = iter.next() {
343 let comment_snippet = context.snippet(chain_item.span);
344 // FIXME: Figure out the way to get a correct span when converting `try!` to `?`.
346 !(context.config.use_try_shorthand() || is_tries(comment_snippet.trim()));
350 let pre_comment_span = mk_sp(prev_span_end, chain_item.span.lo());
351 let pre_comment_snippet = trim_tries(context.snippet(pre_comment_span));
352 let (pre_comment, _) = extract_pre_comment(&pre_comment_snippet);
354 Some(ref comment) if !comment.is_empty() => {
355 children.push(ChainItem::comment(
358 CommentPosition::Top,
365 prev_span_end = chain_item.span.hi();
366 children.push(chain_item);
369 if !handle_comment || iter.peek().is_none() {
373 let next_lo = iter.peek().unwrap().span.lo();
374 let post_comment_span = mk_sp(prev_span_end, next_lo);
375 let post_comment_snippet = context.snippet(post_comment_span);
378 post_comment_snippet,
384 Chain { parent, children }
387 // Returns a Vec of the prefixes of the chain.
388 // E.g., for input `a.b.c` we return [`a.b.c`, `a.b`, 'a']
389 fn make_subexpr_list(expr: &ast::Expr, context: &RewriteContext<'_>) -> Vec<ast::Expr> {
390 let mut subexpr_list = vec![expr.clone()];
392 while let Some(subexpr) = Self::pop_expr_chain(subexpr_list.last().unwrap(), context) {
393 subexpr_list.push(subexpr.clone());
399 // Returns the expression's subexpression, if it exists. When the subexpr
400 // is a try! macro, we'll convert it to shorthand when the option is set.
401 fn pop_expr_chain(expr: &ast::Expr, context: &RewriteContext<'_>) -> Option<ast::Expr> {
403 ast::ExprKind::MethodCall(_, ref receiver, _, _) => {
404 Some(Self::convert_try(&receiver, context))
406 ast::ExprKind::Field(ref subexpr, _)
407 | ast::ExprKind::Try(ref subexpr)
408 | ast::ExprKind::Await(ref subexpr) => Some(Self::convert_try(subexpr, context)),
413 fn convert_try(expr: &ast::Expr, context: &RewriteContext<'_>) -> ast::Expr {
415 ast::ExprKind::MacCall(ref mac) if context.config.use_try_shorthand() => {
416 if let Some(subexpr) = convert_try_mac(mac, context) {
427 impl Rewrite for Chain {
428 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
429 debug!("rewrite chain {:?} {:?}", self, shape);
431 let mut formatter = match context.config.indent_style() {
432 IndentStyle::Block => {
433 Box::new(ChainFormatterBlock::new(self)) as Box<dyn ChainFormatter>
435 IndentStyle::Visual => {
436 Box::new(ChainFormatterVisual::new(self)) as Box<dyn ChainFormatter>
440 formatter.format_root(&self.parent, context, shape)?;
441 if let Some(result) = formatter.pure_root() {
442 return wrap_str(result, context.config.max_width(), shape);
445 // Decide how to layout the rest of the chain.
446 let child_shape = formatter.child_shape(context, shape)?;
448 formatter.format_children(context, child_shape)?;
449 formatter.format_last_child(context, shape, child_shape)?;
451 let result = formatter.join_rewrites(context, child_shape)?;
452 wrap_str(result, context.config.max_width(), shape)
456 // There are a few types for formatting chains. This is because there is a lot
457 // in common between formatting with block vs visual indent, but they are
458 // different enough that branching on the indent all over the place gets ugly.
459 // Anything that can format a chain is a ChainFormatter.
460 trait ChainFormatter {
461 // Parent is the first item in the chain, e.g., `foo` in `foo.bar.baz()`.
462 // Root is the parent plus any other chain items placed on the first line to
463 // avoid an orphan. E.g.,
468 // If `bar` were not part of the root, then foo would be orphaned and 'float'.
472 context: &RewriteContext<'_>,
475 fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape>;
476 fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()>;
477 fn format_last_child(
479 context: &RewriteContext<'_>,
483 fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String>;
484 // Returns `Some` if the chain is only a root, None otherwise.
485 fn pure_root(&mut self) -> Option<String>;
488 // Data and behaviour that is shared by both chain formatters. The concrete
489 // formatters can delegate much behaviour to `ChainFormatterShared`.
490 struct ChainFormatterShared<'a> {
491 // The current working set of child items.
492 children: &'a [ChainItem],
493 // The current rewrites of items (includes trailing `?`s, but not any way to
494 // connect the rewrites together).
495 rewrites: Vec<String>,
496 // Whether the chain can fit on one line.
497 fits_single_line: bool,
498 // The number of children in the chain. This is not equal to `self.children.len()`
499 // because `self.children` will change size as we process the chain.
503 impl<'a> ChainFormatterShared<'a> {
504 fn new(chain: &'a Chain) -> ChainFormatterShared<'a> {
505 ChainFormatterShared {
506 children: &chain.children,
507 rewrites: Vec::with_capacity(chain.children.len() + 1),
508 fits_single_line: false,
509 child_count: chain.children.len(),
513 fn pure_root(&mut self) -> Option<String> {
514 if self.children.is_empty() {
515 assert_eq!(self.rewrites.len(), 1);
516 Some(self.rewrites.pop().unwrap())
522 // Rewrite the last child. The last child of a chain requires special treatment. We need to
523 // know whether 'overflowing' the last child make a better formatting:
525 // A chain with overflowing the last child:
527 // parent.child1.child2.last_child(
534 // A chain without overflowing the last child (in vertical layout):
539 // .last_child(a, b, c)
542 // In particular, overflowing is effective when the last child is a method with a multi-lined
543 // block-like argument (e.g., closure):
545 // parent.child1.child2.last_child(|a, b, c| {
546 // let x = foo(a, b, c);
547 // let y = bar(a, b, c);
554 fn format_last_child(
557 context: &RewriteContext<'_>,
561 let last = self.children.last()?;
562 let extendable = may_extend && last_line_extendable(&self.rewrites[0]);
563 let prev_last_line_width = last_line_width(&self.rewrites[0]);
565 // Total of all items excluding the last.
566 let almost_total = if extendable {
571 .map(|rw| utils::unicode_str_width(rw))
574 let one_line_budget = if self.child_count == 1 {
577 min(shape.width, context.config.chain_width())
579 .saturating_sub(almost_total);
581 let all_in_one_line = !self.children.iter().any(ChainItem::is_comment)
582 && self.rewrites.iter().all(|s| !s.contains('\n'))
583 && one_line_budget > 0;
584 let last_shape = if all_in_one_line {
585 shape.sub_width(last.tries)?
586 } else if extendable {
587 child_shape.sub_width(last.tries)?
589 child_shape.sub_width(shape.rhs_overhead(context.config) + last.tries)?
592 let mut last_subexpr_str = None;
593 if all_in_one_line || extendable {
594 // First we try to 'overflow' the last child and see if it looks better than using
596 let one_line_shape = if context.use_block_indent() {
597 last_shape.offset_left(almost_total)
600 .visual_indent(almost_total)
601 .sub_width(almost_total)
604 if let Some(one_line_shape) = one_line_shape {
605 if let Some(rw) = last.rewrite(context, one_line_shape) {
606 // We allow overflowing here only if both of the following conditions match:
607 // 1. The entire chain fits in a single line except the last child.
608 // 2. `last_child_str.lines().count() >= 5`.
609 let line_count = rw.lines().count();
610 let could_fit_single_line = first_line_width(&rw) <= one_line_budget;
611 if could_fit_single_line && line_count >= 5 {
612 last_subexpr_str = Some(rw);
613 self.fits_single_line = all_in_one_line;
615 // We could not know whether overflowing is better than using vertical
616 // layout, just by looking at the overflowed rewrite. Now we rewrite the
617 // last child on its own line, and compare two rewrites to choose which is
619 let last_shape = child_shape
620 .sub_width(shape.rhs_overhead(context.config) + last.tries)?;
621 match last.rewrite(context, last_shape) {
622 Some(ref new_rw) if !could_fit_single_line => {
623 last_subexpr_str = Some(new_rw.clone());
625 Some(ref new_rw) if new_rw.lines().count() >= line_count => {
626 last_subexpr_str = Some(rw);
627 self.fits_single_line = could_fit_single_line && all_in_one_line;
629 new_rw @ Some(..) => {
630 last_subexpr_str = new_rw;
633 last_subexpr_str = Some(rw);
634 self.fits_single_line = could_fit_single_line && all_in_one_line;
642 let last_shape = if context.use_block_indent() {
645 child_shape.sub_width(shape.rhs_overhead(context.config) + last.tries)?
648 last_subexpr_str = last_subexpr_str.or_else(|| last.rewrite(context, last_shape));
649 self.rewrites.push(last_subexpr_str?);
653 fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
654 let connector = if self.fits_single_line {
655 // Yay, we can put everything on one line.
659 if context.force_one_line_chain.get() {
662 child_shape.to_string_with_newline(context.config)
665 let mut rewrite_iter = self.rewrites.iter();
666 let mut result = rewrite_iter.next().unwrap().clone();
667 let children_iter = self.children.iter();
668 let iter = rewrite_iter.zip(children_iter);
670 for (rewrite, chain_item) in iter {
671 match chain_item.kind {
672 ChainItemKind::Comment(_, CommentPosition::Back) => result.push(' '),
673 ChainItemKind::Comment(_, CommentPosition::Top) => result.push_str(&connector),
674 _ => result.push_str(&connector),
676 result.push_str(rewrite);
683 // Formats a chain using block indent.
684 struct ChainFormatterBlock<'a> {
685 shared: ChainFormatterShared<'a>,
686 root_ends_with_block: bool,
689 impl<'a> ChainFormatterBlock<'a> {
690 fn new(chain: &'a Chain) -> ChainFormatterBlock<'a> {
691 ChainFormatterBlock {
692 shared: ChainFormatterShared::new(chain),
693 root_ends_with_block: false,
698 impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
702 context: &RewriteContext<'_>,
705 let mut root_rewrite: String = parent.rewrite(context, shape)?;
707 let mut root_ends_with_block = parent.kind.is_block_like(context, &root_rewrite);
708 let tab_width = context.config.tab_spaces().saturating_sub(shape.offset);
710 while root_rewrite.len() <= tab_width && !root_rewrite.contains('\n') {
711 let item = &self.shared.children[0];
712 if let ChainItemKind::Comment(..) = item.kind {
715 let shape = shape.offset_left(root_rewrite.len())?;
716 match &item.rewrite(context, shape) {
717 Some(rewrite) => root_rewrite.push_str(rewrite),
721 root_ends_with_block = last_line_extendable(&root_rewrite);
723 self.shared.children = &self.shared.children[1..];
724 if self.shared.children.is_empty() {
728 self.shared.rewrites.push(root_rewrite);
729 self.root_ends_with_block = root_ends_with_block;
733 fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
735 if self.root_ends_with_block {
736 shape.block_indent(0)
738 shape.block_indent(context.config.tab_spaces())
740 .with_max_width(context.config),
744 fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
745 for item in &self.shared.children[..self.shared.children.len() - 1] {
746 let rewrite = item.rewrite(context, child_shape)?;
747 self.shared.rewrites.push(rewrite);
752 fn format_last_child(
754 context: &RewriteContext<'_>,
759 .format_last_child(true, context, shape, child_shape)
762 fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
763 self.shared.join_rewrites(context, child_shape)
766 fn pure_root(&mut self) -> Option<String> {
767 self.shared.pure_root()
771 // Format a chain using visual indent.
772 struct ChainFormatterVisual<'a> {
773 shared: ChainFormatterShared<'a>,
774 // The extra offset from the chain's shape to the position of the `.`
778 impl<'a> ChainFormatterVisual<'a> {
779 fn new(chain: &'a Chain) -> ChainFormatterVisual<'a> {
780 ChainFormatterVisual {
781 shared: ChainFormatterShared::new(chain),
787 impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
791 context: &RewriteContext<'_>,
794 let parent_shape = shape.visual_indent(0);
795 let mut root_rewrite = parent.rewrite(context, parent_shape)?;
796 let multiline = root_rewrite.contains('\n');
797 self.offset = if multiline {
798 last_line_width(&root_rewrite).saturating_sub(shape.used_width())
800 trimmed_last_line_width(&root_rewrite)
803 if !multiline || parent.kind.is_block_like(context, &root_rewrite) {
804 let item = &self.shared.children[0];
805 if let ChainItemKind::Comment(..) = item.kind {
806 self.shared.rewrites.push(root_rewrite);
809 let child_shape = parent_shape
810 .visual_indent(self.offset)
811 .sub_width(self.offset)?;
812 let rewrite = item.rewrite(context, child_shape)?;
813 match wrap_str(rewrite, context.config.max_width(), shape) {
814 Some(rewrite) => root_rewrite.push_str(&rewrite),
816 // We couldn't fit in at the visual indent, try the last
818 let rewrite = item.rewrite(context, parent_shape)?;
819 root_rewrite.push_str(&rewrite);
824 self.shared.children = &self.shared.children[1..];
827 self.shared.rewrites.push(root_rewrite);
831 fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
833 .with_max_width(context.config)
834 .offset_left(self.offset)
835 .map(|s| s.visual_indent(0))
838 fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
839 for item in &self.shared.children[..self.shared.children.len() - 1] {
840 let rewrite = item.rewrite(context, child_shape)?;
841 self.shared.rewrites.push(rewrite);
846 fn format_last_child(
848 context: &RewriteContext<'_>,
853 .format_last_child(false, context, shape, child_shape)
856 fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
857 self.shared.join_rewrites(context, child_shape)
860 fn pure_root(&mut self) -> Option<String> {
861 self.shared.pure_root()
865 /// Removes try operators (`?`s) that appear in the given string. If removing
866 /// them leaves an empty line, remove that line as well unless it is the first
867 /// line (we need the first newline for detecting pre/post comment).
868 fn trim_tries(s: &str) -> String {
869 let mut result = String::with_capacity(s.len());
870 let mut line_buffer = String::with_capacity(s.len());
871 for (kind, rich_char) in CharClasses::new(s.chars()) {
872 match rich_char.get_char() {
874 if result.is_empty() || !line_buffer.trim().is_empty() {
875 result.push_str(&line_buffer);
880 '?' if kind == FullCodeCharKind::Normal => continue,
881 c => line_buffer.push(c),
884 if !line_buffer.trim().is_empty() {
885 result.push_str(&line_buffer);