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.
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.
12 use std::iter::Peekable;
14 use syntax::codemap::{self, CodeMap, BytePos};
16 use utils::{round_up_to_power_of_two, make_indent, wrap_str};
17 use comment::{FindUncommented, rewrite_comment, find_comment_end};
19 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
23 // All items on one row.
25 // Try Horizontal layout, if that fails then vertical
27 // Pack as many items as possible per row over (possibly) many rows.
31 impl_enum_decodable!(ListTactic, Vertical, Horizontal, HorizontalVertical, Mixed);
33 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
34 pub enum SeparatorTactic {
40 impl_enum_decodable!(SeparatorTactic, Always, Never, Vertical);
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,
48 // Available width if we layout horizontally.
50 // Available width if we layout vertically
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,
57 impl<'a> ListFormatting<'a> {
58 pub fn for_fn(width: usize, offset: usize) -> ListFormatting<'a> {
60 tactic: ListTactic::HorizontalVertical,
62 trailing_separator: SeparatorTactic::Never,
66 ends_with_newline: false,
72 pub pre_comment: Option<String>,
73 // Item should include attributes and doc comments.
75 pub post_comment: Option<String>,
76 // Whether there is extra whitespace before this item.
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)
86 pub fn has_line_pre_comment(&self) -> bool {
87 self.pre_comment.as_ref().map_or(false, |comment| comment.starts_with("//"))
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 }
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> {
100 return Some(String::new());
103 let mut tactic = formatting.tactic;
105 // Conservatively overestimates because of the changing separator tactic.
106 let sep_count = if formatting.trailing_separator == SeparatorTactic::Always {
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;
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
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;
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;
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);
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
147 total_width + items.len() * (formatting.indent + 1)
149 let mut result = String::with_capacity(round_up_to_power_of_two(alloc_width));
151 let mut line_len = 0;
152 let indent_str = &make_indent(formatting.indent);
153 for (i, item) in items.iter().enumerate() {
155 let last = i == items.len() - 1;
156 let separate = !last || trailing_separator;
157 let item_sep_len = if separate {
162 let item_width = item.item.len() + item_sep_len;
165 ListTactic::Horizontal if !first => {
168 ListTactic::Vertical if !first => {
170 result.push_str(indent_str);
172 ListTactic::Mixed => {
173 let total_width = total_item_width(item) + item_sep_len;
175 if line_len > 0 && line_len + total_width > formatting.v_width {
177 result.push_str(indent_str);
186 line_len += total_width;
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));
199 if tactic == ListTactic::Vertical {
201 result.push_str(indent_str);
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));
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);
217 result.push_str(&formatted_comment);
221 result.push_str(formatting.separator);
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;
234 let formatted_comment = rewrite_comment(comment, block_style, width, offset);
237 result.push_str(&formatted_comment);
240 if !last && tactic == ListTactic::Vertical && item.new_lines {
248 pub struct ListItems<'a, I, F1, F2, F3>
251 codemap: &'a CodeMap,
256 prev_span_end: BytePos,
257 next_span_start: BytePos,
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,
267 type Item = ListItem;
269 fn next(&mut self) -> Option<Self::Item> {
270 let white_space: &[_] = &[' ', '\t'];
272 self.inner.next().map(|item| {
273 let mut new_lines = false;
275 let pre_snippet = self.codemap
276 .span_to_snippet(codemap::mk_sp(self.prev_span_end,
277 (self.get_lo)(&item)))
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())
287 let next_start = match self.inner.peek() {
288 Some(ref next_item) => (self.get_lo)(next_item),
289 None => self.next_span_start,
291 let post_snippet = self.codemap
292 .span_to_snippet(codemap::mk_sp((self.get_hi)(&item),
296 let comment_end = match self.inner.peek() {
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();
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 => {
308 // Block-style post-comment before the separator.
310 cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
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,
318 // Potential *single* line comment.
319 (_, Some(j)) => j + 1,
320 _ => post_snippet.len(),
324 post_snippet.find_uncommented(self.terminator).unwrap_or(post_snippet.len())
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.
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];
342 if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
343 // There were multiple line breaks which got trimmed to nothing.
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();
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)
360 let post_comment = if !post_snippet_trimmed.is_empty() {
361 Some(post_snippet_trimmed.to_owned())
367 pre_comment: pre_comment,
368 item: (self.get_item_string)(&item),
369 post_comment: post_comment,
370 new_lines: new_lines,
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,
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,
393 inner: inner.peekable(),
396 get_item_string: get_item_string,
397 prev_span_end: prev_span_end,
398 next_span_start: next_span_start,
399 terminator: terminator,
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,
411 fn calculate_width(items: &[ListItem]) -> usize {
412 items.iter().map(total_item_width).fold(0, |a, l| a + l)
415 fn total_item_width(item: &ListItem) -> usize {
416 comment_len(&item.pre_comment) + comment_len(&item.post_comment) + item.item.len()
419 fn comment_len(comment: &Option<String>) -> usize {
422 let text_len = s.trim().len();
424 // We'll put " /*" before and " */" after inline comments.