Skip to content

Commit 9945bf0

Browse files
committed
Add suggested changes from the previous PR.
1 parent 93cb3ea commit 9945bf0

File tree

11 files changed

+70
-67
lines changed

11 files changed

+70
-67
lines changed

Sources/SwiftLexicalLookup/Configurations/LookupConfig.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import Foundation
1515
@_spi(Experimental) public struct LookupConfig {
1616
/// Specifies behaviour of file scope.
1717
/// `memberBlockUpToLastDecl` by default.
18-
public var fileScopeHandling: FileScopeHandlingConfig
18+
@_spi(Experimental) public var fileScopeHandling: FileScopeHandlingConfig
1919

20-
public init(
20+
@_spi(Experimental) public init(
2121
fileScopeHandling: FileScopeHandlingConfig = .memberBlockUpToLastDecl
2222
) {
2323
self.fileScopeHandling = fileScopeHandling

Sources/SwiftLexicalLookup/IdentifiableSyntax.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
import SwiftSyntax
1414

1515
/// Syntax node that can be refered to with an identifier.
16-
public protocol IdentifiableSyntax: SyntaxProtocol {
16+
@_spi(Experimental) public protocol IdentifiableSyntax: SyntaxProtocol {
1717
var identifier: TokenSyntax { get }
1818
}
1919

20-
extension IdentifierPatternSyntax: IdentifiableSyntax {}
20+
@_spi(Experimental) extension IdentifierPatternSyntax: IdentifiableSyntax {}
2121

22-
extension ClosureParameterSyntax: IdentifiableSyntax {
22+
@_spi(Experimental) extension ClosureParameterSyntax: IdentifiableSyntax {
2323
@_spi(Experimental) public var identifier: TokenSyntax {
2424
secondName ?? firstName
2525
}
2626
}
2727

28-
extension ClosureShorthandParameterSyntax: IdentifiableSyntax {
28+
@_spi(Experimental) extension ClosureShorthandParameterSyntax: IdentifiableSyntax {
2929
@_spi(Experimental) public var identifier: TokenSyntax {
3030
name
3131
}
3232
}
3333

34-
extension ClosureCaptureSyntax: IdentifiableSyntax {
34+
@_spi(Experimental) extension ClosureCaptureSyntax: IdentifiableSyntax {
3535
@_spi(Experimental) public var identifier: TokenSyntax {
3636
/* Doesn't work with closures like:
3737
_ = { [y=1+2] in
@@ -42,7 +42,7 @@ extension ClosureCaptureSyntax: IdentifiableSyntax {
4242
}
4343
}
4444

45-
extension AccessorParametersSyntax: IdentifiableSyntax {
45+
@_spi(Experimental) extension AccessorParametersSyntax: IdentifiableSyntax {
4646
@_spi(Experimental) public var identifier: TokenSyntax {
4747
name
4848
}

Sources/SwiftLexicalLookup/LookupName.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@ import SwiftSyntax
124124
return name == lookedUpName
125125
}
126126

127-
/// Extracts names introduced by the given `from` structure.
128-
static func getNames(from syntax: SyntaxProtocol, accessibleAfter: AbsolutePosition? = nil) -> [LookupName] {
127+
/// Extracts names introduced by the given `syntax` structure.
128+
static func getNames(
129+
from syntax: SyntaxProtocol,
130+
accessibleAfter: AbsolutePosition? = nil
131+
) -> [LookupName] {
129132
switch Syntax(syntax).as(SyntaxEnum.self) {
130133
case .variableDecl(let variableDecl):
131134
variableDecl.bindings.flatMap { binding in
@@ -165,21 +168,26 @@ import SwiftSyntax
165168
}
166169

167170
/// Extracts name introduced by `IdentifiableSyntax` node.
168-
private static func handle(identifiable: IdentifiableSyntax, accessibleAfter: AbsolutePosition? = nil) -> [LookupName]
169-
{
171+
private static func handle(
172+
identifiable: IdentifiableSyntax,
173+
accessibleAfter: AbsolutePosition? = nil
174+
) -> [LookupName] {
170175
if let closureCapture = identifiable as? ClosureCaptureSyntax,
171176
closureCapture.identifier.tokenKind == .keyword(.self)
172177
{
173178
return [.implicit(.self(closureCapture))] // Handle `self` closure capture.
174-
} else if identifiable.identifier.text != "_" {
179+
} else if identifiable.identifier.tokenKind != .wildcard {
175180
return [.identifier(identifiable, accessibleAfter: accessibleAfter)]
176181
} else {
177182
return []
178183
}
179184
}
180185

181186
/// Extracts name introduced by `NamedDeclSyntax` node.
182-
private static func handle(namedDecl: NamedDeclSyntax, accessibleAfter: AbsolutePosition? = nil) -> [LookupName] {
187+
private static func handle(
188+
namedDecl: NamedDeclSyntax,
189+
accessibleAfter: AbsolutePosition? = nil
190+
) -> [LookupName] {
183191
[.declaration(namedDecl, accessibleAfter: accessibleAfter)]
184192
}
185193
}

Sources/SwiftLexicalLookup/LookupResult.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SwiftSyntax
1414

15-
/// Represents resul
15+
/// Represents result from a specific scope.
1616
@_spi(Experimental) public enum LookupResult {
1717
/// Scope and the names that matched lookup.
1818
case fromScope(ScopeSyntax, withNames: [LookupName])

Sources/SwiftLexicalLookup/LookupState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Foundation
1515
@_spi(Experimental) public struct LookupState {
1616
/// Specifies scopes that introduce names to their parent and
1717
/// should be skipped during lookup in sequential scopes.
18-
var skipSequentialIntroductionFrom: IntroducingToSequentialParentScopeSyntax?
18+
@_spi(Experimental) public var skipSequentialIntroductionFrom: IntroducingToSequentialParentScopeSyntax?
1919

2020
@_spi(Experimental) public init() {}
2121
}

Sources/SwiftLexicalLookup/ScopeImplementations.swift

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
import SwiftSyntax
1414

15-
extension SyntaxProtocol {
15+
@_spi(Experimental) extension SyntaxProtocol {
1616
/// Parent scope of this syntax node, or scope introduced by this syntax node.
17-
var scope: ScopeSyntax? {
17+
@_spi(Experimental) public var scope: ScopeSyntax? {
1818
if let scopeSyntax = Syntax(self).asProtocol(SyntaxProtocol.self) as? ScopeSyntax {
1919
scopeSyntax
2020
} else {
@@ -26,7 +26,7 @@ extension SyntaxProtocol {
2626
@_spi(Experimental) extension SourceFileSyntax: SequentialScopeSyntax {
2727
/// All names introduced in the file scope
2828
/// according to the default strategy: `memberBlockUpToLastDecl`.
29-
public var introducedNames: [LookupName] {
29+
@_spi(Experimental) public var introducedNames: [LookupName] {
3030
introducedNames(using: .memberBlockUpToLastDecl)
3131
}
3232

@@ -47,10 +47,10 @@ extension SyntaxProtocol {
4747
/// ```
4848
/// During lookup, according to different configurations,
4949
/// names available at the marked place are:
50-
/// - for `fileScopeNameIntroductionStrategy` - a, b, c, d
50+
/// - for `memberBlockUpToLastDecl` - a, b, c, d
5151
/// - for `memberBlock` - a, b, c, d, e, f
5252
/// - for `codeBlock` - a
53-
public func introducedNames(using fileScopeHandling: FileScopeHandlingConfig) -> [LookupName] {
53+
@_spi(Experimental) public func introducedNames(using fileScopeHandling: FileScopeHandlingConfig) -> [LookupName] {
5454
switch fileScopeHandling {
5555
case .memberBlockUpToLastDecl:
5656
var encounteredNonDeclaration = false
@@ -61,7 +61,7 @@ extension SyntaxProtocol {
6161
if encounteredNonDeclaration {
6262
return LookupName.getNames(from: item, accessibleAfter: codeBlockItem.endPosition)
6363
} else {
64-
if item.is(DeclSyntax.self) || item.is(VariableDeclSyntax.self) {
64+
if item.is(DeclSyntax.self) {
6565
return LookupName.getNames(from: item)
6666
} else {
6767
encounteredNonDeclaration = true
@@ -97,10 +97,10 @@ extension SyntaxProtocol {
9797
/// ```
9898
/// According to different configurations,
9999
/// names available at the marked place are:
100-
/// - for `fileScopeNameIntroductionStrategy` - a, b, c, d
100+
/// - for `memberBlockUpToLastDecl` - a, b, c, d
101101
/// - for `memberBlock` - a, b, c, d, e, f
102102
/// - for `codeBlock` - a
103-
public func lookup(
103+
@_spi(Experimental) public func lookup(
104104
for name: String?,
105105
at syntax: SyntaxProtocol,
106106
with config: LookupConfig,
@@ -162,13 +162,13 @@ extension SyntaxProtocol {
162162
@_spi(Experimental) extension CodeBlockSyntax: SequentialScopeSyntax {
163163
/// Names introduced in the code block scope
164164
/// accessible after their declaration.
165-
public var introducedNames: [LookupName] {
165+
@_spi(Experimental) public var introducedNames: [LookupName] {
166166
statements.flatMap { codeBlockItem in
167167
LookupName.getNames(from: codeBlockItem.item, accessibleAfter: codeBlockItem.endPosition)
168168
}
169169
}
170170

171-
public func lookup(
171+
@_spi(Experimental) public func lookup(
172172
for name: String?,
173173
at syntax: SyntaxProtocol,
174174
with config: LookupConfig,
@@ -187,7 +187,7 @@ extension SyntaxProtocol {
187187

188188
@_spi(Experimental) extension ForStmtSyntax: ScopeSyntax {
189189
/// Names introduced in the `for` body.
190-
public var introducedNames: [LookupName] {
190+
@_spi(Experimental) public var introducedNames: [LookupName] {
191191
LookupName.getNames(from: pattern)
192192
}
193193
}
@@ -204,26 +204,20 @@ extension SyntaxProtocol {
204204
/// ```
205205
/// During lookup, names available at the marked place are:
206206
/// `self`, a, b.
207-
public var introducedNames: [LookupName] {
207+
@_spi(Experimental) public var introducedNames: [LookupName] {
208208
let captureNames =
209-
signature?.capture?.children(viewMode: .sourceAccurate).flatMap { child in
210-
if let captureList = child.as(ClosureCaptureListSyntax.self) {
211-
captureList.children(viewMode: .sourceAccurate).flatMap { capture in
212-
LookupName.getNames(from: capture)
213-
}
214-
} else {
215-
LookupName.getNames(from: child)
216-
}
209+
signature?.capture?.items.flatMap { element in
210+
LookupName.getNames(from: element)
217211
} ?? []
218212

219213
let parameterNames =
220214
signature?.parameterClause?.children(viewMode: .sourceAccurate).flatMap { parameter in
221215
if let parameterList = parameter.as(ClosureParameterListSyntax.self) {
222-
parameterList.children(viewMode: .sourceAccurate).flatMap { parameter in
216+
return parameterList.children(viewMode: .sourceAccurate).flatMap { parameter in
223217
LookupName.getNames(from: parameter)
224218
}
225219
} else {
226-
LookupName.getNames(from: parameter)
220+
return LookupName.getNames(from: parameter)
227221
}
228222
} ?? []
229223

@@ -233,7 +227,7 @@ extension SyntaxProtocol {
233227

234228
@_spi(Experimental) extension WhileStmtSyntax: ScopeSyntax {
235229
/// Names introduced by the `while` loop by its conditions.
236-
public var introducedNames: [LookupName] {
230+
@_spi(Experimental) public var introducedNames: [LookupName] {
237231
conditions.flatMap { element in
238232
LookupName.getNames(from: element.condition)
239233
}
@@ -242,7 +236,7 @@ extension SyntaxProtocol {
242236

243237
@_spi(Experimental) extension IfExprSyntax: ScopeSyntax {
244238
/// Parent scope, omitting ancestor `if` statements if part of their `else if` clause.
245-
public var parentScope: ScopeSyntax? {
239+
@_spi(Experimental) public var parentScope: ScopeSyntax? {
246240
getParent(for: self.parent, previousIfElse: self.elseKeyword == nil)
247241
}
248242

@@ -278,7 +272,7 @@ extension SyntaxProtocol {
278272
}
279273

280274
/// Names introduced by the `if` optional binding conditions.
281-
public var introducedNames: [LookupName] {
275+
@_spi(Experimental) public var introducedNames: [LookupName] {
282276
conditions.flatMap { element in
283277
LookupName.getNames(from: element.condition, accessibleAfter: element.endPosition)
284278
}
@@ -296,7 +290,7 @@ extension SyntaxProtocol {
296290
/// // <-- a is not visible here
297291
/// }
298292
/// ```
299-
public func lookup(
293+
@_spi(Experimental) public func lookup(
300294
for name: String?,
301295
at syntax: SyntaxProtocol,
302296
with config: LookupConfig,
@@ -312,15 +306,15 @@ extension SyntaxProtocol {
312306

313307
@_spi(Experimental) extension MemberBlockSyntax: ScopeSyntax {
314308
/// All names introduced by members of this member scope.
315-
public var introducedNames: [LookupName] {
309+
@_spi(Experimental) public var introducedNames: [LookupName] {
316310
members.flatMap { member in
317311
LookupName.getNames(from: member.decl)
318312
}
319313
}
320314
}
321315

322316
@_spi(Experimental) extension GuardStmtSyntax: IntroducingToSequentialParentScopeSyntax {
323-
public func introducesToSequentialParent(
317+
@_spi(Experimental) public func introducesToSequentialParent(
324318
for name: String?,
325319
at syntax: SwiftSyntax.SyntaxProtocol,
326320
with config: LookupConfig,
@@ -335,7 +329,7 @@ extension SyntaxProtocol {
335329
return names.isEmpty ? [] : [.fromScope(self, withNames: names)]
336330
}
337331

338-
public var introducedNames: [LookupName] {
332+
@_spi(Experimental) public var introducedNames: [LookupName] {
339333
[]
340334
}
341335

@@ -350,7 +344,7 @@ extension SyntaxProtocol {
350344
/// }
351345
/// // a is visible here
352346
/// ```
353-
public func lookup(
347+
@_spi(Experimental) public func lookup(
354348
for name: String?,
355349
at syntax: SyntaxProtocol,
356350
with config: LookupConfig,
@@ -373,7 +367,7 @@ extension SyntaxProtocol {
373367
@_spi(Experimental) extension ExtensionDeclSyntax: TypeScopeSyntax {}
374368

375369
@_spi(Experimental) extension AccessorDeclSyntax: ScopeSyntax {
376-
public var introducedNames: [LookupName] {
370+
@_spi(Experimental) public var introducedNames: [LookupName] {
377371
if let parameters {
378372
LookupName.getNames(from: parameters)
379373
} else {

Sources/SwiftLexicalLookup/ScopeSyntax.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import SwiftSyntax
1414

1515
extension SyntaxProtocol {
1616
/// Returns all names that `for` refers to at this syntax node.
17-
/// Optional configuration can be passed as `with` to customize the lookup behavior.
17+
/// Optional configuration can be passed as `config` to customize the lookup behavior.
1818
///
19-
/// - Returns: An array of `LookupResult` for name `for` at this syntax node,
20-
/// ordered by visibility. If set to `nil`, returns all available names ordered by visibility.
19+
/// - Returns: An array of `LookupResult` for `name` at this syntax node,
20+
/// ordered by visibility. If `name` is set to `nil`, returns all available names ordered by visibility.
2121
/// The order is from the innermost to the outermost scope,
2222
/// and within each result, names are ordered by their introduction
2323
/// in the source code.
@@ -56,7 +56,7 @@ extension SyntaxProtocol {
5656
var parentScope: ScopeSyntax? { get }
5757
/// Names found in this scope. Ordered from first to last introduced.
5858
var introducedNames: [LookupName] { get }
59-
/// Finds all declarations `name` refers to. `at` specifies the node lookup was triggered with.
59+
/// Finds all declarations `name` refers to. `syntax` specifies the node lookup was triggered with.
6060
/// If `name` set to `nil`, returns all available names at the given node.
6161
func lookup(for name: String?,
6262
at syntax: SyntaxProtocol,
@@ -65,14 +65,14 @@ extension SyntaxProtocol {
6565
}
6666

6767
@_spi(Experimental) extension ScopeSyntax {
68-
public var parentScope: ScopeSyntax? {
68+
@_spi(Experimental) public var parentScope: ScopeSyntax? {
6969
self.parent?.scope
7070
}
7171

7272
/// Returns `LookupResult` of all names introduced in this scope that `name`
7373
/// refers to and is accessible at given syntax node then passes lookup to the parent.
7474
/// If `name` set to `nil`, returns all available names at the given node.
75-
public func lookup(
75+
@_spi(Experimental) public func lookup(
7676
for name: String?,
7777
at syntax: SyntaxProtocol,
7878
with config: LookupConfig,

Sources/SwiftLexicalLookup/SequentialScopeSyntax.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import SwiftSyntax
3030
}
3131

3232
@_spi(Experimental) extension SequentialScopeSyntax {
33-
public func sequentialLookup(
33+
@_spi(Experimental) public func sequentialLookup(
3434
in codeBlockItems: any Collection<CodeBlockItemSyntax>,
3535
for name: String?,
3636
at syntax: SyntaxProtocol,

Sources/SwiftLexicalLookup/TypeScopeSyntax.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import SwiftSyntax
1717
}
1818

1919
@_spi(Experimental) extension TypeScopeSyntax {
20-
public var implicitInstanceAndTypeNames: [LookupName] {
20+
@_spi(Experimental) public var implicitInstanceAndTypeNames: [LookupName] {
2121
[.implicit(.self(self)), .implicit(.Self(self))]
2222
}
2323

24-
public var introducedNames: [LookupName] {
24+
@_spi(Experimental) public var introducedNames: [LookupName] {
2525
implicitInstanceAndTypeNames
2626
}
2727
}

0 commit comments

Comments
 (0)