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;
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 is_multiline(&self) -> bool {
90 self.item.as_ref().map_or(false, |s| s.contains('\n')) || self.pre_comment.is_some() ||
93 .map_or(false, |s| s.contains('\n'))
96 pub fn has_comment(&self) -> bool {
99 .map_or(false, |comment| comment.starts_with("//")) ||
102 .map_or(false, |comment| comment.starts_with("//"))
105 pub fn from_str<S: Into<String>>(s: S) -> ListItem {
108 item: Some(s.into()),
115 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
116 /// The definitive formatting tactic for lists.
117 pub enum DefinitiveListTactic {
123 pub fn definitive_tactic<I, T>(items: I, tactic: ListTactic, width: usize) -> DefinitiveListTactic
125 I: IntoIterator<Item = T> + Clone,
128 let pre_line_comments = items
131 .any(|item| item.as_ref().has_comment());
133 let limit = match tactic {
134 _ if pre_line_comments => return DefinitiveListTactic::Vertical,
135 ListTactic::Mixed => return DefinitiveListTactic::Mixed,
136 ListTactic::Horizontal => return DefinitiveListTactic::Horizontal,
137 ListTactic::Vertical => return DefinitiveListTactic::Vertical,
138 ListTactic::LimitedHorizontalVertical(limit) => ::std::cmp::min(width, limit),
139 ListTactic::HorizontalVertical => width,
142 let (sep_count, total_width) = calculate_width(items.clone());
143 let sep_len = ", ".len(); // FIXME: make more generic?
144 let total_sep_len = sep_len * sep_count.checked_sub(1).unwrap_or(0);
145 let real_total = total_width + total_sep_len;
147 if real_total <= limit && !pre_line_comments &&
148 !items.into_iter().any(|item| item.as_ref().is_multiline())
150 DefinitiveListTactic::Horizontal
152 DefinitiveListTactic::Vertical
156 // Format a list of commented items into a string.
157 // TODO: add unit tests
158 pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
160 I: IntoIterator<Item = T>,
163 let tactic = formatting.tactic;
164 let sep_len = formatting.separator.len();
166 // Now that we know how we will layout, we can decide for sure if there
167 // will be a trailing separator.
168 let trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
169 let mut result = String::new();
170 let mut iter = items.into_iter().enumerate().peekable();
172 let mut line_len = 0;
173 let indent_str = &formatting.shape.indent.to_string(formatting.config);
174 while let Some((i, item)) = iter.next() {
175 let item = item.as_ref();
176 let inner_item = try_opt!(item.item.as_ref());
178 let last = iter.peek().is_none();
179 let separate = !last || trailing_separator;
180 let item_sep_len = if separate { sep_len } else { 0 };
182 // Item string may be multi-line. Its length (used for block comment alignment)
183 // should be only the length of the last line.
184 let item_last_line = if item.is_multiline() {
185 inner_item.lines().last().unwrap_or("")
189 let mut item_last_line_width = item_last_line.len() + item_sep_len;
190 if item_last_line.starts_with(indent_str) {
191 item_last_line_width -= indent_str.len();
195 DefinitiveListTactic::Horizontal if !first => {
198 DefinitiveListTactic::Vertical if !first => {
200 result.push_str(indent_str);
202 DefinitiveListTactic::Mixed => {
203 let total_width = total_item_width(item) + item_sep_len;
205 // 1 is space between separator and item.
206 if line_len > 0 && line_len + 1 + total_width > formatting.shape.width {
208 result.push_str(indent_str);
217 line_len += total_width;
223 if let Some(ref comment) = item.pre_comment {
224 // Block style in non-vertical mode.
225 let block_mode = tactic != DefinitiveListTactic::Vertical;
226 // Width restriction is only relevant in vertical mode.
227 let comment = try_opt!(rewrite_comment(
233 result.push_str(&comment);
235 if tactic == DefinitiveListTactic::Vertical {
237 result.push_str(indent_str);
243 result.push_str(&inner_item[..]);
246 if tactic != DefinitiveListTactic::Vertical && item.post_comment.is_some() {
247 let comment = item.post_comment.as_ref().unwrap();
248 let formatted_comment = try_opt!(rewrite_comment(
251 Shape::legacy(formatting.shape.width, Indent::empty()),
256 result.push_str(&formatted_comment);
260 result.push_str(formatting.separator);
263 if tactic == DefinitiveListTactic::Vertical && item.post_comment.is_some() {
264 // 1 = space between item and comment.
265 let width = formatting
268 .checked_sub(item_last_line_width + 1)
270 let mut offset = formatting.shape.indent;
271 offset.alignment += item_last_line_width + 1;
272 let comment = item.post_comment.as_ref().unwrap();
274 debug!("Width = {}, offset = {:?}", width, offset);
275 // Use block-style only for the last item or multiline comments.
276 let block_style = !formatting.ends_with_newline && last ||
277 comment.trim().contains('\n') ||
278 comment.trim().len() > width;
280 let formatted_comment = try_opt!(rewrite_comment(
283 Shape::legacy(width, offset),
287 if !formatted_comment.starts_with('\n') {
290 result.push_str(&formatted_comment);
293 if !last && tactic == DefinitiveListTactic::Vertical && item.new_lines {
301 pub struct ListItems<'a, I, F1, F2, F3>
305 codemap: &'a CodeMap,
310 prev_span_end: BytePos,
311 next_span_start: BytePos,
315 impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
317 I: Iterator<Item = T>,
318 F1: Fn(&T) -> BytePos,
319 F2: Fn(&T) -> BytePos,
320 F3: Fn(&T) -> Option<String>,
322 type Item = ListItem;
324 fn next(&mut self) -> Option<Self::Item> {
325 let white_space: &[_] = &[' ', '\t'];
327 self.inner.next().map(|item| {
328 let mut new_lines = false;
330 let pre_snippet = self.codemap
331 .span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
333 let trimmed_pre_snippet = pre_snippet.trim();
334 let has_pre_comment =
335 trimmed_pre_snippet.contains("//") || trimmed_pre_snippet.contains("/*");
336 let pre_comment = if has_pre_comment {
337 Some(trimmed_pre_snippet.to_owned())
343 let next_start = match self.inner.peek() {
344 Some(next_item) => (self.get_lo)(next_item),
345 None => self.next_span_start,
347 let post_snippet = self.codemap
348 .span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
351 let comment_end = match self.inner.peek() {
353 let mut block_open_index = post_snippet.find("/*");
354 // check if it realy is a block comment (and not //*)
355 if let Some(i) = block_open_index {
356 if i > 0 && &post_snippet[i - 1..i] == "/" {
357 block_open_index = None;
360 let newline_index = post_snippet.find('\n');
361 let separator_index = post_snippet.find_uncommented(",").unwrap();
363 match (block_open_index, newline_index) {
364 // Separator before comment, with the next item on same line.
365 // Comment belongs to next item.
366 (Some(i), None) if i > separator_index => separator_index + 1,
367 // Block-style post-comment before the separator.
370 find_comment_end(&post_snippet[i..]).unwrap() + i,
374 // Block-style post-comment. Either before or after the separator.
375 (Some(i), Some(j)) if i < j => {
377 find_comment_end(&post_snippet[i..]).unwrap() + i,
381 // Potential *single* line comment.
382 (_, Some(j)) if j > separator_index => j + 1,
383 _ => post_snippet.len(),
388 .find_uncommented(self.terminator)
389 .unwrap_or(post_snippet.len())
393 if !post_snippet.is_empty() && comment_end > 0 {
394 // Account for extra whitespace between items. This is fiddly
395 // because of the way we divide pre- and post- comments.
397 // Everything from the separator to the next item.
398 let test_snippet = &post_snippet[comment_end - 1..];
399 let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
400 // From the end of the first line of comments.
401 let test_snippet = &test_snippet[first_newline..];
402 let first = test_snippet
403 .find(|c: char| !c.is_whitespace())
404 .unwrap_or(test_snippet.len());
405 // From the end of the first line of comments to the next non-whitespace char.
406 let test_snippet = &test_snippet[..first];
408 if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
409 // There were multiple line breaks which got trimmed to nothing.
414 // Cleanup post-comment: strip separators and whitespace.
415 self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
416 let post_snippet = post_snippet[..comment_end].trim();
418 let post_snippet_trimmed = if post_snippet.starts_with(',') {
419 post_snippet[1..].trim_matches(white_space)
420 } else if post_snippet.ends_with(',') {
421 post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
426 let post_comment = if !post_snippet_trimmed.is_empty() {
427 Some(post_snippet_trimmed.to_owned())
433 pre_comment: pre_comment,
434 item: (self.get_item_string)(&item),
435 post_comment: post_comment,
436 new_lines: new_lines,
442 // Creates an iterator over a list's items with associated comments.
443 pub fn itemize_list<'a, T, I, F1, F2, F3>(
444 codemap: &'a CodeMap,
450 prev_span_end: BytePos,
451 next_span_start: BytePos,
452 ) -> ListItems<'a, I, F1, F2, F3>
454 I: Iterator<Item = T>,
455 F1: Fn(&T) -> BytePos,
456 F2: Fn(&T) -> BytePos,
457 F3: Fn(&T) -> Option<String>,
461 inner: inner.peekable(),
464 get_item_string: get_item_string,
465 prev_span_end: prev_span_end,
466 next_span_start: next_span_start,
467 terminator: terminator,
471 fn needs_trailing_separator(
472 separator_tactic: SeparatorTactic,
473 list_tactic: DefinitiveListTactic,
475 match separator_tactic {
476 SeparatorTactic::Always => true,
477 SeparatorTactic::Vertical => list_tactic == DefinitiveListTactic::Vertical,
478 SeparatorTactic::Never => false,
482 /// Returns the count and total width of the list items.
483 fn calculate_width<I, T>(items: I) -> (usize, usize)
485 I: IntoIterator<Item = T>,
490 .map(|item| total_item_width(item.as_ref()))
491 .fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l))
494 fn total_item_width(item: &ListItem) -> usize {
495 comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..])) +
496 comment_len(item.post_comment.as_ref().map(|x| &(*x)[..])) +
497 item.item.as_ref().map_or(0, |str| str.len())
500 fn comment_len(comment: Option<&str>) -> usize {
503 let text_len = s.trim().len();
505 // We'll put " /*" before and " */" after inline comments.
515 // Compute horizontal and vertical shapes for a struct-lit-like thing.
516 pub fn struct_lit_shape(
518 context: &RewriteContext,
521 ) -> Option<(Option<Shape>, Shape)> {
522 let v_shape = match context.config.struct_lit_style() {
523 IndentStyle::Visual => {
525 try_opt!(shape.visual_indent(0).shrink_left(prefix_width)).sub_width(suffix_width)
528 IndentStyle::Block => {
529 let shape = shape.block_indent(context.config.tab_spaces());
531 width: try_opt!(context.config.max_width().checked_sub(shape.indent.width())),
536 let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
537 if let Some(w) = shape_width {
538 let shape_width = cmp::min(w, context.config.struct_lit_width());
539 Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
541 Some((None, v_shape))
545 // Compute the tactic for the internals of a struct-lit-like thing.
546 pub fn struct_lit_tactic(
547 h_shape: Option<Shape>,
548 context: &RewriteContext,
550 ) -> DefinitiveListTactic {
551 if let Some(h_shape) = h_shape {
552 let prelim_tactic = match (context.config.struct_lit_style(), items.len()) {
553 (IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
554 _ => context.config.struct_lit_multiline_style().to_list_tactic(),
556 definitive_tactic(items, prelim_tactic, h_shape.width)
558 DefinitiveListTactic::Vertical
562 // Given a tactic and possible shapes for horizontal and vertical layout,
563 // come up with the actual shape to use.
564 pub fn shape_for_tactic(
565 tactic: DefinitiveListTactic,
566 h_shape: Option<Shape>,
570 DefinitiveListTactic::Horizontal => h_shape.unwrap(),
575 // Create a ListFormatting object for formatting the internals of a
576 // struct-lit-like thing, that is a series of fields.
577 pub fn struct_lit_formatting<'a>(
579 tactic: DefinitiveListTactic,
580 context: &'a RewriteContext,
581 force_no_trailing_comma: bool,
582 ) -> ListFormatting<'a> {
583 let ends_with_newline = context.config.struct_lit_style() != IndentStyle::Visual &&
584 tactic == DefinitiveListTactic::Vertical;
588 trailing_separator: if force_no_trailing_comma {
589 SeparatorTactic::Never
591 context.config.trailing_comma()
594 ends_with_newline: ends_with_newline,
595 config: context.config,