Skip to content
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

Implement formatting for let_chains #5495

Closed
wants to merge 2 commits into from
Closed
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
94 changes: 39 additions & 55 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,26 @@ pub(crate) fn format_expr(
ast::ExprKind::Tup(ref items) => {
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
}
ast::ExprKind::Let(..) => None,
ast::ExprKind::Let(ref pat, ref expr, ref _span) => {
let kw = "let ";
let equals = " =";
let pat_shape = shape.offset_left(kw.len())?.sub_width(equals.len())?;
let pat_string = pat.rewrite(context, pat_shape)?;
let comments_lo = context
.snippet_provider
.span_after(expr.span.with_lo(pat.span.hi()), "=");
let comments_span = mk_sp(comments_lo, expr.span.lo());
rewrite_assign_rhs_with_comments(
context,
&format!("{}{}{}", kw, pat_string, equals),
expr,
shape,
&RhsAssignKind::Expr(&expr.kind, expr.span),
RhsTactics::Default,
comments_span,
true,
)
}
ast::ExprKind::If(..)
| ast::ExprKind::ForLoop(..)
| ast::ExprKind::Loop(..)
Expand Down Expand Up @@ -624,76 +643,58 @@ struct ControlFlow<'a> {
block: &'a ast::Block,
else_block: Option<&'a ast::Expr>,
label: Option<ast::Label>,
/// the `<pat>` in a `for <pat> in <expr>`; in that case, `cond` refers to only the `<expr>`
pat: Option<&'a ast::Pat>,
keyword: &'a str,
matcher: &'a str,
/// the text between `pat` and `cond` (i.e. " in" `for`)
connector: &'a str,
allow_single_line: bool,
// HACK: `true` if this is an `if` expression in an `else if`.
nested_if: bool,
span: Span,
}

fn extract_pats_and_cond(expr: &ast::Expr) -> (Option<&ast::Pat>, &ast::Expr) {
match expr.kind {
ast::ExprKind::Let(ref pat, ref cond, _) => (Some(pat), cond),
_ => (None, expr),
}
}

// FIXME: Refactor this.
fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow<'_>> {
match expr.kind {
ast::ExprKind::If(ref cond, ref if_block, ref else_block) => {
let (pat, cond) = extract_pats_and_cond(cond);
Some(ControlFlow::new_if(
cond,
pat,
if_block,
else_block.as_ref().map(|e| &**e),
expr_type == ExprType::SubExpression,
false,
expr.span,
))
}
ast::ExprKind::If(ref cond, ref if_block, ref else_block) => Some(ControlFlow::new_if(
cond,
if_block,
else_block.as_ref().map(|e| &**e),
expr_type == ExprType::SubExpression,
false,
expr.span,
)),
ast::ExprKind::ForLoop(ref pat, ref cond, ref block, label) => {
Some(ControlFlow::new_for(pat, cond, block, label, expr.span))
}
ast::ExprKind::Loop(ref block, label) => {
Some(ControlFlow::new_loop(block, label, expr.span))
}
ast::ExprKind::While(ref cond, ref block, label) => {
let (pat, cond) = extract_pats_and_cond(cond);
Some(ControlFlow::new_while(pat, cond, block, label, expr.span))
Some(ControlFlow::new_while(cond, block, label, expr.span))
}
_ => None,
}
}

fn choose_matcher(pat: Option<&ast::Pat>) -> &'static str {
pat.map_or("", |_| "let")
}

impl<'a> ControlFlow<'a> {
fn new_if(
cond: &'a ast::Expr,
pat: Option<&'a ast::Pat>,
block: &'a ast::Block,
else_block: Option<&'a ast::Expr>,
allow_single_line: bool,
nested_if: bool,
span: Span,
) -> ControlFlow<'a> {
let matcher = choose_matcher(pat);
ControlFlow {
cond: Some(cond),
block,
else_block,
label: None,
pat,
pat: None,
keyword: "if",
matcher,
connector: " =",
connector: "",
allow_single_line,
nested_if,
span,
Expand All @@ -708,7 +709,6 @@ impl<'a> ControlFlow<'a> {
label,
pat: None,
keyword: "loop",
matcher: "",
connector: "",
allow_single_line: false,
nested_if: false,
Expand All @@ -717,22 +717,19 @@ impl<'a> ControlFlow<'a> {
}

fn new_while(
pat: Option<&'a ast::Pat>,
cond: &'a ast::Expr,
block: &'a ast::Block,
label: Option<ast::Label>,
span: Span,
) -> ControlFlow<'a> {
let matcher = choose_matcher(pat);
ControlFlow {
cond: Some(cond),
block,
else_block: None,
label,
pat,
pat: None,
keyword: "while",
matcher,
connector: " =",
connector: "",
allow_single_line: false,
nested_if: false,
span,
Expand All @@ -753,7 +750,6 @@ impl<'a> ControlFlow<'a> {
label,
pat: Some(pat),
keyword: "for",
matcher: "",
connector: " in",
allow_single_line: false,
nested_if: false,
Expand Down Expand Up @@ -831,22 +827,16 @@ impl<'a> ControlFlow<'a> {

let cond_shape = shape.offset_left(offset)?;
if let Some(pat) = self.pat {
let matcher = if self.matcher.is_empty() {
self.matcher.to_owned()
} else {
format!("{} ", self.matcher)
};
let pat_shape = cond_shape
.offset_left(matcher.len())?
.sub_width(self.connector.len())?;
// `for <pat> in <expr>`
let pat_shape = cond_shape.sub_width(self.connector.len())?;
let pat_string = pat.rewrite(context, pat_shape)?;
let comments_lo = context
.snippet_provider
.span_after(self.span.with_lo(pat.span.hi()), self.connector.trim());
let comments_span = mk_sp(comments_lo, expr.span.lo());
return rewrite_assign_rhs_with_comments(
context,
&format!("{}{}{}", matcher, pat_string, self.connector),
&format!("{}{}", pat_string, self.connector),
expr,
cond_shape,
&RhsAssignKind::Expr(&expr.kind, expr.span),
Expand Down Expand Up @@ -946,12 +936,8 @@ impl<'a> ControlFlow<'a> {
.span_after(mk_sp(lo, self.span.hi()), self.keyword.trim()),
if self.pat.is_none() {
cond_span.lo()
} else if self.matcher.is_empty() {
self.pat.unwrap().span.lo()
} else {
context
.snippet_provider
.span_before(self.span, self.matcher.trim())
self.pat.unwrap().span.lo()
},
);

Expand Down Expand Up @@ -1040,10 +1026,8 @@ impl<'a> Rewrite for ControlFlow<'a> {
// Note how we're passing the original shape, as the
// cost of "else" should not cascade.
ast::ExprKind::If(ref cond, ref if_block, ref next_else_block) => {
let (pats, cond) = extract_pats_and_cond(cond);
ControlFlow::new_if(
cond,
pats,
if_block,
next_else_block.as_ref().map(|e| &**e),
false,
Expand Down
9 changes: 9 additions & 0 deletions tests/source/let_chains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() {
if let Some( 1) = Some(1)
&&
let Some(2 ) = Some( 2 )
&&true && let true = false &&
let false = true{
let _x=1+ 1; let _y =2;
}
}
11 changes: 11 additions & 0 deletions tests/target/let_chains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
if let Some(1) = Some(1)
&& let Some(2) = Some(2)
&& true
&& let true = false
&& let false = true
{
let _x = 1 + 1;
let _y = 2;
}
}