Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 65ae0b9

Browse files
committed
Change enum align semantics to care about absolute length, rather than difference.
If we're only aligning enum discriminants that are "not too far apart (length-wise)", then this works really well for enums with consistently-long or consistently-short idents, but not for the mixed ones. However, consistently-long idents is somewhate of an uncommon case and overlong idents may be allowed to be formatted suboptimally if that makes mixed-length idents work better (and it does in this case).
1 parent cc22869 commit 65ae0b9

File tree

5 files changed

+68
-56
lines changed

5 files changed

+68
-56
lines changed

Configurations.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -887,28 +887,30 @@ See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_s
887887

888888
## `enum_discrim_align_threshold`
889889

890-
The maximum diff of width between enum variants to have discriminants aligned with each other.
890+
The maximum length of enum variant having discriminant, that gets vertically aligned with others.
891891
Variants without discriminants would be ignored for the purpose of alignment.
892892

893+
Note that this is not how much whitespace is inserted, but instead the longest variant name that
894+
doesn't get ignored when aligning.
895+
893896
- **Default value** : 0
894897
- **Possible values**: any positive integer
895898
- **Stable**: No
896899

897900
#### `0` (default):
898901

899902
```rust
900-
enum Foo {
903+
enum Bar {
901904
A = 0,
902905
Bb = 1,
903-
RandomLongVariantWithoutDiscriminant,
906+
RandomLongVariantGoesHere = 10,
904907
Ccc = 71,
905908
}
906909

907910
enum Bar {
908-
A = 0,
909-
Bb = 1,
910-
ThisOneisWithDiscriminantAndPreventsAlignment = 10,
911-
Ccc = 71,
911+
VeryLongVariantNameHereA = 0,
912+
VeryLongVariantNameHereBb = 1,
913+
VeryLongVariantNameHereCcc = 2,
912914
}
913915
```
914916

@@ -918,15 +920,14 @@ enum Bar {
918920
enum Foo {
919921
A = 0,
920922
Bb = 1,
921-
RandomLongVariantWithoutDiscriminant,
923+
RandomLongVariantGoesHere = 10,
922924
Ccc = 2,
923925
}
924926

925927
enum Bar {
926-
A = 0,
927-
Bb = 1,
928-
ThisOneisWithDiscriminantAndPreventsAlignment = 10,
929-
Ccc = 71,
928+
VeryLongVariantNameHereA = 0,
929+
VeryLongVariantNameHereBb = 1,
930+
VeryLongVariantNameHereCcc = 2,
930931
}
931932
```
932933

src/items.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -501,22 +501,21 @@ impl<'a> FmtVisitor<'a> {
501501
self.block_indent = self.block_indent.block_indent(self.config);
502502

503503
// If enum variants have discriminants, try to vertically align those,
504-
// provided it does not result in too much padding
505-
let pad_discrim_ident_to;
506-
let diff_threshold = self.config.enum_discrim_align_threshold();
507-
let discr_ident_lens: Vec<_> = enum_def
504+
// provided the discrims are not shifted too much to the right
505+
let align_threshold: usize = self.config.enum_discrim_align_threshold();
506+
let discr_ident_lens: Vec<usize> = enum_def
508507
.variants
509508
.iter()
510509
.filter(|var| var.node.disr_expr.is_some())
511510
.map(|var| rewrite_ident(&self.get_context(), var.node.ident).len())
512511
.collect();
513-
let shortest_w_discr = *discr_ident_lens.iter().min().unwrap_or(&0);
514-
let longest_w_discr = *discr_ident_lens.iter().max().unwrap_or(&0);
515-
if longest_w_discr > shortest_w_discr + diff_threshold {
516-
pad_discrim_ident_to = 0;
517-
} else {
518-
pad_discrim_ident_to = longest_w_discr;
519-
}
512+
// cut the list at the point of longest discrim shorter than the threshold
513+
// All of the discrims under the threshold will get padded, and all above - left as is.
514+
let pad_discrim_ident_to = *discr_ident_lens
515+
.iter()
516+
.filter(|&l| *l <= align_threshold)
517+
.max()
518+
.unwrap_or(&0);
520519

521520
let itemize_list_with = |one_line_width: usize| {
522521
itemize_list(

tests/source/configs/enum_discrim_align_threshold/20.rs renamed to tests/source/configs/enum_discrim_align_threshold/40.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
// rustfmt-enum_discrim_align_threshold: 20
1+
// rustfmt-enum_discrim_align_threshold: 40
22

33
enum Standard {
44
A = 1,
55
Bcdef = 2,
66
}
77

8-
enum Mixed {
9-
ThisIsAFairlyLongEnumVariantWithoutDiscrim,
8+
enum NoDiscrims {
9+
ThisIsAFairlyLongEnumVariantWithoutDiscrimLongerThan40,
1010
A = 1,
11-
ThisIsAFairlyLongEnumVariantWithoutDiscrim2,
11+
ThisIsAnotherFairlyLongEnumVariantWithoutDiscrimLongerThan40,
1212
Bcdef = 2,
1313
}
1414

1515
enum TooLong {
16-
ThisOneHasDiscrimAaaaaaaaaaaaaaaaaaaaaaaaaaaa = 10,
16+
ThisOneHasDiscrimAaaaaaaaaaaaaaaaaaaaaaChar40 = 10,
17+
A = 1,
18+
Bcdef = 2,
19+
}
20+
21+
enum Borderline {
22+
ThisOneHasDiscrimAaaaaaaaaaaaaaaaaaaaaa = 10,
1723
A = 1,
1824
Bcdef = 2,
1925
}

tests/target/configs/enum_discrim_align_threshold/20.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// rustfmt-enum_discrim_align_threshold: 40
2+
3+
enum Standard {
4+
A = 1,
5+
Bcdef = 2,
6+
}
7+
8+
enum NoDiscrims {
9+
ThisIsAFairlyLongEnumVariantWithoutDiscrimLongerThan40,
10+
A = 1,
11+
ThisIsAnotherFairlyLongEnumVariantWithoutDiscrimLongerThan40,
12+
Bcdef = 2,
13+
}
14+
15+
enum TooLong {
16+
ThisOneHasDiscrimAaaaaaaaaaaaaaaaaaaaaaChar40 = 10,
17+
A = 1,
18+
Bcdef = 2,
19+
}
20+
21+
enum Borderline {
22+
ThisOneHasDiscrimAaaaaaaaaaaaaaaaaaaaaa = 10,
23+
A = 1,
24+
Bcdef = 2,
25+
}
26+
27+
// Live specimen from #1686
28+
enum LongWithSmallDiff {
29+
SceneColorimetryEstimates = 0x73636F65,
30+
SceneAppearanceEstimates = 0x73617065,
31+
FocalPlaneColorimetryEstimates = 0x66706365,
32+
ReflectionHardcopyOriginalColorimetry = 0x72686F63,
33+
ReflectionPrintOutputColorimetry = 0x72706F63,
34+
}

0 commit comments

Comments
 (0)