@@ -64,50 +64,52 @@ struct HostCapability {
6464 var hasExpandMacroResult : Bool { protocolVersion >= 5 }
6565}
6666
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
6969/// the response.
7070///
7171/// The low level connection and the provider is injected by the client.
7272@_spi ( PluginMessage)
73- public class CompilerPluginMessageHandler < Connection: MessageConnection , Provider: PluginProvider > {
73+ public class CompilerPluginMessageListener < Connection: MessageConnection , Provider: PluginProvider > {
7474 /// Message channel for bidirectional communication with the plugin host.
7575 let connection : Connection
7676
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 >
8278
8379 public init ( connection: Connection , provider: Provider ) {
8480 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)
9782 }
9883
9984 /// Run the main message listener loop.
10085 /// Returns when the message connection was closed.
10186 /// Throws an error when it failed to send/receive the message, or failed
10287 /// to serialize/deserialize the message.
10388 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)
10692 }
10793 }
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+ }
108110
109111 /// Handles a single message received from the plugin host.
110- fileprivate func handleMessage( _ message: HostToPluginMessage ) throws {
112+ public func handleMessage( _ message: HostToPluginMessage ) -> PluginToHostMessage {
111113 switch message {
112114 case . getCapability( let hostCapability) :
113115 // Remember the peer capability if provided.
@@ -120,7 +122,7 @@ extension CompilerPluginMessageHandler {
120122 protocolVersion: PluginMessage . PROTOCOL_VERSION_NUMBER,
121123 features: provider. features. map ( { $0. rawValue } )
122124 )
123- try self . sendMessage ( . getCapabilityResult( capability: capability) )
125+ return . getCapabilityResult( capability: capability)
124126
125127 case . expandFreestandingMacro(
126128 let macro,
@@ -129,7 +131,7 @@ extension CompilerPluginMessageHandler {
129131 let expandingSyntax,
130132 let lexicalContext
131133 ) :
132- try expandFreestandingMacro (
134+ return expandFreestandingMacro (
133135 macro: macro,
134136 macroRole: macroRole,
135137 discriminator: discriminator,
@@ -148,7 +150,7 @@ extension CompilerPluginMessageHandler {
148150 let conformanceListSyntax,
149151 let lexicalContext
150152 ) :
151- try expandAttachedMacro (
153+ return expandAttachedMacro (
152154 macro: macro,
153155 macroRole: macroRole,
154156 discriminator: discriminator,
@@ -176,7 +178,7 @@ extension CompilerPluginMessageHandler {
176178 )
177179 )
178180 }
179- try self . sendMessage ( . loadPluginLibraryResult( loaded: diags. isEmpty, diagnostics: diags) ) ;
181+ return . loadPluginLibraryResult( loaded: diags. isEmpty, diagnostics: diags)
180182 }
181183 }
182184}
0 commit comments