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.
12 // FIXME: Replace `ToExpr` with some enum.
16 use syntax::codemap::Span;
17 use syntax::parse::token::DelimToken;
20 use codemap::SpanUtils;
21 use expr::{is_every_expr_simple, is_nested_call, maybe_get_args_offset, ToExpr};
22 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
23 use rewrite::{Rewrite, RewriteContext};
27 count_newlines, extra_offset, first_line_width, last_line_width, mk_sp, paren_overhead,
32 const SHORT_ITEM_THRESHOLD: usize = 10;
34 pub fn rewrite_with_parens<T>(
35 context: &RewriteContext,
40 item_max_width: usize,
41 force_separator_tactic: Option<SeparatorTactic>,
44 T: Rewrite + ToExpr + Spanned,
55 force_separator_tactic,
60 pub fn rewrite_with_angle_brackets<T>(
61 context: &RewriteContext,
68 T: Rewrite + ToExpr + Spanned,
78 context.config.max_width(),
84 pub fn rewrite_with_square_brackets<T>(
85 context: &RewriteContext,
90 force_separator_tactic: Option<SeparatorTactic>,
91 delim_token: Option<DelimToken>,
94 T: Rewrite + ToExpr + Spanned,
96 let (lhs, rhs) = match delim_token {
97 Some(DelimToken::Paren) => ("(", ")"),
98 Some(DelimToken::Brace) => ("{", "}"),
109 context.config.width_heuristics().array_width,
110 force_separator_tactic,
115 struct Context<'a, T: 'a> {
116 context: &'a RewriteContext<'a>,
119 prefix: &'static str,
120 suffix: &'static str,
121 one_line_shape: Shape,
124 item_max_width: usize,
125 one_line_width: usize,
126 force_separator_tactic: Option<SeparatorTactic>,
127 custom_delims: Option<(&'a str, &'a str)>,
130 impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
132 context: &'a RewriteContext,
137 prefix: &'static str,
138 suffix: &'static str,
139 item_max_width: usize,
140 force_separator_tactic: Option<SeparatorTactic>,
141 custom_delims: Option<(&'a str, &'a str)>,
142 ) -> Context<'a, T> {
144 let paren_overhead = if context.config.spaces_within_parens_and_brackets() {
149 let used_width = extra_offset(ident, shape);
150 let one_line_width = shape
152 .checked_sub(used_width + 2 * paren_overhead)
156 let one_line_shape = shape
157 .offset_left(last_line_width(ident) + 1)
158 .and_then(|shape| shape.sub_width(1))
159 .unwrap_or(Shape { width: 0, ..shape });
160 let nested_shape = shape_from_indent_style(
163 used_width + 2 * paren_overhead,
164 used_width + paren_overhead,
177 force_separator_tactic,
182 fn last_item(&self) -> Option<&&T> {
186 fn items_span(&self) -> Span {
187 let span_lo = self.context
189 .span_after(self.span, self.prefix);
190 mk_sp(span_lo, self.span.hi())
193 fn rewrite_last_item_with_overflow(
195 last_list_item: &mut ListItem,
197 ) -> Option<String> {
198 let last_item = self.last_item()?;
199 let rewrite = if let Some(expr) = last_item.to_expr() {
201 // When overflowing the closure which consists of a single control flow expression,
202 // force to use block if its condition uses multi line.
203 ast::ExprKind::Closure(..) => {
204 // If the argument consists of multiple closures, we do not overflow
206 if closures::args_have_many_closure(self.items) {
209 closures::rewrite_last_closure(self.context, expr, shape)
212 _ => expr.rewrite(self.context, shape),
215 last_item.rewrite(self.context, shape)
218 if let Some(rewrite) = rewrite {
219 let rewrite_first_line = Some(rewrite[..first_line_width(&rewrite)].to_owned());
220 last_list_item.item = rewrite_first_line;
227 fn default_tactic(&self, list_items: &[ListItem]) -> DefinitiveListTactic {
230 ListTactic::LimitedHorizontalVertical(self.item_max_width),
236 fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveListTactic {
238 let combine_arg_with_callee = self.items.len() == 1
239 && self.items[0].to_expr().is_some()
240 && self.ident.len() + 1 <= self.context.config.tab_spaces();
241 let overflow_last = combine_arg_with_callee || can_be_overflowed(self.context, self.items);
243 // Replace the last item with its first line to see if it fits with
245 let placeholder = if overflow_last {
246 let old_value = *self.context.force_one_line_chain.borrow();
247 if !combine_arg_with_callee {
248 if let Some(expr) = self.last_item().and_then(|item| item.to_expr()) {
249 if let ast::ExprKind::MethodCall(..) = expr.node {
250 self.context.force_one_line_chain.replace(true);
254 let result = last_item_shape(
259 ).and_then(|arg_shape| {
260 self.rewrite_last_item_with_overflow(
261 &mut list_items[self.items.len() - 1],
265 self.context.force_one_line_chain.replace(old_value);
271 let mut tactic = definitive_tactic(
273 ListTactic::LimitedHorizontalVertical(self.item_max_width),
278 // Replace the stub with the full overflowing last argument if the rewrite
279 // succeeded and its first line fits with the other arguments.
280 match (overflow_last, tactic, placeholder) {
281 (true, DefinitiveListTactic::Horizontal, Some(ref overflowed))
282 if self.items.len() == 1 =>
284 // When we are rewriting a nested function call, we restrict the
285 // budget for the inner function to avoid them being deeply nested.
286 // However, when the inner function has a prefix or a suffix
287 // (e.g. `foo() as u32`), this budget reduction may produce poorly
288 // formatted code, where a prefix or a suffix being left on its own
289 // line. Here we explicitlly check those cases.
290 if count_newlines(overflowed) == 1 {
293 .and_then(|last_item| last_item.rewrite(self.context, self.nested_shape));
294 let no_newline = rw.as_ref().map_or(false, |s| !s.contains('\n'));
296 list_items[self.items.len() - 1].item = rw;
298 list_items[self.items.len() - 1].item = Some(overflowed.to_owned());
301 list_items[self.items.len() - 1].item = Some(overflowed.to_owned());
304 (true, DefinitiveListTactic::Horizontal, placeholder @ Some(..)) => {
305 list_items[self.items.len() - 1].item = placeholder;
307 _ if self.items.len() >= 1 => {
308 list_items[self.items.len() - 1].item = self.items
310 .and_then(|last_item| last_item.rewrite(self.context, self.nested_shape));
312 // Use horizontal layout for a function with a single argument as long as
313 // everything fits in a single line.
314 // `self.one_line_width == 0` means vertical layout is forced.
315 if self.items.len() == 1
316 && self.one_line_width != 0
317 && !list_items[0].has_comment()
318 && !list_items[0].inner_as_ref().contains('\n')
319 && ::lists::total_item_width(&list_items[0]) <= self.one_line_width
321 tactic = DefinitiveListTactic::Horizontal;
323 tactic = self.default_tactic(list_items);
325 if tactic == DefinitiveListTactic::Vertical {
326 if let Some((all_simple, num_args_before)) =
327 maybe_get_args_offset(self.ident, self.items)
329 let one_line = all_simple
330 && definitive_tactic(
331 &list_items[..num_args_before],
332 ListTactic::HorizontalVertical,
334 self.nested_shape.width,
336 == DefinitiveListTactic::Horizontal
337 && definitive_tactic(
338 &list_items[num_args_before + 1..],
339 ListTactic::HorizontalVertical,
341 self.nested_shape.width,
343 == DefinitiveListTactic::Horizontal;
346 tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
348 } else if is_every_expr_simple(self.items) && no_long_items(list_items) {
349 tactic = DefinitiveListTactic::Mixed;
360 fn rewrite_items(&self) -> Option<(bool, String)> {
361 let span = self.items_span();
362 let items = itemize_list(
363 self.context.snippet_provider,
367 |item| item.span().lo(),
368 |item| item.span().hi(),
369 |item| item.rewrite(self.context, self.nested_shape),
374 let mut list_items: Vec<_> = items.collect();
376 // Try letting the last argument overflow to the next line with block
377 // indentation. If its first line fits on one line with the other arguments,
378 // we format the function arguments horizontally.
379 let tactic = self.try_overflow_last_item(&mut list_items);
381 let fmt = ListFormatting {
384 trailing_separator: if let Some(tactic) = self.force_separator_tactic {
386 } else if !self.context.use_block_indent() {
387 SeparatorTactic::Never
388 } else if tactic == DefinitiveListTactic::Mixed {
389 // We are using mixed layout because everything did not fit within a single line.
390 SeparatorTactic::Always
392 self.context.config.trailing_comma()
394 separator_place: SeparatorPlace::Back,
395 shape: self.nested_shape,
396 ends_with_newline: match tactic {
397 DefinitiveListTactic::Vertical | DefinitiveListTactic::Mixed => {
398 self.context.use_block_indent()
402 preserve_newline: false,
403 config: self.context.config,
406 write_list(&list_items, &fmt)
407 .map(|items_str| (tactic == DefinitiveListTactic::Horizontal, items_str))
410 fn wrap_items(&self, items_str: &str, shape: Shape, is_extendable: bool) -> String {
414 .checked_sub(last_line_width(self.ident))
419 let (prefix, suffix) = match self.custom_delims {
420 Some((lhs, rhs)) => (lhs, rhs),
421 _ => (self.prefix, self.suffix),
423 let paren_overhead = paren_overhead(self.context);
424 let fits_one_line = items_str.len() + paren_overhead <= shape.width;
425 let extend_width = if items_str.is_empty() {
428 first_line_width(items_str) + (paren_overhead / 2)
430 let nested_indent_str = self.nested_shape
432 .to_string_with_newline(self.context.config);
433 let indent_str = shape
436 .to_string_with_newline(self.context.config);
437 let mut result = String::with_capacity(
438 self.ident.len() + items_str.len() + 2 + indent_str.len() + nested_indent_str.len(),
440 result.push_str(self.ident);
441 result.push_str(prefix);
442 if !self.context.use_block_indent()
443 || (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
444 || (is_extendable && extend_width <= shape.width)
446 if self.context.config.spaces_within_parens_and_brackets() && !items_str.is_empty() {
448 result.push_str(items_str);
451 result.push_str(items_str);
454 if !items_str.is_empty() {
455 result.push_str(&nested_indent_str);
456 result.push_str(items_str);
458 result.push_str(&indent_str);
460 result.push_str(suffix);
464 fn rewrite(&self, shape: Shape) -> Option<String> {
465 let (extendable, items_str) = self.rewrite_items()?;
467 // If we are using visual indent style and failed to format, retry with block indent.
468 if !self.context.use_block_indent()
469 && need_block_indent(&items_str, self.nested_shape)
472 self.context.use_block.replace(true);
473 let result = self.rewrite(shape);
474 self.context.use_block.replace(false);
478 Some(self.wrap_items(&items_str, shape, extendable))
482 fn need_block_indent(s: &str, shape: Shape) -> bool {
483 s.lines().skip(1).any(|s| {
484 s.find(|c| !char::is_whitespace(c))
485 .map_or(false, |w| w + 1 < shape.indent.width())
489 fn can_be_overflowed<'a, T>(context: &RewriteContext, items: &[&T]) -> bool
491 T: Rewrite + Spanned + ToExpr + 'a,
495 .map_or(false, |x| x.can_be_overflowed(context, items.len()))
498 /// Returns a shape for the last argument which is going to be overflowed.
499 fn last_item_shape<T>(
503 args_max_width: usize,
506 T: Rewrite + Spanned + ToExpr,
508 let is_nested_call = lists
511 .and_then(|item| item.to_expr())
512 .map_or(false, is_nested_call);
513 if items.len() == 1 && !is_nested_call {
516 let offset = items.iter().rev().skip(1).fold(0, |acc, i| {
518 acc + 2 + i.inner_as_ref().len()
521 width: min(args_max_width, shape.width),
523 }.offset_left(offset)
526 fn shape_from_indent_style(
527 context: &RewriteContext,
532 if context.use_block_indent() {
536 .block_indent(context.config.tab_spaces())
537 .with_max_width(context.config)
541 let shape = shape.visual_indent(offset);
542 if let Some(shape) = shape.sub_width(overhead) {
545 Shape { width: 0, ..shape }
550 fn no_long_items(list: &[ListItem]) -> bool {
552 .all(|item| !item.has_comment() && item.inner_as_ref().len() <= SHORT_ITEM_THRESHOLD)