Skip to content

Improve implementation of BasicFormat (yet again) #1558

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 3 commits into from
Apr 21, 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
20 changes: 11 additions & 9 deletions CodeGeneration/Sources/SyntaxSupport/Trivia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class Trivia {
public let comment: String
public let characters: [Character]
public let swiftCharacters: [Character]
public let isNewLine: Bool
public let isComment: Bool

public var lowerName: String { lowercaseFirstWord(name: name) }
Expand All @@ -36,17 +35,23 @@ public class Trivia {

public var isCollection: Bool { charactersLen > 0 }

public var isBlank: Bool {
characters.contains { $0.isWhitespace }
}

public var isNewLine: Bool {
characters.contains { $0.isNewline }
}

init(
name: String,
comment: String,
characters: [Character] = [],
swiftCharacters: [Character] = [],
isNewLine: Bool = false,
isComment: Bool = false
) {
self.name = name
self.comment = comment
self.isNewLine = isNewLine
self.isComment = isComment
self.characters = characters

Expand Down Expand Up @@ -86,8 +91,7 @@ public let TRIVIAS: [Trivia] = [
],
swiftCharacters: [
Character("\r")
],
isNewLine: true
]
),

Trivia(
Expand All @@ -100,8 +104,7 @@ public let TRIVIAS: [Trivia] = [
swiftCharacters: [
Character("\r"),
Character("\n"),
],
isNewLine: true
]
),

Trivia(
Expand Down Expand Up @@ -142,8 +145,7 @@ public let TRIVIAS: [Trivia] = [
],
swiftCharacters: [
Character("\n")
],
isNewLine: true
]
),

Trivia(
Expand Down
12 changes: 9 additions & 3 deletions CodeGeneration/Sources/Utils/CodeGenerationFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import SwiftSyntax

/// A format style for files generated by CodeGeneration.
public class CodeGenerationFormat: BasicFormat {
public override var indentation: TriviaPiece { .spaces(indentationLevel * 2) }
public init() {
super.init(indentationWidth: .spaces(2))
}

var indentedNewline: Trivia {
.newline + currentIndentationLevel
}

public override func visit(_ node: ArrayElementListSyntax) -> ArrayElementListSyntax {
let children = node.children(viewMode: .all)
Expand Down Expand Up @@ -100,7 +106,7 @@ public class CodeGenerationFormat: BasicFormat {
}

private func formatChildrenSeparatedByNewline<SyntaxType: SyntaxProtocol>(children: SyntaxChildren, elementType: SyntaxType.Type) -> [SyntaxType] {
indentationLevel += 1
increaseIndentationLevel()
var formattedChildren = children.map {
self.visit($0).as(SyntaxType.self)!
}
Expand All @@ -111,7 +117,7 @@ public class CodeGenerationFormat: BasicFormat {
return $0.with(\.leadingTrivia, indentedNewline + $0.leadingTrivia)
}
}
indentationLevel -= 1
decreaseIndentationLevel()
if !formattedChildren.isEmpty {
formattedChildren[formattedChildren.count - 1] = formattedChildren[formattedChildren.count - 1].with(\.trailingTrivia, indentedNewline)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
let fileSpecs: [GeneratedFileSpec] =
[
// SwiftBasicFormat
GeneratedFileSpec(swiftBasicFormatGeneratedDir + ["BasicFormat.swift"], basicFormatFile),
GeneratedFileSpec(swiftBasicFormatGeneratedDir + ["BasicFormat+Extensions.swift"], basicFormatExtensionsFile),

// IDEUtils
GeneratedFileSpec(ideUtilsGeneratedDir + ["SyntaxClassification.swift"], syntaxClassificationFile),
Expand Down
Loading