Skip to content

Fix CodeGenerationFormat to not add extra indentation, leave trailing spaces when adding newlines, or indent empty lines #2833

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
41 changes: 33 additions & 8 deletions CodeGeneration/Sources/Utils/CodeGenerationFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ public class CodeGenerationFormat: BasicFormat {
}
}

public override func requiresIndent(_ node: some SyntaxProtocol) -> Bool {
switch node.kind {
case .arrayElementList, .dictionaryElementList, .functionParameterList, .labeledExprList:
let indentManually = node.children(viewMode: .sourceAccurate).count > maxElementsOnSameLine
if indentManually {
return false
}
let startsOnNewline =
node.leadingTrivia.contains(where: \.isNewline)
|| node.previousToken(viewMode: .sourceAccurate)?.trailingTrivia.contains(where: \.isNewline) ?? false
if !startsOnNewline {
return false
}
default:
break
}
return super.requiresIndent(node)
}

// MARK: - Private

private func shouldBeSeparatedByTwoNewlines(node: CodeBlockItemSyntax) -> Bool {
Expand All @@ -114,9 +133,9 @@ public class CodeGenerationFormat: BasicFormat {

private func ensuringTwoLeadingNewlines<NodeType: SyntaxProtocol>(node: NodeType) -> NodeType {
if node.leadingTrivia.first?.isNewline ?? false {
return node.with(\.leadingTrivia, indentedNewline + node.leadingTrivia)
return node.with(\.leadingTrivia, .newline + node.leadingTrivia)
} else {
return node.with(\.leadingTrivia, indentedNewline + indentedNewline + node.leadingTrivia)
return node.with(\.leadingTrivia, .newlines(2) + node.leadingTrivia)
}
}

Expand All @@ -126,14 +145,20 @@ public class CodeGenerationFormat: BasicFormat {
) -> [SyntaxType] {
increaseIndentationLevel()
var formattedChildren = children.map {
self.rewrite($0.cast(SyntaxType.self)).cast(SyntaxType.self)
return self.rewrite($0.cast(SyntaxType.self)).cast(SyntaxType.self)
}
formattedChildren = formattedChildren.map {
if $0.leadingTrivia.first?.isNewline == true {
return $0
} else {
return $0.with(\.leadingTrivia, indentedNewline + $0.leadingTrivia)
formattedChildren = formattedChildren.map { child in
var child = child
child.trailingTrivia = Trivia(pieces: child.trailingTrivia.drop(while: \.isSpaceOrTab))

let startsOnNewline =
child.leadingTrivia.contains(where: \.isNewline)
|| child.previousToken(viewMode: .sourceAccurate)?.trailingTrivia.contains(where: \.isNewline) ?? false

if !startsOnNewline {
child.leadingTrivia = indentedNewline + child.leadingTrivia
}
return child
}
decreaseIndentationLevel()
if let lastChild = formattedChildren.last {
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftParser/generated/ExperimentalFeatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension Parser {
@_spi(ExperimentalLanguageFeatures)
public struct ExperimentalFeatures: OptionSet, Sendable {
public let rawValue: UInt

public init(rawValue: UInt) {
self.rawValue = rawValue
}
Expand All @@ -26,16 +26,16 @@ extension Parser {
extension Parser.ExperimentalFeatures {
/// Whether to enable the parsing of reference bindings.
public static let referenceBindings = Self (rawValue: 1 << 0)

/// Whether to enable the parsing of 'then' statements.
public static let thenStatements = Self (rawValue: 1 << 1)

/// Whether to enable the parsing of 'do' expressions.
public static let doExpressions = Self (rawValue: 1 << 2)

/// Whether to enable the parsing of NonEscableTypes.
public static let nonescapableTypes = Self (rawValue: 1 << 3)

/// Whether to enable the parsing of trailing comma.
public static let trailingComma = Self (rawValue: 1 << 4)
}
2 changes: 1 addition & 1 deletion Sources/SwiftParser/generated/LayoutNodes+Parsable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ fileprivate extension Parser {
}
return node
}

mutating func parseExpression() -> RawExprSyntax {
return self.parseExpression(flavor: .basic, pattern: .none)
}
Expand Down
Loading