Skip to content

Commit 5c552b9

Browse files
committed
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 5391847 commit 5c552b9

File tree

3 files changed

+46
-40
lines changed

3 files changed

+46
-40
lines changed

Diff for: src/expr.rs

+46-34
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,46 @@ impl<'a> ControlFlow<'a> {
10001000
}
10011001
}
10021002

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

1069-
let between_kwd_else_block = mk_sp(
1070-
self.block.span.hi(),
1071-
context
1072-
.snippet_provider
1073-
.span_before(mk_sp(self.block.span.hi(), else_block.span.lo()), "else"),
1074-
);
1075-
let between_kwd_else_block_comment =
1076-
extract_comment(between_kwd_else_block, context, shape);
1077-
1078-
let after_else = mk_sp(
1079-
context
1080-
.snippet_provider
1081-
.span_after(mk_sp(self.block.span.hi(), else_block.span.lo()), "else"),
1082-
else_block.span.lo(),
1109+
let else_kw = rewrite_else_kw_with_comments(
1110+
last_in_chain,
1111+
context,
1112+
self.block.span.between(else_block.span),
1113+
shape,
10831114
);
1084-
let after_else_comment = extract_comment(after_else, context, shape);
1085-
1086-
let between_sep = match context.config.control_brace_style() {
1087-
ControlBraceStyle::AlwaysNextLine | ControlBraceStyle::ClosingNextLine => {
1088-
&*alt_block_sep
1089-
}
1090-
ControlBraceStyle::AlwaysSameLine => " ",
1091-
};
1092-
let after_sep = match context.config.control_brace_style() {
1093-
ControlBraceStyle::AlwaysNextLine if last_in_chain => &*alt_block_sep,
1094-
_ => " ",
1095-
};
1096-
1097-
result.push_str(&format!(
1098-
"{}else{}",
1099-
between_kwd_else_block_comment
1100-
.as_ref()
1101-
.map_or(between_sep, |s| &**s),
1102-
after_else_comment.as_ref().map_or(after_sep, |s| &**s),
1103-
));
1115+
result.push_str(&else_kw);
11041116
result.push_str(&rewrite?);
11051117
}
11061118

Diff for: tests/source/let_else.rs

-3
This file was deleted.

Diff for: tests/target/let_else.rs

-3
This file was deleted.

0 commit comments

Comments
 (0)