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