@@ -46,7 +46,7 @@ import SwiftSyntax
46
46
}
47
47
48
48
/// The name of the implicit declaration.
49
- private var name : String {
49
+ private var name : StaticString {
50
50
switch self {
51
51
case . self :
52
52
return " self "
@@ -86,17 +86,38 @@ import SwiftSyntax
86
86
/// `self` and `Self` identifers override implicit `self` and `Self` introduced by
87
87
/// the `Foo` class declaration.
88
88
var identifier : Identifier {
89
+ Identifier ( name)
90
+ }
91
+
92
+ /// Position of this implicit name.
93
+ @_spi ( Experimental) public var position : AbsolutePosition {
89
94
switch self {
90
- case . self :
91
- return Identifier ( " self " )
92
- case . Self:
93
- return Identifier ( " Self " )
94
- case . error:
95
- return Identifier ( " error " )
96
- case . newValue:
97
- return Identifier ( " newValue " )
98
- case . oldValue:
99
- return Identifier ( " oldValue " )
95
+ case . self ( let declSyntax) :
96
+ switch Syntax ( declSyntax) . as ( SyntaxEnum . self) {
97
+ case . functionDecl( let functionDecl) :
98
+ return functionDecl. name. positionAfterSkippingLeadingTrivia
99
+ case . initializerDecl( let initializerDecl) :
100
+ return initializerDecl. initKeyword. positionAfterSkippingLeadingTrivia
101
+ case . subscriptDecl( let subscriptDecl) :
102
+ return subscriptDecl. accessorBlock? . positionAfterSkippingLeadingTrivia
103
+ ?? subscriptDecl. endPositionBeforeTrailingTrivia
104
+ case . variableDecl( let variableDecl) :
105
+ return variableDecl. bindings. first? . accessorBlock? . positionAfterSkippingLeadingTrivia
106
+ ?? variableDecl. endPosition
107
+ default :
108
+ return declSyntax. positionAfterSkippingLeadingTrivia
109
+ }
110
+ case . Self( let declSyntax) :
111
+ switch Syntax ( declSyntax) . as ( SyntaxEnum . self) {
112
+ case . protocolDecl( let protocolDecl) :
113
+ return protocolDecl. name. positionAfterSkippingLeadingTrivia
114
+ default :
115
+ return declSyntax. positionAfterSkippingLeadingTrivia
116
+ }
117
+ case . error( let catchClause) :
118
+ return catchClause. catchItems. positionAfterSkippingLeadingTrivia
119
+ default :
120
+ return syntax. positionAfterSkippingLeadingTrivia
100
121
}
101
122
}
102
123
}
@@ -110,6 +131,8 @@ import SwiftSyntax
110
131
case declaration( NamedDeclSyntax )
111
132
/// Name introduced implicitly by certain syntax nodes.
112
133
case implicit( ImplicitDecl )
134
+ /// Dollar identifier introduced by a closure without parameters.
135
+ case dollarIdentifier( ClosureExprSyntax , strRepresentation: String )
113
136
114
137
/// Syntax associated with this name.
115
138
@_spi ( Experimental) public var syntax : SyntaxProtocol {
@@ -120,6 +143,8 @@ import SwiftSyntax
120
143
return syntax
121
144
case . implicit( let implicitName) :
122
145
return implicitName. syntax
146
+ case . dollarIdentifier( let closureExpr, _) :
147
+ return closureExpr
123
148
}
124
149
}
125
150
@@ -132,6 +157,8 @@ import SwiftSyntax
132
157
return Identifier ( syntax. name)
133
158
case . implicit( let kind) :
134
159
return kind. identifier
160
+ case . dollarIdentifier( _, strRepresentation: _) :
161
+ return nil
135
162
}
136
163
}
137
164
@@ -149,34 +176,9 @@ import SwiftSyntax
149
176
case . declaration( let syntax) :
150
177
return syntax. name. positionAfterSkippingLeadingTrivia
151
178
case . implicit( let implicitName) :
152
- switch implicitName {
153
- case . self ( let declSyntax) :
154
- switch Syntax ( declSyntax) . as ( SyntaxEnum . self) {
155
- case . functionDecl( let functionDecl) :
156
- return functionDecl. name. positionAfterSkippingLeadingTrivia
157
- case . initializerDecl( let initializerDecl) :
158
- return initializerDecl. initKeyword. positionAfterSkippingLeadingTrivia
159
- case . subscriptDecl( let subscriptDecl) :
160
- return subscriptDecl. accessorBlock? . positionAfterSkippingLeadingTrivia
161
- ?? subscriptDecl. endPositionBeforeTrailingTrivia
162
- case . variableDecl( let variableDecl) :
163
- return variableDecl. bindings. first? . accessorBlock? . positionAfterSkippingLeadingTrivia
164
- ?? variableDecl. endPosition
165
- default :
166
- return declSyntax. positionAfterSkippingLeadingTrivia
167
- }
168
- case . Self( let declSyntax) :
169
- switch Syntax ( declSyntax) . as ( SyntaxEnum . self) {
170
- case . protocolDecl( let protocolDecl) :
171
- return protocolDecl. name. positionAfterSkippingLeadingTrivia
172
- default :
173
- return declSyntax. positionAfterSkippingLeadingTrivia
174
- }
175
- case . error( let catchClause) :
176
- return catchClause. body. positionAfterSkippingLeadingTrivia
177
- default :
178
- return implicitName. syntax. positionAfterSkippingLeadingTrivia
179
- }
179
+ return implicitName. position
180
+ case . dollarIdentifier( let closureExpr, _) :
181
+ return closureExpr. positionAfterSkippingLeadingTrivia
180
182
}
181
183
}
182
184
@@ -197,6 +199,17 @@ import SwiftSyntax
197
199
return accessibleAfter <= lookUpPosition
198
200
}
199
201
202
+ func refersTo( _ otherIdentifier: Identifier ? ) -> Bool {
203
+ guard let otherIdentifier else { return true }
204
+
205
+ switch self {
206
+ case . dollarIdentifier( _, let strRepresentation) :
207
+ return strRepresentation == otherIdentifier. name
208
+ default :
209
+ return identifier == otherIdentifier
210
+ }
211
+ }
212
+
200
213
/// Extracts names introduced by the given `syntax` structure.
201
214
///
202
215
/// When e.g. looking up a variable declaration like `let a = a`,
@@ -221,6 +234,12 @@ import SwiftSyntax
221
234
return tuplePattern. elements. flatMap { tupleElement in
222
235
getNames ( from: tupleElement. pattern, accessibleAfter: accessibleAfter)
223
236
}
237
+ case . tupleExpr( let tupleExpr) :
238
+ return tupleExpr. elements. flatMap { tupleElement in
239
+ getNames ( from: tupleElement, accessibleAfter: accessibleAfter)
240
+ }
241
+ case . labeledExpr( let labeledExpr) :
242
+ return getNames ( from: labeledExpr. expression, accessibleAfter: accessibleAfter)
224
243
case . valueBindingPattern( let valueBindingPattern) :
225
244
return getNames ( from: valueBindingPattern. pattern, accessibleAfter: accessibleAfter)
226
245
case . expressionPattern( let expressionPattern) :
@@ -288,6 +307,8 @@ import SwiftSyntax
288
307
return " declaration: \( strName) "
289
308
case . implicit:
290
309
return " implicit: \( strName) "
310
+ case . dollarIdentifier( _, strRepresentation: let str) :
311
+ return " dollarIdentifier: \( str) "
291
312
}
292
313
}
293
314
}
0 commit comments