Skip to content

Fix leading newline in source files #1355

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
Feb 24, 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
2 changes: 1 addition & 1 deletion CodeGeneration/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
.executable(name: "generate-swiftsyntax", targets: ["generate-swiftsyntax"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", revision: "cf413a6fe9a82e3f8e1c022cac6f6d23dfb1a035"),
.package(url: "https://github.com/apple/swift-syntax.git", revision: "4727d3c3f81be056cf7f9636d682ac9879270d12"),
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.2.2")),
],
targets: [
Expand Down
26 changes: 16 additions & 10 deletions CodeGeneration/Sources/Utils/CodeGenerationFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,8 @@ public class CodeGenerationFormat: BasicFormat {
}
}

public override func visit(_ node: MemberDeclBlockSyntax) -> MemberDeclBlockSyntax {
if node.members.count == 0 {
return node.with(\.leftBrace, .leftBraceToken())
} else {
return super.visit(node)
}
}

public override func visit(_ node: CodeBlockItemSyntax) -> CodeBlockItemSyntax {
if node.parent?.parent?.is(SourceFileSyntax.self) == true, !node.item.is(ImportDeclSyntax.self) {
if node.parent?.parent?.is(SourceFileSyntax.self) == true, !shouldBeSeparatedByTwoNewlines(node: node) {
let formatted = super.visit(node)
return ensuringTwoLeadingNewlines(node: formatted)
} else {
Expand Down Expand Up @@ -64,9 +56,17 @@ public class CodeGenerationFormat: BasicFormat {
}
}

public override func visit(_ node: MemberDeclBlockSyntax) -> MemberDeclBlockSyntax {
if node.members.count == 0 {
return node.with(\.leftBrace, .leftBraceToken())
} else {
return super.visit(node)
}
}

public override func visit(_ node: MemberDeclListItemSyntax) -> MemberDeclListItemSyntax {
let formatted = super.visit(node)
if node.indexInParent != 0 {
if node.indexInParent != 0 && !node.decl.is(EnumCaseDeclSyntax.self) {
return ensuringTwoLeadingNewlines(node: formatted)
} else {
return formatted
Expand All @@ -85,6 +85,12 @@ public class CodeGenerationFormat: BasicFormat {

// MARK: - Private

private func shouldBeSeparatedByTwoNewlines(node: CodeBlockItemSyntax) -> Bool {
// First item in the `CodeBlockItemListSyntax` don't need a newline or identation if the parent is a `SourceFileSyntax`.
// We want to group imports so newline between them should be omitted
return node.parent?.as(CodeBlockItemListSyntax.self)?.first == node || node.item.is(ImportDeclSyntax.self)
}

private func ensuringTwoLeadingNewlines<NodeType: SyntaxProtocol>(node: NodeType) -> NodeType {
if node.leadingTrivia?.first?.isNewline ?? false {
return node.with(\.leadingTrivia, indentedNewline + (node.leadingTrivia ?? []))
Expand Down
19 changes: 0 additions & 19 deletions Sources/IDEUtils/generated/SyntaxClassification.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//// Automatically Generated by generate-ideutils
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
Expand All @@ -18,58 +17,40 @@
public enum SyntaxClassification {
/// An attribute starting with an `@`.
case attribute

/// A block comment starting with `/**` and ending with `*/.
case blockComment

/// A build configuration directive like `#if`, `#elseif`, `#else`.
case buildConfigId

/// A doc block comment starting with `/**` and ending with `*/.
case docBlockComment

/// A doc line comment starting with `///`.
case docLineComment

/// An identifier starting with `$` like `$0`.
case dollarIdentifier

/// An editor placeholder of the form `<#content#>`
case editorPlaceholder

/// A floating point literal.
case floatingLiteral

/// A generic identifier.
case identifier

/// An integer literal.
case integerLiteral

/// A Swift keyword, including contextual keywords.
case keyword

/// A line comment starting with `//`.
case lineComment

/// The token should not receive syntax coloring.
case none

/// An image, color, etc. literal.
case objectLiteral

/// An identifier referring to an operator.
case operatorIdentifier

/// A `#` keyword like `#warning`.
case poundDirectiveKeyword

/// The opening and closing parenthesis of string interpolation.
case stringInterpolationAnchor

/// A string literal including multiline string literals.
case stringLiteral

/// An identifier referring to a type.
case typeIdentifier
}
Expand Down
1 change: 0 additions & 1 deletion Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//// Automatically Generated by generate-swiftbasicformat
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
Expand Down
33 changes: 0 additions & 33 deletions Sources/SwiftParser/generated/DeclarationModifier.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//// Automatically Generated by generate-swiftparser
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
Expand All @@ -17,69 +16,37 @@

enum DeclarationModifier: TokenSpecSet {
case `static`

case `class`

case final

case required

case optional

case lazy

case dynamic

case infix

case prefix

case postfix

case __consuming

case mutating

case nonmutating

case convenience

case override

case `private`

case `fileprivate`

case `internal`

case `public`

case package

case open

case __setter_access

case weak

case unowned

case `rethrows`

case indirect

case isolated

case async

case reasync

case nonisolated

case distributed

case _const

case _local

init?(lexeme: Lexer.Lexeme) {
Expand Down
1 change: 0 additions & 1 deletion Sources/SwiftParser/generated/Parser+Entry.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//// Automatically Generated by generate-swiftparser
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//// Automatically Generated by generate-swiftparser
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
Expand Down
12 changes: 0 additions & 12 deletions Sources/SwiftParser/generated/TypeAttribute.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//// Automatically Generated by generate-swiftparser
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
Expand All @@ -18,27 +17,16 @@
extension Parser {
enum TypeAttribute: TokenSpecSet {
case autoclosure

case convention

case noescape

case escaping

case differentiable

case noDerivative

case async

case Sendable

case unchecked

case _local

case _noMetadata

case _opaqueReturnTypeOf

init?(lexeme: Lexer.Lexeme) {
Expand Down
Loading