]> git.lizzy.rs Git - rust.git/blob - src/lists.rs
Merge pull request #309 from marcusklaas/array-literals
[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 utils::{round_up_to_power_of_two, make_indent, wrap_str};
17 use comment::{FindUncommented, rewrite_comment, find_comment_end};
18
19 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
20 pub enum ListTactic {
21     // One item per row.
22     Vertical,
23     // All items on one row.
24     Horizontal,
25     // Try Horizontal layout, if that fails then vertical
26     HorizontalVertical,
27     // Pack as many items as possible per row over (possibly) many rows.
28     Mixed,
29 }
30
31 impl_enum_decodable!(ListTactic, Vertical, Horizontal, HorizontalVertical, Mixed);
32
33 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
34 pub enum SeparatorTactic {
35     Always,
36     Never,
37     Vertical,
38 }
39
40 impl_enum_decodable!(SeparatorTactic, Always, Never, Vertical);
41
42 // TODO having some helpful ctors for ListFormatting would be nice.
43 pub struct ListFormatting<'a> {
44     pub tactic: ListTactic,
45     pub separator: &'a str,
46     pub trailing_separator: SeparatorTactic,
47     pub indent: usize,
48     // Available width if we layout horizontally.
49     pub h_width: usize,
50     // Available width if we layout vertically
51     pub v_width: usize,
52     // Non-expressions, e.g. items, will have a new line at the end of the list.
53     // Important for comment styles.
54     pub ends_with_newline: bool,
55 }
56
57 impl<'a> ListFormatting<'a> {
58     pub fn for_fn(width: usize, offset: usize) -> ListFormatting<'a> {
59         ListFormatting {
60             tactic: ListTactic::HorizontalVertical,
61             separator: ",",
62             trailing_separator: SeparatorTactic::Never,
63             indent: offset,
64             h_width: width,
65             v_width: width,
66             ends_with_newline: false,
67         }
68     }
69 }
70
71 pub struct ListItem {
72     pub pre_comment: Option<String>,
73     // Item should include attributes and doc comments.
74     pub item: String,
75     pub post_comment: Option<String>,
76     // Whether there is extra whitespace before this item.
77     pub new_lines: bool,
78 }
79
80 impl ListItem {
81     pub fn is_multiline(&self) -> bool {
82         self.item.contains('\n') || self.pre_comment.is_some() ||
83         self.post_comment.as_ref().map(|s| s.contains('\n')).unwrap_or(false)
84     }
85
86     pub fn has_line_pre_comment(&self) -> bool {
87         self.pre_comment.as_ref().map_or(false, |comment| comment.starts_with("//"))
88     }
89
90     pub fn from_str<S: Into<String>>(s: S) -> ListItem {
91         ListItem { pre_comment: None, item: s.into(), post_comment: None, new_lines: false }
92     }
93 }
94
95 // Format a list of commented items into a string.
96 // FIXME: this has grown into a monstrosity
97 // TODO: add unit tests
98 pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> Option<String> {
99     if items.is_empty() {
100         return Some(String::new());
101     }
102
103     let mut tactic = formatting.tactic;
104
105     // Conservatively overestimates because of the changing separator tactic.
106     let sep_count = if formatting.trailing_separator == SeparatorTactic::Always {
107         items.len()
108     } else {
109         items.len() - 1
110     };
111     let sep_len = formatting.separator.len();
112     let total_sep_len = (sep_len + 1) * sep_count;
113     let total_width = calculate_width(items);
114     let fits_single = total_width + total_sep_len <= formatting.h_width;
115
116     // Check if we need to fallback from horizontal listing, if possible.
117     if tactic == ListTactic::HorizontalVertical {
118         debug!("write_list: total_width: {}, total_sep_len: {}, h_width: {}",
119                total_width, total_sep_len, formatting.h_width);
120         tactic = if fits_single && !items.iter().any(ListItem::is_multiline) {
121             ListTactic::Horizontal
122         } else {
123             ListTactic::Vertical
124         };
125     }
126
127     // Check if we can fit everything on a single line in mixed mode.
128     // The horizontal tactic does not break after v_width columns.
129     if tactic == ListTactic::Mixed && fits_single {
130         tactic = ListTactic::Horizontal;
131     }
132
133     // Switch to vertical mode if we find non-block comments.
134     if items.iter().any(ListItem::has_line_pre_comment) {
135         tactic = ListTactic::Vertical;
136     }
137
138     // Now that we know how we will layout, we can decide for sure if there
139     // will be a trailing separator.
140     let trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
141
142     // Create a buffer for the result.
143     // TODO could use a StringBuffer or rope for this
144     let alloc_width = if tactic == ListTactic::Horizontal {
145         total_width + total_sep_len
146     } else {
147         total_width + items.len() * (formatting.indent + 1)
148     };
149     let mut result = String::with_capacity(round_up_to_power_of_two(alloc_width));
150
151     let mut line_len = 0;
152     let indent_str = &make_indent(formatting.indent);
153     for (i, item) in items.iter().enumerate() {
154         let first = i == 0;
155         let last = i == items.len() - 1;
156         let separate = !last || trailing_separator;
157         let item_sep_len = if separate {
158             sep_len
159         } else {
160             0
161         };
162         let item_width = item.item.len() + item_sep_len;
163
164         match tactic {
165             ListTactic::Horizontal if !first => {
166                 result.push(' ');
167             }
168             ListTactic::Vertical if !first => {
169                 result.push('\n');
170                 result.push_str(indent_str);
171             }
172             ListTactic::Mixed => {
173                 let total_width = total_item_width(item) + item_sep_len;
174
175                 if line_len > 0 && line_len + total_width > formatting.v_width {
176                     result.push('\n');
177                     result.push_str(indent_str);
178                     line_len = 0;
179                 }
180
181                 if line_len > 0 {
182                     result.push(' ');
183                     line_len += 1;
184                 }
185
186                 line_len += total_width;
187             }
188             _ => {}
189         }
190
191         // Pre-comments
192         if let Some(ref comment) = item.pre_comment {
193             // Block style in non-vertical mode.
194             let block_mode = tactic != ListTactic::Vertical;
195             // Width restriction is only relevant in vertical mode.
196             let max_width = formatting.v_width;
197             result.push_str(&rewrite_comment(comment, block_mode, max_width, formatting.indent));
198
199             if tactic == ListTactic::Vertical {
200                 result.push('\n');
201                 result.push_str(indent_str);
202             } else {
203                 result.push(' ');
204             }
205         }
206
207         let max_width = formatting.indent + formatting.v_width;
208         let item_str = wrap_str(&item.item[..], max_width, formatting.v_width, formatting.indent);
209         result.push_str(&&try_opt!(item_str));
210
211         // Post-comments
212         if tactic != ListTactic::Vertical && item.post_comment.is_some() {
213             let comment = item.post_comment.as_ref().unwrap();
214             let formatted_comment = rewrite_comment(comment, true, formatting.v_width, 0);
215
216             result.push(' ');
217             result.push_str(&formatted_comment);
218         }
219
220         if separate {
221             result.push_str(formatting.separator);
222         }
223
224         if tactic == ListTactic::Vertical && item.post_comment.is_some() {
225             // 1 = space between item and comment.
226             let width = formatting.v_width.checked_sub(item_width + 1).unwrap_or(1);
227             let offset = formatting.indent + item_width + 1;
228             let comment = item.post_comment.as_ref().unwrap();
229             // Use block-style only for the last item or multiline comments.
230             let block_style = !formatting.ends_with_newline && last ||
231                               comment.trim().contains('\n') ||
232                               comment.trim().len() > width;
233
234             let formatted_comment = rewrite_comment(comment, block_style, width, offset);
235
236             result.push(' ');
237             result.push_str(&formatted_comment);
238         }
239
240         if !last && tactic == ListTactic::Vertical && item.new_lines {
241             result.push('\n');
242         }
243     }
244
245     Some(result)
246 }
247
248 pub struct ListItems<'a, I, F1, F2, F3>
249     where I: Iterator
250 {
251     codemap: &'a CodeMap,
252     inner: Peekable<I>,
253     get_lo: F1,
254     get_hi: F2,
255     get_item_string: F3,
256     prev_span_end: BytePos,
257     next_span_start: BytePos,
258     terminator: &'a str,
259 }
260
261 impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
262     where I: Iterator<Item = T>,
263           F1: Fn(&T) -> BytePos,
264           F2: Fn(&T) -> BytePos,
265           F3: Fn(&T) -> String
266 {
267     type Item = ListItem;
268
269     fn next(&mut self) -> Option<Self::Item> {
270         let white_space: &[_] = &[' ', '\t'];
271
272         self.inner.next().map(|item| {
273             let mut new_lines = false;
274             // Pre-comment
275             let pre_snippet = self.codemap
276                                   .span_to_snippet(codemap::mk_sp(self.prev_span_end,
277                                                                   (self.get_lo)(&item)))
278                                   .unwrap();
279             let trimmed_pre_snippet = pre_snippet.trim();
280             let pre_comment = if !trimmed_pre_snippet.is_empty() {
281                 Some(trimmed_pre_snippet.to_owned())
282             } else {
283                 None
284             };
285
286             // Post-comment
287             let next_start = match self.inner.peek() {
288                 Some(ref next_item) => (self.get_lo)(next_item),
289                 None => self.next_span_start,
290             };
291             let post_snippet = self.codemap
292                                    .span_to_snippet(codemap::mk_sp((self.get_hi)(&item),
293                                                                    next_start))
294                                    .unwrap();
295
296             let comment_end = match self.inner.peek() {
297                 Some(..) => {
298                     let block_open_index = post_snippet.find("/*");
299                     let newline_index = post_snippet.find('\n');
300                     let separator_index = post_snippet.find_uncommented(",").unwrap();
301
302                     match (block_open_index, newline_index) {
303                         // Separator before comment, with the next item on same line.
304                         // Comment belongs to next item.
305                         (Some(i), None) if i > separator_index => {
306                             separator_index + 1
307                         }
308                         // Block-style post-comment before the separator.
309                         (Some(i), None) => {
310                             cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
311                                      separator_index + 1)
312                         }
313                         // Block-style post-comment. Either before or after the separator.
314                         (Some(i), Some(j)) if i < j => {
315                             cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
316                                      separator_index + 1)
317                         }
318                         // Potential *single* line comment.
319                         (_, Some(j)) => j + 1,
320                         _ => post_snippet.len(),
321                     }
322                 }
323                 None => {
324                     post_snippet.find_uncommented(self.terminator).unwrap_or(post_snippet.len())
325                 }
326             };
327
328             if !post_snippet.is_empty() && comment_end > 0 {
329                 // Account for extra whitespace between items. This is fiddly
330                 // because of the way we divide pre- and post- comments.
331
332                 // Everything from the separator to the next item.
333                 let test_snippet = &post_snippet[comment_end-1..];
334                 let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
335                 // From the end of the first line of comments.
336                 let test_snippet = &test_snippet[first_newline..];
337                 let first = test_snippet.find(|c: char| !c.is_whitespace())
338                                         .unwrap_or(test_snippet.len());
339                 // From the end of the first line of comments to the next non-whitespace char.
340                 let test_snippet = &test_snippet[..first];
341
342                 if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
343                     // There were multiple line breaks which got trimmed to nothing.
344                     new_lines = true;
345                 }
346             }
347
348             // Cleanup post-comment: strip separators and whitespace.
349             self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
350             let post_snippet = post_snippet[..comment_end].trim();
351
352             let post_snippet_trimmed = if post_snippet.starts_with(',') {
353                 post_snippet[1..].trim_matches(white_space)
354             } else if post_snippet.ends_with(",") {
355                 post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
356             } else {
357                 post_snippet
358             };
359
360             let post_comment = if !post_snippet_trimmed.is_empty() {
361                 Some(post_snippet_trimmed.to_owned())
362             } else {
363                 None
364             };
365
366             ListItem {
367                 pre_comment: pre_comment,
368                 item: (self.get_item_string)(&item),
369                 post_comment: post_comment,
370                 new_lines: new_lines,
371             }
372         })
373     }
374 }
375
376 // Creates an iterator over a list's items with associated comments.
377 pub fn itemize_list<'a, T, I, F1, F2, F3>(codemap: &'a CodeMap,
378                                           inner: I,
379                                           terminator: &'a str,
380                                           get_lo: F1,
381                                           get_hi: F2,
382                                           get_item_string: F3,
383                                           prev_span_end: BytePos,
384                                           next_span_start: BytePos)
385                                           -> ListItems<'a, I, F1, F2, F3>
386     where I: Iterator<Item = T>,
387           F1: Fn(&T) -> BytePos,
388           F2: Fn(&T) -> BytePos,
389           F3: Fn(&T) -> String
390 {
391     ListItems {
392         codemap: codemap,
393         inner: inner.peekable(),
394         get_lo: get_lo,
395         get_hi: get_hi,
396         get_item_string: get_item_string,
397         prev_span_end: prev_span_end,
398         next_span_start: next_span_start,
399         terminator: terminator,
400     }
401 }
402
403 fn needs_trailing_separator(separator_tactic: SeparatorTactic, list_tactic: ListTactic) -> bool {
404     match separator_tactic {
405         SeparatorTactic::Always => true,
406         SeparatorTactic::Vertical => list_tactic == ListTactic::Vertical,
407         SeparatorTactic::Never => false,
408     }
409 }
410
411 fn calculate_width(items: &[ListItem]) -> usize {
412     items.iter().map(total_item_width).fold(0, |a, l| a + l)
413 }
414
415 fn total_item_width(item: &ListItem) -> usize {
416     comment_len(&item.pre_comment) + comment_len(&item.post_comment) + item.item.len()
417 }
418
419 fn comment_len(comment: &Option<String>) -> usize {
420     match *comment {
421         Some(ref s) => {
422             let text_len = s.trim().len();
423             if text_len > 0 {
424                 // We'll put " /*" before and " */" after inline comments.
425                 text_len + 6
426             } else {
427                 text_len
428             }
429         }
430         None => 0,
431     }
432 }