Skip to content

Commit 6f50aa4

Browse files
author
Nathan Hawes
authored
Merge pull request #30891 from nathawes/indent-enum-case-decl
[SourceKit/CodeFormat] Column-align enum element decls and the items in their parameter lists.
2 parents b3b7aa8 + c6a06e0 commit 6f50aa4

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lib/IDE/Formatting.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,34 @@ class FormatWalker : public ASTWalker {
14991499
return IndentContext {ContextLoc, false};
15001500
}
15011501

1502+
if (auto *ECD = dyn_cast<EnumCaseDecl>(D)) {
1503+
SourceLoc CaseLoc = ECD->getLoc();
1504+
1505+
if (TrailingTarget)
1506+
return None;
1507+
1508+
ListAligner Aligner(SM, TargetLocation, CaseLoc, CaseLoc);
1509+
for (auto *Elem: ECD->getElements()) {
1510+
SourceRange ElemRange = Elem->getSourceRange();
1511+
Aligner.updateAlignment(ElemRange, Elem);
1512+
}
1513+
return Aligner.getContextAndSetAlignment(CtxOverride);
1514+
}
1515+
1516+
if (auto *EED = dyn_cast<EnumElementDecl>(D)) {
1517+
SourceLoc ContextLoc = EED->getStartLoc();
1518+
if (auto Ctx = getIndentContextFrom(EED->getParameterList()))
1519+
return Ctx;
1520+
1521+
if (TrailingTarget)
1522+
return None;
1523+
1524+
return IndentContext {
1525+
ContextLoc,
1526+
!OutdentChecker::hasOutdent(SM, EED)
1527+
};
1528+
}
1529+
15021530
if (auto *SD = dyn_cast<SubscriptDecl>(D)) {
15031531
SourceLoc ContextLoc = SD->getStartLoc();
15041532

test/swift-indent/basic.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ bax(34949494949)
8383
.baz
8484

8585

86+
// Enum element parameters should be aligned, and raw values should be indented.
87+
88+
enum TestEnum {
89+
case first(x: Int,
90+
y: Int,
91+
z: Int),
92+
second(
93+
x: Int,
94+
y: Int
95+
)
96+
case third
97+
}
98+
99+
enum RawEnum: String {
100+
case aCaseWithAParticularlyLongNameSoTheValueIsWrapped =
101+
"a long message here",
102+
aNotherCaseWithAParticularlyLongNameSoTheValueIsWrapped =
103+
"a long message here"
104+
}
105+
106+
86107
// Condition elements should align with each other.
87108
//
88109
guard let x = Optional.some(10), x > 100,

0 commit comments

Comments
 (0)