@@ -57,49 +57,53 @@ 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 ( )
79
- }
80
- }
81
-
82
- extension CompilerPluginMessageHandler {
83
- func sendMessage( _ message: PluginToHostMessage ) throws {
84
- try connection. sendMessage ( message)
85
- }
86
-
87
- func waitForNextMessage( ) throws -> HostToPluginMessage ? {
88
- try connection. waitForNextMessage ( HostToPluginMessage . self)
73
+ self . handler = CompilerPluginMessageHandler ( provider: provider)
89
74
}
90
75
91
76
/// Run the main message listener loop.
92
77
/// Returns when the message connection was closed.
93
78
/// Throws an error when it failed to send/receive the message, or failed
94
79
/// to serialize/deserialize the message.
95
80
public func main( ) throws {
96
- while let message = try self . waitForNextMessage ( ) {
97
- try handleMessage ( message)
81
+ while let message = try connection. waitForNextMessage ( HostToPluginMessage . self) {
82
+ let result = handler. handleMessage ( message)
83
+ try connection. sendMessage ( result)
98
84
}
99
85
}
86
+ }
87
+
88
+ /// 'CompilerPluginMessageHandler' is a type that handle a message and do the
89
+ /// corresponding operation.
90
+ @_spi ( Compiler)
91
+ public class CompilerPluginMessageHandler < Provider: PluginProvider > {
92
+ /// Object to provide actual plugin functions.
93
+ let provider : Provider
94
+
95
+ /// Plugin host capability
96
+ var hostCapability : HostCapability
100
97
98
+ public init ( provider: Provider ) {
99
+ self . provider = provider
100
+ self . hostCapability = HostCapability ( )
101
+ }
102
+ }
103
+
104
+ extension CompilerPluginMessageHandler {
101
105
/// Handles a single message received from the plugin host.
102
- fileprivate func handleMessage( _ message: HostToPluginMessage ) throws {
106
+ public func handleMessage( _ message: HostToPluginMessage ) -> PluginToHostMessage {
103
107
switch message {
104
108
case . getCapability( let hostCapability) :
105
109
// Remember the peer capability if provided.
@@ -112,10 +116,10 @@ extension CompilerPluginMessageHandler {
112
116
protocolVersion: PluginMessage . PROTOCOL_VERSION_NUMBER,
113
117
features: provider. features. map ( { $0. rawValue } )
114
118
)
115
- try self . sendMessage ( . getCapabilityResult( capability: capability) )
119
+ return . getCapabilityResult( capability: capability)
116
120
117
121
case . expandFreestandingMacro( let macro, let macroRole, let discriminator, let expandingSyntax) :
118
- try expandFreestandingMacro (
122
+ return expandFreestandingMacro (
119
123
macro: macro,
120
124
macroRole: macroRole,
121
125
discriminator: discriminator,
@@ -132,7 +136,7 @@ extension CompilerPluginMessageHandler {
132
136
let extendedTypeSyntax,
133
137
let conformanceListSyntax
134
138
) :
135
- try expandAttachedMacro (
139
+ return expandAttachedMacro (
136
140
macro: macro,
137
141
macroRole: macroRole,
138
142
discriminator: discriminator,
@@ -159,7 +163,7 @@ extension CompilerPluginMessageHandler {
159
163
)
160
164
)
161
165
}
162
- try self . sendMessage ( . loadPluginLibraryResult( loaded: diags. isEmpty, diagnostics: diags) ) ;
166
+ return . loadPluginLibraryResult( loaded: diags. isEmpty, diagnostics: diags)
163
167
}
164
168
}
165
169
}
0 commit comments