2015-04-21 16:47:15 +12: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.
|
|
|
|
|
2015-06-23 15:58:58 +02:00
|
|
|
use std::cmp;
|
2015-08-19 18:12:05 +02:00
|
|
|
use std::iter::Peekable;
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2017-06-06 06:54:22 +02:00
|
|
|
use syntax::codemap::{CodeMap, BytePos};
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2017-01-31 08:28:48 +13:00
|
|
|
use {Indent, Shape};
|
2015-06-23 15:58:58 +02:00
|
|
|
use comment::{FindUncommented, rewrite_comment, find_comment_end};
|
2017-03-21 11:23:59 +13:00
|
|
|
use config::{Config, IndentStyle};
|
|
|
|
use rewrite::RewriteContext;
|
2017-06-06 06:54:22 +02:00
|
|
|
use utils::mk_sp;
|
2015-04-21 16:47:15 +12:00
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
2015-10-06 22:13:14 +02:00
|
|
|
/// Formatting tactic for lists. This will be cast down to a
|
|
|
|
/// DefinitiveListTactic depending on the number and length of the items and
|
|
|
|
/// their comments.
|
2015-04-21 16:47:15 +12:00
|
|
|
pub enum ListTactic {
|
|
|
|
// One item per row.
|
|
|
|
Vertical,
|
|
|
|
// All items on one row.
|
|
|
|
Horizontal,
|
2015-10-06 22:13:14 +02:00
|
|
|
// Try Horizontal layout, if that fails then vertical.
|
2015-04-21 16:47:15 +12:00
|
|
|
HorizontalVertical,
|
2015-09-26 14:00:19 +12:00
|
|
|
// HorizontalVertical with a soft limit of n characters.
|
2015-09-24 11:00:14 +12:00
|
|
|
LimitedHorizontalVertical(usize),
|
2015-04-21 16:47:15 +12:00
|
|
|
// Pack as many items as possible per row over (possibly) many rows.
|
|
|
|
Mixed,
|
|
|
|
}
|
|
|
|
|
2017-05-16 18:08:24 +07:00
|
|
|
impl_enum_serialize_and_deserialize!(ListTactic, Vertical, Horizontal, HorizontalVertical, Mixed);
|
2015-09-01 18:53:16 +12:00
|
|
|
|
2015-04-21 16:47:15 +12:00
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
|
|
|
pub enum SeparatorTactic {
|
|
|
|
Always,
|
|
|
|
Never,
|
|
|
|
Vertical,
|
|
|
|
}
|
|
|
|
|
2017-05-16 18:08:24 +07:00
|
|
|
impl_enum_serialize_and_deserialize!(SeparatorTactic, Always, Never, Vertical);
|
2015-05-25 19:11:53 +12:00
|
|
|
|
2015-11-03 23:14:37 +01:00
|
|
|
impl SeparatorTactic {
|
|
|
|
pub fn from_bool(b: bool) -> SeparatorTactic {
|
|
|
|
if b {
|
|
|
|
SeparatorTactic::Always
|
|
|
|
} else {
|
|
|
|
SeparatorTactic::Never
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 16:47:15 +12:00
|
|
|
pub struct ListFormatting<'a> {
|
2015-10-02 13:50:24 +02:00
|
|
|
pub tactic: DefinitiveListTactic,
|
2015-04-21 16:47:15 +12:00
|
|
|
pub separator: &'a str,
|
|
|
|
pub trailing_separator: SeparatorTactic,
|
2017-01-31 08:28:48 +13:00
|
|
|
pub shape: Shape,
|
2015-06-23 15:58:58 +02:00
|
|
|
// Non-expressions, e.g. items, will have a new line at the end of the list.
|
|
|
|
// Important for comment styles.
|
2015-07-03 11:13:28 +02:00
|
|
|
pub ends_with_newline: bool,
|
2015-09-05 22:39:28 -07:00
|
|
|
pub config: &'a Config,
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
|
|
|
|
2017-01-31 08:28:48 +13:00
|
|
|
pub fn format_item_list<I>(items: I, shape: Shape, config: &Config) -> Option<String>
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
I: Iterator<Item = ListItem>,
|
2015-10-02 13:50:24 +02:00
|
|
|
{
|
2017-01-31 08:28:48 +13:00
|
|
|
list_helper(items, shape, config, ListTactic::HorizontalVertical)
|
2015-10-02 13:50:24 +02:00
|
|
|
}
|
|
|
|
|
2017-01-31 08:28:48 +13:00
|
|
|
pub fn list_helper<I>(items: I, shape: Shape, config: &Config, tactic: ListTactic) -> Option<String>
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
I: Iterator<Item = ListItem>,
|
2015-10-02 13:50:24 +02:00
|
|
|
{
|
|
|
|
let item_vec: Vec<_> = items.collect();
|
2017-01-31 08:28:48 +13:00
|
|
|
let tactic = definitive_tactic(&item_vec, tactic, shape.width);
|
2015-10-02 13:50:24 +02:00
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: tactic,
|
|
|
|
separator: ",",
|
|
|
|
trailing_separator: SeparatorTactic::Never,
|
2017-01-31 08:28:48 +13:00
|
|
|
shape: shape,
|
2015-10-02 13:50:24 +02:00
|
|
|
ends_with_newline: false,
|
|
|
|
config: config,
|
|
|
|
};
|
|
|
|
|
|
|
|
write_list(&item_vec, &fmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<ListItem> for ListItem {
|
|
|
|
fn as_ref(&self) -> &ListItem {
|
|
|
|
self
|
2015-08-19 22:39:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:58:58 +02:00
|
|
|
pub struct ListItem {
|
2015-10-04 20:20:15 +02:00
|
|
|
// None for comments mean that they are not present.
|
2015-06-23 15:58:58 +02:00
|
|
|
pub pre_comment: Option<String>,
|
2015-10-06 22:13:14 +02:00
|
|
|
// Item should include attributes and doc comments. None indicates a failed
|
2015-10-04 20:20:15 +02:00
|
|
|
// rewrite.
|
|
|
|
pub item: Option<String>,
|
2015-07-03 11:13:28 +02:00
|
|
|
pub post_comment: Option<String>,
|
2015-09-02 14:11:19 +12:00
|
|
|
// Whether there is extra whitespace before this item.
|
|
|
|
pub new_lines: bool,
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:58:58 +02:00
|
|
|
impl ListItem {
|
|
|
|
pub fn is_multiline(&self) -> bool {
|
2017-03-28 11:25:59 +13:00
|
|
|
self.item.as_ref().map_or(false, |s| s.contains('\n')) || self.pre_comment.is_some() ||
|
2017-06-17 16:56:54 +09:00
|
|
|
self.post_comment
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |s| s.contains('\n'))
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 03:29:54 +02:00
|
|
|
pub fn has_line_pre_comment(&self) -> bool {
|
2017-06-17 16:56:54 +09:00
|
|
|
self.pre_comment
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |comment| comment.starts_with("//"))
|
2015-06-26 03:29:54 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:58:58 +02:00
|
|
|
pub fn from_str<S: Into<String>>(s: S) -> ListItem {
|
2015-09-26 18:29:48 +12:00
|
|
|
ListItem {
|
|
|
|
pre_comment: None,
|
2015-10-04 20:20:15 +02:00
|
|
|
item: Some(s.into()),
|
2015-09-26 18:29:48 +12:00
|
|
|
post_comment: None,
|
|
|
|
new_lines: false,
|
|
|
|
}
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 13:50:24 +02:00
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
2015-10-06 22:13:14 +02:00
|
|
|
/// The definitive formatting tactic for lists.
|
2015-10-02 13:50:24 +02:00
|
|
|
pub enum DefinitiveListTactic {
|
|
|
|
Vertical,
|
|
|
|
Horizontal,
|
|
|
|
Mixed,
|
|
|
|
}
|
2015-04-21 16:47:15 +12:00
|
|
|
|
2016-01-07 12:13:45 +05:30
|
|
|
pub fn definitive_tactic<I, T>(items: I, tactic: ListTactic, width: usize) -> DefinitiveListTactic
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = T> + Clone,
|
|
|
|
T: AsRef<ListItem>,
|
2015-10-02 13:50:24 +02:00
|
|
|
{
|
2017-06-16 08:49:49 +09:00
|
|
|
let pre_line_comments = items
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.any(|item| item.as_ref().has_line_pre_comment());
|
2015-10-02 13:50:24 +02: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-21 16:47:15 +12:00
|
|
|
};
|
2015-05-04 17:16:51 +02:00
|
|
|
|
2015-10-02 13:50:24 +02:00
|
|
|
let (sep_count, total_width) = calculate_width(items.clone());
|
|
|
|
let sep_len = ", ".len(); // FIXME: make more generic?
|
|
|
|
let total_sep_len = sep_len * sep_count.checked_sub(1).unwrap_or(0);
|
|
|
|
let real_total = total_width + total_sep_len;
|
2015-04-21 16:47:15 +12:00
|
|
|
|
2015-10-02 13:50:24 +02:00
|
|
|
if real_total <= limit && !pre_line_comments &&
|
2017-06-12 15:58:58 +12:00
|
|
|
!items.into_iter().any(|item| item.as_ref().is_multiline())
|
|
|
|
{
|
2015-10-02 13:50:24 +02:00
|
|
|
DefinitiveListTactic::Horizontal
|
|
|
|
} else {
|
|
|
|
DefinitiveListTactic::Vertical
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
2015-10-02 13:50:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format a list of commented items into a string.
|
|
|
|
// TODO: add unit tests
|
2015-11-25 15:39:15 +09:00
|
|
|
pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = T>,
|
|
|
|
T: AsRef<ListItem>,
|
2015-10-02 13:50:24 +02:00
|
|
|
{
|
|
|
|
let tactic = formatting.tactic;
|
|
|
|
let sep_len = formatting.separator.len();
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2015-04-21 16:47:15 +12:00
|
|
|
// Now that we know how we will layout, we can decide for sure if there
|
|
|
|
// will be a trailing separator.
|
2015-04-21 22:50:43 +12:00
|
|
|
let trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
|
2015-10-02 13:50:24 +02:00
|
|
|
let mut result = String::new();
|
|
|
|
let mut iter = items.into_iter().enumerate().peekable();
|
2015-04-21 16:47:15 +12:00
|
|
|
|
|
|
|
let mut line_len = 0;
|
2017-03-28 11:25:59 +13:00
|
|
|
let indent_str = &formatting.shape.indent.to_string(formatting.config);
|
2015-10-02 13:50:24 +02:00
|
|
|
while let Some((i, item)) = iter.next() {
|
|
|
|
let item = item.as_ref();
|
2015-10-04 20:20:15 +02:00
|
|
|
let inner_item = try_opt!(item.item.as_ref());
|
2015-04-21 16:47:15 +12:00
|
|
|
let first = i == 0;
|
2015-10-02 13:50:24 +02:00
|
|
|
let last = iter.peek().is_none();
|
2015-06-23 15:58:58 +02:00
|
|
|
let separate = !last || trailing_separator;
|
2016-05-29 17:58:38 +02:00
|
|
|
let item_sep_len = if separate { sep_len } else { 0 };
|
2015-10-07 19:23:07 -04:00
|
|
|
|
|
|
|
// Item string may be multi-line. Its length (used for block comment alignment)
|
2017-03-28 23:16:52 +09:00
|
|
|
// should be only the length of the last line.
|
2015-10-07 19:23:07 -04: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;
|
|
|
|
if item_last_line.starts_with(indent_str) {
|
|
|
|
item_last_line_width -= indent_str.len();
|
|
|
|
}
|
2015-04-21 16:47:15 +12:00
|
|
|
|
|
|
|
match tactic {
|
2015-10-02 13:50:24 +02:00
|
|
|
DefinitiveListTactic::Horizontal if !first => {
|
2015-04-21 16:47:15 +12:00
|
|
|
result.push(' ');
|
|
|
|
}
|
2015-10-02 13:50:24 +02:00
|
|
|
DefinitiveListTactic::Vertical if !first => {
|
2015-04-21 16:47:15 +12:00
|
|
|
result.push('\n');
|
|
|
|
result.push_str(indent_str);
|
|
|
|
}
|
2015-10-02 13:50:24 +02:00
|
|
|
DefinitiveListTactic::Mixed => {
|
2015-06-23 15:58:58 +02:00
|
|
|
let total_width = total_item_width(item) + item_sep_len;
|
2015-04-21 16:47:15 +12:00
|
|
|
|
2015-10-16 22:54:32 +02:00
|
|
|
// 1 is space between separator and item.
|
2017-01-31 08:28:48 +13:00
|
|
|
if line_len > 0 && line_len + 1 + total_width > formatting.shape.width {
|
2015-04-21 16:47:15 +12:00
|
|
|
result.push('\n');
|
|
|
|
result.push_str(indent_str);
|
|
|
|
line_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if line_len > 0 {
|
|
|
|
result.push(' ');
|
|
|
|
line_len += 1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:58:58 +02:00
|
|
|
line_len += total_width;
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:58:58 +02:00
|
|
|
// Pre-comments
|
|
|
|
if let Some(ref comment) = item.pre_comment {
|
2015-08-21 13:31:09 +02:00
|
|
|
// Block style in non-vertical mode.
|
2015-10-02 13:50:24 +02:00
|
|
|
let block_mode = tactic != DefinitiveListTactic::Vertical;
|
2015-08-21 13:31:09 +02:00
|
|
|
// Width restriction is only relevant in vertical mode.
|
2017-06-12 15:58:58 +12:00
|
|
|
let comment = try_opt!(rewrite_comment(
|
|
|
|
comment,
|
|
|
|
block_mode,
|
|
|
|
formatting.shape,
|
|
|
|
formatting.config,
|
|
|
|
));
|
2015-09-25 12:53:25 +02:00
|
|
|
result.push_str(&comment);
|
2015-04-23 18:02:55 +12:00
|
|
|
|
2015-10-02 13:50:24 +02:00
|
|
|
if tactic == DefinitiveListTactic::Vertical {
|
2015-06-23 15:58:58 +02:00
|
|
|
result.push('\n');
|
|
|
|
result.push_str(indent_str);
|
|
|
|
} else {
|
2015-04-28 21:57:16 +12:00
|
|
|
result.push(' ');
|
|
|
|
}
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
|
|
|
|
2015-11-22 22:55:57 +01:00
|
|
|
result.push_str(&inner_item[..]);
|
2015-06-23 15:58:58 +02:00
|
|
|
|
|
|
|
// Post-comments
|
2015-10-02 13:50:24 +02:00
|
|
|
if tactic != DefinitiveListTactic::Vertical && item.post_comment.is_some() {
|
2015-09-02 14:11:19 +12:00
|
|
|
let comment = item.post_comment.as_ref().unwrap();
|
2017-06-12 15:58:58 +12:00
|
|
|
let formatted_comment = try_opt!(rewrite_comment(
|
|
|
|
comment,
|
|
|
|
true,
|
|
|
|
Shape::legacy(formatting.shape.width, Indent::empty()),
|
|
|
|
formatting.config,
|
|
|
|
));
|
2015-06-23 15:58:58 +02:00
|
|
|
|
|
|
|
result.push(' ');
|
|
|
|
result.push_str(&formatted_comment);
|
2015-04-21 22:50:43 +12:00
|
|
|
}
|
|
|
|
|
2015-04-21 16:47:15 +12:00
|
|
|
if separate {
|
|
|
|
result.push_str(formatting.separator);
|
|
|
|
}
|
2015-04-21 22:50:43 +12:00
|
|
|
|
2015-10-02 13:50:24 +02:00
|
|
|
if tactic == DefinitiveListTactic::Vertical && item.post_comment.is_some() {
|
2015-06-24 01:11:29 +02:00
|
|
|
// 1 = space between item and comment.
|
2017-03-28 10:58:41 +13:00
|
|
|
let width = formatting
|
|
|
|
.shape
|
2017-03-07 09:40:08 +13:00
|
|
|
.width
|
|
|
|
.checked_sub(item_last_line_width + 1)
|
|
|
|
.unwrap_or(1);
|
2017-01-31 08:28:48 +13:00
|
|
|
let mut offset = formatting.shape.indent;
|
2015-10-07 19:23:07 -04:00
|
|
|
offset.alignment += item_last_line_width + 1;
|
2015-06-23 15:58:58 +02:00
|
|
|
let comment = item.post_comment.as_ref().unwrap();
|
2015-10-07 19:23:07 -04:00
|
|
|
|
|
|
|
debug!("Width = {}, offset = {:?}", width, offset);
|
2015-06-24 01:11:29 +02:00
|
|
|
// Use block-style only for the last item or multiline comments.
|
2015-08-14 14:09:19 +02:00
|
|
|
let block_style = !formatting.ends_with_newline && last ||
|
2017-06-12 15:58:58 +12:00
|
|
|
comment.trim().contains('\n') ||
|
|
|
|
comment.trim().len() > width;
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
let formatted_comment = try_opt!(rewrite_comment(
|
|
|
|
comment,
|
|
|
|
block_style,
|
|
|
|
Shape::legacy(width, offset),
|
|
|
|
formatting.config,
|
|
|
|
));
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2017-03-28 16:32:12 +02:00
|
|
|
if !formatted_comment.starts_with('\n') {
|
|
|
|
result.push(' ');
|
|
|
|
}
|
2015-06-23 15:58:58 +02:00
|
|
|
result.push_str(&formatted_comment);
|
|
|
|
}
|
2015-09-02 14:11:19 +12:00
|
|
|
|
2015-10-02 13:50:24 +02:00
|
|
|
if !last && tactic == DefinitiveListTactic::Vertical && item.new_lines {
|
2015-09-02 14:11:19 +12:00
|
|
|
result.push('\n');
|
|
|
|
}
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
|
|
|
|
2015-09-04 18:09:05 +02:00
|
|
|
Some(result)
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 18:12:05 +02:00
|
|
|
pub struct ListItems<'a, I, F1, F2, F3>
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
I: Iterator,
|
2015-08-19 18:12:05 +02:00
|
|
|
{
|
|
|
|
codemap: &'a CodeMap,
|
|
|
|
inner: Peekable<I>,
|
|
|
|
get_lo: F1,
|
|
|
|
get_hi: F2,
|
|
|
|
get_item_string: F3,
|
|
|
|
prev_span_end: BytePos,
|
|
|
|
next_span_start: BytePos,
|
|
|
|
terminator: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
F1: Fn(&T) -> BytePos,
|
|
|
|
F2: Fn(&T) -> BytePos,
|
|
|
|
F3: Fn(&T) -> Option<String>,
|
2015-06-23 15:58:58 +02:00
|
|
|
{
|
2015-08-19 18:12:05 +02:00
|
|
|
type Item = ListItem;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let white_space: &[_] = &[' ', '\t'];
|
|
|
|
|
2017-05-16 23:24:38 +09:00
|
|
|
self.inner.next().map(|item| {
|
|
|
|
let mut new_lines = false;
|
|
|
|
// Pre-comment
|
|
|
|
let pre_snippet = self.codemap
|
2017-06-06 06:54:22 +02:00
|
|
|
.span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
|
2017-05-16 23:24:38 +09:00
|
|
|
.unwrap();
|
|
|
|
let trimmed_pre_snippet = pre_snippet.trim();
|
|
|
|
let has_pre_comment = trimmed_pre_snippet.contains("//") ||
|
2017-06-12 15:58:58 +12:00
|
|
|
trimmed_pre_snippet.contains("/*");
|
2017-05-16 23:24:38 +09:00
|
|
|
let pre_comment = if has_pre_comment {
|
|
|
|
Some(trimmed_pre_snippet.to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// Post-comment
|
|
|
|
let next_start = match self.inner.peek() {
|
|
|
|
Some(next_item) => (self.get_lo)(next_item),
|
|
|
|
None => self.next_span_start,
|
|
|
|
};
|
|
|
|
let post_snippet = self.codemap
|
2017-06-06 06:54:22 +02:00
|
|
|
.span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
|
2017-05-16 23:24:38 +09:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let comment_end = match self.inner.peek() {
|
|
|
|
Some(..) => {
|
|
|
|
let mut block_open_index = post_snippet.find("/*");
|
|
|
|
// check if it realy is a block comment (and not //*)
|
|
|
|
if let Some(i) = block_open_index {
|
|
|
|
if i > 0 && &post_snippet[i - 1..i] == "/" {
|
|
|
|
block_open_index = None;
|
2015-09-09 23:17:31 +02:00
|
|
|
}
|
2017-03-28 11:14:47 +13:00
|
|
|
}
|
2017-05-16 23:24:38 +09:00
|
|
|
let newline_index = post_snippet.find('\n');
|
|
|
|
let separator_index = post_snippet.find_uncommented(",").unwrap();
|
|
|
|
|
|
|
|
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) => {
|
2017-06-12 15:58:58 +12:00
|
|
|
cmp::max(
|
|
|
|
find_comment_end(&post_snippet[i..]).unwrap() + i,
|
|
|
|
separator_index + 1,
|
|
|
|
)
|
2017-05-16 23:24:38 +09:00
|
|
|
}
|
|
|
|
// Block-style post-comment. Either before or after the separator.
|
|
|
|
(Some(i), Some(j)) if i < j => {
|
2017-06-12 15:58:58 +12:00
|
|
|
cmp::max(
|
|
|
|
find_comment_end(&post_snippet[i..]).unwrap() + i,
|
|
|
|
separator_index + 1,
|
|
|
|
)
|
2017-05-16 23:24:38 +09:00
|
|
|
}
|
|
|
|
// Potential *single* line comment.
|
|
|
|
(_, Some(j)) if j > separator_index => j + 1,
|
|
|
|
_ => post_snippet.len(),
|
2015-09-09 23:15:37 +02:00
|
|
|
}
|
2015-09-02 14:11:19 +12:00
|
|
|
}
|
2017-05-16 23:24:38 +09:00
|
|
|
None => {
|
2017-06-16 18:56:32 +09:00
|
|
|
post_snippet
|
|
|
|
.find_uncommented(self.terminator)
|
|
|
|
.unwrap_or(post_snippet.len())
|
2017-05-16 23:24:38 +09: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..];
|
|
|
|
let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
|
|
|
|
// From the end of the first line of comments.
|
|
|
|
let test_snippet = &test_snippet[first_newline..];
|
2017-06-16 18:56:32 +09:00
|
|
|
let first = test_snippet
|
|
|
|
.find(|c: char| !c.is_whitespace())
|
|
|
|
.unwrap_or(test_snippet.len());
|
2017-05-16 23:24:38 +09:00
|
|
|
// From the end of the first line of comments to the next non-whitespace char.
|
|
|
|
let test_snippet = &test_snippet[..first];
|
|
|
|
|
|
|
|
if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
|
|
|
|
// There were multiple line breaks which got trimmed to nothing.
|
|
|
|
new_lines = true;
|
2017-03-28 11:14:47 +13:00
|
|
|
}
|
2017-05-16 23:24:38 +09: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();
|
|
|
|
|
|
|
|
let post_snippet_trimmed = if post_snippet.starts_with(',') {
|
|
|
|
post_snippet[1..].trim_matches(white_space)
|
|
|
|
} else if post_snippet.ends_with(',') {
|
2017-05-25 16:08:08 +09:00
|
|
|
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
|
2017-05-16 23:24:38 +09:00
|
|
|
} else {
|
|
|
|
post_snippet
|
|
|
|
};
|
|
|
|
|
|
|
|
let post_comment = if !post_snippet_trimmed.is_empty() {
|
|
|
|
Some(post_snippet_trimmed.to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
ListItem {
|
|
|
|
pre_comment: pre_comment,
|
|
|
|
item: (self.get_item_string)(&item),
|
|
|
|
post_comment: post_comment,
|
|
|
|
new_lines: new_lines,
|
|
|
|
}
|
|
|
|
})
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
2015-08-19 18:12:05 +02:00
|
|
|
}
|
2015-04-21 16:47:15 +12:00
|
|
|
|
2015-08-19 18:12:05 +02:00
|
|
|
// Creates an iterator over a list's items with associated comments.
|
2017-06-12 15:58:58 +12:00
|
|
|
pub fn itemize_list<'a, T, I, F1, F2, F3>(
|
|
|
|
codemap: &'a CodeMap,
|
|
|
|
inner: I,
|
|
|
|
terminator: &'a str,
|
|
|
|
get_lo: F1,
|
|
|
|
get_hi: F2,
|
|
|
|
get_item_string: F3,
|
|
|
|
prev_span_end: BytePos,
|
|
|
|
next_span_start: BytePos,
|
|
|
|
) -> 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 18:12:05 +02:00
|
|
|
{
|
|
|
|
ListItems {
|
|
|
|
codemap: codemap,
|
|
|
|
inner: inner.peekable(),
|
|
|
|
get_lo: get_lo,
|
|
|
|
get_hi: get_hi,
|
|
|
|
get_item_string: get_item_string,
|
|
|
|
prev_span_end: prev_span_end,
|
|
|
|
next_span_start: next_span_start,
|
|
|
|
terminator: terminator,
|
|
|
|
}
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
2015-04-21 22:50:43 +12:00
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
fn needs_trailing_separator(
|
|
|
|
separator_tactic: SeparatorTactic,
|
|
|
|
list_tactic: DefinitiveListTactic,
|
|
|
|
) -> bool {
|
2015-04-21 22:50:43 +12:00
|
|
|
match separator_tactic {
|
|
|
|
SeparatorTactic::Always => true,
|
2015-10-02 13:50:24 +02:00
|
|
|
SeparatorTactic::Vertical => list_tactic == DefinitiveListTactic::Vertical,
|
2015-04-21 22:50:43 +12:00
|
|
|
SeparatorTactic::Never => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 13:50:24 +02:00
|
|
|
/// Returns the count and total width of the list items.
|
2016-01-07 12:13:45 +05:30
|
|
|
fn calculate_width<I, T>(items: I) -> (usize, usize)
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = T>,
|
|
|
|
T: AsRef<ListItem>,
|
2015-10-02 13:50:24 +02:00
|
|
|
{
|
2017-03-28 11:14:47 +13: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 15:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn total_item_width(item: &ListItem) -> usize {
|
2015-10-02 13:50:24 +02:00
|
|
|
comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..])) +
|
2017-06-12 15:58:58 +12:00
|
|
|
comment_len(item.post_comment.as_ref().map(|x| &(*x)[..])) +
|
|
|
|
item.item.as_ref().map_or(0, |str| str.len())
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
|
|
|
|
2015-10-02 13:50:24 +02:00
|
|
|
fn comment_len(comment: Option<&str>) -> usize {
|
|
|
|
match comment {
|
|
|
|
Some(s) => {
|
2015-06-23 15:58:58 +02: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-16 15:58:17 +12:00
|
|
|
}
|
2015-09-04 23:39:33 +02:00
|
|
|
None => 0,
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
2015-04-21 22:50:43 +12:00
|
|
|
}
|
2017-03-21 11:23:59 +13:00
|
|
|
|
|
|
|
// Compute horizontal and vertical shapes for a struct-lit-like thing.
|
2017-06-12 15:58:58 +12:00
|
|
|
pub fn struct_lit_shape(
|
|
|
|
shape: Shape,
|
|
|
|
context: &RewriteContext,
|
|
|
|
prefix_width: usize,
|
|
|
|
suffix_width: usize,
|
|
|
|
) -> Option<(Option<Shape>, Shape)> {
|
2017-06-13 14:49:47 +12:00
|
|
|
let v_shape = match context.config.struct_lit_style() {
|
|
|
|
IndentStyle::Visual => {
|
|
|
|
try_opt!(
|
|
|
|
try_opt!(shape.visual_indent(0).shrink_left(prefix_width)).sub_width(suffix_width)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
IndentStyle::Block => {
|
|
|
|
let shape = shape.block_indent(context.config.tab_spaces());
|
|
|
|
Shape {
|
|
|
|
width: try_opt!(context.config.max_width().checked_sub(shape.indent.width())),
|
|
|
|
..shape
|
2017-06-12 15:58:58 +12:00
|
|
|
}
|
2017-06-13 14:49:47 +12:00
|
|
|
}
|
|
|
|
};
|
2017-07-03 18:53:47 +09:00
|
|
|
let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
|
|
|
|
if let Some(w) = shape_width {
|
|
|
|
let shape_width = cmp::min(w, context.config.struct_lit_width());
|
|
|
|
Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
|
|
|
|
} else {
|
|
|
|
Some((None, v_shape))
|
|
|
|
}
|
2017-03-21 11:23:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the tactic for the internals of a struct-lit-like thing.
|
2017-06-12 15:58:58 +12:00
|
|
|
pub fn struct_lit_tactic(
|
|
|
|
h_shape: Option<Shape>,
|
|
|
|
context: &RewriteContext,
|
|
|
|
items: &[ListItem],
|
|
|
|
) -> DefinitiveListTactic {
|
2017-03-21 11:23:59 +13:00
|
|
|
if let Some(h_shape) = h_shape {
|
2017-07-03 18:53:47 +09:00
|
|
|
let prelim_tactic = match (context.config.struct_lit_style(), items.len()) {
|
2017-03-21 11:23:59 +13:00
|
|
|
(IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
|
2017-05-16 15:47:09 +07:00
|
|
|
_ => context.config.struct_lit_multiline_style().to_list_tactic(),
|
2017-03-21 11:23:59 +13:00
|
|
|
};
|
|
|
|
definitive_tactic(items, prelim_tactic, h_shape.width)
|
|
|
|
} else {
|
|
|
|
DefinitiveListTactic::Vertical
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a tactic and possible shapes for horizontal and vertical layout,
|
|
|
|
// come up with the actual shape to use.
|
2017-06-12 15:58:58 +12:00
|
|
|
pub fn shape_for_tactic(
|
|
|
|
tactic: DefinitiveListTactic,
|
|
|
|
h_shape: Option<Shape>,
|
|
|
|
v_shape: Shape,
|
|
|
|
) -> Shape {
|
2017-03-21 11:23:59 +13: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-12 15:58:58 +12:00
|
|
|
pub fn struct_lit_formatting<'a>(
|
|
|
|
shape: Shape,
|
|
|
|
tactic: DefinitiveListTactic,
|
|
|
|
context: &'a RewriteContext,
|
|
|
|
force_no_trailing_comma: bool,
|
|
|
|
) -> ListFormatting<'a> {
|
2017-05-16 15:47:09 +07:00
|
|
|
let ends_with_newline = context.config.struct_lit_style() != IndentStyle::Visual &&
|
2017-06-12 15:58:58 +12:00
|
|
|
tactic == DefinitiveListTactic::Vertical;
|
2017-03-21 11:23:59 +13:00
|
|
|
ListFormatting {
|
|
|
|
tactic: tactic,
|
|
|
|
separator: ",",
|
|
|
|
trailing_separator: if force_no_trailing_comma {
|
|
|
|
SeparatorTactic::Never
|
|
|
|
} else {
|
2017-05-16 15:47:09 +07:00
|
|
|
context.config.trailing_comma()
|
2017-03-21 11:23:59 +13:00
|
|
|
},
|
|
|
|
shape: shape,
|
|
|
|
ends_with_newline: ends_with_newline,
|
|
|
|
config: context.config,
|
|
|
|
}
|
|
|
|
}
|