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::{CodeMap, BytePos};
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 pub fn definitive_tactic<I, T>(items: I, tactic: ListTactic, width: usize) -> DefinitiveListTactic
129 I: IntoIterator<Item = T> + Clone,
132 let pre_line_comments = items
135 .any(|item| item.as_ref().has_comment());
137 let limit = match tactic {
138 _ if pre_line_comments => return DefinitiveListTactic::Vertical,
139 ListTactic::Mixed => return DefinitiveListTactic::Mixed,
140 ListTactic::Horizontal => return DefinitiveListTactic::Horizontal,
141 ListTactic::Vertical => return DefinitiveListTactic::Vertical,
142 ListTactic::LimitedHorizontalVertical(limit) => ::std::cmp::min(width, limit),
143 ListTactic::HorizontalVertical => width,
146 let (sep_count, total_width) = calculate_width(items.clone());
147 let sep_len = ", ".len(); // FIXME: make more generic?
148 let total_sep_len = sep_len * sep_count.checked_sub(1).unwrap_or(0);
149 let real_total = total_width + total_sep_len;
151 if real_total <= limit && !pre_line_comments &&
152 !items.into_iter().any(|item| item.as_ref().is_multiline())
154 DefinitiveListTactic::Horizontal
156 DefinitiveListTactic::Vertical
160 // Format a list of commented items into a string.
161 // TODO: add unit tests
162 pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
164 I: IntoIterator<Item = T> + Clone,
167 let tactic = formatting.tactic;
168 let sep_len = formatting.separator.len();
170 // Now that we know how we will layout, we can decide for sure if there
171 // will be a trailing separator.
172 let trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
173 let mut result = String::new();
174 let cloned_items = items.clone();
175 let mut iter = items.into_iter().enumerate().peekable();
176 let mut item_max_width: Option<usize> = None;
178 let mut line_len = 0;
179 let indent_str = &formatting.shape.indent.to_string(formatting.config);
180 while let Some((i, item)) = iter.next() {
181 let item = item.as_ref();
182 let inner_item = try_opt!(item.item.as_ref());
184 let last = iter.peek().is_none();
185 let separate = !last || trailing_separator;
186 let item_sep_len = if separate { sep_len } else { 0 };
188 // Item string may be multi-line. Its length (used for block comment alignment)
189 // should be only the length of the last line.
190 let item_last_line = if item.is_multiline() {
191 inner_item.lines().last().unwrap_or("")
195 let mut item_last_line_width = item_last_line.len() + item_sep_len;
196 if item_last_line.starts_with(indent_str) {
197 item_last_line_width -= indent_str.len();
201 DefinitiveListTactic::Horizontal if !first => {
204 DefinitiveListTactic::Vertical if !first => {
206 result.push_str(indent_str);
208 DefinitiveListTactic::Mixed => {
209 let total_width = total_item_width(item) + item_sep_len;
211 // 1 is space between separator and item.
212 if line_len > 0 && line_len + 1 + total_width > formatting.shape.width {
214 result.push_str(indent_str);
223 line_len += total_width;
229 if let Some(ref comment) = item.pre_comment {
230 // Block style in non-vertical mode.
231 let block_mode = tactic != DefinitiveListTactic::Vertical;
232 // Width restriction is only relevant in vertical mode.
233 let comment = try_opt!(rewrite_comment(
239 result.push_str(&comment);
241 if tactic == DefinitiveListTactic::Vertical {
243 result.push_str(indent_str);
247 item_max_width = None;
250 result.push_str(&inner_item[..]);
253 if tactic != DefinitiveListTactic::Vertical && item.post_comment.is_some() {
254 let comment = item.post_comment.as_ref().unwrap();
255 let formatted_comment = try_opt!(rewrite_comment(
258 Shape::legacy(formatting.shape.width, Indent::empty()),
263 result.push_str(&formatted_comment);
267 result.push_str(formatting.separator);
270 if tactic == DefinitiveListTactic::Vertical && item.post_comment.is_some() {
271 let comment = item.post_comment.as_ref().unwrap();
272 let overhead = last_line_width(&result) + first_line_width(comment.trim());
274 let rewrite_post_comment = |item_max_width: &mut Option<usize>| {
275 if item_max_width.is_none() && !last && !inner_item.contains('\n') {
276 *item_max_width = Some(max_width_of_item_with_post_comment(
280 formatting.config.max_width(),
283 let overhead = if let &mut Some(max_width) = item_max_width {
286 // 1 = space between item and comment.
287 item_last_line_width + 1
289 let width = formatting.shape.width.checked_sub(overhead).unwrap_or(1);
290 let offset = formatting.shape.indent + overhead;
291 let comment_shape = Shape::legacy(width, offset);
293 // Use block-style only for the last item or multiline comments.
294 let block_style = !formatting.ends_with_newline && last ||
295 comment.trim().contains('\n') ||
296 comment.trim().len() > width;
298 rewrite_comment(comment, block_style, comment_shape, formatting.config)
301 let mut formatted_comment = try_opt!(rewrite_post_comment(&mut item_max_width));
303 if !formatted_comment.starts_with('\n') {
304 let mut comment_alignment =
305 post_comment_alignment(item_max_width, inner_item.len());
306 if first_line_width(&formatted_comment) + last_line_width(&result) +
307 comment_alignment + 1 > formatting.config.max_width()
309 item_max_width = None;
310 formatted_comment = try_opt!(rewrite_post_comment(&mut item_max_width));
311 comment_alignment = post_comment_alignment(item_max_width, inner_item.len());
313 for _ in 0..(comment_alignment + 1) {
316 // An additional space for the missing trailing comma
317 if last && item_max_width.is_some() && !separate {
321 if formatted_comment.contains('\n') {
322 item_max_width = None;
324 result.push_str(&formatted_comment);
326 item_max_width = None;
329 if !last && tactic == DefinitiveListTactic::Vertical && item.new_lines {
330 item_max_width = None;
338 fn max_width_of_item_with_post_comment<I, T>(
345 I: IntoIterator<Item = T> + Clone,
348 let mut max_width = 0;
349 let mut first = true;
350 for item in items.clone().into_iter().skip(i) {
351 let item = item.as_ref();
352 let inner_item_width = item.inner_as_ref().len();
354 (item.is_multiline() || !item.post_comment.is_some() ||
355 inner_item_width + overhead > max_budget)
359 if max_width < inner_item_width {
360 max_width = inner_item_width;
370 fn post_comment_alignment(item_max_width: Option<usize>, inner_item_len: usize) -> usize {
372 .and_then(|max_line_width| max_line_width.checked_sub(inner_item_len))
376 pub struct ListItems<'a, I, F1, F2, F3>
380 codemap: &'a CodeMap,
385 prev_span_end: BytePos,
386 next_span_start: BytePos,
390 impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
392 I: Iterator<Item = T>,
393 F1: Fn(&T) -> BytePos,
394 F2: Fn(&T) -> BytePos,
395 F3: Fn(&T) -> Option<String>,
397 type Item = ListItem;
399 fn next(&mut self) -> Option<Self::Item> {
400 let white_space: &[_] = &[' ', '\t'];
402 self.inner.next().map(|item| {
403 let mut new_lines = false;
405 let pre_snippet = self.codemap
406 .span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
408 let trimmed_pre_snippet = pre_snippet.trim();
409 let has_pre_comment =
410 trimmed_pre_snippet.contains("//") || trimmed_pre_snippet.contains("/*");
411 let pre_comment = if has_pre_comment {
412 Some(trimmed_pre_snippet.to_owned())
418 let next_start = match self.inner.peek() {
419 Some(next_item) => (self.get_lo)(next_item),
420 None => self.next_span_start,
422 let post_snippet = self.codemap
423 .span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
426 let comment_end = match self.inner.peek() {
428 let mut block_open_index = post_snippet.find("/*");
429 // check if it realy is a block comment (and not //*)
430 if let Some(i) = block_open_index {
431 if i > 0 && &post_snippet[i - 1..i] == "/" {
432 block_open_index = None;
435 let newline_index = post_snippet.find('\n');
436 let separator_index = post_snippet.find_uncommented(",").unwrap();
438 match (block_open_index, newline_index) {
439 // Separator before comment, with the next item on same line.
440 // Comment belongs to next item.
441 (Some(i), None) if i > separator_index => separator_index + 1,
442 // Block-style post-comment before the separator.
443 (Some(i), None) => cmp::max(
444 find_comment_end(&post_snippet[i..]).unwrap() + i,
447 // Block-style post-comment. Either before or after the separator.
448 (Some(i), Some(j)) if i < j => cmp::max(
449 find_comment_end(&post_snippet[i..]).unwrap() + i,
452 // Potential *single* line comment.
453 (_, Some(j)) if j > separator_index => j + 1,
454 _ => post_snippet.len(),
458 .find_uncommented(self.terminator)
459 .unwrap_or(post_snippet.len()),
462 if !post_snippet.is_empty() && comment_end > 0 {
463 // Account for extra whitespace between items. This is fiddly
464 // because of the way we divide pre- and post- comments.
466 // Everything from the separator to the next item.
467 let test_snippet = &post_snippet[comment_end - 1..];
468 let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
469 // From the end of the first line of comments.
470 let test_snippet = &test_snippet[first_newline..];
471 let first = test_snippet
472 .find(|c: char| !c.is_whitespace())
473 .unwrap_or(test_snippet.len());
474 // From the end of the first line of comments to the next non-whitespace char.
475 let test_snippet = &test_snippet[..first];
477 if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
478 // There were multiple line breaks which got trimmed to nothing.
483 // Cleanup post-comment: strip separators and whitespace.
484 self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
485 let post_snippet = post_snippet[..comment_end].trim();
487 let post_snippet_trimmed = if post_snippet.starts_with(',') {
488 post_snippet[1..].trim_matches(white_space)
489 } else if post_snippet.ends_with(',') {
490 post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
495 let post_comment = if !post_snippet_trimmed.is_empty() {
496 Some(post_snippet_trimmed.to_owned())
502 pre_comment: pre_comment,
503 item: (self.get_item_string)(&item),
504 post_comment: post_comment,
505 new_lines: new_lines,
511 // Creates an iterator over a list's items with associated comments.
512 pub fn itemize_list<'a, T, I, F1, F2, F3>(
513 codemap: &'a CodeMap,
519 prev_span_end: BytePos,
520 next_span_start: BytePos,
521 ) -> ListItems<'a, I, F1, F2, F3>
523 I: Iterator<Item = T>,
524 F1: Fn(&T) -> BytePos,
525 F2: Fn(&T) -> BytePos,
526 F3: Fn(&T) -> Option<String>,
530 inner: inner.peekable(),
533 get_item_string: get_item_string,
534 prev_span_end: prev_span_end,
535 next_span_start: next_span_start,
536 terminator: terminator,
540 fn needs_trailing_separator(
541 separator_tactic: SeparatorTactic,
542 list_tactic: DefinitiveListTactic,
544 match separator_tactic {
545 SeparatorTactic::Always => true,
546 SeparatorTactic::Vertical => list_tactic == DefinitiveListTactic::Vertical,
547 SeparatorTactic::Never => false,
551 /// Returns the count and total width of the list items.
552 fn calculate_width<I, T>(items: I) -> (usize, usize)
554 I: IntoIterator<Item = T>,
559 .map(|item| total_item_width(item.as_ref()))
560 .fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l))
563 fn total_item_width(item: &ListItem) -> usize {
564 comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..])) +
565 comment_len(item.post_comment.as_ref().map(|x| &(*x)[..])) +
566 item.item.as_ref().map_or(0, |str| str.len())
569 fn comment_len(comment: Option<&str>) -> usize {
572 let text_len = s.trim().len();
574 // We'll put " /*" before and " */" after inline comments.
584 // Compute horizontal and vertical shapes for a struct-lit-like thing.
585 pub fn struct_lit_shape(
587 context: &RewriteContext,
590 ) -> Option<(Option<Shape>, Shape)> {
591 let v_shape = match context.config.struct_lit_style() {
592 IndentStyle::Visual => try_opt!(
593 try_opt!(shape.visual_indent(0).shrink_left(prefix_width)).sub_width(suffix_width)
595 IndentStyle::Block => {
596 let shape = shape.block_indent(context.config.tab_spaces());
598 width: try_opt!(context.config.max_width().checked_sub(shape.indent.width())),
603 let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
604 if let Some(w) = shape_width {
605 let shape_width = cmp::min(w, context.config.struct_lit_width());
606 Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
608 Some((None, v_shape))
612 // Compute the tactic for the internals of a struct-lit-like thing.
613 pub fn struct_lit_tactic(
614 h_shape: Option<Shape>,
615 context: &RewriteContext,
617 ) -> DefinitiveListTactic {
618 if let Some(h_shape) = h_shape {
619 let prelim_tactic = match (context.config.struct_lit_style(), items.len()) {
620 (IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
621 _ => context.config.struct_lit_multiline_style().to_list_tactic(),
623 definitive_tactic(items, prelim_tactic, h_shape.width)
625 DefinitiveListTactic::Vertical
629 // Given a tactic and possible shapes for horizontal and vertical layout,
630 // come up with the actual shape to use.
631 pub fn shape_for_tactic(
632 tactic: DefinitiveListTactic,
633 h_shape: Option<Shape>,
637 DefinitiveListTactic::Horizontal => h_shape.unwrap(),
642 // Create a ListFormatting object for formatting the internals of a
643 // struct-lit-like thing, that is a series of fields.
644 pub fn struct_lit_formatting<'a>(
646 tactic: DefinitiveListTactic,
647 context: &'a RewriteContext,
648 force_no_trailing_comma: bool,
649 ) -> ListFormatting<'a> {
650 let ends_with_newline = context.config.struct_lit_style() != IndentStyle::Visual &&
651 tactic == DefinitiveListTactic::Vertical;
655 trailing_separator: if force_no_trailing_comma {
656 SeparatorTactic::Never
658 context.config.trailing_comma()
661 ends_with_newline: ends_with_newline,
662 config: context.config,