1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Rewrite a list some items with overflow.
14 use syntax::parse::token::DelimToken;
15 use syntax::source_map::Span;
16 use syntax::{ast, ptr};
20 can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call, is_simple_expr,
23 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
25 use patterns::{can_be_overflowed_pat, TuplePatField};
26 use rewrite::{Rewrite, RewriteContext};
28 use source_map::SpanUtils;
30 use types::{can_be_overflowed_type, SegmentParam};
31 use utils::{count_newlines, extra_offset, first_line_width, last_line_width, mk_sp};
35 const SHORT_ITEM_THRESHOLD: usize = 10;
37 /// A list of `format!`-like macros, that take a long format string and a list of arguments to
40 /// Organized as a list of `(&str, usize)` tuples, giving the name of the macro and the number of
41 /// arguments before the format string (none for `format!("format", ...)`, one for `assert!(result,
42 /// "format", ...)`, two for `assert_eq!(left, right, "format", ...)`).
43 const SPECIAL_MACRO_WHITELIST: &[(&str, usize)] = &[
44 // format! like macros
45 // From the Rust Standard Library.
54 // From the `log` crate.
64 // assert_eq! like macros
67 ("debug_assert_eq!", 2),
68 ("debug_assert_ne!", 2),
71 const SPECIAL_ATTR_WHITELIST: &[(&str, usize)] = &[
72 // From the `failure` crate.
77 pub enum OverflowableItem<'a> {
79 GenericParam(&'a ast::GenericParam),
80 MacroArg(&'a MacroArg),
81 NestedMetaItem(&'a ast::NestedMetaItem),
82 SegmentParam(&'a SegmentParam<'a>),
83 StructField(&'a ast::StructField),
84 TuplePatField(&'a TuplePatField<'a>),
88 impl<'a> Rewrite for OverflowableItem<'a> {
89 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
90 self.map(|item| item.rewrite(context, shape))
94 impl<'a> Spanned for OverflowableItem<'a> {
95 fn span(&self) -> Span {
96 self.map(|item| item.span())
100 impl<'a> OverflowableItem<'a> {
101 pub fn map<F, T>(&self, f: F) -> T
103 F: Fn(&IntoOverflowableItem<'a>) -> T,
106 OverflowableItem::Expr(expr) => f(*expr),
107 OverflowableItem::GenericParam(gp) => f(*gp),
108 OverflowableItem::MacroArg(macro_arg) => f(*macro_arg),
109 OverflowableItem::NestedMetaItem(nmi) => f(*nmi),
110 OverflowableItem::SegmentParam(sp) => f(*sp),
111 OverflowableItem::StructField(sf) => f(*sf),
112 OverflowableItem::TuplePatField(pat) => f(*pat),
113 OverflowableItem::Ty(ty) => f(*ty),
117 pub fn is_simple(&self) -> bool {
119 OverflowableItem::Expr(expr) => is_simple_expr(expr),
120 OverflowableItem::MacroArg(MacroArg::Expr(expr)) => is_simple_expr(expr),
121 OverflowableItem::NestedMetaItem(nested_meta_item) => match nested_meta_item.node {
122 ast::NestedMetaItemKind::Literal(..) => true,
123 ast::NestedMetaItemKind::MetaItem(ref meta_item) => match meta_item.node {
124 ast::MetaItemKind::Word => true,
132 pub fn is_expr(&self) -> bool {
134 OverflowableItem::Expr(..) => true,
135 OverflowableItem::MacroArg(MacroArg::Expr(..)) => true,
140 pub fn is_nested_call(&self) -> bool {
142 OverflowableItem::Expr(expr) => is_nested_call(expr),
143 OverflowableItem::MacroArg(MacroArg::Expr(expr)) => is_nested_call(expr),
148 pub fn to_expr(&self) -> Option<&'a ast::Expr> {
150 OverflowableItem::Expr(expr) => Some(expr),
151 OverflowableItem::MacroArg(macro_arg) => match macro_arg {
152 MacroArg::Expr(ref expr) => Some(expr),
159 pub fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
161 OverflowableItem::Expr(expr) => can_be_overflowed_expr(context, expr, len),
162 OverflowableItem::MacroArg(macro_arg) => match macro_arg {
163 MacroArg::Expr(ref expr) => can_be_overflowed_expr(context, expr, len),
164 MacroArg::Ty(ref ty) => can_be_overflowed_type(context, ty, len),
165 MacroArg::Pat(..) => false,
166 MacroArg::Item(..) => len == 1,
168 OverflowableItem::NestedMetaItem(nested_meta_item) if len == 1 => {
169 match nested_meta_item.node {
170 ast::NestedMetaItemKind::Literal(..) => false,
171 ast::NestedMetaItemKind::MetaItem(..) => true,
174 OverflowableItem::SegmentParam(seg) => match seg {
175 SegmentParam::Type(ty) => can_be_overflowed_type(context, ty, len),
178 OverflowableItem::TuplePatField(pat) => can_be_overflowed_pat(context, pat, len),
179 OverflowableItem::Ty(ty) => can_be_overflowed_type(context, ty, len),
184 fn whitelist(&self) -> &'static [(&'static str, usize)] {
186 OverflowableItem::MacroArg(..) => SPECIAL_MACRO_WHITELIST,
187 OverflowableItem::NestedMetaItem(..) => SPECIAL_ATTR_WHITELIST,
193 pub trait IntoOverflowableItem<'a>: Rewrite + Spanned {
194 fn into_overflowable_item(&'a self) -> OverflowableItem<'a>;
197 impl<'a, T: 'a + IntoOverflowableItem<'a>> IntoOverflowableItem<'a> for ptr::P<T> {
198 fn into_overflowable_item(&'a self) -> OverflowableItem<'a> {
199 (**self).into_overflowable_item()
203 macro_rules! impl_into_overflowable_item_for_ast_node {
204 ($($ast_node:ident),*) => {
206 impl<'a> IntoOverflowableItem<'a> for ast::$ast_node {
207 fn into_overflowable_item(&'a self) -> OverflowableItem<'a> {
208 OverflowableItem::$ast_node(self)
215 macro_rules! impl_into_overflowable_item_for_rustfmt_types {
216 ([$($ty:ident),*], [$($ty_with_lifetime:ident),*]) => {
218 impl<'a> IntoOverflowableItem<'a> for $ty {
219 fn into_overflowable_item(&'a self) -> OverflowableItem<'a> {
220 OverflowableItem::$ty(self)
225 impl<'a> IntoOverflowableItem<'a> for $ty_with_lifetime<'a> {
226 fn into_overflowable_item(&'a self) -> OverflowableItem<'a> {
227 OverflowableItem::$ty_with_lifetime(self)
234 impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, NestedMetaItem, StructField, Ty);
235 impl_into_overflowable_item_for_rustfmt_types!([MacroArg], [SegmentParam, TuplePatField]);
237 pub fn into_overflowable_list<'a, T>(
238 iter: impl Iterator<Item = &'a T>,
239 ) -> impl Iterator<Item = OverflowableItem<'a>>
241 T: 'a + IntoOverflowableItem<'a>,
243 iter.map(|x| IntoOverflowableItem::into_overflowable_item(x))
246 pub fn rewrite_with_parens<'a, T: 'a + IntoOverflowableItem<'a>>(
247 context: &'a RewriteContext,
249 items: impl Iterator<Item = &'a T>,
252 item_max_width: usize,
253 force_separator_tactic: Option<SeparatorTactic>,
254 ) -> Option<String> {
264 force_separator_tactic,
270 pub fn rewrite_with_angle_brackets<'a, T: 'a + IntoOverflowableItem<'a>>(
271 context: &'a RewriteContext,
273 items: impl Iterator<Item = &'a T>,
276 ) -> Option<String> {
285 context.config.max_width(),
292 pub fn rewrite_with_square_brackets<'a, T: 'a + IntoOverflowableItem<'a>>(
293 context: &'a RewriteContext,
295 items: impl Iterator<Item = &'a T>,
298 force_separator_tactic: Option<SeparatorTactic>,
299 delim_token: Option<DelimToken>,
300 ) -> Option<String> {
301 let (lhs, rhs) = match delim_token {
302 Some(DelimToken::Paren) => ("(", ")"),
303 Some(DelimToken::Brace) => ("{", "}"),
314 context.config.width_heuristics().array_width,
315 force_separator_tactic,
322 context: &'a RewriteContext<'a>,
323 items: Vec<OverflowableItem<'a>>,
325 prefix: &'static str,
326 suffix: &'static str,
327 one_line_shape: Shape,
330 item_max_width: usize,
331 one_line_width: usize,
332 force_separator_tactic: Option<SeparatorTactic>,
333 custom_delims: Option<(&'a str, &'a str)>,
336 impl<'a> Context<'a> {
337 pub fn new<T: 'a + IntoOverflowableItem<'a>>(
338 context: &'a RewriteContext,
339 items: impl Iterator<Item = &'a T>,
343 prefix: &'static str,
344 suffix: &'static str,
345 item_max_width: usize,
346 force_separator_tactic: Option<SeparatorTactic>,
347 custom_delims: Option<(&'a str, &'a str)>,
349 let used_width = extra_offset(ident, shape);
351 let one_line_width = shape.width.saturating_sub(used_width + 2);
354 let one_line_shape = shape
355 .offset_left(last_line_width(ident) + 1)
356 .and_then(|shape| shape.sub_width(1))
357 .unwrap_or(Shape { width: 0, ..shape });
358 let nested_shape = shape_from_indent_style(context, shape, used_width + 2, used_width + 1);
361 items: into_overflowable_list(items).collect(),
370 force_separator_tactic,
375 fn last_item(&self) -> Option<&OverflowableItem> {
379 fn items_span(&self) -> Span {
383 .span_after(self.span, self.prefix);
384 mk_sp(span_lo, self.span.hi())
387 fn rewrite_last_item_with_overflow(
389 last_list_item: &mut ListItem,
391 ) -> Option<String> {
392 let last_item = self.last_item()?;
393 let rewrite = match last_item {
394 OverflowableItem::Expr(ref expr) => {
396 // When overflowing the closure which consists of a single control flow
397 // expression, force to use block if its condition uses multi line.
398 ast::ExprKind::Closure(..) => {
399 // If the argument consists of multiple closures, we do not overflow
401 if closures::args_have_many_closure(&self.items) {
404 closures::rewrite_last_closure(self.context, expr, shape)
407 // When overflowing the expressions which consists of a control flow
408 // expression, avoid condition to use multi line.
409 ast::ExprKind::If(..)
410 | ast::ExprKind::IfLet(..)
411 | ast::ExprKind::ForLoop(..)
412 | ast::ExprKind::Loop(..)
413 | ast::ExprKind::While(..)
414 | ast::ExprKind::WhileLet(..)
415 | ast::ExprKind::Match(..) => {
416 let multi_line = rewrite_cond(self.context, expr, shape)
417 .map_or(false, |cond| cond.contains('\n'));
422 expr.rewrite(self.context, shape)
425 _ => expr.rewrite(self.context, shape),
428 item => item.rewrite(self.context, shape),
431 if let Some(rewrite) = rewrite {
432 let rewrite_first_line = Some(rewrite[..first_line_width(&rewrite)].to_owned());
433 last_list_item.item = rewrite_first_line;
440 fn default_tactic(&self, list_items: &[ListItem]) -> DefinitiveListTactic {
443 ListTactic::LimitedHorizontalVertical(self.item_max_width),
449 fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveListTactic {
451 let combine_arg_with_callee = self.items.len() == 1
452 && self.items[0].is_expr()
453 && self.ident.len() < self.context.config.tab_spaces();
454 let overflow_last = combine_arg_with_callee || can_be_overflowed(self.context, &self.items);
456 // Replace the last item with its first line to see if it fits with
458 let placeholder = if overflow_last {
459 let old_value = *self.context.force_one_line_chain.borrow();
460 match self.last_item() {
461 Some(OverflowableItem::Expr(expr))
462 if !combine_arg_with_callee && is_method_call(expr) =>
464 self.context.force_one_line_chain.replace(true);
468 let result = last_item_shape(
474 .and_then(|arg_shape| {
475 self.rewrite_last_item_with_overflow(
476 &mut list_items[self.items.len() - 1],
480 self.context.force_one_line_chain.replace(old_value);
486 let mut tactic = definitive_tactic(
488 ListTactic::LimitedHorizontalVertical(self.item_max_width),
493 // Replace the stub with the full overflowing last argument if the rewrite
494 // succeeded and its first line fits with the other arguments.
495 match (overflow_last, tactic, placeholder) {
496 (true, DefinitiveListTactic::Horizontal, Some(ref overflowed))
497 if self.items.len() == 1 =>
499 // When we are rewriting a nested function call, we restrict the
500 // budget for the inner function to avoid them being deeply nested.
501 // However, when the inner function has a prefix or a suffix
502 // (e.g. `foo() as u32`), this budget reduction may produce poorly
503 // formatted code, where a prefix or a suffix being left on its own
504 // line. Here we explicitlly check those cases.
505 if count_newlines(overflowed) == 1 {
509 .and_then(|last_item| last_item.rewrite(self.context, self.nested_shape));
510 let no_newline = rw.as_ref().map_or(false, |s| !s.contains('\n'));
512 list_items[self.items.len() - 1].item = rw;
514 list_items[self.items.len() - 1].item = Some(overflowed.to_owned());
517 list_items[self.items.len() - 1].item = Some(overflowed.to_owned());
520 (true, DefinitiveListTactic::Horizontal, placeholder @ Some(..)) => {
521 list_items[self.items.len() - 1].item = placeholder;
523 _ if !self.items.is_empty() => {
524 list_items[self.items.len() - 1].item = self
527 .and_then(|last_item| last_item.rewrite(self.context, self.nested_shape));
529 // Use horizontal layout for a function with a single argument as long as
530 // everything fits in a single line.
531 // `self.one_line_width == 0` means vertical layout is forced.
532 if self.items.len() == 1
533 && self.one_line_width != 0
534 && !list_items[0].has_comment()
535 && !list_items[0].inner_as_ref().contains('\n')
536 && ::lists::total_item_width(&list_items[0]) <= self.one_line_width
538 tactic = DefinitiveListTactic::Horizontal;
540 tactic = self.default_tactic(list_items);
542 if tactic == DefinitiveListTactic::Vertical {
543 if let Some((all_simple, num_args_before)) =
544 maybe_get_args_offset(self.ident, &self.items)
546 let one_line = all_simple
547 && definitive_tactic(
548 &list_items[..num_args_before],
549 ListTactic::HorizontalVertical,
551 self.nested_shape.width,
552 ) == DefinitiveListTactic::Horizontal
553 && definitive_tactic(
554 &list_items[num_args_before + 1..],
555 ListTactic::HorizontalVertical,
557 self.nested_shape.width,
558 ) == DefinitiveListTactic::Horizontal;
561 tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
563 } else if is_every_expr_simple(&self.items) && no_long_items(list_items) {
564 tactic = DefinitiveListTactic::Mixed;
575 fn rewrite_items(&self) -> Option<(bool, String)> {
576 let span = self.items_span();
577 let items = itemize_list(
578 self.context.snippet_provider,
582 |item| item.span().lo(),
583 |item| item.span().hi(),
584 |item| item.rewrite(self.context, self.nested_shape),
589 let mut list_items: Vec<_> = items.collect();
591 // Try letting the last argument overflow to the next line with block
592 // indentation. If its first line fits on one line with the other arguments,
593 // we format the function arguments horizontally.
594 let tactic = self.try_overflow_last_item(&mut list_items);
595 let trailing_separator = if let Some(tactic) = self.force_separator_tactic {
597 } else if !self.context.use_block_indent() {
598 SeparatorTactic::Never
599 } else if tactic == DefinitiveListTactic::Mixed {
600 // We are using mixed layout because everything did not fit within a single line.
601 SeparatorTactic::Always
603 self.context.config.trailing_comma()
605 let ends_with_newline = match tactic {
606 DefinitiveListTactic::Vertical | DefinitiveListTactic::Mixed => {
607 self.context.use_block_indent()
612 let fmt = ListFormatting::new(self.nested_shape, self.context.config)
614 .trailing_separator(trailing_separator)
615 .ends_with_newline(ends_with_newline);
617 write_list(&list_items, &fmt)
618 .map(|items_str| (tactic == DefinitiveListTactic::Horizontal, items_str))
621 fn wrap_items(&self, items_str: &str, shape: Shape, is_extendable: bool) -> String {
623 width: shape.width.saturating_sub(last_line_width(self.ident)),
627 let (prefix, suffix) = match self.custom_delims {
628 Some((lhs, rhs)) => (lhs, rhs),
629 _ => (self.prefix, self.suffix),
633 let fits_one_line = items_str.len() + 2 <= shape.width;
634 let extend_width = if items_str.is_empty() {
637 first_line_width(items_str) + 1
639 let nested_indent_str = self
642 .to_string_with_newline(self.context.config);
643 let indent_str = shape
646 .to_string_with_newline(self.context.config);
647 let mut result = String::with_capacity(
648 self.ident.len() + items_str.len() + 2 + indent_str.len() + nested_indent_str.len(),
650 result.push_str(self.ident);
651 result.push_str(prefix);
652 if !self.context.use_block_indent()
653 || (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
654 || (is_extendable && extend_width <= shape.width)
656 result.push_str(items_str);
658 if !items_str.is_empty() {
659 result.push_str(&nested_indent_str);
660 result.push_str(items_str);
662 result.push_str(&indent_str);
664 result.push_str(suffix);
668 fn rewrite(&self, shape: Shape) -> Option<String> {
669 let (extendable, items_str) = self.rewrite_items()?;
671 // If we are using visual indent style and failed to format, retry with block indent.
672 if !self.context.use_block_indent()
673 && need_block_indent(&items_str, self.nested_shape)
676 self.context.use_block.replace(true);
677 let result = self.rewrite(shape);
678 self.context.use_block.replace(false);
682 Some(self.wrap_items(&items_str, shape, extendable))
686 fn need_block_indent(s: &str, shape: Shape) -> bool {
687 s.lines().skip(1).any(|s| {
688 s.find(|c| !char::is_whitespace(c))
689 .map_or(false, |w| w + 1 < shape.indent.width())
693 fn can_be_overflowed(context: &RewriteContext, items: &[OverflowableItem]) -> bool {
696 .map_or(false, |x| x.can_be_overflowed(context, items.len()))
699 /// Returns a shape for the last argument which is going to be overflowed.
701 lists: &[OverflowableItem],
704 args_max_width: usize,
706 if items.len() == 1 && !lists.get(0)?.is_nested_call() {
709 let offset = items.iter().rev().skip(1).fold(0, |acc, i| {
711 acc + 2 + i.inner_as_ref().len()
714 width: min(args_max_width, shape.width),
720 fn shape_from_indent_style(
721 context: &RewriteContext,
726 let (shape, overhead) = if context.use_block_indent() {
729 .block_indent(context.config.tab_spaces())
730 .with_max_width(context.config);
731 (shape, 1) // 1 = ","
733 (shape.visual_indent(offset), overhead)
736 width: shape.width.saturating_sub(overhead),
741 fn no_long_items(list: &[ListItem]) -> bool {
743 .all(|item| item.inner_as_ref().len() <= SHORT_ITEM_THRESHOLD)
746 /// In case special-case style is required, returns an offset from which we start horizontal layout.
747 pub fn maybe_get_args_offset(callee_str: &str, args: &[OverflowableItem]) -> Option<(bool, usize)> {
748 if let Some(&(_, num_args_before)) = args
752 .find(|&&(s, _)| s == callee_str)
754 let all_simple = args.len() > num_args_before
755 && is_every_expr_simple(&args[0..num_args_before])
756 && is_every_expr_simple(&args[num_args_before + 1..]);
758 Some((all_simple, num_args_before))