-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSwiftSyntax+Extensions.swift
More file actions
186 lines (170 loc) · 6.15 KB
/
SwiftSyntax+Extensions.swift
File metadata and controls
186 lines (170 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import SwiftSyntax
extension SyntaxProtocol {
/// Gets the syntax with trivia removed.
public func withoutTrivia() -> Self {
var syntax = self
syntax.leadingTrivia = []
syntax.trailingTrivia = []
return syntax
}
}
extension SyntaxProtocol {
/// Gets the syntax with a leading newline added.
public func withLeadingNewline() -> Self {
with(\.leadingTrivia, leadingTrivia + [.newlines(1)])
}
/// Gets the syntax indented using the specified `indentation`.
public func indented(_ indentation: Indentation = .spaces(4)) -> Self {
switch indentation {
case .spaces(let spaces):
return with(\.leadingTrivia, leadingTrivia + [.spaces(spaces)])
case .tab:
return with(\.leadingTrivia, leadingTrivia + [.tabs(1)])
}
}
}
/// An indentation style.
public enum Indentation {
/// n-space indentation.
case spaces(Int)
/// Tab-based indentation.
case tab
}
extension DeclGroupSyntax {
/// Intended for use in generating user-facing messages. Use a more strongly typed
/// approach if actually checking the decl kind.
/// - Parameter article: If `true`, an appropriate article is included before the
/// decl kind (e.g. `"a"` or `"an")
public func textualDeclKind(withArticle article: Bool = false) -> String {
// Modified from: https://github.com/DougGregor/swift-macro-examples/blob/f61ac7cdca8dc3557e53f86e7e03df1353908d3e/MacroExamplesPlugin/MetaEnumMacro.swift#L121
switch self {
case is ActorDeclSyntax:
return article ? "an actor" : "actor"
case is ClassDeclSyntax:
return article ? "a class" : "class"
case is ExtensionDeclSyntax:
return article ? "an extension" : "extension"
case is ProtocolDeclSyntax:
return article ? "a protocol" : "protocol"
case is StructDeclSyntax:
return article ? "a struct" : "struct"
case is EnumDeclSyntax:
return article ? "an enum" : "enum"
default:
return "unknown"
}
}
}
extension FunctionDeclSyntax {
/// Gets the signature's effect specifiers, or returns a default effect specifiers
/// syntax (without any specifiers).
public var effectSpecifiersOrDefault: FunctionEffectSpecifiersSyntax {
signature.effectSpecifiers
?? FunctionEffectSpecifiersSyntax(
leadingTrivia: " ", asyncSpecifier: nil, throwsSpecifier: nil
)
}
/// Returns the function with or without the `async` modifier (controlled by `isPresent`).
public func withAsyncModifier(_ isPresent: Bool = true) -> FunctionDeclSyntax {
with(
\.signature,
signature
.with(
\.effectSpecifiers,
effectSpecifiersOrDefault
.with(\.asyncSpecifier, isPresent ? " async" : nil)
)
)
}
/// Returns the function with or without the `throws` modifier (controlled by `isPresent`).
public func withThrowsModifier(_ isPresent: Bool = true) -> FunctionDeclSyntax {
with(
\.signature,
signature
.with(
\.effectSpecifiers,
effectSpecifiersOrDefault
.with(\.throwsSpecifier, isPresent ? " throws" : nil)
)
)
}
/// Returns the function, replacing its parameters with the given new parameter list.
public func withParameters(
_ parameters: some Sequence<FunctionParameter>
) -> FunctionDeclSyntax {
with(
\.signature,
signature
.with(
\.parameterClause,
FunctionParameterClauseSyntax(parameters: parameters.asParameterList)
)
)
}
/// Returns the function, replacing its return type with a new return type.
public func withReturnType(_ type: Type?) -> FunctionDeclSyntax {
if let type = type {
return with(
\.signature,
signature
.with(
\.returnClause,
ReturnClauseSyntax(
leadingTrivia: " ",
type: type._syntax
)
)
)
} else {
return with(\.signature, signature.with(\.returnClause, nil))
}
}
/// Returns the function, replacing its body with a new code block.
public func withBody(_ codeBlock: CodeBlockSyntax) -> FunctionDeclSyntax {
with(
\.body,
codeBlock
)
}
/// Returns the function, replacing its body with a collection of expressions.
public func withBody(_ exprs: [ExprSyntax]) -> FunctionDeclSyntax {
with(
\.body,
CodeBlockSyntax(exprs)
)
}
/// Returns the function, replacing its attributes with a new collection of attributes.
public func withAttributes(_ attributes: [AttributeListElement]) -> FunctionDeclSyntax {
with(
\.attributes,
attributes.asAttributeList
)
}
/// Returns the function with a leading blank line.
public func withLeadingBlankLine() -> FunctionDeclSyntax {
with(
\.leadingTrivia,
.newlines(2)
)
}
}
extension CodeBlockSyntax {
/// Creates a code block from an array of expressions.
public init(_ exprs: [ExprSyntax]) {
self.init(
leftBrace: .leftBraceToken(leadingTrivia: .space),
statements: CodeBlockItemListSyntax(
exprs.map { expr in
CodeBlockItemSyntax(item: .expr(expr))
}
),
rightBrace: .rightBraceToken(leadingTrivia: .newline)
)
}
}
extension DeclGroupSyntax {
/// Gets whether a declaration group has the `public` access level modifier.
public var isPublic: Bool {
modifiers.contains { $0.name.tokenKind == .keyword(.public) } == true
}
}