]> git.lizzy.rs Git - rust.git/blob - src/lists.rs
Make definitive_tactic more generic via enum Separator
[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 use std::cmp;
12 use std::iter::Peekable;
13
14 use syntax::codemap::{BytePos, CodeMap};
15
16 use {Indent, Shape};
17 use comment::{find_comment_end, rewrite_comment, FindUncommented};
18 use config::{Config, IndentStyle};
19 use rewrite::RewriteContext;
20 use utils::{first_line_width, last_line_width, mk_sp};
21
22 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
23 /// Formatting tactic for lists. This will be cast down to a
24 /// DefinitiveListTactic depending on the number and length of the items and
25 /// their comments.
26 pub enum ListTactic {
27     // One item per row.
28     Vertical,
29     // All items on one row.
30     Horizontal,
31     // Try Horizontal layout, if that fails then vertical.
32     HorizontalVertical,
33     // HorizontalVertical with a soft limit of n characters.
34     LimitedHorizontalVertical(usize),
35     // Pack as many items as possible per row over (possibly) many rows.
36     Mixed,
37 }
38
39 impl_enum_serialize_and_deserialize!(ListTactic, Vertical, Horizontal, HorizontalVertical, Mixed);
40
41 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
42 pub enum SeparatorTactic {
43     Always,
44     Never,
45     Vertical,
46 }
47
48 impl_enum_serialize_and_deserialize!(SeparatorTactic, Always, Never, Vertical);
49
50 impl SeparatorTactic {
51     pub fn from_bool(b: bool) -> SeparatorTactic {
52         if b {
53             SeparatorTactic::Always
54         } else {
55             SeparatorTactic::Never
56         }
57     }
58 }
59
60 pub struct ListFormatting<'a> {
61     pub tactic: DefinitiveListTactic,
62     pub separator: &'a str,
63     pub trailing_separator: SeparatorTactic,
64     pub shape: Shape,
65     // Non-expressions, e.g. items, will have a new line at the end of the list.
66     // Important for comment styles.
67     pub ends_with_newline: bool,
68     // Remove newlines between list elements for expressions.
69     pub preserve_newline: bool,
70     pub config: &'a Config,
71 }
72
73 impl AsRef<ListItem> for ListItem {
74     fn as_ref(&self) -> &ListItem {
75         self
76     }
77 }
78
79 pub struct ListItem {
80     // None for comments mean that they are not present.
81     pub pre_comment: Option<String>,
82     // Item should include attributes and doc comments. None indicates a failed
83     // rewrite.
84     pub item: Option<String>,
85     pub post_comment: Option<String>,
86     // Whether there is extra whitespace before this item.
87     pub new_lines: bool,
88 }
89
90 impl ListItem {
91     pub fn inner_as_ref(&self) -> &str {
92         self.item.as_ref().map_or("", |s| &*s)
93     }
94
95     pub fn is_multiline(&self) -> bool {
96         self.inner_as_ref().contains('\n') || self.pre_comment.is_some() ||
97             self.post_comment
98                 .as_ref()
99                 .map_or(false, |s| s.contains('\n'))
100     }
101
102     pub fn has_comment(&self) -> bool {
103         self.pre_comment
104             .as_ref()
105             .map_or(false, |comment| comment.starts_with("//")) ||
106             self.post_comment
107                 .as_ref()
108                 .map_or(false, |comment| comment.starts_with("//"))
109     }
110
111     pub fn from_str<S: Into<String>>(s: S) -> ListItem {
112         ListItem {
113             pre_comment: None,
114             item: Some(s.into()),
115             post_comment: None,
116             new_lines: false,
117         }
118     }
119 }
120
121 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
122 /// The definitive formatting tactic for lists.
123 pub enum DefinitiveListTactic {
124     Vertical,
125     Horizontal,
126     Mixed,
127 }
128
129 impl DefinitiveListTactic {
130     pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool {
131         match indent_style {
132             IndentStyle::Block => *self != DefinitiveListTactic::Horizontal,
133             IndentStyle::Visual => false,
134         }
135     }
136 }
137
138 /// The type of separator for lists.
139 #[derive(Eq, PartialEq, Debug)]
140 pub enum Separator {
141     Comma,
142     VerticalBar,
143 }
144
145 impl Separator {
146     pub fn len(&self) -> usize {
147         match *self {
148             // 2 = `, `
149             Separator::Comma => 2,
150             // 3 = ` | `
151             Separator::VerticalBar => 3,
152         }
153     }
154 }
155
156 pub fn definitive_tactic<I, T>(
157     items: I,
158     tactic: ListTactic,
159     sep: Separator,
160     width: usize,
161 ) -> DefinitiveListTactic
162 where
163     I: IntoIterator<Item = T> + Clone,
164     T: AsRef<ListItem>,
165 {
166     let pre_line_comments = items
167         .clone()
168         .into_iter()
169         .any(|item| item.as_ref().has_comment());
170
171     let limit = match tactic {
172         _ if pre_line_comments => return DefinitiveListTactic::Vertical,
173         ListTactic::Mixed => return DefinitiveListTactic::Mixed,
174         ListTactic::Horizontal => return DefinitiveListTactic::Horizontal,
175         ListTactic::Vertical => return DefinitiveListTactic::Vertical,
176         ListTactic::LimitedHorizontalVertical(limit) => ::std::cmp::min(width, limit),
177         ListTactic::HorizontalVertical => width,
178     };
179
180     let (sep_count, total_width) = calculate_width(items.clone());
181     let total_sep_len = sep.len() * sep_count.checked_sub(1).unwrap_or(0);
182     let real_total = total_width + total_sep_len;
183
184     if real_total <= limit && !pre_line_comments &&
185         !items.into_iter().any(|item| item.as_ref().is_multiline())
186     {
187         DefinitiveListTactic::Horizontal
188     } else {
189         DefinitiveListTactic::Vertical
190     }
191 }
192
193 // Format a list of commented items into a string.
194 // TODO: add unit tests
195 pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
196 where
197     I: IntoIterator<Item = T> + Clone,
198     T: AsRef<ListItem>,
199 {
200     let tactic = formatting.tactic;
201     let sep_len = formatting.separator.len();
202
203     // Now that we know how we will layout, we can decide for sure if there
204     // will be a trailing separator.
205     let mut trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
206     let mut result = String::new();
207     let cloned_items = items.clone();
208     let mut iter = items.into_iter().enumerate().peekable();
209     let mut item_max_width: Option<usize> = None;
210
211     let mut line_len = 0;
212     let indent_str = &formatting.shape.indent.to_string(formatting.config);
213     while let Some((i, item)) = iter.next() {
214         let item = item.as_ref();
215         let inner_item = try_opt!(item.item.as_ref());
216         let first = i == 0;
217         let last = iter.peek().is_none();
218         let mut separate = !last || trailing_separator;
219         let item_sep_len = if separate { sep_len } else { 0 };
220
221         // Item string may be multi-line. Its length (used for block comment alignment)
222         // should be only the length of the last line.
223         let item_last_line = if item.is_multiline() {
224             inner_item.lines().last().unwrap_or("")
225         } else {
226             inner_item.as_ref()
227         };
228         let mut item_last_line_width = item_last_line.len() + item_sep_len;
229         if item_last_line.starts_with(indent_str) {
230             item_last_line_width -= indent_str.len();
231         }
232
233         match tactic {
234             DefinitiveListTactic::Horizontal if !first => {
235                 result.push(' ');
236             }
237             DefinitiveListTactic::Vertical if !first => {
238                 result.push('\n');
239                 result.push_str(indent_str);
240             }
241             DefinitiveListTactic::Mixed => {
242                 let total_width = total_item_width(item) + item_sep_len;
243
244                 // 1 is space between separator and item.
245                 if line_len > 0 && line_len + 1 + total_width > formatting.shape.width {
246                     result.push('\n');
247                     result.push_str(indent_str);
248                     line_len = 0;
249                     if tactic == DefinitiveListTactic::Mixed && formatting.ends_with_newline {
250                         if last {
251                             separate = true;
252                         } else {
253                             trailing_separator = true;
254                         }
255                     }
256                 }
257
258                 if line_len > 0 {
259                     result.push(' ');
260                     line_len += 1;
261                 }
262
263                 line_len += total_width;
264             }
265             _ => {}
266         }
267
268         // Pre-comments
269         if let Some(ref comment) = item.pre_comment {
270             // Block style in non-vertical mode.
271             let block_mode = tactic != DefinitiveListTactic::Vertical;
272             // Width restriction is only relevant in vertical mode.
273             let comment = try_opt!(rewrite_comment(
274                 comment,
275                 block_mode,
276                 formatting.shape,
277                 formatting.config,
278             ));
279             result.push_str(&comment);
280
281             if tactic == DefinitiveListTactic::Vertical {
282                 result.push('\n');
283                 result.push_str(indent_str);
284             } else {
285                 result.push(' ');
286             }
287             item_max_width = None;
288         }
289
290         result.push_str(&inner_item[..]);
291
292         // Post-comments
293         if tactic != DefinitiveListTactic::Vertical && item.post_comment.is_some() {
294             let comment = item.post_comment.as_ref().unwrap();
295             let formatted_comment = try_opt!(rewrite_comment(
296                 comment,
297                 true,
298                 Shape::legacy(formatting.shape.width, Indent::empty()),
299                 formatting.config,
300             ));
301
302             result.push(' ');
303             result.push_str(&formatted_comment);
304         }
305
306         if separate {
307             result.push_str(formatting.separator);
308         }
309
310         if tactic == DefinitiveListTactic::Vertical && item.post_comment.is_some() {
311             let comment = item.post_comment.as_ref().unwrap();
312             let overhead = last_line_width(&result) + first_line_width(comment.trim());
313
314             let rewrite_post_comment = |item_max_width: &mut Option<usize>| {
315                 if item_max_width.is_none() && !last && !inner_item.contains('\n') {
316                     *item_max_width = Some(max_width_of_item_with_post_comment(
317                         &cloned_items,
318                         i,
319                         overhead,
320                         formatting.config.max_width(),
321                     ));
322                 }
323                 let overhead = if let &mut Some(max_width) = item_max_width {
324                     max_width + 2
325                 } else {
326                     // 1 = space between item and comment.
327                     item_last_line_width + 1
328                 };
329                 let width = formatting.shape.width.checked_sub(overhead).unwrap_or(1);
330                 let offset = formatting.shape.indent + overhead;
331                 let comment_shape = Shape::legacy(width, offset);
332
333                 // Use block-style only for the last item or multiline comments.
334                 let block_style = !formatting.ends_with_newline && last ||
335                     comment.trim().contains('\n') ||
336                     comment.trim().len() > width;
337
338                 rewrite_comment(comment, block_style, comment_shape, formatting.config)
339             };
340
341             let mut formatted_comment = try_opt!(rewrite_post_comment(&mut item_max_width));
342
343             if !formatted_comment.starts_with('\n') {
344                 let mut comment_alignment =
345                     post_comment_alignment(item_max_width, inner_item.len());
346                 if first_line_width(&formatted_comment) + last_line_width(&result) +
347                     comment_alignment + 1 > formatting.config.max_width()
348                 {
349                     item_max_width = None;
350                     formatted_comment = try_opt!(rewrite_post_comment(&mut item_max_width));
351                     comment_alignment = post_comment_alignment(item_max_width, inner_item.len());
352                 }
353                 for _ in 0..(comment_alignment + 1) {
354                     result.push(' ');
355                 }
356                 // An additional space for the missing trailing comma
357                 if last && item_max_width.is_some() && !separate {
358                     result.push(' ');
359                 }
360             }
361             if formatted_comment.contains('\n') {
362                 item_max_width = None;
363             }
364             result.push_str(&formatted_comment);
365         } else {
366             item_max_width = None;
367         }
368
369         if formatting.preserve_newline && !last && tactic == DefinitiveListTactic::Vertical &&
370             item.new_lines
371         {
372             item_max_width = None;
373             result.push('\n');
374         }
375     }
376
377     Some(result)
378 }
379
380 fn max_width_of_item_with_post_comment<I, T>(
381     items: &I,
382     i: usize,
383     overhead: usize,
384     max_budget: usize,
385 ) -> usize
386 where
387     I: IntoIterator<Item = T> + Clone,
388     T: AsRef<ListItem>,
389 {
390     let mut max_width = 0;
391     let mut first = true;
392     for item in items.clone().into_iter().skip(i) {
393         let item = item.as_ref();
394         let inner_item_width = item.inner_as_ref().len();
395         if !first &&
396             (item.is_multiline() || !item.post_comment.is_some() ||
397                 inner_item_width + overhead > max_budget)
398         {
399             return max_width;
400         }
401         if max_width < inner_item_width {
402             max_width = inner_item_width;
403         }
404         if item.new_lines {
405             return max_width;
406         }
407         first = false;
408     }
409     max_width
410 }
411
412 fn post_comment_alignment(item_max_width: Option<usize>, inner_item_len: usize) -> usize {
413     item_max_width
414         .and_then(|max_line_width| max_line_width.checked_sub(inner_item_len))
415         .unwrap_or(0)
416 }
417
418 pub struct ListItems<'a, I, F1, F2, F3>
419 where
420     I: Iterator,
421 {
422     codemap: &'a CodeMap,
423     inner: Peekable<I>,
424     get_lo: F1,
425     get_hi: F2,
426     get_item_string: F3,
427     prev_span_end: BytePos,
428     next_span_start: BytePos,
429     terminator: &'a str,
430 }
431
432 impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
433 where
434     I: Iterator<Item = T>,
435     F1: Fn(&T) -> BytePos,
436     F2: Fn(&T) -> BytePos,
437     F3: Fn(&T) -> Option<String>,
438 {
439     type Item = ListItem;
440
441     fn next(&mut self) -> Option<Self::Item> {
442         let white_space: &[_] = &[' ', '\t'];
443
444         self.inner.next().map(|item| {
445             let mut new_lines = false;
446             // Pre-comment
447             let pre_snippet = self.codemap
448                 .span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
449                 .unwrap();
450             let trimmed_pre_snippet = pre_snippet.trim();
451             let has_pre_comment =
452                 trimmed_pre_snippet.contains("//") || trimmed_pre_snippet.contains("/*");
453             let pre_comment = if has_pre_comment {
454                 Some(trimmed_pre_snippet.to_owned())
455             } else {
456                 None
457             };
458
459             // Post-comment
460             let next_start = match self.inner.peek() {
461                 Some(next_item) => (self.get_lo)(next_item),
462                 None => self.next_span_start,
463             };
464             let post_snippet = self.codemap
465                 .span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
466                 .unwrap();
467
468             let comment_end = match self.inner.peek() {
469                 Some(..) => {
470                     let mut block_open_index = post_snippet.find("/*");
471                     // check if it realy is a block comment (and not //*)
472                     if let Some(i) = block_open_index {
473                         if i > 0 && &post_snippet[i - 1..i] == "/" {
474                             block_open_index = None;
475                         }
476                     }
477                     let newline_index = post_snippet.find('\n');
478                     let separator_index = post_snippet.find_uncommented(",").unwrap();
479
480                     match (block_open_index, newline_index) {
481                         // Separator before comment, with the next item on same line.
482                         // Comment belongs to next item.
483                         (Some(i), None) if i > separator_index => separator_index + 1,
484                         // Block-style post-comment before the separator.
485                         (Some(i), None) => cmp::max(
486                             find_comment_end(&post_snippet[i..]).unwrap() + i,
487                             separator_index + 1,
488                         ),
489                         // Block-style post-comment. Either before or after the separator.
490                         (Some(i), Some(j)) if i < j => cmp::max(
491                             find_comment_end(&post_snippet[i..]).unwrap() + i,
492                             separator_index + 1,
493                         ),
494                         // Potential *single* line comment.
495                         (_, Some(j)) if j > separator_index => j + 1,
496                         _ => post_snippet.len(),
497                     }
498                 }
499                 None => post_snippet
500                     .find_uncommented(self.terminator)
501                     .unwrap_or(post_snippet.len()),
502             };
503
504             if !post_snippet.is_empty() && comment_end > 0 {
505                 // Account for extra whitespace between items. This is fiddly
506                 // because of the way we divide pre- and post- comments.
507
508                 // Everything from the separator to the next item.
509                 let test_snippet = &post_snippet[comment_end - 1..];
510                 let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
511                 // From the end of the first line of comments.
512                 let test_snippet = &test_snippet[first_newline..];
513                 let first = test_snippet
514                     .find(|c: char| !c.is_whitespace())
515                     .unwrap_or(test_snippet.len());
516                 // From the end of the first line of comments to the next non-whitespace char.
517                 let test_snippet = &test_snippet[..first];
518
519                 if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
520                     // There were multiple line breaks which got trimmed to nothing.
521                     new_lines = true;
522                 }
523             }
524
525             // Cleanup post-comment: strip separators and whitespace.
526             self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
527             let post_snippet = post_snippet[..comment_end].trim();
528
529             let post_snippet_trimmed = if post_snippet.starts_with(',') {
530                 post_snippet[1..].trim_matches(white_space)
531             } else if post_snippet.ends_with(',') {
532                 post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
533             } else {
534                 post_snippet
535             };
536
537             let post_comment = if !post_snippet_trimmed.is_empty() {
538                 Some(post_snippet_trimmed.to_owned())
539             } else {
540                 None
541             };
542
543             ListItem {
544                 pre_comment: pre_comment,
545                 item: (self.get_item_string)(&item),
546                 post_comment: post_comment,
547                 new_lines: new_lines,
548             }
549         })
550     }
551 }
552
553 // Creates an iterator over a list's items with associated comments.
554 pub fn itemize_list<'a, T, I, F1, F2, F3>(
555     codemap: &'a CodeMap,
556     inner: I,
557     terminator: &'a str,
558     get_lo: F1,
559     get_hi: F2,
560     get_item_string: F3,
561     prev_span_end: BytePos,
562     next_span_start: BytePos,
563 ) -> ListItems<'a, I, F1, F2, F3>
564 where
565     I: Iterator<Item = T>,
566     F1: Fn(&T) -> BytePos,
567     F2: Fn(&T) -> BytePos,
568     F3: Fn(&T) -> Option<String>,
569 {
570     ListItems {
571         codemap: codemap,
572         inner: inner.peekable(),
573         get_lo: get_lo,
574         get_hi: get_hi,
575         get_item_string: get_item_string,
576         prev_span_end: prev_span_end,
577         next_span_start: next_span_start,
578         terminator: terminator,
579     }
580 }
581
582 fn needs_trailing_separator(
583     separator_tactic: SeparatorTactic,
584     list_tactic: DefinitiveListTactic,
585 ) -> bool {
586     match separator_tactic {
587         SeparatorTactic::Always => true,
588         SeparatorTactic::Vertical => list_tactic == DefinitiveListTactic::Vertical,
589         SeparatorTactic::Never => false,
590     }
591 }
592
593 /// Returns the count and total width of the list items.
594 fn calculate_width<I, T>(items: I) -> (usize, usize)
595 where
596     I: IntoIterator<Item = T>,
597     T: AsRef<ListItem>,
598 {
599     items
600         .into_iter()
601         .map(|item| total_item_width(item.as_ref()))
602         .fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l))
603 }
604
605 fn total_item_width(item: &ListItem) -> usize {
606     comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..])) +
607         comment_len(item.post_comment.as_ref().map(|x| &(*x)[..])) +
608         item.item.as_ref().map_or(0, |str| str.len())
609 }
610
611 fn comment_len(comment: Option<&str>) -> usize {
612     match comment {
613         Some(s) => {
614             let text_len = s.trim().len();
615             if text_len > 0 {
616                 // We'll put " /*" before and " */" after inline comments.
617                 text_len + 6
618             } else {
619                 text_len
620             }
621         }
622         None => 0,
623     }
624 }
625
626 // Compute horizontal and vertical shapes for a struct-lit-like thing.
627 pub fn struct_lit_shape(
628     shape: Shape,
629     context: &RewriteContext,
630     prefix_width: usize,
631     suffix_width: usize,
632 ) -> Option<(Option<Shape>, Shape)> {
633     let v_shape = match context.config.struct_lit_style() {
634         IndentStyle::Visual => try_opt!(
635             try_opt!(shape.visual_indent(0).shrink_left(prefix_width)).sub_width(suffix_width)
636         ),
637         IndentStyle::Block => {
638             let shape = shape.block_indent(context.config.tab_spaces());
639             Shape {
640                 width: try_opt!(context.config.max_width().checked_sub(shape.indent.width())),
641                 ..shape
642             }
643         }
644     };
645     let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
646     if let Some(w) = shape_width {
647         let shape_width = cmp::min(w, context.config.struct_lit_width());
648         Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
649     } else {
650         Some((None, v_shape))
651     }
652 }
653
654 // Compute the tactic for the internals of a struct-lit-like thing.
655 pub fn struct_lit_tactic(
656     h_shape: Option<Shape>,
657     context: &RewriteContext,
658     items: &[ListItem],
659 ) -> DefinitiveListTactic {
660     if let Some(h_shape) = h_shape {
661         let prelim_tactic = match (context.config.struct_lit_style(), items.len()) {
662             (IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
663             _ => context.config.struct_lit_multiline_style().to_list_tactic(),
664         };
665         definitive_tactic(items, prelim_tactic, Separator::Comma, h_shape.width)
666     } else {
667         DefinitiveListTactic::Vertical
668     }
669 }
670
671 // Given a tactic and possible shapes for horizontal and vertical layout,
672 // come up with the actual shape to use.
673 pub fn shape_for_tactic(
674     tactic: DefinitiveListTactic,
675     h_shape: Option<Shape>,
676     v_shape: Shape,
677 ) -> Shape {
678     match tactic {
679         DefinitiveListTactic::Horizontal => h_shape.unwrap(),
680         _ => v_shape,
681     }
682 }
683
684 // Create a ListFormatting object for formatting the internals of a
685 // struct-lit-like thing, that is a series of fields.
686 pub fn struct_lit_formatting<'a>(
687     shape: Shape,
688     tactic: DefinitiveListTactic,
689     context: &'a RewriteContext,
690     force_no_trailing_comma: bool,
691 ) -> ListFormatting<'a> {
692     let ends_with_newline = context.config.struct_lit_style() != IndentStyle::Visual &&
693         tactic == DefinitiveListTactic::Vertical;
694     ListFormatting {
695         tactic: tactic,
696         separator: ",",
697         trailing_separator: if force_no_trailing_comma {
698             SeparatorTactic::Never
699         } else {
700             context.config.trailing_comma()
701         },
702         shape: shape,
703         ends_with_newline: ends_with_newline,
704         preserve_newline: true,
705         config: context.config,
706     }
707 }