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 pub config: &'a Config,
71 impl AsRef<ListItem> for ListItem {
72 fn as_ref(&self) -> &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
82 pub item: Option<String>,
83 pub post_comment: Option<String>,
84 // Whether there is extra whitespace before this item.
89 pub fn inner_as_ref(&self) -> &str {
90 self.item.as_ref().map_or("", |s| &*s)
93 pub fn is_multiline(&self) -> bool {
94 self.inner_as_ref().contains('\n') || self.pre_comment.is_some() ||
97 .map_or(false, |s| s.contains('\n'))
100 pub fn has_comment(&self) -> bool {
103 .map_or(false, |comment| comment.starts_with("//")) ||
106 .map_or(false, |comment| comment.starts_with("//"))
109 pub fn from_str<S: Into<String>>(s: S) -> ListItem {
112 item: Some(s.into()),
119 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
120 /// The definitive formatting tactic for lists.
121 pub enum DefinitiveListTactic {
127 impl DefinitiveListTactic {
128 pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool {
130 IndentStyle::Block => *self != DefinitiveListTactic::Horizontal,
131 IndentStyle::Visual => false,
136 pub fn definitive_tactic<I, T>(items: I, tactic: ListTactic, width: usize) -> DefinitiveListTactic
138 I: IntoIterator<Item = T> + Clone,
141 let pre_line_comments = items
144 .any(|item| item.as_ref().has_comment());
146 let limit = match tactic {
147 _ if pre_line_comments => return DefinitiveListTactic::Vertical,
148 ListTactic::Mixed => return DefinitiveListTactic::Mixed,
149 ListTactic::Horizontal => return DefinitiveListTactic::Horizontal,
150 ListTactic::Vertical => return DefinitiveListTactic::Vertical,
151 ListTactic::LimitedHorizontalVertical(limit) => ::std::cmp::min(width, limit),
152 ListTactic::HorizontalVertical => width,
155 let (sep_count, total_width) = calculate_width(items.clone());
156 let sep_len = ", ".len(); // FIXME: make more generic?
157 let total_sep_len = sep_len * sep_count.checked_sub(1).unwrap_or(0);
158 let real_total = total_width + total_sep_len;
160 if real_total <= limit && !pre_line_comments &&
161 !items.into_iter().any(|item| item.as_ref().is_multiline())
163 DefinitiveListTactic::Horizontal
165 DefinitiveListTactic::Vertical
169 // Format a list of commented items into a string.
170 // TODO: add unit tests
171 pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
173 I: IntoIterator<Item = T> + Clone,
176 let tactic = formatting.tactic;
177 let sep_len = formatting.separator.len();
179 // Now that we know how we will layout, we can decide for sure if there
180 // will be a trailing separator.
181 let mut trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
182 let mut result = String::new();
183 let cloned_items = items.clone();
184 let mut iter = items.into_iter().enumerate().peekable();
185 let mut item_max_width: Option<usize> = None;
187 let mut line_len = 0;
188 let indent_str = &formatting.shape.indent.to_string(formatting.config);
189 while let Some((i, item)) = iter.next() {
190 let item = item.as_ref();
191 let inner_item = try_opt!(item.item.as_ref());
193 let last = iter.peek().is_none();
194 let mut separate = !last || trailing_separator;
195 let item_sep_len = if separate { sep_len } else { 0 };
197 // Item string may be multi-line. Its length (used for block comment alignment)
198 // should be only the length of the last line.
199 let item_last_line = if item.is_multiline() {
200 inner_item.lines().last().unwrap_or("")
204 let mut item_last_line_width = item_last_line.len() + item_sep_len;
205 if item_last_line.starts_with(indent_str) {
206 item_last_line_width -= indent_str.len();
210 DefinitiveListTactic::Horizontal if !first => {
213 DefinitiveListTactic::Vertical if !first => {
215 result.push_str(indent_str);
217 DefinitiveListTactic::Mixed => {
218 let total_width = total_item_width(item) + item_sep_len;
220 // 1 is space between separator and item.
221 if line_len > 0 && line_len + 1 + total_width > formatting.shape.width {
223 result.push_str(indent_str);
225 if tactic == DefinitiveListTactic::Mixed && formatting.ends_with_newline {
229 trailing_separator = true;
239 line_len += total_width;
245 if let Some(ref comment) = item.pre_comment {
246 // Block style in non-vertical mode.
247 let block_mode = tactic != DefinitiveListTactic::Vertical;
248 // Width restriction is only relevant in vertical mode.
249 let comment = try_opt!(rewrite_comment(
255 result.push_str(&comment);
257 if tactic == DefinitiveListTactic::Vertical {
259 result.push_str(indent_str);
263 item_max_width = None;
266 result.push_str(&inner_item[..]);
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(
274 Shape::legacy(formatting.shape.width, Indent::empty()),
279 result.push_str(&formatted_comment);
283 result.push_str(formatting.separator);
286 if tactic == DefinitiveListTactic::Vertical && item.post_comment.is_some() {
287 let comment = item.post_comment.as_ref().unwrap();
288 let overhead = last_line_width(&result) + first_line_width(comment.trim());
290 let rewrite_post_comment = |item_max_width: &mut Option<usize>| {
291 if item_max_width.is_none() && !last && !inner_item.contains('\n') {
292 *item_max_width = Some(max_width_of_item_with_post_comment(
296 formatting.config.max_width(),
299 let overhead = if let &mut Some(max_width) = item_max_width {
302 // 1 = space between item and comment.
303 item_last_line_width + 1
305 let width = formatting.shape.width.checked_sub(overhead).unwrap_or(1);
306 let offset = formatting.shape.indent + overhead;
307 let comment_shape = Shape::legacy(width, offset);
309 // Use block-style only for the last item or multiline comments.
310 let block_style = !formatting.ends_with_newline && last ||
311 comment.trim().contains('\n') ||
312 comment.trim().len() > width;
314 rewrite_comment(comment, block_style, comment_shape, formatting.config)
317 let mut formatted_comment = try_opt!(rewrite_post_comment(&mut item_max_width));
319 if !formatted_comment.starts_with('\n') {
320 let mut comment_alignment =
321 post_comment_alignment(item_max_width, inner_item.len());
322 if first_line_width(&formatted_comment) + last_line_width(&result) +
323 comment_alignment + 1 > formatting.config.max_width()
325 item_max_width = None;
326 formatted_comment = try_opt!(rewrite_post_comment(&mut item_max_width));
327 comment_alignment = post_comment_alignment(item_max_width, inner_item.len());
329 for _ in 0..(comment_alignment + 1) {
332 // An additional space for the missing trailing comma
333 if last && item_max_width.is_some() && !separate {
337 if formatted_comment.contains('\n') {
338 item_max_width = None;
340 result.push_str(&formatted_comment);
342 item_max_width = None;
345 if !last && tactic == DefinitiveListTactic::Vertical && item.new_lines {
346 item_max_width = None;
354 fn max_width_of_item_with_post_comment<I, T>(
361 I: IntoIterator<Item = T> + Clone,
364 let mut max_width = 0;
365 let mut first = true;
366 for item in items.clone().into_iter().skip(i) {
367 let item = item.as_ref();
368 let inner_item_width = item.inner_as_ref().len();
370 (item.is_multiline() || !item.post_comment.is_some() ||
371 inner_item_width + overhead > max_budget)
375 if max_width < inner_item_width {
376 max_width = inner_item_width;
386 fn post_comment_alignment(item_max_width: Option<usize>, inner_item_len: usize) -> usize {
388 .and_then(|max_line_width| max_line_width.checked_sub(inner_item_len))
392 pub struct ListItems<'a, I, F1, F2, F3>
396 codemap: &'a CodeMap,
401 prev_span_end: BytePos,
402 next_span_start: BytePos,
406 impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
408 I: Iterator<Item = T>,
409 F1: Fn(&T) -> BytePos,
410 F2: Fn(&T) -> BytePos,
411 F3: Fn(&T) -> Option<String>,
413 type Item = ListItem;
415 fn next(&mut self) -> Option<Self::Item> {
416 let white_space: &[_] = &[' ', '\t'];
418 self.inner.next().map(|item| {
419 let mut new_lines = false;
421 let pre_snippet = self.codemap
422 .span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
424 let trimmed_pre_snippet = pre_snippet.trim();
425 let has_pre_comment =
426 trimmed_pre_snippet.contains("//") || trimmed_pre_snippet.contains("/*");
427 let pre_comment = if has_pre_comment {
428 Some(trimmed_pre_snippet.to_owned())
434 let next_start = match self.inner.peek() {
435 Some(next_item) => (self.get_lo)(next_item),
436 None => self.next_span_start,
438 let post_snippet = self.codemap
439 .span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
442 let comment_end = match self.inner.peek() {
444 let mut block_open_index = post_snippet.find("/*");
445 // check if it realy is a block comment (and not //*)
446 if let Some(i) = block_open_index {
447 if i > 0 && &post_snippet[i - 1..i] == "/" {
448 block_open_index = None;
451 let newline_index = post_snippet.find('\n');
452 let separator_index = post_snippet.find_uncommented(",").unwrap();
454 match (block_open_index, newline_index) {
455 // Separator before comment, with the next item on same line.
456 // Comment belongs to next item.
457 (Some(i), None) if i > separator_index => separator_index + 1,
458 // Block-style post-comment before the separator.
459 (Some(i), None) => cmp::max(
460 find_comment_end(&post_snippet[i..]).unwrap() + i,
463 // Block-style post-comment. Either before or after the separator.
464 (Some(i), Some(j)) if i < j => cmp::max(
465 find_comment_end(&post_snippet[i..]).unwrap() + i,
468 // Potential *single* line comment.
469 (_, Some(j)) if j > separator_index => j + 1,
470 _ => post_snippet.len(),
474 .find_uncommented(self.terminator)
475 .unwrap_or(post_snippet.len()),
478 if !post_snippet.is_empty() && comment_end > 0 {
479 // Account for extra whitespace between items. This is fiddly
480 // because of the way we divide pre- and post- comments.
482 // Everything from the separator to the next item.
483 let test_snippet = &post_snippet[comment_end - 1..];
484 let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
485 // From the end of the first line of comments.
486 let test_snippet = &test_snippet[first_newline..];
487 let first = test_snippet
488 .find(|c: char| !c.is_whitespace())
489 .unwrap_or(test_snippet.len());
490 // From the end of the first line of comments to the next non-whitespace char.
491 let test_snippet = &test_snippet[..first];
493 if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
494 // There were multiple line breaks which got trimmed to nothing.
499 // Cleanup post-comment: strip separators and whitespace.
500 self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
501 let post_snippet = post_snippet[..comment_end].trim();
503 let post_snippet_trimmed = if post_snippet.starts_with(',') {
504 post_snippet[1..].trim_matches(white_space)
505 } else if post_snippet.ends_with(',') {
506 post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
511 let post_comment = if !post_snippet_trimmed.is_empty() {
512 Some(post_snippet_trimmed.to_owned())
518 pre_comment: pre_comment,
519 item: (self.get_item_string)(&item),
520 post_comment: post_comment,
521 new_lines: new_lines,
527 // Creates an iterator over a list's items with associated comments.
528 pub fn itemize_list<'a, T, I, F1, F2, F3>(
529 codemap: &'a CodeMap,
535 prev_span_end: BytePos,
536 next_span_start: BytePos,
537 ) -> ListItems<'a, I, F1, F2, F3>
539 I: Iterator<Item = T>,
540 F1: Fn(&T) -> BytePos,
541 F2: Fn(&T) -> BytePos,
542 F3: Fn(&T) -> Option<String>,
546 inner: inner.peekable(),
549 get_item_string: get_item_string,
550 prev_span_end: prev_span_end,
551 next_span_start: next_span_start,
552 terminator: terminator,
556 fn needs_trailing_separator(
557 separator_tactic: SeparatorTactic,
558 list_tactic: DefinitiveListTactic,
560 match separator_tactic {
561 SeparatorTactic::Always => true,
562 SeparatorTactic::Vertical => list_tactic == DefinitiveListTactic::Vertical,
563 SeparatorTactic::Never => false,
567 /// Returns the count and total width of the list items.
568 fn calculate_width<I, T>(items: I) -> (usize, usize)
570 I: IntoIterator<Item = T>,
575 .map(|item| total_item_width(item.as_ref()))
576 .fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l))
579 fn total_item_width(item: &ListItem) -> usize {
580 comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..])) +
581 comment_len(item.post_comment.as_ref().map(|x| &(*x)[..])) +
582 item.item.as_ref().map_or(0, |str| str.len())
585 fn comment_len(comment: Option<&str>) -> usize {
588 let text_len = s.trim().len();
590 // We'll put " /*" before and " */" after inline comments.
600 // Compute horizontal and vertical shapes for a struct-lit-like thing.
601 pub fn struct_lit_shape(
603 context: &RewriteContext,
606 ) -> Option<(Option<Shape>, Shape)> {
607 let v_shape = match context.config.struct_lit_style() {
608 IndentStyle::Visual => try_opt!(
609 try_opt!(shape.visual_indent(0).shrink_left(prefix_width)).sub_width(suffix_width)
611 IndentStyle::Block => {
612 let shape = shape.block_indent(context.config.tab_spaces());
614 width: try_opt!(context.config.max_width().checked_sub(shape.indent.width())),
619 let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
620 if let Some(w) = shape_width {
621 let shape_width = cmp::min(w, context.config.struct_lit_width());
622 Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
624 Some((None, v_shape))
628 // Compute the tactic for the internals of a struct-lit-like thing.
629 pub fn struct_lit_tactic(
630 h_shape: Option<Shape>,
631 context: &RewriteContext,
633 ) -> DefinitiveListTactic {
634 if let Some(h_shape) = h_shape {
635 let prelim_tactic = match (context.config.struct_lit_style(), items.len()) {
636 (IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
637 _ => context.config.struct_lit_multiline_style().to_list_tactic(),
639 definitive_tactic(items, prelim_tactic, h_shape.width)
641 DefinitiveListTactic::Vertical
645 // Given a tactic and possible shapes for horizontal and vertical layout,
646 // come up with the actual shape to use.
647 pub fn shape_for_tactic(
648 tactic: DefinitiveListTactic,
649 h_shape: Option<Shape>,
653 DefinitiveListTactic::Horizontal => h_shape.unwrap(),
658 // Create a ListFormatting object for formatting the internals of a
659 // struct-lit-like thing, that is a series of fields.
660 pub fn struct_lit_formatting<'a>(
662 tactic: DefinitiveListTactic,
663 context: &'a RewriteContext,
664 force_no_trailing_comma: bool,
665 ) -> ListFormatting<'a> {
666 let ends_with_newline = context.config.struct_lit_style() != IndentStyle::Visual &&
667 tactic == DefinitiveListTactic::Vertical;
671 trailing_separator: if force_no_trailing_comma {
672 SeparatorTactic::Never
674 context.config.trailing_comma()
677 ends_with_newline: ends_with_newline,
678 config: context.config,