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)
408 // When overflowing the expressions which consists of a control flow
409 // expression, avoid condition to use multi line.
410 ast::ExprKind::If(..)
411 | ast::ExprKind::IfLet(..)
412 | ast::ExprKind::ForLoop(..)
413 | ast::ExprKind::Loop(..)
414 | ast::ExprKind::While(..)
415 | ast::ExprKind::WhileLet(..)
416 | ast::ExprKind::Match(..) => {
417 let multi_line = rewrite_cond(self.context, expr, shape)
418 .map_or(false, |cond| cond.contains('\n'));
423 expr.rewrite(self.context, shape)
427 _ => expr.rewrite(self.context, shape),
430 item => item.rewrite(self.context, shape),
433 if let Some(rewrite) = rewrite {
434 let rewrite_first_line = Some(rewrite[..first_line_width(&rewrite)].to_owned());
435 last_list_item.item = rewrite_first_line;
442 fn default_tactic(&self, list_items: &[ListItem]) -> DefinitiveListTactic {
445 ListTactic::LimitedHorizontalVertical(self.item_max_width),
451 fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveListTactic {
453 let combine_arg_with_callee = self.items.len() == 1
454 && self.items[0].is_expr()
455 && self.ident.len() < self.context.config.tab_spaces();
456 let overflow_last = combine_arg_with_callee || can_be_overflowed(self.context, &self.items);
458 // Replace the last item with its first line to see if it fits with
460 let placeholder = if overflow_last {
461 let old_value = *self.context.force_one_line_chain.borrow();
462 match self.last_item() {
463 Some(OverflowableItem::Expr(expr))
464 if !combine_arg_with_callee && is_method_call(expr) =>
466 self.context.force_one_line_chain.replace(true);
470 let result = last_item_shape(
476 .and_then(|arg_shape| {
477 self.rewrite_last_item_with_overflow(
478 &mut list_items[self.items.len() - 1],
482 self.context.force_one_line_chain.replace(old_value);
488 let mut tactic = definitive_tactic(
490 ListTactic::LimitedHorizontalVertical(self.item_max_width),
495 // Replace the stub with the full overflowing last argument if the rewrite
496 // succeeded and its first line fits with the other arguments.
497 match (overflow_last, tactic, placeholder) {
498 (true, DefinitiveListTactic::Horizontal, Some(ref overflowed))
499 if self.items.len() == 1 =>
501 // When we are rewriting a nested function call, we restrict the
502 // budget for the inner function to avoid them being deeply nested.
503 // However, when the inner function has a prefix or a suffix
504 // (e.g. `foo() as u32`), this budget reduction may produce poorly
505 // formatted code, where a prefix or a suffix being left on its own
506 // line. Here we explicitlly check those cases.
507 if count_newlines(overflowed) == 1 {
511 .and_then(|last_item| last_item.rewrite(self.context, self.nested_shape));
512 let no_newline = rw.as_ref().map_or(false, |s| !s.contains('\n'));
514 list_items[self.items.len() - 1].item = rw;
516 list_items[self.items.len() - 1].item = Some(overflowed.to_owned());
519 list_items[self.items.len() - 1].item = Some(overflowed.to_owned());
522 (true, DefinitiveListTactic::Horizontal, placeholder @ Some(..)) => {
523 list_items[self.items.len() - 1].item = placeholder;
525 _ if !self.items.is_empty() => {
526 list_items[self.items.len() - 1].item = self
529 .and_then(|last_item| last_item.rewrite(self.context, self.nested_shape));
531 // Use horizontal layout for a function with a single argument as long as
532 // everything fits in a single line.
533 // `self.one_line_width == 0` means vertical layout is forced.
534 if self.items.len() == 1
535 && self.one_line_width != 0
536 && !list_items[0].has_comment()
537 && !list_items[0].inner_as_ref().contains('\n')
538 && ::lists::total_item_width(&list_items[0]) <= self.one_line_width
540 tactic = DefinitiveListTactic::Horizontal;
542 tactic = self.default_tactic(list_items);
544 if tactic == DefinitiveListTactic::Vertical {
545 if let Some((all_simple, num_args_before)) =
546 maybe_get_args_offset(self.ident, &self.items)
548 let one_line = all_simple
549 && definitive_tactic(
550 &list_items[..num_args_before],
551 ListTactic::HorizontalVertical,
553 self.nested_shape.width,
554 ) == DefinitiveListTactic::Horizontal
555 && definitive_tactic(
556 &list_items[num_args_before + 1..],
557 ListTactic::HorizontalVertical,
559 self.nested_shape.width,
560 ) == DefinitiveListTactic::Horizontal;
563 tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
565 } else if is_every_expr_simple(&self.items) && no_long_items(list_items) {
566 tactic = DefinitiveListTactic::Mixed;
577 fn rewrite_items(&self) -> Option<(bool, String)> {
578 let span = self.items_span();
579 let items = itemize_list(
580 self.context.snippet_provider,
584 |item| item.span().lo(),
585 |item| item.span().hi(),
586 |item| item.rewrite(self.context, self.nested_shape),
591 let mut list_items: Vec<_> = items.collect();
593 // Try letting the last argument overflow to the next line with block
594 // indentation. If its first line fits on one line with the other arguments,
595 // we format the function arguments horizontally.
596 let tactic = self.try_overflow_last_item(&mut list_items);
597 let trailing_separator = if let Some(tactic) = self.force_separator_tactic {
599 } else if !self.context.use_block_indent() {
600 SeparatorTactic::Never
601 } else if tactic == DefinitiveListTactic::Mixed {
602 // We are using mixed layout because everything did not fit within a single line.
603 SeparatorTactic::Always
605 self.context.config.trailing_comma()
607 let ends_with_newline = match tactic {
608 DefinitiveListTactic::Vertical | DefinitiveListTactic::Mixed => {
609 self.context.use_block_indent()
614 let fmt = ListFormatting::new(self.nested_shape, self.context.config)
616 .trailing_separator(trailing_separator)
617 .ends_with_newline(ends_with_newline);
619 write_list(&list_items, &fmt)
620 .map(|items_str| (tactic == DefinitiveListTactic::Horizontal, items_str))
623 fn wrap_items(&self, items_str: &str, shape: Shape, is_extendable: bool) -> String {
625 width: shape.width.saturating_sub(last_line_width(self.ident)),
629 let (prefix, suffix) = match self.custom_delims {
630 Some((lhs, rhs)) => (lhs, rhs),
631 _ => (self.prefix, self.suffix),
635 let fits_one_line = items_str.len() + 2 <= shape.width;
636 let extend_width = if items_str.is_empty() {
639 first_line_width(items_str) + 1
641 let nested_indent_str = self
644 .to_string_with_newline(self.context.config);
645 let indent_str = shape
648 .to_string_with_newline(self.context.config);
649 let mut result = String::with_capacity(
650 self.ident.len() + items_str.len() + 2 + indent_str.len() + nested_indent_str.len(),
652 result.push_str(self.ident);
653 result.push_str(prefix);
654 if !self.context.use_block_indent()
655 || (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
656 || (is_extendable && extend_width <= shape.width)
658 result.push_str(items_str);
660 if !items_str.is_empty() {
661 result.push_str(&nested_indent_str);
662 result.push_str(items_str);
664 result.push_str(&indent_str);
666 result.push_str(suffix);
670 fn rewrite(&self, shape: Shape) -> Option<String> {
671 let (extendable, items_str) = self.rewrite_items()?;
673 // If we are using visual indent style and failed to format, retry with block indent.
674 if !self.context.use_block_indent()
675 && need_block_indent(&items_str, self.nested_shape)
678 self.context.use_block.replace(true);
679 let result = self.rewrite(shape);
680 self.context.use_block.replace(false);
684 Some(self.wrap_items(&items_str, shape, extendable))
688 fn need_block_indent(s: &str, shape: Shape) -> bool {
689 s.lines().skip(1).any(|s| {
690 s.find(|c| !char::is_whitespace(c))
691 .map_or(false, |w| w + 1 < shape.indent.width())
695 fn can_be_overflowed(context: &RewriteContext, items: &[OverflowableItem]) -> bool {
698 .map_or(false, |x| x.can_be_overflowed(context, items.len()))
701 /// Returns a shape for the last argument which is going to be overflowed.
703 lists: &[OverflowableItem],
706 args_max_width: usize,
708 if items.len() == 1 && !lists.get(0)?.is_nested_call() {
711 let offset = items.iter().rev().skip(1).fold(0, |acc, i| {
713 acc + 2 + i.inner_as_ref().len()
716 width: min(args_max_width, shape.width),
722 fn shape_from_indent_style(
723 context: &RewriteContext,
728 let (shape, overhead) = if context.use_block_indent() {
731 .block_indent(context.config.tab_spaces())
732 .with_max_width(context.config);
733 (shape, 1) // 1 = ","
735 (shape.visual_indent(offset), overhead)
738 width: shape.width.saturating_sub(overhead),
743 fn no_long_items(list: &[ListItem]) -> bool {
745 .all(|item| item.inner_as_ref().len() <= SHORT_ITEM_THRESHOLD)
748 /// In case special-case style is required, returns an offset from which we start horizontal layout.
749 pub fn maybe_get_args_offset(callee_str: &str, args: &[OverflowableItem]) -> Option<(bool, usize)> {
750 if let Some(&(_, num_args_before)) = args
754 .find(|&&(s, _)| s == callee_str)
756 let all_simple = args.len() > num_args_before
757 && is_every_expr_simple(&args[0..num_args_before])
758 && is_every_expr_simple(&args[num_args_before + 1..]);
760 Some((all_simple, num_args_before))