Skip to content

Commit 75870c5

Browse files
ytmimicalebcartwright
authored andcommitted
Extract logic for rewriting else keyword into function
The function properly handles recovering comments before and after the `else` keyword, and properly handles how to write the else when users configure `control_brace_style`.
1 parent 3f7c366 commit 75870c5

File tree

3 files changed

+46
-40
lines changed

3 files changed

+46
-40
lines changed

src/expr.rs

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,46 @@ impl<'a> ControlFlow<'a> {
10041004
}
10051005
}
10061006

1007+
/// Rewrite the `else` keyword with surrounding comments.
1008+
///
1009+
/// is_last: true if this is an `else` and `false` if this is an `else if` block.
1010+
/// context: rewrite context
1011+
/// span: Span between the end of the last expression and the start of the else block,
1012+
/// which contains the `else` keyword
1013+
/// shape: Shape
1014+
pub(crate) fn rewrite_else_kw_with_comments(
1015+
is_last: bool,
1016+
context: &RewriteContext<'_>,
1017+
span: Span,
1018+
shape: Shape,
1019+
) -> String {
1020+
let else_kw_lo = context.snippet_provider.span_before(span, "else");
1021+
let before_else_kw = mk_sp(span.lo(), else_kw_lo);
1022+
let before_else_kw_comment = extract_comment(before_else_kw, context, shape);
1023+
1024+
let else_kw_hi = context.snippet_provider.span_after(span, "else");
1025+
let after_else_kw = mk_sp(else_kw_hi, span.hi());
1026+
let after_else_kw_comment = extract_comment(after_else_kw, context, shape);
1027+
1028+
let newline_sep = &shape.indent.to_string_with_newline(context.config);
1029+
let before_sep = match context.config.control_brace_style() {
1030+
ControlBraceStyle::AlwaysNextLine | ControlBraceStyle::ClosingNextLine => {
1031+
newline_sep.as_ref()
1032+
}
1033+
ControlBraceStyle::AlwaysSameLine => " ",
1034+
};
1035+
let after_sep = match context.config.control_brace_style() {
1036+
ControlBraceStyle::AlwaysNextLine if is_last => newline_sep.as_ref(),
1037+
_ => " ",
1038+
};
1039+
1040+
format!(
1041+
"{}else{}",
1042+
before_else_kw_comment.as_ref().map_or(before_sep, |s| &**s),
1043+
after_else_kw_comment.as_ref().map_or(after_sep, |s| &**s),
1044+
)
1045+
}
1046+
10071047
impl<'a> Rewrite for ControlFlow<'a> {
10081048
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
10091049
debug!("ControlFlow::rewrite {:?} {:?}", self, shape);
@@ -1070,41 +1110,13 @@ impl<'a> Rewrite for ControlFlow<'a> {
10701110
}
10711111
};
10721112

1073-
let between_kwd_else_block = mk_sp(
1074-
self.block.span.hi(),
1075-
context
1076-
.snippet_provider
1077-
.span_before(mk_sp(self.block.span.hi(), else_block.span.lo()), "else"),
1078-
);
1079-
let between_kwd_else_block_comment =
1080-
extract_comment(between_kwd_else_block, context, shape);
1081-
1082-
let after_else = mk_sp(
1083-
context
1084-
.snippet_provider
1085-
.span_after(mk_sp(self.block.span.hi(), else_block.span.lo()), "else"),
1086-
else_block.span.lo(),
1113+
let else_kw = rewrite_else_kw_with_comments(
1114+
last_in_chain,
1115+
context,
1116+
self.block.span.between(else_block.span),
1117+
shape,
10871118
);
1088-
let after_else_comment = extract_comment(after_else, context, shape);
1089-
1090-
let between_sep = match context.config.control_brace_style() {
1091-
ControlBraceStyle::AlwaysNextLine | ControlBraceStyle::ClosingNextLine => {
1092-
&*alt_block_sep
1093-
}
1094-
ControlBraceStyle::AlwaysSameLine => " ",
1095-
};
1096-
let after_sep = match context.config.control_brace_style() {
1097-
ControlBraceStyle::AlwaysNextLine if last_in_chain => &*alt_block_sep,
1098-
_ => " ",
1099-
};
1100-
1101-
result.push_str(&format!(
1102-
"{}else{}",
1103-
between_kwd_else_block_comment
1104-
.as_ref()
1105-
.map_or(between_sep, |s| &**s),
1106-
after_else_comment.as_ref().map_or(after_sep, |s| &**s),
1107-
));
1119+
result.push_str(&else_kw);
11081120
result.push_str(&rewrite?);
11091121
}
11101122

tests/source/let_else.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/target/let_else.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)