Skip to content

Clean up formatting of function body macros #2384

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
27 changes: 20 additions & 7 deletions Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,17 @@ public func collapse<Node: SyntaxProtocol>(
var expansions = expansions
var separator = "\n\n"

// Wrap the expansions in a set of braces.
func wrapInBraces() {
// Default to 4 spaces if no indentation was passed.
// In the future, we could consider inferring the indentation width from
// the expansions to collapse.
expansions = expansions.map({ $0.indented(by: indentationWidth ?? .spaces(4)) })
expansions[0] = "{\n" + expansions[0]
expansions[expansions.count - 1] += "\n}"
separator = "\n"
}

switch role {
case .accessor:
let onDeclarationWithoutAccessor: Bool
Expand All @@ -469,16 +480,18 @@ public func collapse<Node: SyntaxProtocol>(
onDeclarationWithoutAccessor = false
}
if onDeclarationWithoutAccessor {
// Default to 4 spaces if no indentation was passed.
// In the future, we could consider inferring the indentation width from
// the expansions to collapse.
expansions = expansions.map({ $0.indented(by: indentationWidth ?? .spaces(4)) })
expansions[0] = "{\n" + expansions[0]
expansions[expansions.count - 1] += "\n}"
separator = "\n"
wrapInBraces()
}
case .memberAttribute:
separator = " "

case .body:
wrapInBraces()

case .preamble:
// Only place a single newline between statements.
separator = "\n"

default:
break
}
Expand Down
13 changes: 8 additions & 5 deletions Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private func expandPreambleMacro(
// Match the indentation of the statements if we can, and put newlines around
// the preamble to separate it from the rest of the body.
let indentation = decl.body?.statements.indentationOfFirstLine ?? (decl.indentationOfFirstLine + indentationWidth)
let indentedSource = "\n" + expanded.indented(by: indentation) + "\n\n"
let indentedSource = "\n" + expanded.indented(by: indentation) + "\n"
return "\(raw: indentedSource)"
}

Expand Down Expand Up @@ -409,10 +409,13 @@ private func expandBodyMacro(
return nil
}

// Wrap the body in braces.
let beforeBody = decl.body == nil ? " " : "";
let indentedSource = beforeBody + "{\n" + expanded.indented(by: decl.indentationOfFirstLine + indentationWidth) + "\n}\n"
return "\(raw: indentedSource)" as CodeBlockSyntax
// `expandAttachedMacro` adds the `{` and `}` to wrap the accessor block and
// then indents it.
// Remove any indentaiton from the first line using `drop(while:)` and then
// prepend a space to separate it from the variable declaration
Comment on lines +412 to +415
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems out of date, it references accessor blocks and variables.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks. It also has a copy-pasted type (indentaiton). I'll fix both

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, it seemed like the sort of typo I made.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is still incorrect, though. It mentioned accessor block and variables but this is about function bodies.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, follow-up PR is at #2392

let leadingWhitespace = decl.body == nil ? " " : ""
let indentedSource = leadingWhitespace + expanded.indented(by: decl.indentationOfFirstLine).drop(while: { $0.isWhitespace })
return "\(raw: indentedSource)"
}

// MARK: - MacroSystem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ final class PreambleMacroTests: XCTestCase {

func doSomethingDangerous(operation: String) {
log("Entering doSomethingDangerous(operation: \\(operation))")

defer {
log("Exiting doSomethingDangerous(operation:)")
}
Expand All @@ -104,12 +103,10 @@ final class PreambleMacroTests: XCTestCase {

func doSomethingDangerous(operation: String) {
log("Entering doSomethingDangerous(operation: \\(operation))")

defer {
log("Exiting doSomethingDangerous(operation:)")
}
log("Entering doSomethingDangerous(operation: \\(operation))")

defer {
log("Exiting doSomethingDangerous(operation:)")
}
Expand All @@ -131,7 +128,6 @@ final class PreambleMacroTests: XCTestCase {

func f(a: Int, b: String) async throws -> String {
log("Entering f(a: \\(a), b: \\(b))")

defer {
log("Exiting f(a:b:)")
}
Expand Down