Skip to content

Commit aabdda7

Browse files
feat: support configurability of leading pipe
1 parent 2772c94 commit aabdda7

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ create_config! {
106106
"Align enum variants discrims, if their diffs fit within threshold";
107107
match_arm_blocks: bool, true, false, "Wrap the body of arms in blocks when it does not fit on \
108108
the same line with the pattern of arms";
109+
match_arm_leading_pipes: MatchArmLeadingPipe, MatchArmLeadingPipe::Never, true,
110+
"Determines whether leading pipes are emitted on match arms";
109111
force_multiline_blocks: bool, false, false,
110112
"Force multiline closure bodies and match arms to be wrapped in a block";
111113
fn_params_layout: Density, Density::Tall, true,
@@ -551,6 +553,7 @@ overflow_delimited_expr = false
551553
struct_field_align_threshold = 0
552554
enum_discrim_align_threshold = 0
553555
match_arm_blocks = true
556+
match_arm_leading_pipes = "Never"
554557
force_multiline_blocks = false
555558
fn_params_layout = "Tall"
556559
brace_style = "SameLineWhere"

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,14 @@ impl From<Edition> for rustc_span::edition::Edition {
374374
}
375375
}
376376
}
377+
378+
/// Controls how rustfmt should handle leading pipes on match arms.
379+
#[config_type]
380+
pub enum MatchArmLeadingPipe {
381+
/// Place leading pipes on all match arms
382+
Always,
383+
/// Never emit leading pipes on match arms
384+
Never,
385+
/// Maintain any existing leading pipes
386+
KeepExisting,
387+
}

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

Lines changed: 20 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};
10+
use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe};
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,18 @@ 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::KeepExisting if !has_leading_pipe => (0, ""),
247+
MatchArmLeadingPipe::KeepExisting | 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)?;
253+
238254
let pats_str = arm.pat.rewrite(context, pat_shape)?;
239255

240256
// Guard
@@ -251,7 +267,7 @@ fn rewrite_match_arm(
251267
let lhs_str = combine_strs_with_missing_comments(
252268
context,
253269
&attrs_str,
254-
&format!("{}{}", pats_str, guard_str),
270+
&format!("{}{}{}", pipe_str, pats_str, guard_str),
255271
missing_span,
256272
shape,
257273
false,

0 commit comments

Comments
 (0)