Skip to content

Commit 32dfaff

Browse files
committed
Make sure enums are always laid out vertically
Close #266
1 parent d2ddca2 commit 32dfaff

6 files changed

Lines changed: 47 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This file documents the changes made to the formatter with each release.
2121
- Remove all `unwrap()` calls from the project and follow stricter linting rules (#145)
2222
- Remove multiple third-party dependencies and greatly improve compilation speed
2323
- Improve performance by an order of magnitude (10x to over 50x speedup depending on the case)
24+
- Always lay enums vertically (one member per line) (#266)
2425

2526
### Fixed
2627

src/formatter.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,9 @@ fn process_container(
12381238
) {
12391239
let node_kind = GDScriptNodeKind::get_kind_from_ast_node(node);
12401240
let child_count = node.child_count();
1241-
if child_count < 3 {
1241+
// The style guide requires enum members to be written vertically, even
1242+
// when the list would fit on one line. Empty enums are the only exception.
1243+
if child_count < 3 && node_kind != GDScriptNodeKind::EnumeratorList {
12421244
if let Some(open) = node.child(0) {
12431245
process_node(input, open, render_elements);
12441246
}
@@ -1371,6 +1373,7 @@ fn process_container(
13711373
// for a forced line break.
13721374
const MIN_CHILD_COUNT_BEYOND_SINGLE_ELEMENT: usize = 4;
13731375
let contains_more_than_one_element = child_count >= MIN_CHILD_COUNT_BEYOND_SINGLE_ELEMENT;
1376+
let is_non_empty_enum = node_kind == GDScriptNodeKind::EnumeratorList;
13741377
// A single lambda in a collection will cause a syntax error unless we wrap
13751378
// it in parentheses or insert a trailing comma on the last line
13761379
let mut is_array_with_single_lambda = false;
@@ -1395,12 +1398,20 @@ fn process_container(
13951398
render_elements.push(RenderElement::TextStatic(","));
13961399
}
13971400

1398-
if contains_more_than_one_element && let Some(open) = node.child(0) {
1401+
// A single enum member also needs a trailing comma in its mandatory
1402+
// multiline layout.
1403+
if is_non_empty_enum && !contains_more_than_one_element && !trailing_comma_handled {
1404+
render_elements.push(RenderElement::TextStatic(","));
1405+
}
1406+
1407+
if (contains_more_than_one_element || is_non_empty_enum)
1408+
&& let Some(open) = node.child(0)
1409+
{
13991410
let close_byte = node
14001411
.child((child_count - 1) as u32)
14011412
.expect("container node has close delimiter")
14021413
.start_byte();
1403-
if has_newline(input.source, open.end_byte(), close_byte) {
1414+
if is_non_empty_enum || has_newline(input.source, open.end_byte(), close_byte) {
14041415
render_elements.push(RenderElement::ForceBreakingParent);
14051416
}
14061417
}

tests/expected/enums.gd

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# Enum spacing and wrapping
2-
# Single line enum get spaces between members, multi-line enums get 1 indent and
3-
# trailing commas.
4-
enum { A, B, C }
2+
# Enums are always vertical, with one indent and trailing commas.
3+
enum {
4+
A,
5+
B,
6+
C,
7+
}
58

6-
enum Named { A, B, C }
9+
enum Named {
10+
A,
11+
B,
12+
C,
13+
}
714

815
enum ThirdEnum {
916
Aaaaaaaaaaaaaaaaaaaaaa,
@@ -25,3 +32,7 @@ enum Test {
2532
A,
2633
B,
2734
}
35+
36+
enum Single {
37+
ONLY,
38+
}

tests/expected/trailing_comma.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ enum Foo2 {
4242
C, # comment
4343
}
4444

45-
enum Foo3 { A, B, C }
45+
enum Foo3 {
46+
A,
47+
B,
48+
C,
49+
}
4650

4751

4852
func foo(

tests/input/enums.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Enum spacing and wrapping
2-
# Single line enum get spaces between members, multi-line enums get 1 indent and
3-
# trailing commas.
2+
# Enums are always vertical, with one indent and trailing commas.
43
enum {A,B,C}
54

65

@@ -23,3 +22,5 @@ func foo():
2322

2423
enum Test {
2524
A, B }
25+
26+
enum Single { ONLY }

tests/reorder_code/expected/reorder_complete.gd

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ extends Node
66

77
signal test_signal(value: int)
88

9-
enum TestEnum { OPTION_A, OPTION_B, OPTION_C }
10-
enum { UNNAMED_A, UNNAMED_B }
9+
enum TestEnum {
10+
OPTION_A,
11+
OPTION_B,
12+
OPTION_C,
13+
}
14+
enum {
15+
UNNAMED_A,
16+
UNNAMED_B,
17+
}
1118

1219
const TEST_CONSTANT = 42
1320
const _PRIVATE_CONSTANT = "hidden"

0 commit comments

Comments
 (0)