2015-04-20 23:47:15 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-02-07 07:48:05 -06:00
|
|
|
//! Format list-like expressions and items.
|
|
|
|
|
2015-06-23 08:58:58 -05:00
|
|
|
use std::cmp;
|
2015-08-19 11:12:05 -05:00
|
|
|
use std::iter::Peekable;
|
2015-06-23 08:58:58 -05:00
|
|
|
|
2018-02-07 07:48:05 -06:00
|
|
|
use config::lists::*;
|
2018-02-18 21:41:43 -06:00
|
|
|
use syntax::codemap::BytePos;
|
2015-06-23 08:58:58 -05:00
|
|
|
|
2017-06-24 05:46:36 -05:00
|
|
|
use comment::{find_comment_end, rewrite_comment, FindUncommented};
|
2017-03-20 17:23:59 -05:00
|
|
|
use config::{Config, IndentStyle};
|
|
|
|
use rewrite::RewriteContext;
|
2017-09-17 01:23:25 -05:00
|
|
|
use shape::{Indent, Shape};
|
2017-12-05 00:17:40 -06:00
|
|
|
use utils::{count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline};
|
2018-02-18 21:41:43 -06:00
|
|
|
use visitor::SnippetProvider;
|
2015-04-20 23:47:15 -05:00
|
|
|
|
|
|
|
pub struct ListFormatting<'a> {
|
2015-10-02 06:50:24 -05:00
|
|
|
pub tactic: DefinitiveListTactic,
|
2015-04-20 23:47:15 -05:00
|
|
|
pub separator: &'a str,
|
|
|
|
pub trailing_separator: SeparatorTactic,
|
2017-08-18 09:19:47 -05:00
|
|
|
pub separator_place: SeparatorPlace,
|
2017-01-30 13:28:48 -06:00
|
|
|
pub shape: Shape,
|
2015-06-23 08:58:58 -05:00
|
|
|
// Non-expressions, e.g. items, will have a new line at the end of the list.
|
|
|
|
// Important for comment styles.
|
2015-07-03 04:13:28 -05:00
|
|
|
pub ends_with_newline: bool,
|
2017-07-26 08:43:36 -05:00
|
|
|
// Remove newlines between list elements for expressions.
|
|
|
|
pub preserve_newline: bool,
|
2015-09-06 00:39:28 -05:00
|
|
|
pub config: &'a Config,
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
|
2017-08-18 09:19:47 -05:00
|
|
|
impl<'a> ListFormatting<'a> {
|
|
|
|
pub fn needs_trailing_separator(&self) -> bool {
|
|
|
|
match self.trailing_separator {
|
|
|
|
// We always put separator in front.
|
|
|
|
SeparatorTactic::Always => true,
|
|
|
|
SeparatorTactic::Vertical => self.tactic == DefinitiveListTactic::Vertical,
|
|
|
|
SeparatorTactic::Never => {
|
|
|
|
self.tactic == DefinitiveListTactic::Vertical && self.separator_place.is_front()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 06:50:24 -05:00
|
|
|
impl AsRef<ListItem> for ListItem {
|
|
|
|
fn as_ref(&self) -> &ListItem {
|
|
|
|
self
|
2015-08-19 15:39:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 04:24:06 -05:00
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
pub enum ListItemCommentStyle {
|
|
|
|
// Try to keep the comment on the same line with the item.
|
|
|
|
SameLine,
|
|
|
|
// Put the comment on the previous or the next line of the item.
|
|
|
|
DifferentLine,
|
|
|
|
// No comment available.
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2015-06-23 08:58:58 -05:00
|
|
|
pub struct ListItem {
|
2015-10-04 13:20:15 -05:00
|
|
|
// None for comments mean that they are not present.
|
2015-06-23 08:58:58 -05:00
|
|
|
pub pre_comment: Option<String>,
|
2017-08-08 04:24:06 -05:00
|
|
|
pub pre_comment_style: ListItemCommentStyle,
|
2015-10-06 15:13:14 -05:00
|
|
|
// Item should include attributes and doc comments. None indicates a failed
|
2015-10-04 13:20:15 -05:00
|
|
|
// rewrite.
|
|
|
|
pub item: Option<String>,
|
2015-07-03 04:13:28 -05:00
|
|
|
pub post_comment: Option<String>,
|
2015-09-01 21:11:19 -05:00
|
|
|
// Whether there is extra whitespace before this item.
|
|
|
|
pub new_lines: bool,
|
2015-04-20 23:47:15 -05:00
|
|
|
}
|
|
|
|
|
2015-06-23 08:58:58 -05:00
|
|
|
impl ListItem {
|
2017-07-05 11:02:37 -05:00
|
|
|
pub fn inner_as_ref(&self) -> &str {
|
2017-09-15 03:09:30 -05:00
|
|
|
self.item.as_ref().map_or("", |s| s)
|
2017-07-05 11:02:37 -05:00
|
|
|
}
|
|
|
|
|
2017-08-27 00:41:15 -05:00
|
|
|
pub fn is_different_group(&self) -> bool {
|
2017-09-14 22:10:58 -05:00
|
|
|
self.inner_as_ref().contains('\n') || self.pre_comment.is_some()
|
|
|
|
|| self.post_comment
|
2017-06-17 02:56:54 -05:00
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |s| s.contains('\n'))
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
|
2017-08-27 00:41:15 -05:00
|
|
|
pub fn is_multiline(&self) -> bool {
|
2017-09-14 22:10:58 -05:00
|
|
|
self.inner_as_ref().contains('\n')
|
|
|
|
|| self.pre_comment
|
2017-08-27 00:41:15 -05:00
|
|
|
.as_ref()
|
2017-09-14 22:10:58 -05:00
|
|
|
.map_or(false, |s| s.contains('\n'))
|
|
|
|
|| self.post_comment
|
2017-08-27 00:41:15 -05:00
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |s| s.contains('\n'))
|
|
|
|
}
|
|
|
|
|
2017-06-24 05:46:36 -05:00
|
|
|
pub fn has_comment(&self) -> bool {
|
2017-06-17 02:56:54 -05:00
|
|
|
self.pre_comment
|
|
|
|
.as_ref()
|
2017-11-22 21:37:12 -06:00
|
|
|
.map_or(false, |comment| comment.trim_left().starts_with("//"))
|
2017-09-14 22:10:58 -05:00
|
|
|
|| self.post_comment
|
2017-06-24 05:46:36 -05:00
|
|
|
.as_ref()
|
2017-11-22 21:37:12 -06:00
|
|
|
.map_or(false, |comment| comment.trim_left().starts_with("//"))
|
2015-06-25 20:29:54 -05:00
|
|
|
}
|
|
|
|
|
2015-06-23 08:58:58 -05:00
|
|
|
pub fn from_str<S: Into<String>>(s: S) -> ListItem {
|
2015-09-26 01:29:48 -05:00
|
|
|
ListItem {
|
|
|
|
pre_comment: None,
|
2017-08-08 04:24:06 -05:00
|
|
|
pre_comment_style: ListItemCommentStyle::None,
|
2015-10-04 13:20:15 -05:00
|
|
|
item: Some(s.into()),
|
2015-09-26 01:29:48 -05:00
|
|
|
post_comment: None,
|
|
|
|
new_lines: false,
|
|
|
|
}
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 02:23:42 -05:00
|
|
|
/// The type of separator for lists.
|
2017-08-29 08:16:04 -05:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
2017-07-31 02:23:42 -05:00
|
|
|
pub enum Separator {
|
|
|
|
Comma,
|
|
|
|
VerticalBar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Separator {
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
match *self {
|
|
|
|
// 2 = `, `
|
|
|
|
Separator::Comma => 2,
|
|
|
|
// 3 = ` | `
|
|
|
|
Separator::VerticalBar => 3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 08:12:18 -05:00
|
|
|
pub fn definitive_tactic<I, T>(
|
|
|
|
items: I,
|
|
|
|
tactic: ListTactic,
|
2017-07-31 02:23:42 -05:00
|
|
|
sep: Separator,
|
2017-07-29 08:12:18 -05:00
|
|
|
width: usize,
|
|
|
|
) -> DefinitiveListTactic
|
2017-06-11 22:58:58 -05:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = T> + Clone,
|
|
|
|
T: AsRef<ListItem>,
|
2015-10-02 06:50:24 -05:00
|
|
|
{
|
2017-06-15 18:49:49 -05:00
|
|
|
let pre_line_comments = items
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
2017-06-24 05:46:36 -05:00
|
|
|
.any(|item| item.as_ref().has_comment());
|
2015-10-02 06:50:24 -05:00
|
|
|
|
|
|
|
let limit = match tactic {
|
|
|
|
_ if pre_line_comments => return DefinitiveListTactic::Vertical,
|
|
|
|
ListTactic::Mixed => return DefinitiveListTactic::Mixed,
|
|
|
|
ListTactic::Horizontal => return DefinitiveListTactic::Horizontal,
|
|
|
|
ListTactic::Vertical => return DefinitiveListTactic::Vertical,
|
|
|
|
ListTactic::LimitedHorizontalVertical(limit) => ::std::cmp::min(width, limit),
|
|
|
|
ListTactic::HorizontalVertical => width,
|
2015-04-20 23:47:15 -05:00
|
|
|
};
|
2015-05-04 10:16:51 -05:00
|
|
|
|
2015-10-02 06:50:24 -05:00
|
|
|
let (sep_count, total_width) = calculate_width(items.clone());
|
2017-07-31 02:23:42 -05:00
|
|
|
let total_sep_len = sep.len() * sep_count.checked_sub(1).unwrap_or(0);
|
2015-10-02 06:50:24 -05:00
|
|
|
let real_total = total_width + total_sep_len;
|
2015-04-20 23:47:15 -05:00
|
|
|
|
2017-09-14 22:10:58 -05:00
|
|
|
if real_total <= limit && !pre_line_comments
|
|
|
|
&& !items.into_iter().any(|item| item.as_ref().is_multiline())
|
2017-06-11 22:58:58 -05:00
|
|
|
{
|
2015-10-02 06:50:24 -05:00
|
|
|
DefinitiveListTactic::Horizontal
|
|
|
|
} else {
|
|
|
|
DefinitiveListTactic::Vertical
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
2015-10-02 06:50:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format a list of commented items into a string.
|
|
|
|
// TODO: add unit tests
|
2015-11-25 00:39:15 -06:00
|
|
|
pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
|
2017-06-11 22:58:58 -05:00
|
|
|
where
|
2017-07-05 09:12:58 -05:00
|
|
|
I: IntoIterator<Item = T> + Clone,
|
2017-06-11 22:58:58 -05:00
|
|
|
T: AsRef<ListItem>,
|
2015-10-02 06:50:24 -05:00
|
|
|
{
|
2017-12-22 19:06:17 -06:00
|
|
|
let tactic = formatting.tactic;
|
2015-10-02 06:50:24 -05:00
|
|
|
let sep_len = formatting.separator.len();
|
2015-06-23 08:58:58 -05:00
|
|
|
|
2015-04-20 23:47:15 -05:00
|
|
|
// Now that we know how we will layout, we can decide for sure if there
|
|
|
|
// will be a trailing separator.
|
2017-08-18 09:19:47 -05:00
|
|
|
let mut trailing_separator = formatting.needs_trailing_separator();
|
2017-09-04 02:15:32 -05:00
|
|
|
let mut result = String::with_capacity(128);
|
2017-07-05 11:02:37 -05:00
|
|
|
let cloned_items = items.clone();
|
2015-10-02 06:50:24 -05:00
|
|
|
let mut iter = items.into_iter().enumerate().peekable();
|
2017-07-05 11:02:37 -05:00
|
|
|
let mut item_max_width: Option<usize> = None;
|
2017-11-16 02:39:38 -06:00
|
|
|
let sep_place =
|
|
|
|
SeparatorPlace::from_tactic(formatting.separator_place, tactic, formatting.separator);
|
2015-04-20 23:47:15 -05:00
|
|
|
|
|
|
|
let mut line_len = 0;
|
2017-03-27 17:25:59 -05:00
|
|
|
let indent_str = &formatting.shape.indent.to_string(formatting.config);
|
2015-10-02 06:50:24 -05:00
|
|
|
while let Some((i, item)) = iter.next() {
|
|
|
|
let item = item.as_ref();
|
2017-10-05 06:50:19 -05:00
|
|
|
let inner_item = item.item.as_ref()?;
|
2015-04-20 23:47:15 -05:00
|
|
|
let first = i == 0;
|
2015-10-02 06:50:24 -05:00
|
|
|
let last = iter.peek().is_none();
|
2017-11-16 02:39:38 -06:00
|
|
|
let mut separate = match sep_place {
|
|
|
|
SeparatorPlace::Front => !first,
|
|
|
|
SeparatorPlace::Back => !last || trailing_separator,
|
|
|
|
};
|
2016-05-29 10:58:38 -05:00
|
|
|
let item_sep_len = if separate { sep_len } else { 0 };
|
2015-10-07 18:23:07 -05:00
|
|
|
|
|
|
|
// Item string may be multi-line. Its length (used for block comment alignment)
|
2017-03-28 09:16:52 -05:00
|
|
|
// should be only the length of the last line.
|
2015-10-07 18:23:07 -05:00
|
|
|
let item_last_line = if item.is_multiline() {
|
|
|
|
inner_item.lines().last().unwrap_or("")
|
|
|
|
} else {
|
|
|
|
inner_item.as_ref()
|
|
|
|
};
|
|
|
|
let mut item_last_line_width = item_last_line.len() + item_sep_len;
|
2017-09-15 04:11:24 -05:00
|
|
|
if item_last_line.starts_with(&**indent_str) {
|
2015-10-07 18:23:07 -05:00
|
|
|
item_last_line_width -= indent_str.len();
|
|
|
|
}
|
2015-04-20 23:47:15 -05:00
|
|
|
|
|
|
|
match tactic {
|
2015-10-02 06:50:24 -05:00
|
|
|
DefinitiveListTactic::Horizontal if !first => {
|
2015-04-20 23:47:15 -05:00
|
|
|
result.push(' ');
|
|
|
|
}
|
2017-12-27 15:19:42 -06:00
|
|
|
DefinitiveListTactic::SpecialMacro(num_args_before) => {
|
2017-12-22 19:06:17 -06:00
|
|
|
if i == 0 {
|
2017-11-30 22:30:04 -06:00
|
|
|
// Nothing
|
2017-12-22 19:06:17 -06:00
|
|
|
} else if i < num_args_before {
|
2017-12-27 15:19:42 -06:00
|
|
|
result.push(' ');
|
2017-12-24 06:29:54 -06:00
|
|
|
} else if i <= num_args_before + 1 {
|
2017-11-30 22:30:04 -06:00
|
|
|
result.push('\n');
|
|
|
|
result.push_str(indent_str);
|
2017-12-24 06:29:54 -06:00
|
|
|
} else {
|
2017-12-27 15:19:42 -06:00
|
|
|
result.push(' ');
|
2017-11-30 22:30:04 -06:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 06:50:24 -05:00
|
|
|
DefinitiveListTactic::Vertical if !first => {
|
2015-04-20 23:47:15 -05:00
|
|
|
result.push('\n');
|
|
|
|
result.push_str(indent_str);
|
|
|
|
}
|
2015-10-02 06:50:24 -05:00
|
|
|
DefinitiveListTactic::Mixed => {
|
2015-06-23 08:58:58 -05:00
|
|
|
let total_width = total_item_width(item) + item_sep_len;
|
2015-04-20 23:47:15 -05:00
|
|
|
|
2015-10-16 15:54:32 -05:00
|
|
|
// 1 is space between separator and item.
|
2017-01-30 13:28:48 -06:00
|
|
|
if line_len > 0 && line_len + 1 + total_width > formatting.shape.width {
|
2015-04-20 23:47:15 -05:00
|
|
|
result.push('\n');
|
|
|
|
result.push_str(indent_str);
|
|
|
|
line_len = 0;
|
2017-08-18 09:19:47 -05:00
|
|
|
if formatting.ends_with_newline {
|
2017-07-13 06:32:46 -05:00
|
|
|
if last {
|
|
|
|
separate = true;
|
|
|
|
} else {
|
|
|
|
trailing_separator = true;
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 23:47:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if line_len > 0 {
|
|
|
|
result.push(' ');
|
|
|
|
line_len += 1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 08:58:58 -05:00
|
|
|
line_len += total_width;
|
2015-04-20 23:47:15 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2015-06-23 08:58:58 -05:00
|
|
|
// Pre-comments
|
|
|
|
if let Some(ref comment) = item.pre_comment {
|
2015-08-21 06:31:09 -05:00
|
|
|
// Block style in non-vertical mode.
|
2015-10-02 06:50:24 -05:00
|
|
|
let block_mode = tactic != DefinitiveListTactic::Vertical;
|
2015-08-21 06:31:09 -05:00
|
|
|
// Width restriction is only relevant in vertical mode.
|
2017-10-05 06:50:19 -05:00
|
|
|
let comment =
|
|
|
|
rewrite_comment(comment, block_mode, formatting.shape, formatting.config)?;
|
2015-09-25 05:53:25 -05:00
|
|
|
result.push_str(&comment);
|
2015-04-23 01:02:55 -05:00
|
|
|
|
2015-10-02 06:50:24 -05:00
|
|
|
if tactic == DefinitiveListTactic::Vertical {
|
2017-08-08 04:24:06 -05:00
|
|
|
// We cannot keep pre-comments on the same line if the comment if normalized.
|
2017-09-14 22:10:58 -05:00
|
|
|
let keep_comment = if formatting.config.normalize_comments()
|
|
|
|
|| item.pre_comment_style == ListItemCommentStyle::DifferentLine
|
2017-08-29 08:16:04 -05:00
|
|
|
{
|
2017-08-08 04:24:06 -05:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
// We will try to keep the comment on the same line with the item here.
|
|
|
|
// 1 = ` `
|
|
|
|
let total_width = total_item_width(item) + item_sep_len + 1;
|
|
|
|
total_width <= formatting.shape.width
|
|
|
|
};
|
|
|
|
if keep_comment {
|
|
|
|
result.push(' ');
|
|
|
|
} else {
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(indent_str);
|
|
|
|
}
|
2015-06-23 08:58:58 -05:00
|
|
|
} else {
|
2015-04-28 04:57:16 -05:00
|
|
|
result.push(' ');
|
|
|
|
}
|
2017-07-05 11:02:37 -05:00
|
|
|
item_max_width = None;
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
|
2017-08-18 09:19:47 -05:00
|
|
|
if separate && sep_place.is_front() && !first {
|
|
|
|
result.push_str(formatting.separator.trim());
|
|
|
|
result.push(' ');
|
|
|
|
}
|
2015-11-22 15:55:57 -06:00
|
|
|
result.push_str(&inner_item[..]);
|
2015-06-23 08:58:58 -05:00
|
|
|
|
|
|
|
// Post-comments
|
2015-10-02 06:50:24 -05:00
|
|
|
if tactic != DefinitiveListTactic::Vertical && item.post_comment.is_some() {
|
2015-09-01 21:11:19 -05:00
|
|
|
let comment = item.post_comment.as_ref().unwrap();
|
2017-10-05 06:50:19 -05:00
|
|
|
let formatted_comment = rewrite_comment(
|
2017-06-11 22:58:58 -05:00
|
|
|
comment,
|
|
|
|
true,
|
|
|
|
Shape::legacy(formatting.shape.width, Indent::empty()),
|
|
|
|
formatting.config,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2015-06-23 08:58:58 -05:00
|
|
|
|
|
|
|
result.push(' ');
|
|
|
|
result.push_str(&formatted_comment);
|
2015-04-21 05:50:43 -05:00
|
|
|
}
|
|
|
|
|
2017-08-18 09:19:47 -05:00
|
|
|
if separate && sep_place.is_back() {
|
2015-04-20 23:47:15 -05:00
|
|
|
result.push_str(formatting.separator);
|
|
|
|
}
|
2015-04-21 05:50:43 -05:00
|
|
|
|
2015-10-02 06:50:24 -05:00
|
|
|
if tactic == DefinitiveListTactic::Vertical && item.post_comment.is_some() {
|
2015-06-23 08:58:58 -05:00
|
|
|
let comment = item.post_comment.as_ref().unwrap();
|
2017-07-06 18:51:19 -05:00
|
|
|
let overhead = last_line_width(&result) + first_line_width(comment.trim());
|
|
|
|
|
|
|
|
let rewrite_post_comment = |item_max_width: &mut Option<usize>| {
|
|
|
|
if item_max_width.is_none() && !last && !inner_item.contains('\n') {
|
|
|
|
*item_max_width = Some(max_width_of_item_with_post_comment(
|
|
|
|
&cloned_items,
|
|
|
|
i,
|
|
|
|
overhead,
|
|
|
|
formatting.config.max_width(),
|
|
|
|
));
|
|
|
|
}
|
2017-11-22 21:38:34 -06:00
|
|
|
let overhead = if starts_with_newline(comment) {
|
|
|
|
0
|
|
|
|
} else if let Some(max_width) = *item_max_width {
|
2017-07-06 18:51:19 -05:00
|
|
|
max_width + 2
|
|
|
|
} else {
|
|
|
|
// 1 = space between item and comment.
|
|
|
|
item_last_line_width + 1
|
|
|
|
};
|
|
|
|
let width = formatting.shape.width.checked_sub(overhead).unwrap_or(1);
|
|
|
|
let offset = formatting.shape.indent + overhead;
|
|
|
|
let comment_shape = Shape::legacy(width, offset);
|
|
|
|
|
|
|
|
// Use block-style only for the last item or multiline comments.
|
2017-09-14 22:10:58 -05:00
|
|
|
let block_style = !formatting.ends_with_newline && last
|
|
|
|
|| comment.trim().contains('\n')
|
|
|
|
|| comment.trim().len() > width;
|
2017-07-06 18:51:19 -05:00
|
|
|
|
2017-11-22 21:38:34 -06:00
|
|
|
rewrite_comment(
|
|
|
|
comment.trim_left(),
|
|
|
|
block_style,
|
|
|
|
comment_shape,
|
|
|
|
formatting.config,
|
|
|
|
)
|
2017-07-06 18:51:19 -05:00
|
|
|
};
|
|
|
|
|
2017-10-05 06:50:19 -05:00
|
|
|
let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?;
|
2015-06-23 08:58:58 -05:00
|
|
|
|
2017-11-30 02:22:00 -06:00
|
|
|
if !starts_with_newline(comment) {
|
2017-07-05 11:02:37 -05:00
|
|
|
let mut comment_alignment =
|
|
|
|
post_comment_alignment(item_max_width, inner_item.len());
|
2017-09-14 22:10:58 -05:00
|
|
|
if first_line_width(&formatted_comment) + last_line_width(&result)
|
|
|
|
+ comment_alignment + 1 > formatting.config.max_width()
|
2017-07-05 11:02:37 -05:00
|
|
|
{
|
|
|
|
item_max_width = None;
|
2017-10-05 06:50:19 -05:00
|
|
|
formatted_comment = rewrite_post_comment(&mut item_max_width)?;
|
2017-07-05 11:02:37 -05:00
|
|
|
comment_alignment = post_comment_alignment(item_max_width, inner_item.len());
|
|
|
|
}
|
|
|
|
for _ in 0..(comment_alignment + 1) {
|
|
|
|
result.push(' ');
|
|
|
|
}
|
2017-08-18 09:15:56 -05:00
|
|
|
// An additional space for the missing trailing separator.
|
|
|
|
if last && item_max_width.is_some() && !separate && !formatting.separator.is_empty()
|
|
|
|
{
|
2017-07-05 11:02:37 -05:00
|
|
|
result.push(' ');
|
|
|
|
}
|
2017-11-22 21:38:34 -06:00
|
|
|
} else {
|
|
|
|
result.push('\n');
|
2017-11-30 02:22:00 -06:00
|
|
|
result.push_str(indent_str);
|
2017-07-05 11:02:37 -05:00
|
|
|
}
|
|
|
|
if formatted_comment.contains('\n') {
|
|
|
|
item_max_width = None;
|
2017-03-28 09:32:12 -05:00
|
|
|
}
|
2015-06-23 08:58:58 -05:00
|
|
|
result.push_str(&formatted_comment);
|
2017-07-05 11:02:37 -05:00
|
|
|
} else {
|
|
|
|
item_max_width = None;
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
2015-09-01 21:11:19 -05:00
|
|
|
|
2017-09-14 22:10:58 -05:00
|
|
|
if formatting.preserve_newline && !last && tactic == DefinitiveListTactic::Vertical
|
|
|
|
&& item.new_lines
|
2017-07-26 08:43:36 -05:00
|
|
|
{
|
2017-07-05 11:02:37 -05:00
|
|
|
item_max_width = None;
|
2015-09-01 21:11:19 -05:00
|
|
|
result.push('\n');
|
|
|
|
}
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 11:09:05 -05:00
|
|
|
Some(result)
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
|
2017-07-06 18:51:19 -05:00
|
|
|
fn max_width_of_item_with_post_comment<I, T>(
|
|
|
|
items: &I,
|
2017-07-05 11:02:37 -05:00
|
|
|
i: usize,
|
2017-07-06 18:51:19 -05:00
|
|
|
overhead: usize,
|
|
|
|
max_budget: usize,
|
|
|
|
) -> usize
|
2017-07-05 11:02:37 -05:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = T> + Clone,
|
|
|
|
T: AsRef<ListItem>,
|
|
|
|
{
|
|
|
|
let mut max_width = 0;
|
|
|
|
let mut first = true;
|
|
|
|
for item in items.clone().into_iter().skip(i) {
|
|
|
|
let item = item.as_ref();
|
2017-07-06 18:51:19 -05:00
|
|
|
let inner_item_width = item.inner_as_ref().len();
|
2017-09-14 22:10:58 -05:00
|
|
|
if !first
|
|
|
|
&& (item.is_different_group() || !item.post_comment.is_some()
|
|
|
|
|| inner_item_width + overhead > max_budget)
|
2017-07-06 18:51:19 -05:00
|
|
|
{
|
2017-07-05 11:02:37 -05:00
|
|
|
return max_width;
|
|
|
|
}
|
|
|
|
if max_width < inner_item_width {
|
|
|
|
max_width = inner_item_width;
|
|
|
|
}
|
|
|
|
if item.new_lines {
|
|
|
|
return max_width;
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
max_width
|
|
|
|
}
|
|
|
|
|
|
|
|
fn post_comment_alignment(item_max_width: Option<usize>, inner_item_len: usize) -> usize {
|
|
|
|
item_max_width
|
|
|
|
.and_then(|max_line_width| max_line_width.checked_sub(inner_item_len))
|
|
|
|
.unwrap_or(0)
|
|
|
|
}
|
|
|
|
|
2015-08-19 11:12:05 -05:00
|
|
|
pub struct ListItems<'a, I, F1, F2, F3>
|
2017-06-11 22:58:58 -05:00
|
|
|
where
|
|
|
|
I: Iterator,
|
2015-08-19 11:12:05 -05:00
|
|
|
{
|
2018-02-18 21:41:43 -06:00
|
|
|
snippet_provider: &'a SnippetProvider<'a>,
|
2015-08-19 11:12:05 -05:00
|
|
|
inner: Peekable<I>,
|
|
|
|
get_lo: F1,
|
|
|
|
get_hi: F2,
|
|
|
|
get_item_string: F3,
|
|
|
|
prev_span_end: BytePos,
|
|
|
|
next_span_start: BytePos,
|
|
|
|
terminator: &'a str,
|
2017-11-16 02:38:12 -06:00
|
|
|
separator: &'a str,
|
2017-08-07 03:29:55 -05:00
|
|
|
leave_last: bool,
|
2015-08-19 11:12:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
|
2017-06-11 22:58:58 -05:00
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
F1: Fn(&T) -> BytePos,
|
|
|
|
F2: Fn(&T) -> BytePos,
|
|
|
|
F3: Fn(&T) -> Option<String>,
|
2015-06-23 08:58:58 -05:00
|
|
|
{
|
2015-08-19 11:12:05 -05:00
|
|
|
type Item = ListItem;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let white_space: &[_] = &[' ', '\t'];
|
|
|
|
|
2017-05-16 09:24:38 -05:00
|
|
|
self.inner.next().map(|item| {
|
|
|
|
let mut new_lines = false;
|
|
|
|
// Pre-comment
|
2018-02-18 21:41:43 -06:00
|
|
|
let pre_snippet = self.snippet_provider
|
2017-06-05 23:54:22 -05:00
|
|
|
.span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
|
2017-05-16 09:24:38 -05:00
|
|
|
.unwrap();
|
|
|
|
let trimmed_pre_snippet = pre_snippet.trim();
|
2017-08-08 04:24:06 -05:00
|
|
|
let has_single_line_comment = trimmed_pre_snippet.starts_with("//");
|
|
|
|
let has_block_comment = trimmed_pre_snippet.starts_with("/*");
|
|
|
|
let (pre_comment, pre_comment_style) = if has_single_line_comment {
|
|
|
|
(
|
|
|
|
Some(trimmed_pre_snippet.to_owned()),
|
|
|
|
ListItemCommentStyle::DifferentLine,
|
|
|
|
)
|
|
|
|
} else if has_block_comment {
|
|
|
|
let comment_end = pre_snippet.chars().rev().position(|c| c == '/').unwrap();
|
|
|
|
if pre_snippet
|
|
|
|
.chars()
|
|
|
|
.rev()
|
|
|
|
.take(comment_end + 1)
|
2017-08-29 08:16:04 -05:00
|
|
|
.any(|c| c == '\n')
|
2017-08-08 04:24:06 -05:00
|
|
|
{
|
|
|
|
(
|
|
|
|
Some(trimmed_pre_snippet.to_owned()),
|
|
|
|
ListItemCommentStyle::DifferentLine,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
Some(trimmed_pre_snippet.to_owned()),
|
|
|
|
ListItemCommentStyle::SameLine,
|
|
|
|
)
|
|
|
|
}
|
2017-05-16 09:24:38 -05:00
|
|
|
} else {
|
2017-08-08 04:24:06 -05:00
|
|
|
(None, ListItemCommentStyle::None)
|
2017-05-16 09:24:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Post-comment
|
|
|
|
let next_start = match self.inner.peek() {
|
|
|
|
Some(next_item) => (self.get_lo)(next_item),
|
|
|
|
None => self.next_span_start,
|
|
|
|
};
|
2018-02-18 21:41:43 -06:00
|
|
|
let post_snippet = self.snippet_provider
|
2017-06-05 23:54:22 -05:00
|
|
|
.span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
|
2017-05-16 09:24:38 -05:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let comment_end = match self.inner.peek() {
|
|
|
|
Some(..) => {
|
|
|
|
let mut block_open_index = post_snippet.find("/*");
|
2017-11-15 00:46:00 -06:00
|
|
|
// check if it really is a block comment (and not `//*` or a nested comment)
|
2017-05-16 09:24:38 -05:00
|
|
|
if let Some(i) = block_open_index {
|
2017-11-30 02:22:00 -06:00
|
|
|
match post_snippet.find('/') {
|
2017-11-15 00:46:00 -06:00
|
|
|
Some(j) if j < i => block_open_index = None,
|
|
|
|
_ if i > 0 && &post_snippet[i - 1..i] == "/" => block_open_index = None,
|
|
|
|
_ => (),
|
2015-09-09 16:17:31 -05:00
|
|
|
}
|
2017-03-27 17:14:47 -05:00
|
|
|
}
|
2017-05-16 09:24:38 -05:00
|
|
|
let newline_index = post_snippet.find('\n');
|
2017-11-16 02:38:12 -06:00
|
|
|
if let Some(separator_index) = post_snippet.find_uncommented(self.separator) {
|
2017-08-18 09:15:56 -05:00
|
|
|
match (block_open_index, newline_index) {
|
|
|
|
// Separator before comment, with the next item on same line.
|
|
|
|
// Comment belongs to next item.
|
|
|
|
(Some(i), None) if i > separator_index => separator_index + 1,
|
|
|
|
// Block-style post-comment before the separator.
|
|
|
|
(Some(i), None) => cmp::max(
|
|
|
|
find_comment_end(&post_snippet[i..]).unwrap() + i,
|
|
|
|
separator_index + 1,
|
|
|
|
),
|
|
|
|
// Block-style post-comment. Either before or after the separator.
|
|
|
|
(Some(i), Some(j)) if i < j => cmp::max(
|
|
|
|
find_comment_end(&post_snippet[i..]).unwrap() + i,
|
|
|
|
separator_index + 1,
|
|
|
|
),
|
|
|
|
// Potential *single* line comment.
|
|
|
|
(_, Some(j)) if j > separator_index => j + 1,
|
|
|
|
_ => post_snippet.len(),
|
|
|
|
}
|
2017-11-30 02:22:00 -06:00
|
|
|
} else if let Some(newline_index) = newline_index {
|
2017-08-18 09:15:56 -05:00
|
|
|
// Match arms may not have trailing comma. In any case, for match arms,
|
|
|
|
// we will assume that the post comment belongs to the next arm if they
|
|
|
|
// do not end with trailing comma.
|
2017-11-30 02:22:00 -06:00
|
|
|
newline_index + 1
|
|
|
|
} else {
|
|
|
|
0
|
2015-09-09 16:15:37 -05:00
|
|
|
}
|
2015-09-01 21:11:19 -05:00
|
|
|
}
|
2017-07-11 07:53:10 -05:00
|
|
|
None => post_snippet
|
|
|
|
.find_uncommented(self.terminator)
|
2017-08-29 08:16:04 -05:00
|
|
|
.unwrap_or_else(|| post_snippet.len()),
|
2017-05-16 09:24:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if !post_snippet.is_empty() && comment_end > 0 {
|
|
|
|
// Account for extra whitespace between items. This is fiddly
|
|
|
|
// because of the way we divide pre- and post- comments.
|
|
|
|
|
|
|
|
// Everything from the separator to the next item.
|
|
|
|
let test_snippet = &post_snippet[comment_end - 1..];
|
2017-08-29 08:16:04 -05:00
|
|
|
let first_newline = test_snippet
|
|
|
|
.find('\n')
|
|
|
|
.unwrap_or_else(|| test_snippet.len());
|
2017-05-16 09:24:38 -05:00
|
|
|
// From the end of the first line of comments.
|
|
|
|
let test_snippet = &test_snippet[first_newline..];
|
2017-06-16 04:56:32 -05:00
|
|
|
let first = test_snippet
|
|
|
|
.find(|c: char| !c.is_whitespace())
|
2017-08-29 08:16:04 -05:00
|
|
|
.unwrap_or_else(|| test_snippet.len());
|
2017-05-16 09:24:38 -05:00
|
|
|
// From the end of the first line of comments to the next non-whitespace char.
|
|
|
|
let test_snippet = &test_snippet[..first];
|
|
|
|
|
2017-12-05 00:17:40 -06:00
|
|
|
if count_newlines(test_snippet) > 1 {
|
2017-05-16 09:24:38 -05:00
|
|
|
// There were multiple line breaks which got trimmed to nothing.
|
|
|
|
new_lines = true;
|
2017-03-27 17:14:47 -05:00
|
|
|
}
|
2017-05-16 09:24:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup post-comment: strip separators and whitespace.
|
|
|
|
self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
|
|
|
|
let post_snippet = post_snippet[..comment_end].trim();
|
|
|
|
|
2017-09-04 11:04:31 -05:00
|
|
|
let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
|
2017-05-16 09:24:38 -05:00
|
|
|
post_snippet[1..].trim_matches(white_space)
|
|
|
|
} else if post_snippet.ends_with(',') {
|
2017-05-25 02:08:08 -05:00
|
|
|
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
|
2017-05-16 09:24:38 -05:00
|
|
|
} else {
|
|
|
|
post_snippet
|
|
|
|
};
|
|
|
|
|
|
|
|
let post_comment = if !post_snippet_trimmed.is_empty() {
|
|
|
|
Some(post_snippet_trimmed.to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
ListItem {
|
2018-01-21 22:05:18 -06:00
|
|
|
pre_comment,
|
|
|
|
pre_comment_style,
|
2017-08-07 03:29:55 -05:00
|
|
|
item: if self.inner.peek().is_none() && self.leave_last {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
(self.get_item_string)(&item)
|
|
|
|
},
|
2018-01-21 22:05:18 -06:00
|
|
|
post_comment,
|
|
|
|
new_lines,
|
2017-05-16 09:24:38 -05:00
|
|
|
}
|
|
|
|
})
|
2015-04-20 23:47:15 -05:00
|
|
|
}
|
2015-08-19 11:12:05 -05:00
|
|
|
}
|
2015-04-20 23:47:15 -05:00
|
|
|
|
2018-02-14 04:05:34 -06:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))]
|
2015-08-19 11:12:05 -05:00
|
|
|
// Creates an iterator over a list's items with associated comments.
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn itemize_list<'a, T, I, F1, F2, F3>(
|
2018-02-18 21:41:43 -06:00
|
|
|
snippet_provider: &'a SnippetProvider,
|
2017-06-11 22:58:58 -05:00
|
|
|
inner: I,
|
|
|
|
terminator: &'a str,
|
2017-11-16 02:38:12 -06:00
|
|
|
separator: &'a str,
|
2017-06-11 22:58:58 -05:00
|
|
|
get_lo: F1,
|
|
|
|
get_hi: F2,
|
|
|
|
get_item_string: F3,
|
|
|
|
prev_span_end: BytePos,
|
|
|
|
next_span_start: BytePos,
|
2017-08-07 03:29:55 -05:00
|
|
|
leave_last: bool,
|
2017-06-11 22:58:58 -05:00
|
|
|
) -> ListItems<'a, I, F1, F2, F3>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
F1: Fn(&T) -> BytePos,
|
|
|
|
F2: Fn(&T) -> BytePos,
|
|
|
|
F3: Fn(&T) -> Option<String>,
|
2015-08-19 11:12:05 -05:00
|
|
|
{
|
|
|
|
ListItems {
|
2018-02-18 21:41:43 -06:00
|
|
|
snippet_provider,
|
2015-08-19 11:12:05 -05:00
|
|
|
inner: inner.peekable(),
|
2018-01-21 22:05:18 -06:00
|
|
|
get_lo,
|
|
|
|
get_hi,
|
|
|
|
get_item_string,
|
|
|
|
prev_span_end,
|
|
|
|
next_span_start,
|
|
|
|
terminator,
|
|
|
|
separator,
|
|
|
|
leave_last,
|
2015-08-19 11:12:05 -05:00
|
|
|
}
|
2015-04-20 23:47:15 -05:00
|
|
|
}
|
2015-04-21 05:50:43 -05:00
|
|
|
|
2015-10-02 06:50:24 -05:00
|
|
|
/// Returns the count and total width of the list items.
|
2016-01-07 00:43:45 -06:00
|
|
|
fn calculate_width<I, T>(items: I) -> (usize, usize)
|
2017-06-11 22:58:58 -05:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = T>,
|
|
|
|
T: AsRef<ListItem>,
|
2015-10-02 06:50:24 -05:00
|
|
|
{
|
2017-03-27 17:14:47 -05:00
|
|
|
items
|
|
|
|
.into_iter()
|
|
|
|
.map(|item| total_item_width(item.as_ref()))
|
|
|
|
.fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l))
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
|
2017-09-18 21:41:05 -05:00
|
|
|
pub fn total_item_width(item: &ListItem) -> usize {
|
2017-09-14 22:10:58 -05:00
|
|
|
comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..]))
|
|
|
|
+ comment_len(item.post_comment.as_ref().map(|x| &(*x)[..]))
|
|
|
|
+ item.item.as_ref().map_or(0, |str| str.len())
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
|
2015-10-02 06:50:24 -05:00
|
|
|
fn comment_len(comment: Option<&str>) -> usize {
|
|
|
|
match comment {
|
|
|
|
Some(s) => {
|
2015-06-23 08:58:58 -05:00
|
|
|
let text_len = s.trim().len();
|
|
|
|
if text_len > 0 {
|
|
|
|
// We'll put " /*" before and " */" after inline comments.
|
|
|
|
text_len + 6
|
|
|
|
} else {
|
|
|
|
text_len
|
|
|
|
}
|
2015-08-15 22:58:17 -05:00
|
|
|
}
|
2015-09-04 16:39:33 -05:00
|
|
|
None => 0,
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
2015-04-21 05:50:43 -05:00
|
|
|
}
|
2017-03-20 17:23:59 -05:00
|
|
|
|
|
|
|
// Compute horizontal and vertical shapes for a struct-lit-like thing.
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn struct_lit_shape(
|
|
|
|
shape: Shape,
|
|
|
|
context: &RewriteContext,
|
|
|
|
prefix_width: usize,
|
|
|
|
suffix_width: usize,
|
|
|
|
) -> Option<(Option<Shape>, Shape)> {
|
2017-11-13 01:42:29 -06:00
|
|
|
let v_shape = match context.config.indent_style() {
|
2017-10-05 06:50:19 -05:00
|
|
|
IndentStyle::Visual => shape
|
|
|
|
.visual_indent(0)
|
|
|
|
.shrink_left(prefix_width)?
|
|
|
|
.sub_width(suffix_width)?,
|
2017-06-12 21:49:47 -05:00
|
|
|
IndentStyle::Block => {
|
|
|
|
let shape = shape.block_indent(context.config.tab_spaces());
|
|
|
|
Shape {
|
2017-08-29 08:16:04 -05:00
|
|
|
width: context.budget(shape.indent.width()),
|
2017-06-12 21:49:47 -05:00
|
|
|
..shape
|
2017-06-11 22:58:58 -05:00
|
|
|
}
|
2017-06-12 21:49:47 -05:00
|
|
|
}
|
|
|
|
};
|
2017-07-03 04:53:47 -05:00
|
|
|
let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
|
|
|
|
if let Some(w) = shape_width {
|
2017-11-24 01:17:06 -06:00
|
|
|
let shape_width = cmp::min(w, context.config.width_heuristics().struct_lit_width);
|
2017-07-03 04:53:47 -05:00
|
|
|
Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
|
|
|
|
} else {
|
|
|
|
Some((None, v_shape))
|
|
|
|
}
|
2017-03-20 17:23:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the tactic for the internals of a struct-lit-like thing.
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn struct_lit_tactic(
|
|
|
|
h_shape: Option<Shape>,
|
|
|
|
context: &RewriteContext,
|
|
|
|
items: &[ListItem],
|
|
|
|
) -> DefinitiveListTactic {
|
2017-03-20 17:23:59 -05:00
|
|
|
if let Some(h_shape) = h_shape {
|
2017-11-13 01:42:29 -06:00
|
|
|
let prelim_tactic = match (context.config.indent_style(), items.len()) {
|
2017-03-20 17:23:59 -05:00
|
|
|
(IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
|
2017-11-23 19:45:18 -06:00
|
|
|
_ if context.config.struct_lit_single_line() => ListTactic::HorizontalVertical,
|
|
|
|
_ => ListTactic::Vertical,
|
2017-03-20 17:23:59 -05:00
|
|
|
};
|
2017-07-31 02:23:42 -05:00
|
|
|
definitive_tactic(items, prelim_tactic, Separator::Comma, h_shape.width)
|
2017-03-20 17:23:59 -05:00
|
|
|
} else {
|
|
|
|
DefinitiveListTactic::Vertical
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a tactic and possible shapes for horizontal and vertical layout,
|
|
|
|
// come up with the actual shape to use.
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn shape_for_tactic(
|
|
|
|
tactic: DefinitiveListTactic,
|
|
|
|
h_shape: Option<Shape>,
|
|
|
|
v_shape: Shape,
|
|
|
|
) -> Shape {
|
2017-03-20 17:23:59 -05:00
|
|
|
match tactic {
|
|
|
|
DefinitiveListTactic::Horizontal => h_shape.unwrap(),
|
|
|
|
_ => v_shape,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a ListFormatting object for formatting the internals of a
|
|
|
|
// struct-lit-like thing, that is a series of fields.
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn struct_lit_formatting<'a>(
|
|
|
|
shape: Shape,
|
|
|
|
tactic: DefinitiveListTactic,
|
|
|
|
context: &'a RewriteContext,
|
|
|
|
force_no_trailing_comma: bool,
|
|
|
|
) -> ListFormatting<'a> {
|
2017-11-13 01:42:29 -06:00
|
|
|
let ends_with_newline = context.config.indent_style() != IndentStyle::Visual
|
2017-09-14 22:10:58 -05:00
|
|
|
&& tactic == DefinitiveListTactic::Vertical;
|
2017-03-20 17:23:59 -05:00
|
|
|
ListFormatting {
|
2018-01-21 22:05:18 -06:00
|
|
|
tactic,
|
2017-03-20 17:23:59 -05:00
|
|
|
separator: ",",
|
|
|
|
trailing_separator: if force_no_trailing_comma {
|
|
|
|
SeparatorTactic::Never
|
|
|
|
} else {
|
2017-05-16 03:47:09 -05:00
|
|
|
context.config.trailing_comma()
|
2017-03-20 17:23:59 -05:00
|
|
|
},
|
2017-08-18 09:19:47 -05:00
|
|
|
separator_place: SeparatorPlace::Back,
|
2018-01-21 22:05:18 -06:00
|
|
|
shape,
|
|
|
|
ends_with_newline,
|
2017-07-26 08:43:36 -05:00
|
|
|
preserve_newline: true,
|
2017-03-20 17:23:59 -05:00
|
|
|
config: context.config,
|
|
|
|
}
|
|
|
|
}
|