Skip to content

Commit 14ff9b3

Browse files
authored
Merge pull request #2301 from rintaro/macros-message-listener
[Macros] Introduce CompilerPluginMessageListener
2 parents 6c459f2 + b27503b commit 14ff9b3

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ extension CompilerPlugin {
182182
// Handle messages from the host until the input stream is closed,
183183
// indicating that we're done.
184184
let provider = MacroProviderAdapter(plugin: Self())
185-
let impl = CompilerPluginMessageHandler(connection: connection, provider: provider)
185+
let impl = CompilerPluginMessageListener(connection: connection, provider: provider)
186186
do {
187187
try impl.main()
188188
} catch {

Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extension CompilerPluginMessageHandler {
5555
discriminator: String,
5656
expandingSyntax: PluginMessage.Syntax,
5757
lexicalContext: [PluginMessage.Syntax]?
58-
) throws {
58+
) -> PluginToHostMessage {
5959
let sourceManager = SourceManager()
6060
let syntax = sourceManager.add(expandingSyntax, foldingWith: .standardOperators)
6161

@@ -105,7 +105,7 @@ extension CompilerPluginMessageHandler {
105105
// TODO: Remove this when all compilers have 'hasExpandMacroResult'.
106106
response = .expandFreestandingMacroResult(expandedSource: expandedSource, diagnostics: diagnostics)
107107
}
108-
try self.sendMessage(response)
108+
return response
109109
}
110110

111111
/// Expand `@attached(XXX)` macros.
@@ -119,7 +119,7 @@ extension CompilerPluginMessageHandler {
119119
extendedTypeSyntax: PluginMessage.Syntax?,
120120
conformanceListSyntax: PluginMessage.Syntax?,
121121
lexicalContext: [PluginMessage.Syntax]?
122-
) throws {
122+
) -> PluginToHostMessage {
123123
let sourceManager = SourceManager()
124124
let attributeNode = sourceManager.add(
125125
attributeSyntax,
@@ -189,7 +189,7 @@ extension CompilerPluginMessageHandler {
189189
} else {
190190
response = .expandAttachedMacroResult(expandedSources: expandedSources, diagnostics: diagnostics)
191191
}
192-
try self.sendMessage(response)
192+
return response
193193
}
194194
}
195195

0 commit comments

Comments
 (0)