Skip to content

[SourceKit/CodeFormat] Column-align enum element decls and the items in their parameter lists. #30891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,34 @@ class FormatWalker : public ASTWalker {
return IndentContext {ContextLoc, false};
}

if (auto *ECD = dyn_cast<EnumCaseDecl>(D)) {
SourceLoc CaseLoc = ECD->getLoc();

if (TrailingTarget)
return None;

ListAligner Aligner(SM, TargetLocation, CaseLoc, CaseLoc);
for (auto *Elem: ECD->getElements()) {
SourceRange ElemRange = Elem->getSourceRange();
Aligner.updateAlignment(ElemRange, Elem);
}
return Aligner.getContextAndSetAlignment(CtxOverride);
}

if (auto *EED = dyn_cast<EnumElementDecl>(D)) {
SourceLoc ContextLoc = EED->getStartLoc();
if (auto Ctx = getIndentContextFrom(EED->getParameterList()))
return Ctx;

if (TrailingTarget)
return None;

return IndentContext {
ContextLoc,
!OutdentChecker::hasOutdent(SM, EED)
};
}

if (auto *SD = dyn_cast<SubscriptDecl>(D)) {
SourceLoc ContextLoc = SD->getStartLoc();

Expand Down
21 changes: 21 additions & 0 deletions test/swift-indent/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ bax(34949494949)
.baz


// Enum element parameters should be aligned, and raw values should be indented.

enum TestEnum {
case first(x: Int,
y: Int,
z: Int),
second(
x: Int,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 columns?

Copy link
Contributor Author

@nathawes nathawes Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

12 total. Lines that aren't column aligned themselves round down to the nearest tab-width. Not sure that's the best behavior, but it's not new.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks.

y: Int
)
case third
}

enum RawEnum: String {
case aCaseWithAParticularlyLongNameSoTheValueIsWrapped =
"a long message here",
aNotherCaseWithAParticularlyLongNameSoTheValueIsWrapped =
"a long message here"
}


// Condition elements should align with each other.
//
guard let x = Optional.some(10), x > 100,
Expand Down