Skip to content

Commit 0604ce4

Browse files
feat: support configurability of leading pipe
1 parent 9124dd8 commit 0604ce4

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

rustfmt-core/rustfmt-config/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ create_config! {
8888
"Align enum variants discrims, if their diffs fit within threshold";
8989
match_arm_blocks: bool, true, false, "Wrap the body of arms in blocks when it does not fit on \
9090
the same line with the pattern of arms";
91+
strip_leading_match_arm_pipes: bool, true, true, "";
9192
force_multiline_blocks: bool, false, false,
9293
"Force multiline closure bodies and match arms to be wrapped in a block";
9394
fn_args_layout: Density, Density::Tall, true,
@@ -522,6 +523,7 @@ overflow_delimited_expr = false
522523
struct_field_align_threshold = 0
523524
enum_discrim_align_threshold = 0
524525
match_arm_blocks = true
526+
strip_leading_match_arm_pipes = true
525527
force_multiline_blocks = false
526528
fn_args_layout = "Tall"
527529
brace_style = "SameLineWhere"

rustfmt-core/rustfmt-lib/src/matches.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ impl<'a> Spanned for ArmWrapper<'a> {
5555

5656
impl<'a> Rewrite for ArmWrapper<'a> {
5757
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
58-
rewrite_match_arm(context, self.arm, shape, self.is_last)
58+
rewrite_match_arm(
59+
context,
60+
self.arm,
61+
shape,
62+
self.is_last,
63+
self.beginning_vert.is_some(),
64+
)
5965
}
6066
}
6167

@@ -215,6 +221,7 @@ fn rewrite_match_arm(
215221
arm: &ast::Arm,
216222
shape: Shape,
217223
is_last: bool,
224+
has_leading_pipe: bool,
218225
) -> Option<String> {
219226
let (missing_span, attrs_str) = if !arm.attrs.is_empty() {
220227
if contains_skip(&arm.attrs) {
@@ -232,9 +239,17 @@ fn rewrite_match_arm(
232239
(mk_sp(arm.span().lo(), arm.span().lo()), String::new())
233240
};
234241

242+
let needs_leading_pipe = has_leading_pipe && !context.config.strip_leading_match_arm_pipes();
243+
let (pipe_offset, pipe_str) = if needs_leading_pipe {
244+
(2, "| ")
245+
} else {
246+
(0, "")
247+
};
235248
// Patterns
236249
// 5 = ` => {`
237-
let pat_shape = shape.sub_width(5)?;
250+
// 2 = `| `
251+
let pat_shape = shape.sub_width(5)?.offset_left(pipe_offset)?;
252+
238253
let pats_str = arm.pat.rewrite(context, pat_shape)?;
239254

240255
// Guard
@@ -251,7 +266,7 @@ fn rewrite_match_arm(
251266
let lhs_str = combine_strs_with_missing_comments(
252267
context,
253268
&attrs_str,
254-
&format!("{}{}", pats_str, guard_str),
269+
&format!("{}{}{}", pipe_str, pats_str, guard_str),
255270
missing_span,
256271
shape,
257272
false,

0 commit comments

Comments
 (0)