Skip to content

Commit 6853537

Browse files
vnixxcursoragent
andcommitted
Fix #route expansion indentation via DeclSyntax interpolation.
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 6304d00 commit 6853537

2 files changed

Lines changed: 19 additions & 23 deletions

File tree

‎Sources/ReerRouterMacros/ReerRouterMacros.swift‎

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public struct WriteRouteActionToSectionMacro: DeclarationMacro {
3333
) throws -> [DeclSyntax] {
3434
let argumentList = node.arguments
3535
var key: String = ""
36-
var functionBody: String = ""
37-
var signature: String?
36+
var actionClosure: ClosureExprSyntax?
3837

3938
for argument in argumentList {
4039
switch argument.label?.text {
@@ -49,41 +48,38 @@ public struct WriteRouteActionToSectionMacro: DeclarationMacro {
4948
key = hostValue
5049
}
5150
case "action":
52-
if let closureExpr = argument.expression.as(ClosureExprSyntax.self) {
53-
functionBody = closureExpr.statements.trimmedDescription
54-
if let sig = closureExpr.signature {
55-
signature = sig.trimmedDescription
56-
}
57-
}
51+
actionClosure = argument.expression.as(ClosureExprSyntax.self)
5852
default:
5953
break
6054
}
6155
}
6256

6357
// `#route(key: "haha") { params in ... }` puts the closure in trailingClosure.
64-
if functionBody.isEmpty, let trailing = node.trailingClosure {
65-
functionBody = trailing.statements.trimmedDescription
66-
if let sig = trailing.signature {
67-
signature = sig.trimmedDescription
68-
}
58+
actionClosure = actionClosure ?? node.trailingClosure
59+
60+
// Interpolate as a syntax node (not `raw:`) so SwiftSyntax's Indenter
61+
// applies the insertion-site indent to every line of the closure.
62+
let closure: ExprSyntax
63+
if let actionClosure {
64+
closure = ExprSyntax(actionClosure.trimmed)
65+
} else {
66+
closure = "{ param in }"
6967
}
7068

7169
let isGlobal = context.lexicalContext.isEmpty
7270
let staticString = isGlobal ? "" : "static "
7371
let infoName = "\(context.makeUniqueName("rhea"))"
7472
let hashLiteral = fnv1aHashLiteral(key)
7573

76-
let declarationString = """
77-
@used
74+
let declaration: DeclSyntax = """
75+
@used
7876
@section("__DATA,__rerouter_ac")
79-
\(staticString)let \(infoName): RouteActionInfo = (
80-
\(hashLiteral),
81-
{ \(signature ?? "param in")
82-
\(functionBody)
83-
}
77+
\(raw: staticString)let \(raw: infoName): RouteActionInfo = (
78+
\(raw: hashLiteral),
79+
\(closure)
8480
)
8581
"""
86-
return [DeclSyntax(stringLiteral: declarationString)]
82+
return [declaration]
8783
}
8884
}
8985

‎Tests/ReerRouterTests/RouteMacroExpansionTests.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class RouteMacroExpansionTests: XCTestCase {
2626
let __macro_local_4rheafMu_: RouteActionInfo = (
2727
0x2e25cdcc7406360d,
2828
{ params in
29-
print(123333333)
29+
print(123333333)
3030
}
3131
)
3232
""",
@@ -51,7 +51,7 @@ final class RouteMacroExpansionTests: XCTestCase {
5151
let __macro_local_4rheafMu_: RouteActionInfo = (
5252
0x2e25cdcc7406360d,
5353
{ params in
54-
print(6666666)
54+
print(6666666)
5555
}
5656
)
5757
""",

0 commit comments

Comments
 (0)