Skip to content

Fix indentation of multiline strings in enum case raw values. #555

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
Jun 29, 2023
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
23 changes: 22 additions & 1 deletion Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,26 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
arrangeEnumCaseParameterClause(associatedValue, forcesBreakBeforeRightParen: false)
}

if let initializer = node.rawValue {
if let (unindentingNode, _, breakKind, shouldGroup) =
stackedIndentationBehavior(rhs: initializer.value)
{
var openTokens: [Token] = [.break(.open(kind: breakKind))]
if shouldGroup {
openTokens.append(.open)
}
after(initializer.equal, tokens: openTokens)

var closeTokens: [Token] = [.break(.close(mustBreak: false), size: 0)]
if shouldGroup {
closeTokens.append(.close)
}
after(unindentingNode.lastToken(viewMode: .sourceAccurate), tokens: closeTokens)
} else {
after(initializer.equal, tokens: .break(.continue))
}
}

return .visitChildren
}

Expand Down Expand Up @@ -2303,12 +2323,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
override func visit(_ node: InitializerClauseSyntax) -> SyntaxVisitorContinueKind {
before(node.equal, tokens: .space)

// InitializerClauses that are children of a PatternBindingSyntax or
// InitializerClauses that are children of a PatternBindingSyntax, EnumCaseElementSyntax, or
// OptionalBindingConditionSyntax are already handled in the latter node, to ensure that
// continuations stack appropriately.
if let parent = node.parent,
!parent.is(PatternBindingSyntax.self)
&& !parent.is(OptionalBindingConditionSyntax.self)
&& !parent.is(EnumCaseElementSyntax.self)
{
after(node.equal, tokens: .break)
}
Expand Down
11 changes: 11 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ final class StringTests: PrettyPrintTestCase {
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 100)
}

func testMultilineStringsAsEnumRawValues() {
let input = #"""
enum E: String {
case x = """
blah blah
"""
}
"""#
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 100)
}

func testMultilineStringsNestedInAnotherWrappingContext() {
let input =
#"""
Expand Down