use std::iter::Peekable;
use config::lists::*;
-use syntax::codemap::BytePos;
+use syntax::source_map::BytePos;
use comment::{find_comment_end, rewrite_comment, FindUncommented};
use config::{Config, IndentStyle};
use visitor::SnippetProvider;
pub struct ListFormatting<'a> {
- pub tactic: DefinitiveListTactic,
- pub separator: &'a str,
- pub trailing_separator: SeparatorTactic,
- pub separator_place: SeparatorPlace,
- pub shape: Shape,
+ tactic: DefinitiveListTactic,
+ separator: &'a str,
+ trailing_separator: SeparatorTactic,
+ separator_place: SeparatorPlace,
+ shape: Shape,
// Non-expressions, e.g. items, will have a new line at the end of the list.
// Important for comment styles.
- pub ends_with_newline: bool,
+ ends_with_newline: bool,
// Remove newlines between list elements for expressions.
- pub preserve_newline: bool,
+ preserve_newline: bool,
// Nested import lists get some special handling for the "Mixed" list type
- pub nested: bool,
- pub config: &'a Config,
+ nested: bool,
+ config: &'a Config,
}
impl<'a> ListFormatting<'a> {
+ pub fn new(shape: Shape, config: &'a Config) -> Self {
+ ListFormatting {
+ tactic: DefinitiveListTactic::Vertical,
+ separator: ",",
+ trailing_separator: SeparatorTactic::Never,
+ separator_place: SeparatorPlace::Back,
+ shape,
+ ends_with_newline: true,
+ preserve_newline: false,
+ nested: false,
+ config: config,
+ }
+ }
+
+ pub fn tactic(mut self, tactic: DefinitiveListTactic) -> Self {
+ self.tactic = tactic;
+ self
+ }
+
+ pub fn separator(mut self, separator: &'a str) -> Self {
+ self.separator = separator;
+ self
+ }
+
+ pub fn trailing_separator(mut self, trailing_separator: SeparatorTactic) -> Self {
+ self.trailing_separator = trailing_separator;
+ self
+ }
+
+ pub fn separator_place(mut self, separator_place: SeparatorPlace) -> Self {
+ self.separator_place = separator_place;
+ self
+ }
+
+ pub fn ends_with_newline(mut self, ends_with_newline: bool) -> Self {
+ self.ends_with_newline = ends_with_newline;
+ self
+ }
+
+ pub fn preserve_newline(mut self, preserve_newline: bool) -> Self {
+ self.preserve_newline = preserve_newline;
+ self
+ }
+
+ pub fn nested(mut self, nested: bool) -> Self {
+ self.nested = nested;
+ self
+ }
+
pub fn needs_trailing_separator(&self) -> bool {
match self.trailing_separator {
// We always put separator in front.
}
pub fn is_different_group(&self) -> bool {
- self.inner_as_ref().contains('\n') || self.pre_comment.is_some() || self
- .post_comment
- .as_ref()
- .map_or(false, |s| s.contains('\n'))
+ self.inner_as_ref().contains('\n')
+ || self.pre_comment.is_some()
+ || self
+ .post_comment
+ .as_ref()
+ .map_or(false, |s| s.contains('\n'))
}
pub fn is_multiline(&self) -> bool {
}
impl Separator {
- pub fn len(&self) -> usize {
- match *self {
+ pub fn len(self) -> usize {
+ match self {
// 2 = `, `
Separator::Comma => 2,
// 3 = ` | `
formatted_comment = rewrite_post_comment(&mut item_max_width)?;
comment_alignment = post_comment_alignment(item_max_width, inner_item.len());
}
- for _ in 0..(comment_alignment + 1) {
+ for _ in 0..=comment_alignment {
result.push(' ');
}
// An additional space for the missing trailing separator.
pub fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommentStyle) {
let trimmed_pre_snippet = pre_snippet.trim();
+ let has_block_comment = trimmed_pre_snippet.ends_with("*/");
let has_single_line_comment = trimmed_pre_snippet.starts_with("//");
- let has_block_comment = trimmed_pre_snippet.starts_with("/*");
- if has_single_line_comment {
- (
- Some(trimmed_pre_snippet.to_owned()),
- ListItemCommentStyle::DifferentLine,
- )
- } else if has_block_comment {
+ if has_block_comment {
let comment_end = pre_snippet.chars().rev().position(|c| c == '/').unwrap();
if pre_snippet
.chars()
ListItemCommentStyle::SameLine,
)
}
+ } else if has_single_line_comment {
+ (
+ Some(trimmed_pre_snippet.to_owned()),
+ ListItemCommentStyle::DifferentLine,
+ )
} else {
(None, ListItemCommentStyle::None)
}