-
Notifications
You must be signed in to change notification settings - Fork 931
implement single_line_let_else_max_width
#5790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,8 +127,8 @@ impl Rewrite for ast::Local { | |
|
||
if let Some(block) = else_block { | ||
let else_kw_span = init.span.between(block.span); | ||
let force_newline_else = | ||
!same_line_else_kw_and_brace(&result, context, else_kw_span, nested_shape); | ||
let force_newline_else = pat_str.contains('\n') | ||
|| !same_line_else_kw_and_brace(&result, context, else_kw_span, nested_shape); | ||
let else_kw = rewrite_else_kw_with_comments( | ||
force_newline_else, | ||
true, | ||
|
@@ -138,17 +138,31 @@ impl Rewrite for ast::Local { | |
); | ||
result.push_str(&else_kw); | ||
|
||
let allow_single_line = allow_single_line_let_else_block(&result, block); | ||
// At this point we've written `let {pat} = {expr} else' into the buffer, and we | ||
// want to calculate up front if there's room to write the divergent block on the | ||
// same line. The available space varies based on indentation so we clamp the width | ||
// on the smaller of `shape.width` and `single_line_let_else_max_width`. | ||
let max_width = | ||
std::cmp::min(shape.width, context.config.single_line_let_else_max_width()); | ||
|
||
// If available_space hits zero we know for sure this will be a multi-lined block | ||
let available_space = max_width.saturating_sub(result.len()); | ||
|
||
let allow_single_line = !force_newline_else | ||
&& available_space > 0 | ||
&& allow_single_line_let_else_block(&result, block); | ||
|
||
let mut rw_else_block = | ||
rewrite_let_else_block(block, allow_single_line, context, shape)?; | ||
|
||
if allow_single_line && !rw_else_block.contains('\n') { | ||
let available_space = shape.width.saturating_sub(result.len()); | ||
if available_space <= rw_else_block.len() { | ||
// writing this on one line would exceed the available width | ||
rw_else_block = rewrite_let_else_block(block, false, context, shape)?; | ||
} | ||
let single_line_else = !rw_else_block.contains('\n'); | ||
// +1 for the trailing `;` | ||
let else_block_exceeds_width = rw_else_block.len() + 1 > available_space; | ||
|
||
if allow_single_line && single_line_else && else_block_exceeds_width { | ||
// writing this on one line would exceed the available width | ||
// so rewrite the else block over multiple lines. | ||
rw_else_block = rewrite_let_else_block(block, false, context, shape)?; | ||
Comment on lines
+145
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I concur with some of the earlier discussions on this that the high level theme of limit derivation, checking if a formatted version can fit within that limit, else format over multiple lines, feels like a potentially recurrent pattern that could be encapsulated in some manner that lends itself to reuse. However, even if so, that's the type of internal change we could make afterwards with no impact on resultant behavior so I'd also agree that should be punted to a hypothetical future iteration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great! appreciate you sharing your thoughts on that! |
||
} | ||
|
||
result.push_str(&rw_else_block); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// rustfmt-single_line_let_else_max_width: 100 | ||
|
||
fn main() { | ||
let Some(a) = opt else {}; | ||
|
||
let Some(b) = opt else { return }; | ||
|
||
let Some(c) = opt else { | ||
return | ||
}; | ||
|
||
let Some(c) = opt else { | ||
// a comment should always force the block to be multi-lined | ||
return | ||
}; | ||
|
||
let Some(c) = opt else { /* a comment should always force the block to be multi-lined */ return }; | ||
|
||
let Some(d) = some_very_very_very_very_long_name else { return }; | ||
|
||
let Expr::Slice(ast::ExprSlice { lower, upper, step, range: _ }) = slice.as_ref() else { | ||
return | ||
}; | ||
|
||
let Some((base_place, current)) = self.lower_expr_as_place(current, *base, true)? else { | ||
return Ok(None) | ||
}; | ||
|
||
let Some(doc_attr) = variant.attrs.iter().find(|attr| attr.path().is_ident("doc")) else { | ||
return Err(Error::new(variant.span(), r#"expected a doc comment"#)) | ||
}; | ||
|
||
let Some((base_place, current)) = self.lower_expr_as_place(current, *base, true) else { | ||
return Ok(None) | ||
}; | ||
|
||
let Stmt::Expr(Expr::Call(ExprCall { args: some_args, .. }), _) = last_stmt else { | ||
return Err(Error::new(last_stmt.span(), "expected last expression to be `Some(match (..) { .. })`")) | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// rustfmt-single_line_let_else_max_width: 50 | ||
|
||
fn main() { | ||
let Some(a) = opt else {}; | ||
|
||
let Some(b) = opt else { return }; | ||
|
||
let Some(c) = opt else { | ||
return | ||
}; | ||
|
||
let Some(c) = opt else { | ||
// a comment should always force the block to be multi-lined | ||
return | ||
}; | ||
|
||
let Some(c) = opt else { /* a comment should always force the block to be multi-lined */ return }; | ||
|
||
let Some(d) = some_very_very_very_very_long_name else { return }; | ||
|
||
let Expr::Slice(ast::ExprSlice { lower, upper, step, range: _ }) = slice.as_ref() else { | ||
return | ||
}; | ||
|
||
let Some((base_place, current)) = self.lower_expr_as_place(current, *base, true)? else { | ||
return Ok(None) | ||
}; | ||
|
||
let Some(doc_attr) = variant.attrs.iter().find(|attr| attr.path().is_ident("doc")) else { | ||
return Err(Error::new(variant.span(), r#"expected a doc comment"#)) | ||
}; | ||
|
||
let Some((base_place, current)) = self.lower_expr_as_place(current, *base, true) else { | ||
return Ok(None) | ||
}; | ||
|
||
let Stmt::Expr(Expr::Call(ExprCall { args: some_args, .. }), _) = last_stmt else { | ||
return Err(Error::new(last_stmt.span(), "expected last expression to be `Some(match (..) { .. })`")) | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// rustfmt-single_line_let_else_max_width: 0 | ||
|
||
fn main() { | ||
let Some(a) = opt else {}; | ||
|
||
let Some(b) = opt else { return }; | ||
|
||
let Some(c) = opt else { | ||
return | ||
}; | ||
|
||
let Some(c) = opt else { | ||
// a comment should always force the block to be multi-lined | ||
return | ||
}; | ||
|
||
let Some(c) = opt else { /* a comment should always force the block to be multi-lined */ return }; | ||
|
||
let Some(d) = some_very_very_very_very_long_name else { return }; | ||
|
||
let Expr::Slice(ast::ExprSlice { lower, upper, step, range: _ }) = slice.as_ref() else { | ||
return | ||
}; | ||
|
||
let Some((base_place, current)) = self.lower_expr_as_place(current, *base, true)? else { | ||
return Ok(None) | ||
}; | ||
|
||
let Some(doc_attr) = variant.attrs.iter().find(|attr| attr.path().is_ident("doc")) else { | ||
return Err(Error::new(variant.span(), r#"expected a doc comment"#)) | ||
}; | ||
|
||
let Some((base_place, current)) = self.lower_expr_as_place(current, *base, true) else { | ||
return Ok(None) | ||
}; | ||
|
||
let Stmt::Expr(Expr::Call(ExprCall { args: some_args, .. }), _) = last_stmt else { | ||
return Err(Error::new(last_stmt.span(), "expected last expression to be `Some(match (..) { .. })`")) | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove the semicolon here.
in this context we want to demonstrate snippets that are eligible for single line based on all other rules, so that whether they are wrapped is solely dependent upon length and the value of the config option. the semis are load bearing though which means the snippets would always have to be wrapped regardless
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I can't remove the
;
. The configuration_snippet_tests would fail without it since rustfmt will add the;
by default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes I forget that we can't readily utilize other config options (e.g. trailing_semicolon) in these snippets.
I also don't recall whether we've discussed this before, but I think having the ability to specify additional options in the snippets (perhaps similar to what we do in the system&idempotence tests?) would be helpful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the idea has briefly come up before. It's a change that I'd really like to see! I think it'll also be very helpful if we need to demonstrate formatting differences between
version
(and eventuallystyle_edition
)