File tree Expand file tree Collapse file tree 2 files changed +20
-18
lines changed
Sources/SwiftSyntaxMacros Expand file tree Collapse file tree 2 files changed +20
-18
lines changed Original file line number Diff line number Diff line change @@ -65,14 +65,28 @@ public class BasicMacroExpansionContext {
65
65
/// Used in conjunction with `expansionDiscriminator`.
66
66
private var uniqueNames : [ String : Int ] = [ : ]
67
67
68
+ }
69
+
70
+ extension BasicMacroExpansionContext {
68
71
/// Note that the given node that was at the given position in the provided
69
72
/// source file has been disconnected and is now a new root.
70
- internal func addDisconnected(
71
- _ node: Syntax ,
73
+ private func addDisconnected< Node : SyntaxProtocol > (
74
+ _ node: Node ,
72
75
at offset: AbsolutePosition ,
73
76
in sourceFile: SourceFileSyntax
74
77
) {
75
- disconnectedNodes [ node] = ( sourceFile, offset. utf8Offset)
78
+ disconnectedNodes [ Syntax ( node) ] = ( sourceFile, offset. utf8Offset)
79
+ }
80
+
81
+ /// Detach the given node, and record where it came from.
82
+ public func detach< Node: SyntaxProtocol > ( _ node: Node ) -> Node {
83
+ let detached = node. detach ( )
84
+
85
+ if let rootSourceFile = node. root. as ( SourceFileSyntax . self) {
86
+ addDisconnected ( detached, at: node. position, in: rootSourceFile)
87
+ }
88
+
89
+ return detached
76
90
}
77
91
}
78
92
Original file line number Diff line number Diff line change @@ -28,23 +28,11 @@ extension SyntaxProtocol {
28
28
/// Detach the current node and inform the macro expansion context,
29
29
/// if it needs to know.
30
30
fileprivate func detach( in context: MacroExpansionContext ) -> Self {
31
- let detached = detach ( )
32
-
33
- // Testing contexts want to know where the detach occurred so they can
34
- // track it.
35
- //
36
- // TODO: Should this be generalized?
37
- if let testingContext = context as? BasicMacroExpansionContext ,
38
- let parentSourceFile = root. as ( SourceFileSyntax . self)
39
- {
40
- testingContext. addDisconnected (
41
- Syntax ( detached) ,
42
- at: position,
43
- in: parentSourceFile
44
- )
31
+ if let basicContext = context as? BasicMacroExpansionContext {
32
+ return basicContext. detach ( self )
45
33
}
46
34
47
- return detached
35
+ return self . detach ( )
48
36
}
49
37
}
50
38
You can’t perform that action at this time.
0 commit comments