Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,13 @@ impl Rewrite for Chain {

formatter.format_root(&self.parent, context, shape)?;
if let Some(result) = formatter.pure_root() {
return wrap_str(result, context.config.max_width(), shape)
.max_width_error(shape.width, self.parent.span);
return wrap_str(
result,
context.config.max_width(),
context.config.tab_spaces(),
shape,
)
.max_width_error(shape.width, self.parent.span);
}

let first = self.children.first().unwrap_or(&self.parent);
Expand All @@ -582,7 +587,13 @@ impl Rewrite for Chain {
formatter.format_last_child(context, shape, child_shape)?;

let result = formatter.join_rewrites(context, child_shape)?;
wrap_str(result, context.config.max_width(), shape).max_width_error(shape.width, full_span)
wrap_str(
result,
context.config.max_width(),
context.config.tab_spaces(),
shape,
)
.max_width_error(shape.width, full_span)
}
}

Expand Down Expand Up @@ -718,7 +729,7 @@ impl<'a> ChainFormatterShared<'a> {
) -> Result<(), RewriteError> {
let last = self.children.last().unknown_error()?;
let extendable = may_extend && last_line_extendable(&self.rewrites[0]);
let prev_last_line_width = last_line_width(&self.rewrites[0]);
let prev_last_line_width = last_line_width(&self.rewrites[0], context.config.tab_spaces());

// Total of all items excluding the last.
let almost_total = if extendable {
Expand Down Expand Up @@ -958,7 +969,8 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
let mut root_rewrite = parent.rewrite_result(context, parent_shape)?;
let multiline = root_rewrite.contains('\n');
self.offset = if multiline {
last_line_width(&root_rewrite).saturating_sub(shape.used_width())
last_line_width(&root_rewrite, context.config.tab_spaces())
.saturating_sub(shape.used_width())
} else {
trimmed_last_line_width(&root_rewrite)
};
Expand All @@ -973,7 +985,12 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
.visual_indent(self.offset)
.sub_width(self.offset, item.span)?;
let rewrite = item.rewrite_result(context, child_shape)?;
if filtered_str_fits(&rewrite, context.config.max_width(), shape) {
if filtered_str_fits(
&rewrite,
context.config.max_width(),
context.config.tab_spaces(),
shape,
) {
root_rewrite.push_str(&rewrite);
} else {
// We couldn't fit in at the visual indent, try the last
Expand Down
2 changes: 1 addition & 1 deletion src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ fn rewrite_closure_fn_decl(
prefix.push_str(&ret_str);
}
// 1 = space between `|...|` and body.
let extra_offset = last_line_width(&prefix) + 1;
let extra_offset = last_line_width(&prefix, context.config.tab_spaces()) + 1;

Ok((prefix, extra_offset))
}
Expand Down
12 changes: 8 additions & 4 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ pub(crate) fn combine_strs_with_missing_comments(
} else {
" "
};
let mut one_line_width =
last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
let mut one_line_width = last_line_width(prev_str, context.config.tab_spaces())
+ first_line_width(next_str)
+ first_sep.len();

let config = context.config;
let indent = shape.indent;
Expand Down Expand Up @@ -207,7 +208,9 @@ pub(crate) fn combine_strs_with_missing_comments(
let first_sep = if prev_str.is_empty() || missing_comment.is_empty() {
Cow::from("")
} else {
let one_line_width = last_line_width(prev_str) + first_line_width(&missing_comment) + 1;
let one_line_width = last_line_width(prev_str, context.config.tab_spaces())
+ first_line_width(&missing_comment)
+ 1;
if prefer_same_line && one_line_width <= shape.width {
Cow::from(" ")
} else {
Expand Down Expand Up @@ -871,7 +874,8 @@ impl<'a> CommentRewrite<'a> {

self.fmt.shape = if self.is_prev_line_multi_line {
// 1 = " "
let offset = 1 + last_line_width(&self.result) - self.line_start.len();
let offset = 1 + last_line_width(&self.result, self.fmt.config.tab_spaces())
- self.line_start.len();
Shape {
width: self.max_width.saturating_sub(offset),
indent: self.fmt_indent,
Expand Down
42 changes: 31 additions & 11 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ pub(crate) fn format_expr(
wrap_str(
context.snippet(expr.span).to_owned(),
context.config.max_width(),
context.config.tab_spaces(),
shape,
)
.max_width_error(shape.width, expr.span)
Expand Down Expand Up @@ -597,7 +598,10 @@ fn rewrite_single_line_block(
shape: Shape,
) -> RewriteResult {
if let Some(block_expr) = stmt::Stmt::from_simple_block(context, block, attrs) {
let expr_shape = shape.offset_left(last_line_width(prefix), block_expr.span())?;
let expr_shape = shape.offset_left(
last_line_width(prefix, context.config.tab_spaces()),
block_expr.span(),
)?;
let expr_str = block_expr.rewrite_result(context, expr_shape)?;
let label_str = rewrite_label(context, label);
let result = format!("{prefix}{label_str}{{ {expr_str} }}");
Expand Down Expand Up @@ -1097,7 +1101,7 @@ impl<'a> ControlFlow<'a> {
};

let used_width = if pat_expr_string.contains('\n') {
last_line_width(&pat_expr_string)
last_line_width(&pat_expr_string, context.config.tab_spaces())
} else {
// 2 = spaces after keyword and condition.
label_string.len() + self.keyword.len() + pat_expr_string.len() + 2
Expand Down Expand Up @@ -1342,6 +1346,7 @@ pub(crate) fn rewrite_literal(
_ => wrap_str(
context.snippet(span).to_owned(),
context.config.max_width(),
context.config.tab_spaces(),
shape,
)
.max_width_error(shape.width, span),
Expand All @@ -1360,8 +1365,13 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
{
return Ok(string_lit.to_owned());
} else {
return wrap_str(string_lit.to_owned(), context.config.max_width(), shape)
.max_width_error(shape.width, span);
return wrap_str(
string_lit.to_owned(),
context.config.max_width(),
context.config.tab_spaces(),
shape,
)
.max_width_error(shape.width, span);
}
}

Expand Down Expand Up @@ -1402,6 +1412,7 @@ fn rewrite_int_lit(
token_lit.suffix.as_ref().map_or("", |s| s.as_str())
),
context.config.max_width(),
context.config.tab_spaces(),
shape,
)
.max_width_error(shape.width, span);
Expand All @@ -1411,6 +1422,7 @@ fn rewrite_int_lit(
wrap_str(
context.snippet(span).to_owned(),
context.config.max_width(),
context.config.tab_spaces(),
shape,
)
.max_width_error(shape.width, span)
Expand All @@ -1429,6 +1441,7 @@ fn rewrite_float_lit(
return wrap_str(
context.snippet(span).to_owned(),
context.config.max_width(),
context.config.tab_spaces(),
shape,
)
.max_width_error(shape.width, span);
Expand Down Expand Up @@ -1477,6 +1490,7 @@ fn rewrite_float_lit(
suffix.unwrap_or(""),
),
context.config.max_width(),
context.config.tab_spaces(),
shape,
)
.max_width_error(shape.width, span)
Expand Down Expand Up @@ -1698,7 +1712,7 @@ fn rewrite_index(
) -> RewriteResult {
let expr_str = expr.rewrite_result(context, shape)?;

let offset = last_line_width(&expr_str) + 1;
let offset = last_line_width(&expr_str, context.config.tab_spaces()) + 1;
let rhs_overhead = shape.rhs_overhead(context.config);
let index_shape = if expr_str.contains('\n') {
Shape::legacy(context.config.max_width(), shape.indent)
Expand Down Expand Up @@ -2220,11 +2234,12 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
rhs_kind: &RhsAssignKind<'_>,
rhs_tactics: RhsTactics,
) -> RewriteResult {
let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') {
shape.indent.width()
} else {
0
});
let last_line_width =
last_line_width(lhs, context.config.tab_spaces()).saturating_sub(if lhs.contains('\n') {
shape.indent.width()
} else {
0
});
// 1 = space between operator and rhs.
let orig_shape = shape.offset_left_opt(last_line_width + 1).unwrap_or(Shape {
width: 0,
Expand Down Expand Up @@ -2321,7 +2336,12 @@ fn choose_rhs<R: Rewrite>(

match (orig_rhs, new_rhs) {
(Ok(ref orig_rhs), Ok(ref new_rhs))
if !filtered_str_fits(&new_rhs, context.config.max_width(), new_shape) =>
if !filtered_str_fits(
&new_rhs,
context.config.max_width(),
context.config.tab_spaces(),
new_shape,
) =>
{
Ok(format!("{before_space_str}{orig_rhs}"))
}
Expand Down
Loading
Loading