use syntax::codemap::{BytePos, CodeMap};
-use {Indent, Shape};
use comment::{find_comment_end, rewrite_comment, FindUncommented};
use config::{Config, IndentStyle};
use rewrite::RewriteContext;
-use utils::{first_line_width, last_line_width, mk_sp};
+use shape::{Indent, Shape};
+use utils::{first_line_width, last_line_width, mk_sp, starts_with_newline};
/// Formatting tactic for lists. This will be cast down to a
/// `DefinitiveListTactic` depending on the number and length of the items and
}
pub fn is_different_group(&self) -> bool {
- self.inner_as_ref().contains('\n') || self.pre_comment.is_some() ||
- self.post_comment
+ 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 {
- self.inner_as_ref().contains('\n') ||
- self.pre_comment
+ self.inner_as_ref().contains('\n')
+ || self.pre_comment
.as_ref()
- .map_or(false, |s| s.contains('\n')) ||
- self.post_comment
+ .map_or(false, |s| s.contains('\n'))
+ || self.post_comment
.as_ref()
.map_or(false, |s| s.contains('\n'))
}
pub fn has_comment(&self) -> bool {
self.pre_comment
.as_ref()
- .map_or(false, |comment| comment.starts_with("//")) ||
- self.post_comment
+ .map_or(false, |comment| comment.starts_with("//"))
+ || self.post_comment
.as_ref()
.map_or(false, |comment| comment.starts_with("//"))
}
let total_sep_len = sep.len() * sep_count.checked_sub(1).unwrap_or(0);
let real_total = total_width + total_sep_len;
- if real_total <= limit && !pre_line_comments &&
- !items.into_iter().any(|item| item.as_ref().is_multiline())
+ if real_total <= limit && !pre_line_comments
+ && !items.into_iter().any(|item| item.as_ref().is_multiline())
{
DefinitiveListTactic::Horizontal
} else {
let indent_str = &formatting.shape.indent.to_string(formatting.config);
while let Some((i, item)) = iter.next() {
let item = item.as_ref();
- let inner_item = try_opt!(item.item.as_ref());
+ let inner_item = item.item.as_ref()?;
let first = i == 0;
let last = iter.peek().is_none();
let mut separate = !last || trailing_separator;
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) {
+ if item_last_line.starts_with(&**indent_str) {
item_last_line_width -= indent_str.len();
}
// Block style in non-vertical mode.
let block_mode = tactic != DefinitiveListTactic::Vertical;
// Width restriction is only relevant in vertical mode.
- let comment = try_opt!(rewrite_comment(
- comment,
- block_mode,
- formatting.shape,
- formatting.config,
- ));
+ let comment =
+ rewrite_comment(comment, block_mode, formatting.shape, formatting.config)?;
result.push_str(&comment);
if tactic == DefinitiveListTactic::Vertical {
// We cannot keep pre-comments on the same line if the comment if normalized.
- let keep_comment = if formatting.config.normalize_comments() ||
- item.pre_comment_style == ListItemCommentStyle::DifferentLine
+ let keep_comment = if formatting.config.normalize_comments()
+ || item.pre_comment_style == ListItemCommentStyle::DifferentLine
{
false
} else {
// Post-comments
if tactic != DefinitiveListTactic::Vertical && item.post_comment.is_some() {
let comment = item.post_comment.as_ref().unwrap();
- let formatted_comment = try_opt!(rewrite_comment(
+ let formatted_comment = rewrite_comment(
comment,
true,
Shape::legacy(formatting.shape.width, Indent::empty()),
formatting.config,
- ));
+ )?;
result.push(' ');
result.push_str(&formatted_comment);
let comment_shape = Shape::legacy(width, offset);
// Use block-style only for the last item or multiline comments.
- let block_style = !formatting.ends_with_newline && last ||
- comment.trim().contains('\n') ||
- comment.trim().len() > width;
+ let block_style = !formatting.ends_with_newline && last
+ || comment.trim().contains('\n')
+ || comment.trim().len() > width;
rewrite_comment(comment, block_style, comment_shape, formatting.config)
};
- let mut formatted_comment = try_opt!(rewrite_post_comment(&mut item_max_width));
+ let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?;
- if !formatted_comment.starts_with('\n') {
+ if !starts_with_newline(&formatted_comment) {
let mut comment_alignment =
post_comment_alignment(item_max_width, inner_item.len());
- if first_line_width(&formatted_comment) + last_line_width(&result) +
- comment_alignment + 1 > formatting.config.max_width()
+ if first_line_width(&formatted_comment) + last_line_width(&result)
+ + comment_alignment + 1 > formatting.config.max_width()
{
item_max_width = None;
- formatted_comment = try_opt!(rewrite_post_comment(&mut item_max_width));
+ 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) {
item_max_width = None;
}
- if formatting.preserve_newline && !last && tactic == DefinitiveListTactic::Vertical &&
- item.new_lines
+ if formatting.preserve_newline && !last && tactic == DefinitiveListTactic::Vertical
+ && item.new_lines
{
item_max_width = None;
result.push('\n');
for item in items.clone().into_iter().skip(i) {
let item = item.as_ref();
let inner_item_width = item.inner_as_ref().len();
- if !first &&
- (item.is_different_group() || !item.post_comment.is_some() ||
- inner_item_width + overhead > max_budget)
+ if !first
+ && (item.is_different_group() || !item.post_comment.is_some()
+ || inner_item_width + overhead > max_budget)
{
return max_width;
}
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 //*)
+ // check if it really 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;
.fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l))
}
-fn total_item_width(item: &ListItem) -> usize {
- 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())
+pub fn total_item_width(item: &ListItem) -> usize {
+ 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())
}
fn comment_len(comment: Option<&str>) -> usize {
prefix_width: usize,
suffix_width: usize,
) -> Option<(Option<Shape>, Shape)> {
- 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)
- ),
+ let v_shape = match context.config.struct_lit_indent() {
+ IndentStyle::Visual => shape
+ .visual_indent(0)
+ .shrink_left(prefix_width)?
+ .sub_width(suffix_width)?,
IndentStyle::Block => {
let shape = shape.block_indent(context.config.tab_spaces());
Shape {
items: &[ListItem],
) -> DefinitiveListTactic {
if let Some(h_shape) = h_shape {
- let prelim_tactic = match (context.config.struct_lit_style(), items.len()) {
+ let prelim_tactic = match (context.config.struct_lit_indent(), items.len()) {
(IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
_ => context.config.struct_lit_multiline_style().to_list_tactic(),
};
context: &'a RewriteContext,
force_no_trailing_comma: bool,
) -> ListFormatting<'a> {
- let ends_with_newline = context.config.struct_lit_style() != IndentStyle::Visual &&
- tactic == DefinitiveListTactic::Vertical;
+ let ends_with_newline = context.config.struct_lit_indent() != IndentStyle::Visual
+ && tactic == DefinitiveListTactic::Vertical;
ListFormatting {
tactic: tactic,
separator: ",",