Skip to content

Commit 4d77997

Browse files
committed
Add documentation to all not-yet-documented symbols
1 parent 666dc31 commit 4d77997

File tree

11 files changed

+157
-28
lines changed

11 files changed

+157
-28
lines changed

Sources/MacroToolkit/Destructuring.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// TODO: Figure out a destructuring implementation that uses variadic generics (tricky without same type requirements)
2+
/// Destructures the given `elements` into a tuple of 0 elements if there are exactly 0 elements.
23
public func destructure<Element>(_ elements: some Sequence<Element>) -> ()? {
34
let array = Array(elements)
45
guard array.count == 0 else {
@@ -8,6 +9,7 @@ public func destructure<Element>(_ elements: some Sequence<Element>) -> ()? {
89
}
910

1011
/// Destructures the given `elements` into a single element if there is exactly one element.
12+
///
1113
/// Named differently to allow type inference to still work correctly (single element tuples
1214
/// are weird in Swift).
1315
public func destructureSingle<Element>(_ elements: some Sequence<Element>) -> (Element)? {
@@ -70,12 +72,15 @@ public func destructure<Element>(_ elements: some Sequence<Element>) -> (
7072
return (array[0], array[1], array[2], array[3], array[4], array[5])
7173
}
7274

75+
/// Destructures the given `type` into a name and a tuple of 0 generic type parameters if there are exactly 0 generic type parameters.
7376
public func destructure(_ type: SimpleType) -> (String, ())? {
7477
destructure(type.genericArguments ?? []).map { arguments in
7578
(type.name, arguments)
7679
}
7780
}
7881

82+
/// Destructures the given `type` into a name and 1 generic type parameter if there is exactly 1 generic type parameter.
83+
///
7984
/// Named differently to allow type inference to still work correctly (single element tuples
8085
/// are weird in Swift).
8186
public func destructureSingle(_ type: SimpleType) -> (String, (Type))? {
@@ -84,42 +89,50 @@ public func destructureSingle(_ type: SimpleType) -> (String, (Type))? {
8489
}
8590
}
8691

92+
/// Destructures the given `type` into a name and a tuple of 2 generic type parameters if there are exactly 2 generic type parameters.
8793
public func destructure(_ type: SimpleType) -> (String, (Type, Type))? {
8894
destructure(type.genericArguments ?? []).map { arguments in
8995
(type.name, arguments)
9096
}
9197
}
9298

99+
/// Destructures the given `type` into a name and a tuple of 3 generic type parameters if there are exactly 3 generic type parameters.
93100
public func destructure(_ type: SimpleType) -> (String, (Type, Type, Type))? {
94101
destructure(type.genericArguments ?? []).map { arguments in
95102
(type.name, arguments)
96103
}
97104
}
98105

106+
/// Destructures the given `type` into a name and a tuple of 4 generic type parameters if there are exactly 4 generic type parameters.
99107
public func destructure(_ type: SimpleType) -> (String, (Type, Type, Type, Type))? {
100108
destructure(type.genericArguments ?? []).map { arguments in
101109
(type.name, arguments)
102110
}
103111
}
104112

113+
/// Destructures the given `type` into a name and a tuple of 5 generic type parameters if there are exactly 5 generic type parameters.
105114
public func destructure(_ type: SimpleType) -> (String, (Type, Type, Type, Type, Type))? {
106115
destructure(type.genericArguments ?? []).map { arguments in
107116
(type.name, arguments)
108117
}
109118
}
110119

120+
/// Destructures the given `type` into a name and a tuple of 6 generic type parameters if there are exactly 6 generic type parameters.
111121
public func destructure(_ type: SimpleType) -> (String, (Type, Type, Type, Type, Type, Type))? {
112122
destructure(type.genericArguments ?? []).map { arguments in
113123
(type.name, arguments)
114124
}
115125
}
116126

127+
/// Destructures the given `type` into a tuple of 0 parameter types and a return type if there are exactly 0 parameters.
117128
public func destructure(_ type: FunctionType) -> ((), Type)? {
118129
destructure(type.parameters).map { parameters in
119130
(parameters, type.returnType)
120131
}
121132
}
122133

134+
/// Destructures the given `type` into a parameter type and a return type if there is exactly 1 parameter.
135+
///
123136
/// Named differently to allow type inference to still work correctly (single element tuples
124137
/// are weird in Swift).
125138
public func destructureSingle(_ type: FunctionType) -> ((Type), Type)? {
@@ -128,36 +141,42 @@ public func destructureSingle(_ type: FunctionType) -> ((Type), Type)? {
128141
}
129142
}
130143

144+
/// Destructures the given `type` into a tuple of 2 parameter types and a return type if there are exactly 2 parameters.
131145
public func destructure(_ type: FunctionType) -> ((Type, Type), Type)? {
132146
destructure(type.parameters).map { parameters in
133147
(parameters, type.returnType)
134148
}
135149
}
136150

151+
/// Destructures the given `type` into a tuple of 3 parameter types and a return type if there are exactly 3 parameters.
137152
public func destructure(_ type: FunctionType) -> ((Type, Type, Type), Type)? {
138153
destructure(type.parameters).map { parameters in
139154
(parameters, type.returnType)
140155
}
141156
}
142157

158+
/// Destructures the given `type` into a tuple of 4 parameter types and a return type if there are exactly 4 parameters.
143159
public func destructure(_ type: FunctionType) -> ((Type, Type, Type, Type), Type)? {
144160
destructure(type.parameters).map { parameters in
145161
(parameters, type.returnType)
146162
}
147163
}
148164

165+
/// Destructures the given `type` into a tuple of 5 parameter types and a return type if there are exactly 5 parameters.
149166
public func destructure(_ type: FunctionType) -> ((Type, Type, Type, Type, Type), Type)? {
150167
destructure(type.parameters).map { parameters in
151168
(parameters, type.returnType)
152169
}
153170
}
154171

172+
/// Destructures the given `type` into a tuple of 6 parameter types and a return type if there are exactly 6 parameters.
155173
public func destructure(_ type: FunctionType) -> ((Type, Type, Type, Type, Type, Type), Type)? {
156174
destructure(type.parameters).map { parameters in
157175
(parameters, type.returnType)
158176
}
159177
}
160178

179+
/// Destructures the given `type` into a ``DestructuredType`` for easy pattern matching.
161180
public func destructure(_ type: Type) -> DestructuredType<()>? {
162181
if let type = type.asSimpleType {
163182
return destructure(type).map { destructured in
@@ -172,6 +191,8 @@ public func destructure(_ type: Type) -> DestructuredType<()>? {
172191
}
173192
}
174193

194+
/// Destructures the given `type` into a ``DestructuredType`` for easy pattern matching.
195+
///
175196
/// Named differently to allow type inference to still work correctly (single element tuples
176197
/// are weird in Swift).
177198
public func destructureSingle(_ type: Type) -> DestructuredType<(Type)>? {
@@ -188,6 +209,7 @@ public func destructureSingle(_ type: Type) -> DestructuredType<(Type)>? {
188209
}
189210
}
190211

212+
/// Destructures the given `type` into a ``DestructuredType`` for easy pattern matching.
191213
public func destructure(_ type: Type) -> DestructuredType<(Type, Type)>? {
192214
if let type = type.asSimpleType {
193215
return destructure(type).map { destructured in
@@ -202,6 +224,7 @@ public func destructure(_ type: Type) -> DestructuredType<(Type, Type)>? {
202224
}
203225
}
204226

227+
/// Destructures the given `type` into a ``DestructuredType`` for easy pattern matching.
205228
public func destructure(_ type: Type) -> DestructuredType<(Type, Type, Type)>? {
206229
if let type = type.asSimpleType {
207230
return destructure(type).map { destructured in
@@ -216,6 +239,7 @@ public func destructure(_ type: Type) -> DestructuredType<(Type, Type, Type)>? {
216239
}
217240
}
218241

242+
/// Destructures the given `type` into a ``DestructuredType`` for easy pattern matching.
219243
public func destructure(_ type: Type) -> DestructuredType<(Type, Type, Type, Type)>? {
220244
if let type = type.asSimpleType {
221245
return destructure(type).map { destructured in
@@ -230,6 +254,7 @@ public func destructure(_ type: Type) -> DestructuredType<(Type, Type, Type, Typ
230254
}
231255
}
232256

257+
/// Destructures the given `type` into a ``DestructuredType`` for easy pattern matching.
233258
public func destructure(_ type: Type) -> DestructuredType<(Type, Type, Type, Type, Type)>? {
234259
if let type = type.asSimpleType {
235260
return destructure(type).map { destructured in
@@ -244,6 +269,7 @@ public func destructure(_ type: Type) -> DestructuredType<(Type, Type, Type, Typ
244269
}
245270
}
246271

272+
/// Destructures the given `type` into a ``DestructuredType`` for easy pattern matching.
247273
public func destructure(_ type: Type) -> DestructuredType<(Type, Type, Type, Type, Type, Type)>? {
248274
if let type = type.asSimpleType {
249275
return destructure(type).map { destructured in

Sources/MacroToolkit/Diagnostics.swift

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import SwiftSyntax
2-
import SwiftDiagnostics
31
import Foundation
2+
import SwiftDiagnostics
3+
import SwiftSyntax
44

55
// Taken from: https://github.com/DougGregor/swift-macro-examples/blob/f61ac7cdca8dc3557e53f86e7e03df1353908d3e/MacroExamplesPlugin/Diagnostics.swift
6+
/// A simple diagnostic with a message, id, and severity.
67
public struct SimpleDiagnosticMessage: DiagnosticMessage, Error {
8+
/// The human-readable message.
79
public let message: String
10+
/// The unique diagnostic id (should be the same for all diagnostics produced by the same codepath).
811
public let diagnosticID: MessageID
12+
/// The diagnostic's severity.
913
public let severity: DiagnosticSeverity
1014

15+
/// Creates a new diagnostic message.
1116
public init(message: String, diagnosticID: MessageID, severity: DiagnosticSeverity) {
1217
self.message = message
1318
self.diagnosticID = diagnosticID
@@ -16,6 +21,7 @@ public struct SimpleDiagnosticMessage: DiagnosticMessage, Error {
1621
}
1722

1823
extension SimpleDiagnosticMessage: FixItMessage {
24+
/// The unique fix-it id (should be the same for all fix-its produced by the same codepath).
1925
public var fixItID: MessageID { diagnosticID }
2026
}
2127

@@ -34,44 +40,57 @@ public struct MacroError: LocalizedError {
3440
}
3541
}
3642

43+
/// A way to create rich diagnostics with no unnecessary boilerplate code. Only provide the
44+
/// important details and the rest will be given sensible defaults.
3745
public struct DiagnosticBuilder {
46+
/// The node that the diagnostic will be attached to.
3847
var node: Syntax
48+
/// The message that the diagnostic will show.
3949
var message: String?
50+
/// The diagnostic id (should be the same for all diagnostics produced by the same codepath).
4051
var messageID = MessageID(domain: "UnknownDomain", id: "UnknownError")
52+
/// The fix-its that will be associated with the diagnostics.
4153
var fixIts: [FixIt] = []
54+
/// The additional syntax nodes that will be highlighted by the diagnostic.
4255
var highlights: [Syntax] = []
4356

4457
/// Defaults to ``DiagnosticSeverity/error``.
4558
var severity: DiagnosticSeverity = .error
4659

60+
/// Creates a new builder for a diagnostics related to the given `node`.
4761
public init(for node: some SyntaxProtocol) {
4862
self.node = Syntax(node)
4963
}
5064

65+
/// Set the message.
5166
public func message(_ message: String) -> Self {
5267
var builder = self
5368
builder.message = message
5469
return builder
5570
}
5671

72+
/// Set the severity.
5773
public func severity(_ severity: DiagnosticSeverity) -> Self {
5874
var builder = self
5975
builder.severity = severity
6076
return builder
6177
}
6278

79+
/// Set the message id.
6380
public func messageID(_ messageID: MessageID) -> Self {
6481
var builder = self
6582
builder.messageID = messageID
6683
return builder
6784
}
6885

86+
/// Add a fix-it suggestion.
6987
public func fixIt(_ fixIt: FixIt) -> Self {
7088
var builder = self
7189
builder.fixIts.append(fixIt)
7290
return builder
7391
}
7492

93+
/// Highlight a syntax node related to the diagnostic.
7594
public func highlight(_ node: some SyntaxProtocol) -> Self {
7695
var builder = self
7796
builder.highlights.append(Syntax(node))
@@ -88,25 +107,29 @@ public struct DiagnosticBuilder {
88107
old: some SyntaxProtocol,
89108
new: some SyntaxProtocol
90109
) -> Self {
91-
fixIt(FixIt(
92-
message: SimpleDiagnosticMessage(
93-
message: message ?? "suggested replacement",
94-
diagnosticID: messageID ?? self.messageID,
95-
severity: .error
96-
),
97-
changes: [
98-
FixIt.Change.replace(
99-
oldNode: Syntax(old),
100-
newNode: Syntax(new)
101-
)
102-
]
103-
))
110+
fixIt(
111+
FixIt(
112+
message: SimpleDiagnosticMessage(
113+
message: message ?? "suggested replacement",
114+
diagnosticID: messageID ?? self.messageID,
115+
severity: .error
116+
),
117+
changes: [
118+
FixIt.Change.replace(
119+
oldNode: Syntax(old),
120+
newNode: Syntax(new)
121+
)
122+
]
123+
))
104124
}
105125

126+
/// Set the message id (shorthand for ``messageID(_:)``).
106127
public func messageID(domain: String, id: String) -> Self {
107128
messageID(MessageID(domain: domain, id: id))
108129
}
109130

131+
/// Build the final diagnostic to be emitted. Defaults will be used for any
132+
/// unset configuration values.
110133
public func build() -> Diagnostic {
111134
let messageID = messageID
112135
return Diagnostic(
@@ -120,4 +143,4 @@ public struct DiagnosticBuilder {
120143
fixIts: fixIts
121144
)
122145
}
123-
}
146+
}

Sources/MacroToolkit/Expr.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
import SwiftSyntax
22

3+
/// Wraps an expression syntax node.
34
public struct Expr {
5+
/// The underlying syntax node.
46
public var _syntax: ExprSyntax
57

6-
public init(_ syntax: ExprSyntax) {
7-
_syntax = syntax
8-
}
9-
8+
/// Wraps a syntax node.
109
public init(_ syntax: any ExprSyntaxProtocol) {
1110
_syntax = ExprSyntax(syntax)
1211
}
1312

13+
/// Attempts to get the expression as a string literal.
1414
public var asStringLiteral: StringLiteral? {
1515
StringLiteral(self._syntax)
1616
}
1717

18+
/// Attempts to get the expression as a boolean literal.
1819
public var asBooleanLiteral: BooleanLiteral? {
1920
BooleanLiteral(self._syntax)
2021
}
2122

23+
/// Attempts to get the expression as a floating point literal.
2224
public var asFloatLiteral: FloatLiteral? {
2325
FloatLiteral(self._syntax)
2426
}
2527

28+
/// Attempts to get the expression as an integer literal.
2629
public var asIntegerLiteral: IntegerLiteral? {
2730
IntegerLiteral(self._syntax)
2831
}
2932

33+
/// Attempts to get the expression as a nil literal.
3034
public var asNilLiteral: NilLiteral? {
3135
NilLiteral(self._syntax)
3236
}
3337

38+
/// Attempts to get the expression as a regex literal.
3439
public var asRegexLiteral: RegexLiteral? {
3540
RegexLiteral(self._syntax)
3641
}

Sources/MacroToolkit/ExprProtocol.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import SwiftSyntax
22

3+
/// A protocol for expression syntax wrappers to conform to. Allows simple conversion between expression wrappers.
34
public protocol ExprProtocol {
5+
/// The type of the underlying syntax node being wrapped.
46
associatedtype WrappedSyntax: ExprSyntaxProtocol
7+
/// The underlying syntax node being wrapped.
58
var _syntax: WrappedSyntax { get }
9+
/// Wraps a syntax node.
610
init(_ syntax: WrappedSyntax)
711
}
812

913
extension ExprProtocol {
14+
/// Attempts to initialize the wrapper from an arbitrary expression (succeeds
15+
/// if the expression is the right type of syntax).
1016
public init?(_ syntax: ExprSyntaxProtocol) {
1117
guard let syntax = syntax.as(WrappedSyntax.self) else {
1218
return nil
1319
}
1420
self.init(syntax)
1521
}
1622

23+
/// Attempts to initialize the wrapper from an arbitrary expression (succeeds
24+
/// if the expression is the right type of syntax).
1725
public init?(_ expr: Expr) {
1826
guard let syntax = expr._syntax.as(WrappedSyntax.self) else {
1927
return nil

0 commit comments

Comments
 (0)