X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fchains.rs;h=c7acf19ee0cea6e42bb41891bb371a394b829b23;hb=66c27c9161b2aa70c2902807be12952bd4a0a62b;hp=6686211632357117abc9720215b53fd464ee31a4;hpb=e5284b13773ce155175e16be04178d2091a9f143;p=rust.git diff --git a/src/chains.rs b/src/chains.rs index 66862116323..c7acf19ee0c 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -1,15 +1,5 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Formatting of chained expressions, i.e. expressions which are chained by -//! dots: struct and enum field access, method calls, and try shorthand (?). +//! Formatting of chained expressions, i.e., expressions that are chained by +//! dots: struct and enum field access, method calls, and try shorthand (`?`). //! //! Instead of walking these subexpressions one-by-one, as is our usual strategy //! for expression formatting, we collect maximal sequences of these expressions @@ -26,7 +16,7 @@ //! following values of `chain_indent`: //! Block: //! -//! ```ignore +//! ```text //! let foo = { //! aaaa; //! bbb; @@ -37,7 +27,7 @@ //! //! Visual: //! -//! ```ignore +//! ```text //! let foo = { //! aaaa; //! bbb; @@ -51,7 +41,7 @@ //! the braces. //! Block: //! -//! ```ignore +//! ```text //! let a = foo.bar //! .baz() //! .qux @@ -59,7 +49,7 @@ //! //! Visual: //! -//! ```ignore +//! ```text //! let a = foo.bar //! .baz() //! .qux @@ -84,7 +74,7 @@ trimmed_last_line_width, wrap_str, }; -pub fn rewrite_chain( +pub(crate) fn rewrite_chain( expr: &ast::Expr, context: &RewriteContext<'_>, shape: Shape, @@ -128,6 +118,7 @@ enum ChainItemKind { ), StructField(ast::Ident), TupleField(ast::Ident, bool), + Await, Comment(String, CommentPosition), } @@ -138,6 +129,7 @@ fn is_block_like(&self, context: &RewriteContext<'_>, reps: &str) -> bool { ChainItemKind::MethodCall(..) | ChainItemKind::StructField(..) | ChainItemKind::TupleField(..) + | ChainItemKind::Await | ChainItemKind::Comment(..) => false, } } @@ -176,6 +168,10 @@ fn from_ast(context: &RewriteContext<'_>, expr: &ast::Expr) -> (ChainItemKind, S let span = mk_sp(nested.span.hi(), field.span.hi()); (kind, span) } + ast::ExprKind::Await(ast::AwaitOrigin::FieldLike, ref nested) => { + let span = mk_sp(nested.span.hi(), expr.span.hi()); + (ChainItemKind::Await, span) + } _ => return (ChainItemKind::Parent(expr.clone()), expr.span), }; @@ -199,6 +195,7 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option if nested { " " } else { "" }, rewrite_ident(context, ident) ), + ChainItemKind::Await => ".await".to_owned(), ChainItemKind::Comment(ref comment, _) => { rewrite_comment(comment, false, shape, context.config)? } @@ -397,7 +394,9 @@ fn pop_expr_chain(expr: &ast::Expr, context: &RewriteContext<'_>) -> Option { Some(Self::convert_try(&expressions[0], context)) } - ast::ExprKind::Field(ref subexpr, _) | ast::ExprKind::Try(ref subexpr) => { + ast::ExprKind::Field(ref subexpr, _) + | ast::ExprKind::Try(ref subexpr) + | ast::ExprKind::Await(ast::AwaitOrigin::FieldLike, ref subexpr) => { Some(Self::convert_try(subexpr, context)) } _ => None, @@ -455,7 +454,7 @@ trait ChainFormatter { // Parent is the first item in the chain, e.g., `foo` in `foo.bar.baz()`. // Root is the parent plus any other chain items placed on the first line to // avoid an orphan. E.g., - // ```ignore + // ```text // foo.bar // .baz() // ``` @@ -517,7 +516,7 @@ fn pure_root(&mut self) -> Option { // know whether 'overflowing' the last child make a better formatting: // // A chain with overflowing the last child: - // ```ignore + // ```text // parent.child1.child2.last_child( // a, // b, @@ -526,7 +525,7 @@ fn pure_root(&mut self) -> Option { // ``` // // A chain without overflowing the last child (in vertical layout): - // ```ignore + // ```text // parent // .child1 // .child2 @@ -534,8 +533,8 @@ fn pure_root(&mut self) -> Option { // ``` // // In particular, overflowing is effective when the last child is a method with a multi-lined - // block-like argument (e.g. closure): - // ```ignore + // block-like argument (e.g., closure): + // ```text // parent.child1.child2.last_child(|a, b, c| { // let x = foo(a, b, c); // let y = bar(a, b, c); @@ -560,7 +559,10 @@ fn format_last_child( let almost_total = if extendable { prev_last_line_width } else { - self.rewrites.iter().fold(0, |a, b| a + b.len()) + self.rewrites + .iter() + .map(|rw| utils::unicode_str_width(&rw)) + .sum() } + last.tries; let one_line_budget = if self.child_count == 1 { shape.width @@ -853,7 +855,7 @@ fn pure_root(&mut self) -> Option { } } -/// Remove try operators (`?`s) that appear in the given string. If removing +/// Removes try operators (`?`s) that appear in the given string. If removing /// them leaves an empty line, remove that line as well unless it is the first /// line (we need the first newline for detecting pre/post comment). fn trim_tries(s: &str) -> String {