@@ -57,29 +57,22 @@ struct HostCapability {
57
57
var hasExpandMacroResult : Bool { protocolVersion >= 5 }
58
58
}
59
59
60
- /// 'CompilerPluginMessageHandler ' is a type that listens to the message
61
- /// connection and dispatches them to the actual plugin provider , then send back
60
+ /// 'CompilerPluginMessageListener ' is a type that listens to the message
61
+ /// connection, delegate them to the message handler , then send back
62
62
/// the response.
63
63
///
64
- /// The low level connection and the provider is injected by the client.
65
- public class CompilerPluginMessageHandler < Connection: MessageConnection , Provider: PluginProvider > {
64
+ /// The low level connection and the plugin provider is injected by the client.
65
+ public class CompilerPluginMessageListener < Connection: MessageConnection , Provider: PluginProvider > {
66
66
/// Message channel for bidirectional communication with the plugin host.
67
67
let connection : Connection
68
68
69
- /// Object to provide actual plugin functions.
70
- let provider : Provider
71
-
72
- /// Plugin host capability
73
- var hostCapability : HostCapability
69
+ let handler : CompilerPluginMessageHandler < Provider >
74
70
75
71
public init ( connection: Connection , provider: Provider ) {
76
72
self . connection = connection
77
- self . provider = provider
78
- self . hostCapability = HostCapability ( )
73
+ self . handler = CompilerPluginMessageHandler ( provider: provider)
79
74
}
80
- }
81
75
82
- extension CompilerPluginMessageHandler {
83
76
func sendMessage( _ message: PluginToHostMessage ) throws {
84
77
try connection. sendMessage ( message)
85
78
}
@@ -94,12 +87,31 @@ extension CompilerPluginMessageHandler {
94
87
/// to serialize/deserialize the message.
95
88
public func main( ) throws {
96
89
while let message = try self . waitForNextMessage ( ) {
97
- try handleMessage ( message)
90
+ let result = handler. handleMessage ( message)
91
+ try connection. sendMessage ( result)
98
92
}
99
93
}
94
+ }
95
+
96
+ /// 'CompilerPluginMessageHandler' is a type that handle a message and do the
97
+ /// corresponding operation.
98
+ @_spi ( Compiler)
99
+ public class CompilerPluginMessageHandler < Provider: PluginProvider > {
100
+ /// Object to provide actual plugin functions.
101
+ let provider : Provider
102
+
103
+ /// Plugin host capability
104
+ var hostCapability : HostCapability
105
+
106
+ public init ( provider: Provider ) {
107
+ self . provider = provider
108
+ self . hostCapability = HostCapability ( )
109
+ }
110
+ }
100
111
112
+ extension CompilerPluginMessageHandler {
101
113
/// Handles a single message received from the plugin host.
102
- fileprivate func handleMessage( _ message: HostToPluginMessage ) throws {
114
+ public func handleMessage( _ message: HostToPluginMessage ) -> PluginToHostMessage {
103
115
switch message {
104
116
case . getCapability( let hostCapability) :
105
117
// Remember the peer capability if provided.
@@ -112,10 +124,10 @@ extension CompilerPluginMessageHandler {
112
124
protocolVersion: PluginMessage . PROTOCOL_VERSION_NUMBER,
113
125
features: provider. features. map ( { $0. rawValue } )
114
126
)
115
- try self . sendMessage ( . getCapabilityResult( capability: capability) )
127
+ return . getCapabilityResult( capability: capability)
116
128
117
129
case . expandFreestandingMacro( let macro, let macroRole, let discriminator, let expandingSyntax) :
118
- try expandFreestandingMacro (
130
+ return expandFreestandingMacro (
119
131
macro: macro,
120
132
macroRole: macroRole,
121
133
discriminator: discriminator,
@@ -132,7 +144,7 @@ extension CompilerPluginMessageHandler {
132
144
let extendedTypeSyntax,
133
145
let conformanceListSyntax
134
146
) :
135
- try expandAttachedMacro (
147
+ return expandAttachedMacro (
136
148
macro: macro,
137
149
macroRole: macroRole,
138
150
discriminator: discriminator,
@@ -159,7 +171,7 @@ extension CompilerPluginMessageHandler {
159
171
)
160
172
)
161
173
}
162
- try self . sendMessage ( . loadPluginLibraryResult( loaded: diags. isEmpty, diagnostics: diags) ) ;
174
+ return . loadPluginLibraryResult( loaded: diags. isEmpty, diagnostics: diags)
163
175
}
164
176
}
165
177
}
0 commit comments