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