Skip to content

[5.9] Automatically format expanded macros #1596

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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_swift_host_library(SwiftCompilerPluginMessageHandling

target_link_libraries(SwiftCompilerPluginMessageHandling PUBLIC
SwiftSyntax
SwiftBasicFormat
SwiftDiagnostics
SwiftParser
SwiftSyntaxMacros
Expand Down
30 changes: 23 additions & 7 deletions Sources/SwiftCompilerPluginMessageHandling/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import SwiftBasicFormat
import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxMacros
Expand Down Expand Up @@ -49,21 +50,21 @@ extension CompilerPluginMessageHandler {
try exprMacroDef.expansion(of: node, in: context)
}
let rewritten = try _openExistential(macroSyntax, do: _expand)
expandedSource = rewritten.description
expandedSource = rewritten.formattedExpansion(macroDefinition.formatMode)

case let declMacroDef as DeclarationMacro.Type:
func _expand<Node: FreestandingMacroExpansionSyntax>(node: Node) throws -> [DeclSyntax] {
try declMacroDef.expansion(of: node, in: context)
}
let rewritten = try _openExistential(macroSyntax, do: _expand)
expandedSource = CodeBlockItemListSyntax(rewritten.map { CodeBlockItemSyntax(item: .decl($0)) }).description
expandedSource = CodeBlockItemListSyntax(rewritten.map { CodeBlockItemSyntax(item: .decl($0)) }).formattedExpansion(macroDefinition.formatMode)

case let codeItemMacroDef as CodeItemMacro.Type:
func _expand<Node: FreestandingMacroExpansionSyntax>(node: Node) throws -> [CodeBlockItemSyntax] {
try codeItemMacroDef.expansion(of: node, in: context)
}
let rewritten = try _openExistential(macroSyntax, do: _expand)
expandedSource = CodeBlockItemListSyntax(rewritten).description
expandedSource = CodeBlockItemListSyntax(rewritten).formattedExpansion(macroDefinition.formatMode)

default:
throw MacroExpansionError.unmathedMacroRole
Expand Down Expand Up @@ -113,7 +114,7 @@ extension CompilerPluginMessageHandler {
in: context
)
expandedSources = accessors.map {
$0.trimmedDescription
$0.formattedExpansion(macroDefinition.formatMode)
}

case (let attachedMacro as MemberAttributeMacro.Type, .memberAttribute):
Expand Down Expand Up @@ -145,7 +146,7 @@ extension CompilerPluginMessageHandler {

// Form a buffer containing an attribute list to return to the caller.
expandedSources = attributes.map {
$0.trimmedDescription
$0.formattedExpansion(macroDefinition.formatMode)
}

case (let attachedMacro as MemberMacro.Type, .member):
Expand All @@ -170,7 +171,7 @@ extension CompilerPluginMessageHandler {
let members = try _openExistential(declGroup, do: expandMemberMacro)

// Form a buffer of member declarations to return to the caller.
expandedSources = members.map { $0.trimmedDescription }
expandedSources = members.map { $0.formattedExpansion(macroDefinition.formatMode) }

case (let attachedMacro as PeerMacro.Type, .peer):
let peers = try attachedMacro.expansion(
Expand All @@ -181,7 +182,7 @@ extension CompilerPluginMessageHandler {

// Form a buffer of peer declarations to return to the caller.
expandedSources = peers.map {
$0.trimmedDescription
$0.formattedExpansion(macroDefinition.formatMode)
}

case (let attachedMacro as ConformanceMacro.Type, .conformance):
Expand Down Expand Up @@ -234,3 +235,18 @@ extension CompilerPluginMessageHandler {
)
}
}

fileprivate extension SyntaxProtocol {
/// Perform a format if required and then trim any leading/trailing
/// whitespace.
func formattedExpansion(_ mode: FormatMode) -> String {
let formatted: Syntax
switch mode {
case .auto:
formatted = self.formatted()
case .disabled:
formatted = Syntax(self)
}
return formatted.trimmedDescription(matching: { $0.isWhitespace })
}
}
30 changes: 26 additions & 4 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,35 @@ public extension SyntaxProtocol {
return self.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
}

/// The description of this node without the leading trivia of the first token
/// in the node and the trailing trivia of the last token in the node.
/// A copy of this node with pieces that match `matching` trimmed from the
/// leading trivia of the first token and trailing trivia of the last token.
func trimmed(matching filter: (TriviaPiece) -> Bool) -> Self {
// TODO: Should only need one new node here
return self.with(
\.leadingTrivia,
Trivia(pieces: leadingTrivia.pieces.drop(while: filter))
).with(
\.trailingTrivia,
Trivia(pieces: trailingTrivia.pieces.reversed().drop(while: filter).reversed())
)
}

/// The description of this node with leading whitespace of the first token
/// and trailing whitespace of the last token removed.
var trimmedDescription: String {
// TODO: We shouldn't need to create to copies just to get the description
// without trivia.
// TODO: We shouldn't need to create to copies just to get the trimmed
// description.
return self.trimmed.description
}

/// The description of this node with pieces that match `matching` removed
/// from the leading trivia of the first token and trailing trivia of the
/// last token.
func trimmedDescription(matching filter: (TriviaPiece) -> Bool) -> String {
// TODO: We shouldn't need to create to copies just to get the trimmed
// description.
return self.trimmed(matching: filter).description
}
}

/// Provides debug descriptions for a node
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftSyntaxMacros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_swift_host_library(SwiftSyntaxMacros
MacroProtocols/ExpressionMacro.swift
MacroProtocols/FreestandingMacro.swift
MacroProtocols/Macro.swift
MacroProtocols/Macro+Format.swift
MacroProtocols/MemberAttributeMacro.swift
MacroProtocols/MemberMacro.swift
MacroProtocols/PeerMacro.swift
Expand Down
29 changes: 29 additions & 0 deletions Sources/SwiftSyntaxMacros/MacroProtocols/Macro+Format.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// Describes the mode to use to format the result of an expansion.
public enum FormatMode {
/// Perform a basic format of the expansion. This is primarily for inserting
/// whitespace as required (eg. between two keywords), but also adds simple
/// newline and indentation.
case auto

/// Disable automatically formatting the expanded macro. Trivia must be
/// manually inserted where required (eg. adding spaces between keywords).
case disabled
}

public extension Macro {
static var formatMode: FormatMode {
return .auto
}
}
6 changes: 5 additions & 1 deletion Sources/SwiftSyntaxMacros/MacroProtocols/Macro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
//===----------------------------------------------------------------------===//

/// Describes a macro.
public protocol Macro {}
public protocol Macro {
/// How the resulting expansion should be formatted, `.auto` by default.
/// Use `.disabled` for the expansion to be used as is.
static var formatMode: FormatMode { get }
}
4 changes: 3 additions & 1 deletion lit_tests/compiler_plugin_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class MyClass {
}

// For '@Metadata'
// CHECK: static var __metadata__: [String: String] { ["name": "MyClass"] }
// CHECK: {{^}}static var __metadata__: [String: String] {
// CHECK-NEXT: {{^}} ["name": "MyClass"]
// CHECK-NEXT: {{^}}}

// For '#echo(12)'
// CHECK: /* echo */12