Skip to content

Commit 2a5fe53

Browse files
authored
Merge pull request #1866 from rintaro/5.9-macros-public-message
[5.9][Macros] Unify PluginMessages.swift
2 parents 3609c77 + dd0fb50 commit 2a5fe53

File tree

3 files changed

+116
-52
lines changed

3 files changed

+116
-52
lines changed

Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_swift_host_library(SwiftCompilerPluginMessageHandling
1111
Diagnostics.swift
1212
Macros.swift
1313
PluginMacroExpansionContext.swift
14+
PluginMessageCompatibility.swift
1415
PluginMessages.swift
1516
)
1617

Sources/SwiftCompilerPluginMessageHandling/PluginMessageCompatibility.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
/// Old compiler might send '.declaration' as "freeStandingDeclaration".
14-
extension PluginMessage.MacroRole {
14+
@_spi(PluginMessage) public extension PluginMessage.MacroRole {
1515
init(from decoder: Decoder) throws {
1616
let stringValue = try decoder.singleValueContainer().decode(String.self)
1717
if let role = Self(rawValue: stringValue) {

Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift

Lines changed: 114 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
// NOTE: This file should be synced between swift and swift-syntax repository.
1413
// NOTE: Types in this file should be self-contained and should not depend on any non-stdlib types.
1514

16-
internal enum HostToPluginMessage: Codable {
15+
@_spi(PluginMessage) public enum HostToPluginMessage: Codable {
1716
/// Send capability of the host, and get capability of the plugin.
1817
case getCapability(
1918
capability: PluginMessage.HostCapability?
@@ -47,7 +46,7 @@ internal enum HostToPluginMessage: Codable {
4746
)
4847
}
4948

50-
internal enum PluginToHostMessage: Codable {
49+
@_spi(PluginMessage) public enum PluginToHostMessage: Codable {
5150
case getCapabilityResult(
5251
capability: PluginMessage.PluginCapability
5352
)
@@ -76,30 +75,45 @@ internal enum PluginToHostMessage: Codable {
7675
)
7776
}
7877

79-
/*namespace*/ internal enum PluginMessage {
80-
static var PROTOCOL_VERSION_NUMBER: Int { 6 } // Added 'expandMacroResult'.
78+
@_spi(PluginMessage) public enum PluginMessage {
79+
public static var PROTOCOL_VERSION_NUMBER: Int { 6 } // Added 'expandMacroResult'.
8180

82-
struct HostCapability: Codable {
81+
public struct HostCapability: Codable {
8382
var protocolVersion: Int
83+
84+
public init(protocolVersion: Int) {
85+
self.protocolVersion = protocolVersion
86+
}
8487
}
8588

86-
struct PluginCapability: Codable {
87-
var protocolVersion: Int
89+
public struct PluginCapability: Codable {
90+
public var protocolVersion: Int
8891

8992
/// Optional features this plugin provides.
9093
/// * 'load-plugin-library': 'loadPluginLibrary' message is implemented.
91-
var features: [String]?
94+
public var features: [String]?
95+
96+
public init(protocolVersion: Int, features: [String]? = nil) {
97+
self.protocolVersion = protocolVersion
98+
self.features = features
99+
}
92100
}
93101

94-
struct MacroReference: Codable {
95-
var moduleName: String
96-
var typeName: String
102+
public struct MacroReference: Codable {
103+
public var moduleName: String
104+
public var typeName: String
97105

98106
// The name of 'macro' declaration the client is using.
99-
var name: String
107+
public var name: String
108+
109+
public init(moduleName: String, typeName: String, name: String) {
110+
self.moduleName = moduleName
111+
self.typeName = typeName
112+
self.name = name
113+
}
100114
}
101115

102-
enum MacroRole: String, Codable {
116+
public enum MacroRole: String, Codable {
103117
case expression
104118
case declaration
105119
case accessor
@@ -111,79 +125,128 @@ internal enum PluginToHostMessage: Codable {
111125
case `extension`
112126
}
113127

114-
struct SourceLocation: Codable {
128+
public struct SourceLocation: Codable {
115129
/// A file ID consisting of the module name and file name (without full path),
116130
/// as would be generated by the macro expansion `#fileID`.
117-
var fileID: String
131+
public var fileID: String
118132

119133
/// A full path name as would be generated by the macro expansion `#filePath`,
120134
/// e.g., `/home/taylor/alison.swift`.
121-
var fileName: String
135+
public var fileName: String
122136

123137
/// UTF-8 offset of the location in the file.
124-
var offset: Int
138+
public var offset: Int
139+
140+
public var line: Int
141+
public var column: Int
125142

126-
var line: Int
127-
var column: Int
143+
public init(fileID: String, fileName: String, offset: Int, line: Int, column: Int) {
144+
self.fileID = fileID
145+
self.fileName = fileName
146+
self.offset = offset
147+
self.line = line
148+
self.column = column
149+
}
128150
}
129151

130-
struct Diagnostic: Codable {
131-
enum Severity: String, Codable {
152+
public struct Diagnostic: Codable {
153+
public enum Severity: String, Codable {
132154
case error
133155
case warning
134156
case note
135157
}
136-
struct Position: Codable {
137-
var fileName: String
158+
public struct Position: Codable {
159+
public var fileName: String
138160
/// UTF-8 offset in the file.
139-
var offset: Int
161+
public var offset: Int
140162

141-
static var invalid: Self {
163+
public init(fileName: String, offset: Int) {
164+
self.fileName = fileName
165+
self.offset = offset
166+
}
167+
168+
public static var invalid: Self {
142169
.init(fileName: "", offset: 0)
143170
}
144171
}
145-
struct PositionRange: Codable {
146-
var fileName: String
172+
public struct PositionRange: Codable {
173+
public var fileName: String
147174
/// UTF-8 offset of the start of the range in the file.
148-
var startOffset: Int
175+
public var startOffset: Int
149176
/// UTF-8 offset of the end of the range in the file.
150-
var endOffset: Int
177+
public var endOffset: Int
178+
179+
public init(fileName: String, startOffset: Int, endOffset: Int) {
180+
self.fileName = fileName
181+
self.startOffset = startOffset
182+
self.endOffset = endOffset
183+
}
151184

152-
static var invalid: Self {
185+
public static var invalid: Self {
153186
.init(fileName: "", startOffset: 0, endOffset: 0)
154187
}
155188
}
156-
struct Note: Codable {
157-
var position: Position
158-
var message: String
189+
public struct Note: Codable {
190+
public var position: Position
191+
public var message: String
192+
193+
public init(position: Position, message: String) {
194+
self.position = position
195+
self.message = message
196+
}
159197
}
160-
struct FixIt: Codable {
161-
struct Change: Codable {
162-
var range: PositionRange
163-
var newText: String
198+
public struct FixIt: Codable {
199+
public struct Change: Codable {
200+
public var range: PositionRange
201+
public var newText: String
202+
203+
internal init(range: PositionRange, newText: String) {
204+
self.range = range
205+
self.newText = newText
206+
}
207+
}
208+
public var message: String
209+
public var changes: [Change]
210+
211+
internal init(message: String, changes: [Change]) {
212+
self.message = message
213+
self.changes = changes
164214
}
165-
var message: String
166-
var changes: [Change]
167215
}
168-
var message: String
169-
var severity: Severity
170-
var position: Position
171-
var highlights: [PositionRange]
172-
var notes: [Note]
173-
var fixIts: [FixIt]
216+
public var message: String
217+
public var severity: Severity
218+
public var position: Position
219+
public var highlights: [PositionRange]
220+
public var notes: [Note]
221+
public var fixIts: [FixIt]
222+
223+
internal init(message: String, severity: Severity, position: Position, highlights: [PositionRange], notes: [Note], fixIts: [FixIt]) {
224+
self.message = message
225+
self.severity = severity
226+
self.position = position
227+
self.highlights = highlights
228+
self.notes = notes
229+
self.fixIts = fixIts
230+
}
174231
}
175232

176-
struct Syntax: Codable {
177-
enum Kind: String, Codable {
233+
public struct Syntax: Codable {
234+
public enum Kind: String, Codable {
178235
case declaration
179236
case statement
180237
case expression
181238
case type
182239
case pattern
183240
case attribute
184241
}
185-
var kind: Kind
186-
var source: String
187-
var location: SourceLocation
242+
public var kind: Kind
243+
public var source: String
244+
public var location: SourceLocation
245+
246+
public init(kind: Kind, source: String, location: SourceLocation) {
247+
self.kind = kind
248+
self.source = source
249+
self.location = location
250+
}
188251
}
189252
}

0 commit comments

Comments
 (0)