Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_swift_host_library(SwiftCompilerPluginMessageHandling
Diagnostics.swift
Macros.swift
PluginMacroExpansionContext.swift
PluginMessageCompatibility.swift
PluginMessages.swift
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//===----------------------------------------------------------------------===//

/// Old compiler might send '.declaration' as "freeStandingDeclaration".
extension PluginMessage.MacroRole {
@_spi(PluginMessage) public extension PluginMessage.MacroRole {
init(from decoder: Decoder) throws {
let stringValue = try decoder.singleValueContainer().decode(String.self)
if let role = Self(rawValue: stringValue) {
Expand Down
165 changes: 114 additions & 51 deletions Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
//
//===----------------------------------------------------------------------===//

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

internal enum HostToPluginMessage: Codable {
@_spi(PluginMessage) public enum HostToPluginMessage: Codable {
/// Send capability of the host, and get capability of the plugin.
case getCapability(
capability: PluginMessage.HostCapability?
Expand Down Expand Up @@ -47,7 +46,7 @@ internal enum HostToPluginMessage: Codable {
)
}

internal enum PluginToHostMessage: Codable {
@_spi(PluginMessage) public enum PluginToHostMessage: Codable {
case getCapabilityResult(
capability: PluginMessage.PluginCapability
)
Expand Down Expand Up @@ -76,30 +75,45 @@ internal enum PluginToHostMessage: Codable {
)
}

/*namespace*/ internal enum PluginMessage {
static var PROTOCOL_VERSION_NUMBER: Int { 6 } // Added 'expandMacroResult'.
@_spi(PluginMessage) public enum PluginMessage {
public static var PROTOCOL_VERSION_NUMBER: Int { 6 } // Added 'expandMacroResult'.

struct HostCapability: Codable {
public struct HostCapability: Codable {
var protocolVersion: Int

public init(protocolVersion: Int) {
self.protocolVersion = protocolVersion
}
}

struct PluginCapability: Codable {
var protocolVersion: Int
public struct PluginCapability: Codable {
public var protocolVersion: Int

/// Optional features this plugin provides.
/// * 'load-plugin-library': 'loadPluginLibrary' message is implemented.
var features: [String]?
public var features: [String]?

public init(protocolVersion: Int, features: [String]? = nil) {
self.protocolVersion = protocolVersion
self.features = features
}
}

struct MacroReference: Codable {
var moduleName: String
var typeName: String
public struct MacroReference: Codable {
public var moduleName: String
public var typeName: String

// The name of 'macro' declaration the client is using.
var name: String
public var name: String

public init(moduleName: String, typeName: String, name: String) {
self.moduleName = moduleName
self.typeName = typeName
self.name = name
}
}

enum MacroRole: String, Codable {
public enum MacroRole: String, Codable {
case expression
case declaration
case accessor
Expand All @@ -111,79 +125,128 @@ internal enum PluginToHostMessage: Codable {
case `extension`
}

struct SourceLocation: Codable {
public struct SourceLocation: Codable {
/// A file ID consisting of the module name and file name (without full path),
/// as would be generated by the macro expansion `#fileID`.
var fileID: String
public var fileID: String

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

/// UTF-8 offset of the location in the file.
var offset: Int
public var offset: Int

public var line: Int
public var column: Int

var line: Int
var column: Int
public init(fileID: String, fileName: String, offset: Int, line: Int, column: Int) {
self.fileID = fileID
self.fileName = fileName
self.offset = offset
self.line = line
self.column = column
}
}

struct Diagnostic: Codable {
enum Severity: String, Codable {
public struct Diagnostic: Codable {
public enum Severity: String, Codable {
case error
case warning
case note
}
struct Position: Codable {
var fileName: String
public struct Position: Codable {
public var fileName: String
/// UTF-8 offset in the file.
var offset: Int
public var offset: Int

static var invalid: Self {
public init(fileName: String, offset: Int) {
self.fileName = fileName
self.offset = offset
}

public static var invalid: Self {
.init(fileName: "", offset: 0)
}
}
struct PositionRange: Codable {
var fileName: String
public struct PositionRange: Codable {
public var fileName: String
/// UTF-8 offset of the start of the range in the file.
var startOffset: Int
public var startOffset: Int
/// UTF-8 offset of the end of the range in the file.
var endOffset: Int
public var endOffset: Int

public init(fileName: String, startOffset: Int, endOffset: Int) {
self.fileName = fileName
self.startOffset = startOffset
self.endOffset = endOffset
}

static var invalid: Self {
public static var invalid: Self {
.init(fileName: "", startOffset: 0, endOffset: 0)
}
}
struct Note: Codable {
var position: Position
var message: String
public struct Note: Codable {
public var position: Position
public var message: String

public init(position: Position, message: String) {
self.position = position
self.message = message
}
}
struct FixIt: Codable {
struct Change: Codable {
var range: PositionRange
var newText: String
public struct FixIt: Codable {
public struct Change: Codable {
public var range: PositionRange
public var newText: String

internal init(range: PositionRange, newText: String) {
self.range = range
self.newText = newText
}
}
public var message: String
public var changes: [Change]

internal init(message: String, changes: [Change]) {
self.message = message
self.changes = changes
}
var message: String
var changes: [Change]
}
var message: String
var severity: Severity
var position: Position
var highlights: [PositionRange]
var notes: [Note]
var fixIts: [FixIt]
public var message: String
public var severity: Severity
public var position: Position
public var highlights: [PositionRange]
public var notes: [Note]
public var fixIts: [FixIt]

internal init(message: String, severity: Severity, position: Position, highlights: [PositionRange], notes: [Note], fixIts: [FixIt]) {
self.message = message
self.severity = severity
self.position = position
self.highlights = highlights
self.notes = notes
self.fixIts = fixIts
}
}

struct Syntax: Codable {
enum Kind: String, Codable {
public struct Syntax: Codable {
public enum Kind: String, Codable {
case declaration
case statement
case expression
case type
case pattern
case attribute
}
var kind: Kind
var source: String
var location: SourceLocation
public var kind: Kind
public var source: String
public var location: SourceLocation

public init(kind: Kind, source: String, location: SourceLocation) {
self.kind = kind
self.source = source
self.location = location
}
}
}