Skip to content

[SourceKit/CodeFormat] Column-align multiple patterns in catch clauses #30870

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
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
72 changes: 48 additions & 24 deletions lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1809,38 +1809,27 @@ class FormatWalker : public ASTWalker {

auto *CS = dyn_cast<CaseStmt>(S);
if (CS && CS->getParentKind() == CaseParentKind::Switch) {
if (TrailingTarget && !TrailingTarget->isEmpty())
return None;

SourceLoc CaseLoc = CS->getLoc();
if (!SM.isBeforeInBuffer(CaseLoc, TargetLocation))
return None;

SourceRange LabelItemsRange = CS->getLabelItemsRange();
SourceLoc ColonLoc = getLocIfKind(SM, LabelItemsRange.End, tok::colon);

if (isTargetContext(CaseLoc, ColonLoc)) {
ListAligner Aligner(SM, TargetLocation, CaseLoc, CaseLoc, ColonLoc);
for (auto &Elem: CS->getCaseLabelItems()) {
SourceRange ElemRange = Elem.getSourceRange();
Aligner.updateAlignment(ElemRange, CS);
if (isTargetContext(ElemRange)) {
Aligner.setAlignmentIfNeeded(CtxOverride);
return IndentContext {
ElemRange.Start,
!OutdentChecker::hasOutdent(SM, ElemRange, CS)
};
}
}
return Aligner.getContextAndSetAlignment(CtxOverride);
}
if (ColonLoc.isValid() && isTargetContext(ColonLoc, SourceLoc())) {
if (auto Ctx = getIndentContextFromCaseItems(CS, /*CloseLoc=*/ColonLoc))
return Ctx;

if (ColonLoc.isValid() && isTargetContext(ColonLoc, SourceLoc()) &&
(!TrailingTarget || TrailingTarget->isEmpty())) {
SourceRange ColonToEnd = SourceRange(ColonLoc, CS->getEndLoc());
return IndentContext {
CaseLoc,
!OutdentChecker::hasOutdent(SM, ColonToEnd, CS)
};
}

if (TrailingTarget)
return None;
return IndentContext {CaseLoc, false};
}

Expand All @@ -1858,13 +1847,21 @@ class FormatWalker : public ASTWalker {
}

if (CS && CS->getParentKind() == CaseParentKind::DoCatch) {
if (auto *BS = dyn_cast<BraceStmt>(CS->getBody())) {
if (auto Ctx = getIndentContextFrom(BS, CS->getStartLoc()))
return Ctx;
}
SourceLoc CatchLoc = CS->getLoc();
SourceLoc L;

auto *BS = dyn_cast<BraceStmt>(CS->getBody());
if (BS) L = getLocIfKind(SM, BS->getLBraceLoc(), tok::l_brace);

if (auto Ctx = getIndentContextFromCaseItems(CS, /*CloseLoc=*/L))
return Ctx;

if (auto Ctx = getIndentContextFrom(BS, CS->getStartLoc()))
return Ctx;

if (TrailingTarget)
return None;
return IndentContext {CS->getStartLoc(), true};
return IndentContext {CatchLoc, false};
}

if (auto *IS = dyn_cast<IfStmt>(S)) {
Expand Down Expand Up @@ -2086,6 +2083,33 @@ class FormatWalker : public ASTWalker {
return Bounds;
}

Optional<IndentContext>
getIndentContextFromCaseItems(CaseStmt *CS, SourceLoc CloseLoc) {
SourceLoc IntroducerLoc = CS->getLoc();
if (!isTargetContext(IntroducerLoc, CloseLoc))
return None;

ListAligner Aligner(SM, TargetLocation, IntroducerLoc, IntroducerLoc,
CloseLoc);
for (auto &Elem: CS->getCaseLabelItems()) {
// Skip the implicit 'error' pattern for empty catch CaseStmts.
if ((!Elem.getPattern() || Elem.getPattern()->isImplicit()) &&
Elem.getWhereLoc().isInvalid())
continue;

SourceRange ElemRange = Elem.getSourceRange();
Aligner.updateAlignment(ElemRange, CS);
if (isTargetContext(ElemRange)) {
Aligner.setAlignmentIfNeeded(CtxOverride);
return IndentContext {
ElemRange.Start,
!OutdentChecker::hasOutdent(SM, ElemRange, CS)
};
}
}
return Aligner.getContextAndSetAlignment(CtxOverride);
}

#pragma mark Expression indent contexts

Optional<IndentContext>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
do {
print("hello")
} catch MyErr.a(let code),
MyErr.b(let code),

// RUN: %sourcekitd-test -req=format -line=5 -length=1 %s | %FileCheck --strict-whitespace %s
// CHECK: key.sourcetext: " "
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
do {
print("hello")
} catch MyErr.a(let code),
MyErr.b(let code)

// RUN: %sourcekitd-test -req=format -line=5 -length=1 %s | %FileCheck --strict-whitespace %s
// CHECK: key.sourcetext: ""
33 changes: 33 additions & 0 deletions test/swift-indent/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -939,3 +939,36 @@ IncrementedFirst++
}++
.baz()


// Multiple patterns in catch should align exactly.

do {
print("hello")
} catch MyErr.a(let code, let message),
MyErr.b(
let code,
let message
),
MyErr.c(let code, let message) {
print("ahhh!")
}

do {
throw MyErr.a
} catch where foo == 0,
where bar == 1 {
}

do
{
print("hello")
}
catch MyErr.a(let code, let message),
MyErr.b(
let code,
let message
),
MyErr.c(let code, let message)
{
print("ahhh!")
}