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