public struct PreviewWithBindingsMacro: ExpressionMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> ExprSyntax {
guard let builder = node.trailingClosure?.trimmed else {
fatalError("trailing closure expected")
}
let prefix = context.createUniqueName("View")
let previews = try! StructDeclSyntax("struct \(raw: prefix)_Previews: PreviewProvider") {
try VariableDeclSyntax("static var previews: some View") {
StmtSyntax("PreviewView \(raw: builder)")
}
}
return "\(raw: previews.formatted())"
}
}
I wonder if im holding this right.. there are a couple of problems
Firstly, The test input is
#PreviewWithBindings { bindings in
Text("Hello")
}
and the output is:
struct __macro_local_4ViewfMu__Previews: PreviewProvider {
static var previews: some View {
PreviewView { bindings in
Text(" Hello")
}
}
}
Note the additional spaces in the text String literal. Am I constructing the struct and outputting correctly?
Secondly, literal issue aside no matter what I do xcode will not recognize my preview and open the preview pane.. is there something hardcoded into xcode to only look for blessed macros? copy/pasting my macro output allows the pane to appear but xcode must not look at it otherwise?
The problem is that you declared PreviewWithBindingsMacro as an ExpressionMacro, but the macro is generating a declaration. This probably throws the parser off in multiple ways and makes it generate a completely invalid tree. Change the macro to a DeclarationMacro and you should be fine. That way you can also return previews as DecSyntax(previews), without the need for another string interpolation.
Can you share the macro package with a test case that reproduces the issue? I suspect there’s some other issue where a node is parsed as a different type than it should be.