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