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::{BytePos, CodeMap};
17 use comment::{find_comment_end, rewrite_comment, FindUncommented};
18 use config::{Config, IndentStyle};
19 use rewrite::RewriteContext;
20 use utils::{first_line_width, last_line_width, mk_sp};
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
29 // All items on one row.
31 // Try Horizontal layout, if that fails then vertical.
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.
39 impl_enum_serialize_and_deserialize!(ListTactic, Vertical, Horizontal, HorizontalVertical, Mixed);
41 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
42 pub enum SeparatorTactic {
48 impl_enum_serialize_and_deserialize!(SeparatorTactic, Always, Never, Vertical);
50 impl SeparatorTactic {
51 pub fn from_bool(b: bool) -> SeparatorTactic {
53 SeparatorTactic::Always
55 SeparatorTactic::Never
60 pub struct ListFormatting<'a> {
61 pub tactic: DefinitiveListTactic,
62 pub separator: &'a str,
63 pub trailing_separator: SeparatorTactic,
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 // Remove newlines between list elements for expressions.
69 pub preserve_newline: bool,
70 pub config: &'a Config,
73 impl AsRef<ListItem> for ListItem {
74 fn as_ref(&self) -> &ListItem {
80 // None for comments mean that they are not present.
81 pub pre_comment: Option<String>,
82 // Item should include attributes and doc comments. None indicates a failed
84 pub item: Option<String>,
85 pub post_comment: Option<String>,
86 // Whether there is extra whitespace before this item.
91 pub fn inner_as_ref(&self) -> &str {
92 self.item.as_ref().map_or("", |s| &*s)
95 pub fn is_multiline(&self) -> bool {
96 self.inner_as_ref().contains('\n') || self.pre_comment.is_some() ||
99 .map_or(false, |s| s.contains('\n'))
102 pub fn has_comment(&self) -> bool {
105 .map_or(false, |comment| comment.starts_with("//")) ||
108 .map_or(false, |comment| comment.starts_with("//"))
111 pub fn from_str<S: Into<String>>(s: S) -> ListItem {
114 item: Some(s.into()),
121 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
122 /// The definitive formatting tactic for lists.
123 pub enum DefinitiveListTactic {
129 impl DefinitiveListTactic {
130 pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool {
132 IndentStyle::Block => *self != DefinitiveListTactic::Horizontal,
133 IndentStyle::Visual => false,
138 /// The type of separator for lists.
139 #[derive(Eq, PartialEq, Debug)]
146 pub fn len(&self) -> usize {
149 Separator::Comma => 2,
151 Separator::VerticalBar => 3,
156 pub fn definitive_tactic<I, T>(
161 ) -> DefinitiveListTactic
163 I: IntoIterator<Item = T> + Clone,
166 let pre_line_comments = items
169 .any(|item| item.as_ref().has_comment());
171 let limit = match tactic {
172 _ if pre_line_comments => return DefinitiveListTactic::Vertical,
173 ListTactic::Mixed => return DefinitiveListTactic::Mixed,
174 ListTactic::Horizontal => return DefinitiveListTactic::Horizontal,
175 ListTactic::Vertical => return DefinitiveListTactic::Vertical,
176 ListTactic::LimitedHorizontalVertical(limit) => ::std::cmp::min(width, limit),
177 ListTactic::HorizontalVertical => width,
180 let (sep_count, total_width) = calculate_width(items.clone());
181 let total_sep_len = sep.len() * sep_count.checked_sub(1).unwrap_or(0);
182 let real_total = total_width + total_sep_len;
184 if real_total <= limit && !pre_line_comments &&
185 !items.into_iter().any(|item| item.as_ref().is_multiline())
187 DefinitiveListTactic::Horizontal
189 DefinitiveListTactic::Vertical
193 // Format a list of commented items into a string.
194 // TODO: add unit tests
195 pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
197 I: IntoIterator<Item = T> + Clone,
200 let tactic = formatting.tactic;
201 let sep_len = formatting.separator.len();
203 // Now that we know how we will layout, we can decide for sure if there
204 // will be a trailing separator.
205 let mut trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
206 let mut result = String::new();
207 let cloned_items = items.clone();
208 let mut iter = items.into_iter().enumerate().peekable();
209 let mut item_max_width: Option<usize> = None;
211 let mut line_len = 0;
212 let indent_str = &formatting.shape.indent.to_string(formatting.config);
213 while let Some((i, item)) = iter.next() {
214 let item = item.as_ref();
215 let inner_item = try_opt!(item.item.as_ref());
217 let last = iter.peek().is_none();
218 let mut separate = !last || trailing_separator;
219 let item_sep_len = if separate { sep_len } else { 0 };
221 // Item string may be multi-line. Its length (used for block comment alignment)
222 // should be only the length of the last line.
223 let item_last_line = if item.is_multiline() {
224 inner_item.lines().last().unwrap_or("")
228 let mut item_last_line_width = item_last_line.len() + item_sep_len;
229 if item_last_line.starts_with(indent_str) {
230 item_last_line_width -= indent_str.len();
234 DefinitiveListTactic::Horizontal if !first => {
237 DefinitiveListTactic::Vertical if !first => {
239 result.push_str(indent_str);
241 DefinitiveListTactic::Mixed => {
242 let total_width = total_item_width(item) + item_sep_len;
244 // 1 is space between separator and item.
245 if line_len > 0 && line_len + 1 + total_width > formatting.shape.width {
247 result.push_str(indent_str);
249 if tactic == DefinitiveListTactic::Mixed && formatting.ends_with_newline {
253 trailing_separator = true;
263 line_len += total_width;
269 if let Some(ref comment) = item.pre_comment {
270 // Block style in non-vertical mode.
271 let block_mode = tactic != DefinitiveListTactic::Vertical;
272 // Width restriction is only relevant in vertical mode.
273 let comment = try_opt!(rewrite_comment(
279 result.push_str(&comment);
281 if tactic == DefinitiveListTactic::Vertical {
283 result.push_str(indent_str);
287 item_max_width = None;
290 result.push_str(&inner_item[..]);
293 if tactic != DefinitiveListTactic::Vertical && item.post_comment.is_some() {
294 let comment = item.post_comment.as_ref().unwrap();
295 let formatted_comment = try_opt!(rewrite_comment(
298 Shape::legacy(formatting.shape.width, Indent::empty()),
303 result.push_str(&formatted_comment);
307 result.push_str(formatting.separator);
310 if tactic == DefinitiveListTactic::Vertical && item.post_comment.is_some() {
311 let comment = item.post_comment.as_ref().unwrap();
312 let overhead = last_line_width(&result) + first_line_width(comment.trim());
314 let rewrite_post_comment = |item_max_width: &mut Option<usize>| {
315 if item_max_width.is_none() && !last && !inner_item.contains('\n') {
316 *item_max_width = Some(max_width_of_item_with_post_comment(
320 formatting.config.max_width(),
323 let overhead = if let &mut Some(max_width) = item_max_width {
326 // 1 = space between item and comment.
327 item_last_line_width + 1
329 let width = formatting.shape.width.checked_sub(overhead).unwrap_or(1);
330 let offset = formatting.shape.indent + overhead;
331 let comment_shape = Shape::legacy(width, offset);
333 // Use block-style only for the last item or multiline comments.
334 let block_style = !formatting.ends_with_newline && last ||
335 comment.trim().contains('\n') ||
336 comment.trim().len() > width;
338 rewrite_comment(comment, block_style, comment_shape, formatting.config)
341 let mut formatted_comment = try_opt!(rewrite_post_comment(&mut item_max_width));
343 if !formatted_comment.starts_with('\n') {
344 let mut comment_alignment =
345 post_comment_alignment(item_max_width, inner_item.len());
346 if first_line_width(&formatted_comment) + last_line_width(&result) +
347 comment_alignment + 1 > formatting.config.max_width()
349 item_max_width = None;
350 formatted_comment = try_opt!(rewrite_post_comment(&mut item_max_width));
351 comment_alignment = post_comment_alignment(item_max_width, inner_item.len());
353 for _ in 0..(comment_alignment + 1) {
356 // An additional space for the missing trailing comma
357 if last && item_max_width.is_some() && !separate {
361 if formatted_comment.contains('\n') {
362 item_max_width = None;
364 result.push_str(&formatted_comment);
366 item_max_width = None;
369 if formatting.preserve_newline && !last && tactic == DefinitiveListTactic::Vertical &&
372 item_max_width = None;
380 fn max_width_of_item_with_post_comment<I, T>(
387 I: IntoIterator<Item = T> + Clone,
390 let mut max_width = 0;
391 let mut first = true;
392 for item in items.clone().into_iter().skip(i) {
393 let item = item.as_ref();
394 let inner_item_width = item.inner_as_ref().len();
396 (item.is_multiline() || !item.post_comment.is_some() ||
397 inner_item_width + overhead > max_budget)
401 if max_width < inner_item_width {
402 max_width = inner_item_width;
412 fn post_comment_alignment(item_max_width: Option<usize>, inner_item_len: usize) -> usize {
414 .and_then(|max_line_width| max_line_width.checked_sub(inner_item_len))
418 pub struct ListItems<'a, I, F1, F2, F3>
422 codemap: &'a CodeMap,
427 prev_span_end: BytePos,
428 next_span_start: BytePos,
432 impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
434 I: Iterator<Item = T>,
435 F1: Fn(&T) -> BytePos,
436 F2: Fn(&T) -> BytePos,
437 F3: Fn(&T) -> Option<String>,
439 type Item = ListItem;
441 fn next(&mut self) -> Option<Self::Item> {
442 let white_space: &[_] = &[' ', '\t'];
444 self.inner.next().map(|item| {
445 let mut new_lines = false;
447 let pre_snippet = self.codemap
448 .span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
450 let trimmed_pre_snippet = pre_snippet.trim();
451 let has_pre_comment =
452 trimmed_pre_snippet.contains("//") || trimmed_pre_snippet.contains("/*");
453 let pre_comment = if has_pre_comment {
454 Some(trimmed_pre_snippet.to_owned())
460 let next_start = match self.inner.peek() {
461 Some(next_item) => (self.get_lo)(next_item),
462 None => self.next_span_start,
464 let post_snippet = self.codemap
465 .span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
468 let comment_end = match self.inner.peek() {
470 let mut block_open_index = post_snippet.find("/*");
471 // check if it realy is a block comment (and not //*)
472 if let Some(i) = block_open_index {
473 if i > 0 && &post_snippet[i - 1..i] == "/" {
474 block_open_index = None;
477 let newline_index = post_snippet.find('\n');
478 let separator_index = post_snippet.find_uncommented(",").unwrap();
480 match (block_open_index, newline_index) {
481 // Separator before comment, with the next item on same line.
482 // Comment belongs to next item.
483 (Some(i), None) if i > separator_index => separator_index + 1,
484 // Block-style post-comment before the separator.
485 (Some(i), None) => cmp::max(
486 find_comment_end(&post_snippet[i..]).unwrap() + i,
489 // Block-style post-comment. Either before or after the separator.
490 (Some(i), Some(j)) if i < j => cmp::max(
491 find_comment_end(&post_snippet[i..]).unwrap() + i,
494 // Potential *single* line comment.
495 (_, Some(j)) if j > separator_index => j + 1,
496 _ => post_snippet.len(),
500 .find_uncommented(self.terminator)
501 .unwrap_or(post_snippet.len()),
504 if !post_snippet.is_empty() && comment_end > 0 {
505 // Account for extra whitespace between items. This is fiddly
506 // because of the way we divide pre- and post- comments.
508 // Everything from the separator to the next item.
509 let test_snippet = &post_snippet[comment_end - 1..];
510 let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
511 // From the end of the first line of comments.
512 let test_snippet = &test_snippet[first_newline..];
513 let first = test_snippet
514 .find(|c: char| !c.is_whitespace())
515 .unwrap_or(test_snippet.len());
516 // From the end of the first line of comments to the next non-whitespace char.
517 let test_snippet = &test_snippet[..first];
519 if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
520 // There were multiple line breaks which got trimmed to nothing.
525 // Cleanup post-comment: strip separators and whitespace.
526 self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
527 let post_snippet = post_snippet[..comment_end].trim();
529 let post_snippet_trimmed = if post_snippet.starts_with(',') {
530 post_snippet[1..].trim_matches(white_space)
531 } else if post_snippet.ends_with(',') {
532 post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
537 let post_comment = if !post_snippet_trimmed.is_empty() {
538 Some(post_snippet_trimmed.to_owned())
544 pre_comment: pre_comment,
545 item: (self.get_item_string)(&item),
546 post_comment: post_comment,
547 new_lines: new_lines,
553 // Creates an iterator over a list's items with associated comments.
554 pub fn itemize_list<'a, T, I, F1, F2, F3>(
555 codemap: &'a CodeMap,
561 prev_span_end: BytePos,
562 next_span_start: BytePos,
563 ) -> ListItems<'a, I, F1, F2, F3>
565 I: Iterator<Item = T>,
566 F1: Fn(&T) -> BytePos,
567 F2: Fn(&T) -> BytePos,
568 F3: Fn(&T) -> Option<String>,
572 inner: inner.peekable(),
575 get_item_string: get_item_string,
576 prev_span_end: prev_span_end,
577 next_span_start: next_span_start,
578 terminator: terminator,
582 fn needs_trailing_separator(
583 separator_tactic: SeparatorTactic,
584 list_tactic: DefinitiveListTactic,
586 match separator_tactic {
587 SeparatorTactic::Always => true,
588 SeparatorTactic::Vertical => list_tactic == DefinitiveListTactic::Vertical,
589 SeparatorTactic::Never => false,
593 /// Returns the count and total width of the list items.
594 fn calculate_width<I, T>(items: I) -> (usize, usize)
596 I: IntoIterator<Item = T>,
601 .map(|item| total_item_width(item.as_ref()))
602 .fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l))
605 fn total_item_width(item: &ListItem) -> usize {
606 comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..])) +
607 comment_len(item.post_comment.as_ref().map(|x| &(*x)[..])) +
608 item.item.as_ref().map_or(0, |str| str.len())
611 fn comment_len(comment: Option<&str>) -> usize {
614 let text_len = s.trim().len();
616 // We'll put " /*" before and " */" after inline comments.
626 // Compute horizontal and vertical shapes for a struct-lit-like thing.
627 pub fn struct_lit_shape(
629 context: &RewriteContext,
632 ) -> Option<(Option<Shape>, Shape)> {
633 let v_shape = match context.config.struct_lit_style() {
634 IndentStyle::Visual => try_opt!(
635 try_opt!(shape.visual_indent(0).shrink_left(prefix_width)).sub_width(suffix_width)
637 IndentStyle::Block => {
638 let shape = shape.block_indent(context.config.tab_spaces());
640 width: try_opt!(context.config.max_width().checked_sub(shape.indent.width())),
645 let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
646 if let Some(w) = shape_width {
647 let shape_width = cmp::min(w, context.config.struct_lit_width());
648 Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
650 Some((None, v_shape))
654 // Compute the tactic for the internals of a struct-lit-like thing.
655 pub fn struct_lit_tactic(
656 h_shape: Option<Shape>,
657 context: &RewriteContext,
659 ) -> DefinitiveListTactic {
660 if let Some(h_shape) = h_shape {
661 let prelim_tactic = match (context.config.struct_lit_style(), items.len()) {
662 (IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
663 _ => context.config.struct_lit_multiline_style().to_list_tactic(),
665 definitive_tactic(items, prelim_tactic, Separator::Comma, h_shape.width)
667 DefinitiveListTactic::Vertical
671 // Given a tactic and possible shapes for horizontal and vertical layout,
672 // come up with the actual shape to use.
673 pub fn shape_for_tactic(
674 tactic: DefinitiveListTactic,
675 h_shape: Option<Shape>,
679 DefinitiveListTactic::Horizontal => h_shape.unwrap(),
684 // Create a ListFormatting object for formatting the internals of a
685 // struct-lit-like thing, that is a series of fields.
686 pub fn struct_lit_formatting<'a>(
688 tactic: DefinitiveListTactic,
689 context: &'a RewriteContext,
690 force_no_trailing_comma: bool,
691 ) -> ListFormatting<'a> {
692 let ends_with_newline = context.config.struct_lit_style() != IndentStyle::Visual &&
693 tactic == DefinitiveListTactic::Vertical;
697 trailing_separator: if force_no_trailing_comma {
698 SeparatorTactic::Never
700 context.config.trailing_comma()
703 ends_with_newline: ends_with_newline,
704 preserve_newline: true,
705 config: context.config,