@@ -64,50 +64,52 @@ struct HostCapability {
64
64
var hasExpandMacroResult : Bool { protocolVersion >= 5 }
65
65
}
66
66
67
- /// 'CompilerPluginMessageHandler ' is a type that listens to the message
68
- /// connection and dispatches them to the actual plugin provider , then send back
67
+ /// 'CompilerPluginMessageListener ' is a type that listens to the message
68
+ /// connection, delegate them to the message handler , then send back
69
69
/// the response.
70
70
///
71
71
/// The low level connection and the provider is injected by the client.
72
72
@_spi ( PluginMessage)
73
- public class CompilerPluginMessageHandler < Connection: MessageConnection , Provider: PluginProvider > {
73
+ public class CompilerPluginMessageListener < Connection: MessageConnection , Provider: PluginProvider > {
74
74
/// Message channel for bidirectional communication with the plugin host.
75
75
let connection : Connection
76
76
77
- /// Object to provide actual plugin functions.
78
- let provider : Provider
79
-
80
- /// Plugin host capability
81
- var hostCapability : HostCapability
77
+ let handler : CompilerPluginMessageHandler < Provider >
82
78
83
79
public init ( connection: Connection , provider: Provider ) {
84
80
self . connection = connection
85
- self . provider = provider
86
- self . hostCapability = HostCapability ( )
87
- }
88
- }
89
-
90
- extension CompilerPluginMessageHandler {
91
- func sendMessage( _ message: PluginToHostMessage ) throws {
92
- try connection. sendMessage ( message)
93
- }
94
-
95
- func waitForNextMessage( ) throws -> HostToPluginMessage ? {
96
- try connection. waitForNextMessage ( HostToPluginMessage . self)
81
+ self . handler = CompilerPluginMessageHandler ( provider: provider)
97
82
}
98
83
99
84
/// Run the main message listener loop.
100
85
/// Returns when the message connection was closed.
101
86
/// Throws an error when it failed to send/receive the message, or failed
102
87
/// to serialize/deserialize the message.
103
88
public func main( ) throws {
104
- while let message = try self . waitForNextMessage ( ) {
105
- try handleMessage ( message)
89
+ while let message = try connection. waitForNextMessage ( HostToPluginMessage . self) {
90
+ let result = handler. handleMessage ( message)
91
+ try connection. sendMessage ( result)
106
92
}
107
93
}
94
+ }
95
+
96
+ /// 'CompilerPluginMessageHandler' is a type that handle a message and do the
97
+ /// corresponding operation.
98
+ @_spi ( PluginMessage)
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
+ }
108
110
109
111
/// Handles a single message received from the plugin host.
110
- fileprivate func handleMessage( _ message: HostToPluginMessage ) throws {
112
+ public func handleMessage( _ message: HostToPluginMessage ) -> PluginToHostMessage {
111
113
switch message {
112
114
case . getCapability( let hostCapability) :
113
115
// Remember the peer capability if provided.
@@ -120,7 +122,7 @@ extension CompilerPluginMessageHandler {
120
122
protocolVersion: PluginMessage . PROTOCOL_VERSION_NUMBER,
121
123
features: provider. features. map ( { $0. rawValue } )
122
124
)
123
- try self . sendMessage ( . getCapabilityResult( capability: capability) )
125
+ return . getCapabilityResult( capability: capability)
124
126
125
127
case . expandFreestandingMacro(
126
128
let macro,
@@ -129,7 +131,7 @@ extension CompilerPluginMessageHandler {
129
131
let expandingSyntax,
130
132
let lexicalContext
131
133
) :
132
- try expandFreestandingMacro (
134
+ return expandFreestandingMacro (
133
135
macro: macro,
134
136
macroRole: macroRole,
135
137
discriminator: discriminator,
@@ -148,7 +150,7 @@ extension CompilerPluginMessageHandler {
148
150
let conformanceListSyntax,
149
151
let lexicalContext
150
152
) :
151
- try expandAttachedMacro (
153
+ return expandAttachedMacro (
152
154
macro: macro,
153
155
macroRole: macroRole,
154
156
discriminator: discriminator,
@@ -176,7 +178,7 @@ extension CompilerPluginMessageHandler {
176
178
)
177
179
)
178
180
}
179
- try self . sendMessage ( . loadPluginLibraryResult( loaded: diags. isEmpty, diagnostics: diags) )
181
+ return . loadPluginLibraryResult( loaded: diags. isEmpty, diagnostics: diags)
180
182
}
181
183
}
182
184
}
0 commit comments