]> git.lizzy.rs Git - rust.git/blob - src/lists.rs
change new line point in the case of no args
[rust.git] / src / lists.rs
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.
4 //
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.
10
11 //! Format list-like expressions and items.
12
13 use std::cmp;
14 use std::iter::Peekable;
15
16 use config::lists::*;
17 use syntax::source_map::BytePos;
18
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;
25
26 pub struct ListFormatting<'a> {
27     tactic: DefinitiveListTactic,
28     separator: &'a str,
29     trailing_separator: SeparatorTactic,
30     separator_place: SeparatorPlace,
31     shape: Shape,
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
38     nested: bool,
39     // Whether comments should be visually aligned.
40     align_comments: bool,
41     config: &'a Config,
42 }
43
44 impl<'a> ListFormatting<'a> {
45     pub fn new(shape: Shape, config: &'a Config) -> Self {
46         ListFormatting {
47             tactic: DefinitiveListTactic::Vertical,
48             separator: ",",
49             trailing_separator: SeparatorTactic::Never,
50             separator_place: SeparatorPlace::Back,
51             shape,
52             ends_with_newline: true,
53             preserve_newline: false,
54             nested: false,
55             align_comments: true,
56             config,
57         }
58     }
59
60     pub fn tactic(mut self, tactic: DefinitiveListTactic) -> Self {
61         self.tactic = tactic;
62         self
63     }
64
65     pub fn separator(mut self, separator: &'a str) -> Self {
66         self.separator = separator;
67         self
68     }
69
70     pub fn trailing_separator(mut self, trailing_separator: SeparatorTactic) -> Self {
71         self.trailing_separator = trailing_separator;
72         self
73     }
74
75     pub fn separator_place(mut self, separator_place: SeparatorPlace) -> Self {
76         self.separator_place = separator_place;
77         self
78     }
79
80     pub fn ends_with_newline(mut self, ends_with_newline: bool) -> Self {
81         self.ends_with_newline = ends_with_newline;
82         self
83     }
84
85     pub fn preserve_newline(mut self, preserve_newline: bool) -> Self {
86         self.preserve_newline = preserve_newline;
87         self
88     }
89
90     pub fn nested(mut self, nested: bool) -> Self {
91         self.nested = nested;
92         self
93     }
94
95     pub fn align_comments(mut self, align_comments: bool) -> Self {
96         self.align_comments = align_comments;
97         self
98     }
99
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()
107             }
108         }
109     }
110 }
111
112 impl AsRef<ListItem> for ListItem {
113     fn as_ref(&self) -> &ListItem {
114         self
115     }
116 }
117
118 #[derive(PartialEq, Eq, Debug, Copy, Clone)]
119 pub enum ListItemCommentStyle {
120     // Try to keep the comment on the same line with the item.
121     SameLine,
122     // Put the comment on the previous or the next line of the item.
123     DifferentLine,
124     // No comment available.
125     None,
126 }
127
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
134     // rewrite.
135     pub item: Option<String>,
136     pub post_comment: Option<String>,
137     // Whether there is extra whitespace before this item.
138     pub new_lines: bool,
139 }
140
141 impl ListItem {
142     pub fn empty() -> ListItem {
143         ListItem {
144             pre_comment: None,
145             pre_comment_style: ListItemCommentStyle::None,
146             item: None,
147             post_comment: None,
148             new_lines: false,
149         }
150     }
151
152     pub fn inner_as_ref(&self) -> &str {
153         self.item.as_ref().map_or("", |s| s)
154     }
155
156     pub fn is_different_group(&self) -> bool {
157         self.inner_as_ref().contains('\n')
158             || self.pre_comment.is_some()
159             || self
160                 .post_comment
161                 .as_ref()
162                 .map_or(false, |s| s.contains('\n'))
163     }
164
165     pub fn is_multiline(&self) -> bool {
166         self.inner_as_ref().contains('\n')
167             || self
168                 .pre_comment
169                 .as_ref()
170                 .map_or(false, |s| s.contains('\n'))
171             || self
172                 .post_comment
173                 .as_ref()
174                 .map_or(false, |s| s.contains('\n'))
175     }
176
177     pub fn has_single_line_comment(&self) -> bool {
178         self.pre_comment
179             .as_ref()
180             .map_or(false, |comment| comment.trim_start().starts_with("//"))
181             || self
182                 .post_comment
183                 .as_ref()
184                 .map_or(false, |comment| comment.trim_start().starts_with("//"))
185     }
186
187     pub fn has_comment(&self) -> bool {
188         self.pre_comment.is_some() || self.post_comment.is_some()
189     }
190
191     pub fn from_str<S: Into<String>>(s: S) -> ListItem {
192         ListItem {
193             pre_comment: None,
194             pre_comment_style: ListItemCommentStyle::None,
195             item: Some(s.into()),
196             post_comment: None,
197             new_lines: false,
198         }
199     }
200
201     // true if the item causes something to be written.
202     fn is_substantial(&self) -> bool {
203         fn empty(s: &Option<String>) -> bool {
204             match *s {
205                 Some(ref s) if !s.is_empty() => false,
206                 _ => true,
207             }
208         }
209
210         !(empty(&self.pre_comment) && empty(&self.item) && empty(&self.post_comment))
211     }
212 }
213
214 /// The type of separator for lists.
215 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
216 pub enum Separator {
217     Comma,
218     VerticalBar,
219 }
220
221 impl Separator {
222     pub fn len(self) -> usize {
223         match self {
224             // 2 = `, `
225             Separator::Comma => 2,
226             // 3 = ` | `
227             Separator::VerticalBar => 3,
228         }
229     }
230 }
231
232 pub fn definitive_tactic<I, T>(
233     items: I,
234     tactic: ListTactic,
235     sep: Separator,
236     width: usize,
237 ) -> DefinitiveListTactic
238 where
239     I: IntoIterator<Item = T> + Clone,
240     T: AsRef<ListItem>,
241 {
242     let pre_line_comments = items
243         .clone()
244         .into_iter()
245         .any(|item| item.as_ref().has_single_line_comment());
246
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,
253     };
254
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;
258
259     if real_total <= limit
260         && !pre_line_comments
261         && !items.into_iter().any(|item| item.as_ref().is_multiline())
262     {
263         DefinitiveListTactic::Horizontal
264     } else {
265         match tactic {
266             ListTactic::Mixed => DefinitiveListTactic::Mixed,
267             _ => DefinitiveListTactic::Vertical,
268         }
269     }
270 }
271
272 // Format a list of commented items into a string.
273 pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
274 where
275     I: IntoIterator<Item = T> + Clone,
276     T: AsRef<ListItem>,
277 {
278     let tactic = formatting.tactic;
279     let sep_len = formatting.separator.len();
280
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;
288     let sep_place =
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;
292
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()?;
298         let first = i == 0;
299         let last = iter.peek().is_none();
300         let mut separate = match sep_place {
301             SeparatorPlace::Front => !first,
302             SeparatorPlace::Back => !last || trailing_separator,
303         };
304         let item_sep_len = if separate { sep_len } else { 0 };
305
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("")
310         } else {
311             inner_item.as_ref()
312         };
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();
316         }
317
318         if !item.is_substantial() {
319             continue;
320         }
321
322         match tactic {
323             DefinitiveListTactic::Horizontal if !first => {
324                 result.push(' ');
325             }
326             DefinitiveListTactic::SpecialMacro(num_args_before) => {
327                 if i == 0 {
328                     // Nothing
329                 } else if i < num_args_before {
330                     result.push(' ');
331                 } else if i <= num_args_before + 1 {
332                     result.push('\n');
333                     result.push_str(indent_str);
334                 } else {
335                     result.push(' ');
336                 }
337             }
338             DefinitiveListTactic::Vertical
339                 if !first && !inner_item.is_empty() && !result.is_empty() =>
340             {
341                 result.push('\n');
342                 result.push_str(indent_str);
343             }
344             DefinitiveListTactic::Mixed => {
345                 let total_width = total_item_width(item) + item_sep_len;
346
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("::"))))
352                 {
353                     result.push('\n');
354                     result.push_str(indent_str);
355                     line_len = 0;
356                     if formatting.ends_with_newline {
357                         trailing_separator = true;
358                     }
359                 } else if line_len > 0 {
360                     result.push(' ');
361                     line_len += 1;
362                 }
363
364                 if last && formatting.ends_with_newline {
365                     separate = formatting.trailing_separator != SeparatorTactic::Never;
366                 }
367
368                 line_len += total_width;
369             }
370             _ => {}
371         }
372
373         // Pre-comments
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.
378             let comment =
379                 rewrite_comment(comment, block_mode, formatting.shape, formatting.config)?;
380             result.push_str(&comment);
381
382             if !inner_item.is_empty() {
383                 if tactic == DefinitiveListTactic::Vertical || tactic == DefinitiveListTactic::Mixed
384                 {
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
388                     {
389                         false
390                     } else {
391                         // We will try to keep the comment on the same line with the item here.
392                         // 1 = ` `
393                         let total_width = total_item_width(item) + item_sep_len + 1;
394                         total_width <= formatting.shape.width
395                     };
396                     if keep_comment {
397                         result.push(' ');
398                     } else {
399                         result.push('\n');
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());
403                     }
404                 } else {
405                     result.push(' ');
406                 }
407             }
408             item_max_width = None;
409         }
410
411         if separate && sep_place.is_front() && !first {
412             result.push_str(formatting.separator.trim());
413             result.push(' ');
414         }
415         result.push_str(inner_item);
416
417         // Post-comments
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(
421                 comment,
422                 true,
423                 Shape::legacy(formatting.shape.width, Indent::empty()),
424                 formatting.config,
425             )?;
426
427             result.push(' ');
428             result.push_str(&formatted_comment);
429         }
430
431         if separate && sep_place.is_back() {
432             result.push_str(formatting.separator);
433         }
434
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());
438
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(
442                         &cloned_items,
443                         i,
444                         overhead,
445                         formatting.config.max_width(),
446                     ));
447                 }
448                 let overhead = if starts_with_newline(comment) {
449                     0
450                 } else if let Some(max_width) = *item_max_width {
451                     max_width + 2
452                 } else {
453                     // 1 = space between item and comment.
454                     item_last_line_width + 1
455                 };
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);
459
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;
464
465                 rewrite_comment(
466                     comment.trim_start(),
467                     block_style,
468                     comment_shape,
469                     formatting.config,
470                 )
471             };
472
473             let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?;
474
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)
481                         + comment_alignment
482                         + 1
483                         > formatting.config.max_width()
484                     {
485                         item_max_width = None;
486                         formatted_comment = rewrite_post_comment(&mut item_max_width)?;
487                         comment_alignment =
488                             post_comment_alignment(item_max_width, inner_item.len());
489                     }
490                     for _ in 0..=comment_alignment {
491                         result.push(' ');
492                     }
493                 }
494                 // An additional space for the missing trailing separator (or
495                 // if we skipped alignment above).
496                 if !formatting.align_comments
497                     || (last
498                         && item_max_width.is_some()
499                         && !separate
500                         && !formatting.separator.is_empty())
501                 {
502                     result.push(' ');
503                 }
504             } else {
505                 result.push('\n');
506                 result.push_str(indent_str);
507             }
508             if formatted_comment.contains('\n') {
509                 item_max_width = None;
510             }
511             result.push_str(&formatted_comment);
512         } else {
513             item_max_width = None;
514         }
515
516         if formatting.preserve_newline
517             && !last
518             && tactic == DefinitiveListTactic::Vertical
519             && item.new_lines
520         {
521             item_max_width = None;
522             result.push('\n');
523         }
524
525         prev_item_had_post_comment = item.post_comment.is_some();
526         prev_item_is_nested_import = inner_item.contains("::");
527     }
528
529     Some(result)
530 }
531
532 fn max_width_of_item_with_post_comment<I, T>(
533     items: &I,
534     i: usize,
535     overhead: usize,
536     max_budget: usize,
537 ) -> usize
538 where
539     I: IntoIterator<Item = T> + Clone,
540     T: AsRef<ListItem>,
541 {
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();
547         if !first
548             && (item.is_different_group()
549                 || item.post_comment.is_none()
550                 || inner_item_width + overhead > max_budget)
551         {
552             return max_width;
553         }
554         if max_width < inner_item_width {
555             max_width = inner_item_width;
556         }
557         if item.new_lines {
558             return max_width;
559         }
560         first = false;
561     }
562     max_width
563 }
564
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)
567 }
568
569 pub struct ListItems<'a, I, F1, F2, F3>
570 where
571     I: Iterator,
572 {
573     snippet_provider: &'a SnippetProvider<'a>,
574     inner: Peekable<I>,
575     get_lo: F1,
576     get_hi: F2,
577     get_item_string: F3,
578     prev_span_end: BytePos,
579     next_span_start: BytePos,
580     terminator: &'a str,
581     separator: &'a str,
582     leave_last: bool,
583 }
584
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();
591         if pre_snippet
592             .chars()
593             .rev()
594             .take(comment_end + 1)
595             .any(|c| c == '\n')
596         {
597             (
598                 Some(trimmed_pre_snippet.to_owned()),
599                 ListItemCommentStyle::DifferentLine,
600             )
601         } else {
602             (
603                 Some(trimmed_pre_snippet.to_owned()),
604                 ListItemCommentStyle::SameLine,
605             )
606         }
607     } else if has_single_line_comment {
608         (
609             Some(trimmed_pre_snippet.to_owned()),
610             ListItemCommentStyle::DifferentLine,
611         )
612     } else {
613         (None, ListItemCommentStyle::None)
614     }
615 }
616
617 pub fn extract_post_comment(
618     post_snippet: &str,
619     comment_end: usize,
620     separator: &str,
621 ) -> Option<String> {
622     let white_space: &[_] = &[' ', '\t'];
623
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)
632     } else {
633         post_snippet
634     };
635
636     if !post_snippet_trimmed.is_empty() {
637         Some(post_snippet_trimmed.to_owned())
638     } else {
639         None
640     }
641 }
642
643 pub fn get_comment_end(
644     post_snippet: &str,
645     separator: &str,
646     terminator: &str,
647     is_last: bool,
648 ) -> usize {
649     if is_last {
650         return post_snippet
651             .find_uncommented(terminator)
652             .unwrap_or_else(|| post_snippet.len());
653     }
654
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,
661             _ => (),
662         }
663     }
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,
673                 separator_index + 1,
674             ),
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,
678                 separator_index + 1,
679             ),
680             // Potential *single* line comment.
681             (_, Some(j)) if j > separator_index => j + 1,
682             _ => post_snippet.len(),
683         }
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.
688         newline_index + 1
689     } else {
690         0
691     }
692 }
693
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 {
698         return false;
699     }
700
701     // Everything from the separator to the next item.
702     let test_snippet = &post_snippet[comment_end - 1..];
703     let first_newline = test_snippet
704         .find('\n')
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];
713
714     // There were multiple line breaks which got trimmed to nothing.
715     count_newlines(test_snippet) > 1
716 }
717
718 impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
719 where
720     I: Iterator<Item = T>,
721     F1: Fn(&T) -> BytePos,
722     F2: Fn(&T) -> BytePos,
723     F3: Fn(&T) -> Option<String>,
724 {
725     type Item = ListItem;
726
727     fn next(&mut self) -> Option<Self::Item> {
728         self.inner.next().map(|item| {
729             // Pre-comment
730             let pre_snippet = self
731                 .snippet_provider
732                 .span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
733                 .unwrap_or("");
734             let (pre_comment, pre_comment_style) = extract_pre_comment(pre_snippet);
735
736             // Post-comment
737             let next_start = match self.inner.peek() {
738                 Some(next_item) => (self.get_lo)(next_item),
739                 None => self.next_span_start,
740             };
741             let post_snippet = self
742                 .snippet_provider
743                 .span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
744                 .unwrap_or("");
745             let comment_end = get_comment_end(
746                 post_snippet,
747                 self.separator,
748                 self.terminator,
749                 self.inner.peek().is_none(),
750             );
751             let new_lines = has_extra_newline(post_snippet, comment_end);
752             let post_comment = extract_post_comment(post_snippet, comment_end, self.separator);
753
754             self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
755
756             ListItem {
757                 pre_comment,
758                 pre_comment_style,
759                 item: if self.inner.peek().is_none() && self.leave_last {
760                     None
761                 } else {
762                     (self.get_item_string)(&item)
763                 },
764                 post_comment,
765                 new_lines,
766             }
767         })
768     }
769 }
770
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,
775     inner: I,
776     terminator: &'a str,
777     separator: &'a str,
778     get_lo: F1,
779     get_hi: F2,
780     get_item_string: F3,
781     prev_span_end: BytePos,
782     next_span_start: BytePos,
783     leave_last: bool,
784 ) -> ListItems<'a, I, F1, F2, F3>
785 where
786     I: Iterator<Item = T>,
787     F1: Fn(&T) -> BytePos,
788     F2: Fn(&T) -> BytePos,
789     F3: Fn(&T) -> Option<String>,
790 {
791     ListItems {
792         snippet_provider,
793         inner: inner.peekable(),
794         get_lo,
795         get_hi,
796         get_item_string,
797         prev_span_end,
798         next_span_start,
799         terminator,
800         separator,
801         leave_last,
802     }
803 }
804
805 /// Returns the count and total width of the list items.
806 fn calculate_width<I, T>(items: I) -> (usize, usize)
807 where
808     I: IntoIterator<Item = T>,
809     T: AsRef<ListItem>,
810 {
811     items
812         .into_iter()
813         .map(|item| total_item_width(item.as_ref()))
814         .fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l))
815 }
816
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())
821 }
822
823 fn comment_len(comment: Option<&str>) -> usize {
824     match comment {
825         Some(s) => {
826             let text_len = s.trim().len();
827             if text_len > 0 {
828                 // We'll put " /*" before and " */" after inline comments.
829                 text_len + 6
830             } else {
831                 text_len
832             }
833         }
834         None => 0,
835     }
836 }
837
838 // Compute horizontal and vertical shapes for a struct-lit-like thing.
839 pub fn struct_lit_shape(
840     shape: Shape,
841     context: &RewriteContext,
842     prefix_width: usize,
843     suffix_width: usize,
844 ) -> Option<(Option<Shape>, Shape)> {
845     let v_shape = match context.config.indent_style() {
846         IndentStyle::Visual => shape
847             .visual_indent(0)
848             .shrink_left(prefix_width)?
849             .sub_width(suffix_width)?,
850         IndentStyle::Block => {
851             let shape = shape.block_indent(context.config.tab_spaces());
852             Shape {
853                 width: context.budget(shape.indent.width()),
854                 ..shape
855             }
856         }
857     };
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))
862     } else {
863         Some((None, v_shape))
864     }
865 }
866
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,
871     items: &[ListItem],
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,
878         };
879         definitive_tactic(items, prelim_tactic, Separator::Comma, h_shape.width)
880     } else {
881         DefinitiveListTactic::Vertical
882     }
883 }
884
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>,
890     v_shape: Shape,
891 ) -> Shape {
892     match tactic {
893         DefinitiveListTactic::Horizontal => h_shape.unwrap(),
894         _ => v_shape,
895     }
896 }
897
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>(
901     shape: Shape,
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;
908     ListFormatting {
909         tactic,
910         separator: ",",
911         trailing_separator: if force_no_trailing_comma {
912             SeparatorTactic::Never
913         } else {
914             context.config.trailing_comma()
915         },
916         separator_place: SeparatorPlace::Back,
917         shape,
918         ends_with_newline,
919         preserve_newline: true,
920         nested: false,
921         align_comments: true,
922         config: context.config,
923     }
924 }