Skip to content

Commit d6a220b

Browse files
feat: support config. of leading match arm pipe
1 parent f6b9625 commit d6a220b

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ create_config! {
9191
"Align enum variants discrims, if their diffs fit within threshold";
9292
match_arm_blocks: bool, true, false, "Wrap the body of arms in blocks when it does not fit on \
9393
the same line with the pattern of arms";
94+
match_arm_leading_pipes: MatchArmLeadingPipe, MatchArmLeadingPipe::Never, true,
95+
"Determines whether leading pipes are emitted on match arms";
9496
force_multiline_blocks: bool, false, false,
9597
"Force multiline closure bodies and match arms to be wrapped in a block";
9698
fn_args_layout: Density, Density::Tall, true,
@@ -528,6 +530,7 @@ overflow_delimited_expr = false
528530
struct_field_align_threshold = 0
529531
enum_discrim_align_threshold = 0
530532
match_arm_blocks = true
533+
match_arm_leading_pipes = "Never"
531534
force_multiline_blocks = false
532535
fn_args_layout = "Tall"
533536
brace_style = "SameLineWhere"

src/config/options.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,14 @@ impl Edition {
392392
}
393393
}
394394
}
395+
396+
/// Controls how rustfmt should handle leading pipes on match arms.
397+
#[config_type]
398+
pub enum MatchArmLeadingPipe {
399+
/// Place leading pipes on all match arms
400+
Always,
401+
/// Never emit leading pipes on match arms
402+
Never,
403+
/// Preserve any existing leading pipes
404+
Preserve,
405+
}

src/matches.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::{BytePos, Span};
77

88
use crate::comment::{combine_strs_with_missing_comments, rewrite_comment};
99
use crate::config::lists::*;
10-
use crate::config::{Config, ControlBraceStyle, IndentStyle, Version};
10+
use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, Version};
1111
use crate::expr::{
1212
format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line, rewrite_cond,
1313
ExprType, RhsTactics,
@@ -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+
// Leading pipe offset
243+
// 2 = `| `
244+
let (pipe_offset, pipe_str) = match context.config.match_arm_leading_pipes() {
245+
MatchArmLeadingPipe::Never => (0, ""),
246+
MatchArmLeadingPipe::Preserve if !has_leading_pipe => (0, ""),
247+
MatchArmLeadingPipe::Preserve | MatchArmLeadingPipe::Always => (2, "| "),
248+
};
249+
235250
// Patterns
236251
// 5 = ` => {`
237-
let pat_shape = shape.sub_width(5)?;
252+
let pat_shape = shape.sub_width(5)?.offset_left(pipe_offset)?;
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)