1 // Copyright 2015 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 //! Format list-like expressions and items.
14 use std::iter::Peekable;
17 use syntax::source_map::BytePos;
19 use comment::{find_comment_end, rewrite_comment, FindUncommented};
20 use config::{Config, IndentStyle};
21 use rewrite::RewriteContext;
22 use shape::{Indent, Shape};
23 use utils::{count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline};
24 use visitor::SnippetProvider;
26 pub struct ListFormatting<'a> {
27 tactic: DefinitiveListTactic,
29 trailing_separator: SeparatorTactic,
30 separator_place: SeparatorPlace,
32 // Non-expressions, e.g. items, will have a new line at the end of the list.
33 // Important for comment styles.
34 ends_with_newline: bool,
35 // Remove newlines between list elements for expressions.
36 preserve_newline: bool,
37 // Nested import lists get some special handling for the "Mixed" list type
39 // Whether comments should be visually aligned.
44 impl<'a> ListFormatting<'a> {
45 pub fn new(shape: Shape, config: &'a Config) -> Self {
47 tactic: DefinitiveListTactic::Vertical,
49 trailing_separator: SeparatorTactic::Never,
50 separator_place: SeparatorPlace::Back,
52 ends_with_newline: true,
53 preserve_newline: false,
60 pub fn tactic(mut self, tactic: DefinitiveListTactic) -> Self {
65 pub fn separator(mut self, separator: &'a str) -> Self {
66 self.separator = separator;
70 pub fn trailing_separator(mut self, trailing_separator: SeparatorTactic) -> Self {
71 self.trailing_separator = trailing_separator;
75 pub fn separator_place(mut self, separator_place: SeparatorPlace) -> Self {
76 self.separator_place = separator_place;
80 pub fn ends_with_newline(mut self, ends_with_newline: bool) -> Self {
81 self.ends_with_newline = ends_with_newline;
85 pub fn preserve_newline(mut self, preserve_newline: bool) -> Self {
86 self.preserve_newline = preserve_newline;
90 pub fn nested(mut self, nested: bool) -> Self {
95 pub fn align_comments(mut self, align_comments: bool) -> Self {
96 self.align_comments = align_comments;
100 pub fn needs_trailing_separator(&self) -> bool {
101 match self.trailing_separator {
102 // We always put separator in front.
103 SeparatorTactic::Always => true,
104 SeparatorTactic::Vertical => self.tactic == DefinitiveListTactic::Vertical,
105 SeparatorTactic::Never => {
106 self.tactic == DefinitiveListTactic::Vertical && self.separator_place.is_front()
112 impl AsRef<ListItem> for ListItem {
113 fn as_ref(&self) -> &ListItem {
118 #[derive(PartialEq, Eq, Debug, Copy, Clone)]
119 pub enum ListItemCommentStyle {
120 // Try to keep the comment on the same line with the item.
122 // Put the comment on the previous or the next line of the item.
124 // No comment available.
128 #[derive(Debug, Clone)]
129 pub struct ListItem {
130 // None for comments mean that they are not present.
131 pub pre_comment: Option<String>,
132 pub pre_comment_style: ListItemCommentStyle,
133 // Item should include attributes and doc comments. None indicates a failed
135 pub item: Option<String>,
136 pub post_comment: Option<String>,
137 // Whether there is extra whitespace before this item.
142 pub fn empty() -> ListItem {
145 pre_comment_style: ListItemCommentStyle::None,
152 pub fn inner_as_ref(&self) -> &str {
153 self.item.as_ref().map_or("", |s| s)
156 pub fn is_different_group(&self) -> bool {
157 self.inner_as_ref().contains('\n')
158 || self.pre_comment.is_some()
162 .map_or(false, |s| s.contains('\n'))
165 pub fn is_multiline(&self) -> bool {
166 self.inner_as_ref().contains('\n')
170 .map_or(false, |s| s.contains('\n'))
174 .map_or(false, |s| s.contains('\n'))
177 pub fn has_single_line_comment(&self) -> bool {
180 .map_or(false, |comment| comment.trim_start().starts_with("//"))
184 .map_or(false, |comment| comment.trim_start().starts_with("//"))
187 pub fn has_comment(&self) -> bool {
188 self.pre_comment.is_some() || self.post_comment.is_some()
191 pub fn from_str<S: Into<String>>(s: S) -> ListItem {
194 pre_comment_style: ListItemCommentStyle::None,
195 item: Some(s.into()),
201 // true if the item causes something to be written.
202 fn is_substantial(&self) -> bool {
203 fn empty(s: &Option<String>) -> bool {
205 Some(ref s) if !s.is_empty() => false,
210 !(empty(&self.pre_comment) && empty(&self.item) && empty(&self.post_comment))
214 /// The type of separator for lists.
215 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
222 pub fn len(self) -> usize {
225 Separator::Comma => 2,
227 Separator::VerticalBar => 3,
232 pub fn definitive_tactic<I, T>(
237 ) -> DefinitiveListTactic
239 I: IntoIterator<Item = T> + Clone,
242 let pre_line_comments = items
245 .any(|item| item.as_ref().has_single_line_comment());
247 let limit = match tactic {
248 _ if pre_line_comments => return DefinitiveListTactic::Vertical,
249 ListTactic::Horizontal => return DefinitiveListTactic::Horizontal,
250 ListTactic::Vertical => return DefinitiveListTactic::Vertical,
251 ListTactic::LimitedHorizontalVertical(limit) => ::std::cmp::min(width, limit),
252 ListTactic::Mixed | ListTactic::HorizontalVertical => width,
255 let (sep_count, total_width) = calculate_width(items.clone());
256 let total_sep_len = sep.len() * sep_count.saturating_sub(1);
257 let real_total = total_width + total_sep_len;
259 if real_total <= limit
260 && !pre_line_comments
261 && !items.into_iter().any(|item| item.as_ref().is_multiline())
263 DefinitiveListTactic::Horizontal
266 ListTactic::Mixed => DefinitiveListTactic::Mixed,
267 _ => DefinitiveListTactic::Vertical,
272 // Format a list of commented items into a string.
273 pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
275 I: IntoIterator<Item = T> + Clone,
278 let tactic = formatting.tactic;
279 let sep_len = formatting.separator.len();
281 // Now that we know how we will layout, we can decide for sure if there
282 // will be a trailing separator.
283 let mut trailing_separator = formatting.needs_trailing_separator();
284 let mut result = String::with_capacity(128);
285 let cloned_items = items.clone();
286 let mut iter = items.into_iter().enumerate().peekable();
287 let mut item_max_width: Option<usize> = None;
289 SeparatorPlace::from_tactic(formatting.separator_place, tactic, formatting.separator);
290 let mut prev_item_had_post_comment = false;
291 let mut prev_item_is_nested_import = false;
293 let mut line_len = 0;
294 let indent_str = &formatting.shape.indent.to_string(formatting.config);
295 while let Some((i, item)) = iter.next() {
296 let item = item.as_ref();
297 let inner_item = item.item.as_ref()?;
299 let last = iter.peek().is_none();
300 let mut separate = match sep_place {
301 SeparatorPlace::Front => !first,
302 SeparatorPlace::Back => !last || trailing_separator,
304 let item_sep_len = if separate { sep_len } else { 0 };
306 // Item string may be multi-line. Its length (used for block comment alignment)
307 // should be only the length of the last line.
308 let item_last_line = if item.is_multiline() {
309 inner_item.lines().last().unwrap_or("")
313 let mut item_last_line_width = item_last_line.len() + item_sep_len;
314 if item_last_line.starts_with(&**indent_str) {
315 item_last_line_width -= indent_str.len();
318 if !item.is_substantial() {
323 DefinitiveListTactic::Horizontal if !first => {
326 DefinitiveListTactic::SpecialMacro(num_args_before) => {
329 } else if i < num_args_before {
331 } else if i <= num_args_before + 1 {
333 result.push_str(indent_str);
338 DefinitiveListTactic::Vertical
339 if !first && !inner_item.is_empty() && !result.is_empty() =>
342 result.push_str(indent_str);
344 DefinitiveListTactic::Mixed => {
345 let total_width = total_item_width(item) + item_sep_len;
347 // 1 is space between separator and item.
348 if (line_len > 0 && line_len + 1 + total_width > formatting.shape.width)
349 || prev_item_had_post_comment
350 || (formatting.nested
351 && (prev_item_is_nested_import || (!first && inner_item.contains("::"))))
354 result.push_str(indent_str);
356 if formatting.ends_with_newline {
357 trailing_separator = true;
359 } else if line_len > 0 {
364 if last && formatting.ends_with_newline {
365 separate = formatting.trailing_separator != SeparatorTactic::Never;
368 line_len += total_width;
374 if let Some(ref comment) = item.pre_comment {
375 // Block style in non-vertical mode.
376 let block_mode = tactic == DefinitiveListTactic::Horizontal;
377 // Width restriction is only relevant in vertical mode.
379 rewrite_comment(comment, block_mode, formatting.shape, formatting.config)?;
380 result.push_str(&comment);
382 if !inner_item.is_empty() {
383 if tactic == DefinitiveListTactic::Vertical || tactic == DefinitiveListTactic::Mixed
385 // We cannot keep pre-comments on the same line if the comment if normalized.
386 let keep_comment = if formatting.config.normalize_comments()
387 || item.pre_comment_style == ListItemCommentStyle::DifferentLine
391 // We will try to keep the comment on the same line with the item here.
393 let total_width = total_item_width(item) + item_sep_len + 1;
394 total_width <= formatting.shape.width
400 result.push_str(indent_str);
401 // This is the width of the item (without comments).
402 line_len = item.item.as_ref().map_or(0, |str| str.len());
408 item_max_width = None;
411 if separate && sep_place.is_front() && !first {
412 result.push_str(formatting.separator.trim());
415 result.push_str(inner_item);
418 if tactic == DefinitiveListTactic::Horizontal && item.post_comment.is_some() {
419 let comment = item.post_comment.as_ref().unwrap();
420 let formatted_comment = rewrite_comment(
423 Shape::legacy(formatting.shape.width, Indent::empty()),
428 result.push_str(&formatted_comment);
431 if separate && sep_place.is_back() {
432 result.push_str(formatting.separator);
435 if tactic != DefinitiveListTactic::Horizontal && item.post_comment.is_some() {
436 let comment = item.post_comment.as_ref().unwrap();
437 let overhead = last_line_width(&result) + first_line_width(comment.trim());
439 let rewrite_post_comment = |item_max_width: &mut Option<usize>| {
440 if item_max_width.is_none() && !last && !inner_item.contains('\n') {
441 *item_max_width = Some(max_width_of_item_with_post_comment(
445 formatting.config.max_width(),
448 let overhead = if starts_with_newline(comment) {
450 } else if let Some(max_width) = *item_max_width {
453 // 1 = space between item and comment.
454 item_last_line_width + 1
456 let width = formatting.shape.width.checked_sub(overhead).unwrap_or(1);
457 let offset = formatting.shape.indent + overhead;
458 let comment_shape = Shape::legacy(width, offset);
460 // Use block-style only for the last item or multiline comments.
461 let block_style = !formatting.ends_with_newline && last
462 || comment.trim().contains('\n')
463 || comment.trim().len() > width;
466 comment.trim_start(),
473 let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?;
475 if !starts_with_newline(comment) {
476 if formatting.align_comments {
477 let mut comment_alignment =
478 post_comment_alignment(item_max_width, inner_item.len());
479 if first_line_width(&formatted_comment)
480 + last_line_width(&result)
483 > formatting.config.max_width()
485 item_max_width = None;
486 formatted_comment = rewrite_post_comment(&mut item_max_width)?;
488 post_comment_alignment(item_max_width, inner_item.len());
490 for _ in 0..=comment_alignment {
494 // An additional space for the missing trailing separator (or
495 // if we skipped alignment above).
496 if !formatting.align_comments
498 && item_max_width.is_some()
500 && !formatting.separator.is_empty())
506 result.push_str(indent_str);
508 if formatted_comment.contains('\n') {
509 item_max_width = None;
511 result.push_str(&formatted_comment);
513 item_max_width = None;
516 if formatting.preserve_newline
518 && tactic == DefinitiveListTactic::Vertical
521 item_max_width = None;
525 prev_item_had_post_comment = item.post_comment.is_some();
526 prev_item_is_nested_import = inner_item.contains("::");
532 fn max_width_of_item_with_post_comment<I, T>(
539 I: IntoIterator<Item = T> + Clone,
542 let mut max_width = 0;
543 let mut first = true;
544 for item in items.clone().into_iter().skip(i) {
545 let item = item.as_ref();
546 let inner_item_width = item.inner_as_ref().len();
548 && (item.is_different_group()
549 || item.post_comment.is_none()
550 || inner_item_width + overhead > max_budget)
554 if max_width < inner_item_width {
555 max_width = inner_item_width;
565 fn post_comment_alignment(item_max_width: Option<usize>, inner_item_len: usize) -> usize {
566 item_max_width.unwrap_or(0).saturating_sub(inner_item_len)
569 pub struct ListItems<'a, I, F1, F2, F3>
573 snippet_provider: &'a SnippetProvider<'a>,
578 prev_span_end: BytePos,
579 next_span_start: BytePos,
585 pub fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommentStyle) {
586 let trimmed_pre_snippet = pre_snippet.trim();
587 let has_block_comment = trimmed_pre_snippet.ends_with("*/");
588 let has_single_line_comment = trimmed_pre_snippet.starts_with("//");
589 if has_block_comment {
590 let comment_end = pre_snippet.chars().rev().position(|c| c == '/').unwrap();
594 .take(comment_end + 1)
598 Some(trimmed_pre_snippet.to_owned()),
599 ListItemCommentStyle::DifferentLine,
603 Some(trimmed_pre_snippet.to_owned()),
604 ListItemCommentStyle::SameLine,
607 } else if has_single_line_comment {
609 Some(trimmed_pre_snippet.to_owned()),
610 ListItemCommentStyle::DifferentLine,
613 (None, ListItemCommentStyle::None)
617 pub fn extract_post_comment(
621 ) -> Option<String> {
622 let white_space: &[_] = &[' ', '\t'];
624 // Cleanup post-comment: strip separators and whitespace.
625 let post_snippet = post_snippet[..comment_end].trim();
626 let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
627 post_snippet[1..].trim_matches(white_space)
628 } else if post_snippet.starts_with(separator) {
629 post_snippet[separator.len()..].trim_matches(white_space)
630 } else if post_snippet.ends_with(',') {
631 post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
636 if !post_snippet_trimmed.is_empty() {
637 Some(post_snippet_trimmed.to_owned())
643 pub fn get_comment_end(
651 .find_uncommented(terminator)
652 .unwrap_or_else(|| post_snippet.len());
655 let mut block_open_index = post_snippet.find("/*");
656 // check if it really is a block comment (and not `//*` or a nested comment)
657 if let Some(i) = block_open_index {
658 match post_snippet.find('/') {
659 Some(j) if j < i => block_open_index = None,
660 _ if i > 0 && &post_snippet[i - 1..i] == "/" => block_open_index = None,
664 let newline_index = post_snippet.find('\n');
665 if let Some(separator_index) = post_snippet.find_uncommented(separator) {
666 match (block_open_index, newline_index) {
667 // Separator before comment, with the next item on same line.
668 // Comment belongs to next item.
669 (Some(i), None) if i > separator_index => separator_index + 1,
670 // Block-style post-comment before the separator.
671 (Some(i), None) => cmp::max(
672 find_comment_end(&post_snippet[i..]).unwrap() + i,
675 // Block-style post-comment. Either before or after the separator.
676 (Some(i), Some(j)) if i < j => cmp::max(
677 find_comment_end(&post_snippet[i..]).unwrap() + i,
680 // Potential *single* line comment.
681 (_, Some(j)) if j > separator_index => j + 1,
682 _ => post_snippet.len(),
684 } else if let Some(newline_index) = newline_index {
685 // Match arms may not have trailing comma. In any case, for match arms,
686 // we will assume that the post comment belongs to the next arm if they
687 // do not end with trailing comma.
694 // Account for extra whitespace between items. This is fiddly
695 // because of the way we divide pre- and post- comments.
696 fn has_extra_newline(post_snippet: &str, comment_end: usize) -> bool {
697 if post_snippet.is_empty() || comment_end == 0 {
701 // Everything from the separator to the next item.
702 let test_snippet = &post_snippet[comment_end - 1..];
703 let first_newline = test_snippet
705 .unwrap_or_else(|| test_snippet.len());
706 // From the end of the first line of comments.
707 let test_snippet = &test_snippet[first_newline..];
708 let first = test_snippet
709 .find(|c: char| !c.is_whitespace())
710 .unwrap_or_else(|| test_snippet.len());
711 // From the end of the first line of comments to the next non-whitespace char.
712 let test_snippet = &test_snippet[..first];
714 // There were multiple line breaks which got trimmed to nothing.
715 count_newlines(test_snippet) > 1
718 impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
720 I: Iterator<Item = T>,
721 F1: Fn(&T) -> BytePos,
722 F2: Fn(&T) -> BytePos,
723 F3: Fn(&T) -> Option<String>,
725 type Item = ListItem;
727 fn next(&mut self) -> Option<Self::Item> {
728 self.inner.next().map(|item| {
730 let pre_snippet = self
732 .span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
734 let (pre_comment, pre_comment_style) = extract_pre_comment(pre_snippet);
737 let next_start = match self.inner.peek() {
738 Some(next_item) => (self.get_lo)(next_item),
739 None => self.next_span_start,
741 let post_snippet = self
743 .span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
745 let comment_end = get_comment_end(
749 self.inner.peek().is_none(),
751 let new_lines = has_extra_newline(post_snippet, comment_end);
752 let post_comment = extract_post_comment(post_snippet, comment_end, self.separator);
754 self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
759 item: if self.inner.peek().is_none() && self.leave_last {
762 (self.get_item_string)(&item)
771 #[allow(clippy::too_many_arguments)]
772 // Creates an iterator over a list's items with associated comments.
773 pub fn itemize_list<'a, T, I, F1, F2, F3>(
774 snippet_provider: &'a SnippetProvider,
781 prev_span_end: BytePos,
782 next_span_start: BytePos,
784 ) -> ListItems<'a, I, F1, F2, F3>
786 I: Iterator<Item = T>,
787 F1: Fn(&T) -> BytePos,
788 F2: Fn(&T) -> BytePos,
789 F3: Fn(&T) -> Option<String>,
793 inner: inner.peekable(),
805 /// Returns the count and total width of the list items.
806 fn calculate_width<I, T>(items: I) -> (usize, usize)
808 I: IntoIterator<Item = T>,
813 .map(|item| total_item_width(item.as_ref()))
814 .fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l))
817 pub fn total_item_width(item: &ListItem) -> usize {
818 comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..]))
819 + comment_len(item.post_comment.as_ref().map(|x| &(*x)[..]))
820 + item.item.as_ref().map_or(0, |str| str.len())
823 fn comment_len(comment: Option<&str>) -> usize {
826 let text_len = s.trim().len();
828 // We'll put " /*" before and " */" after inline comments.
838 // Compute horizontal and vertical shapes for a struct-lit-like thing.
839 pub fn struct_lit_shape(
841 context: &RewriteContext,
844 ) -> Option<(Option<Shape>, Shape)> {
845 let v_shape = match context.config.indent_style() {
846 IndentStyle::Visual => shape
848 .shrink_left(prefix_width)?
849 .sub_width(suffix_width)?,
850 IndentStyle::Block => {
851 let shape = shape.block_indent(context.config.tab_spaces());
853 width: context.budget(shape.indent.width()),
858 let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
859 if let Some(w) = shape_width {
860 let shape_width = cmp::min(w, context.config.width_heuristics().struct_lit_width);
861 Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
863 Some((None, v_shape))
867 // Compute the tactic for the internals of a struct-lit-like thing.
868 pub fn struct_lit_tactic(
869 h_shape: Option<Shape>,
870 context: &RewriteContext,
872 ) -> DefinitiveListTactic {
873 if let Some(h_shape) = h_shape {
874 let prelim_tactic = match (context.config.indent_style(), items.len()) {
875 (IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
876 _ if context.config.struct_lit_single_line() => ListTactic::HorizontalVertical,
877 _ => ListTactic::Vertical,
879 definitive_tactic(items, prelim_tactic, Separator::Comma, h_shape.width)
881 DefinitiveListTactic::Vertical
885 // Given a tactic and possible shapes for horizontal and vertical layout,
886 // come up with the actual shape to use.
887 pub fn shape_for_tactic(
888 tactic: DefinitiveListTactic,
889 h_shape: Option<Shape>,
893 DefinitiveListTactic::Horizontal => h_shape.unwrap(),
898 // Create a ListFormatting object for formatting the internals of a
899 // struct-lit-like thing, that is a series of fields.
900 pub fn struct_lit_formatting<'a>(
902 tactic: DefinitiveListTactic,
903 context: &'a RewriteContext,
904 force_no_trailing_comma: bool,
905 ) -> ListFormatting<'a> {
906 let ends_with_newline = context.config.indent_style() != IndentStyle::Visual
907 && tactic == DefinitiveListTactic::Vertical;
911 trailing_separator: if force_no_trailing_comma {
912 SeparatorTactic::Never
914 context.config.trailing_comma()
916 separator_place: SeparatorPlace::Back,
919 preserve_newline: true,
921 align_comments: true,
922 config: context.config,