Skip to content

Commit 5519ccb

Browse files
committed
Format.
1 parent 7fdb145 commit 5519ccb

File tree

6 files changed

+89
-62
lines changed

6 files changed

+89
-62
lines changed

Sources/SwiftLexicalLookup/Configurations/LookupConfigDictionary.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ import SwiftSyntax
1515
/// Stores and provides easy access for lookup configuration.
1616
@_spi(Experimental) public struct LookupConfigDictionary {
1717
private var dictionary: [ObjectIdentifier: LookupConfig]
18-
18+
1919
/// Creates a new lookup configuration dictionary
2020
/// from a given array of configurations.
2121
init(from configArray: [LookupConfig]) {
2222
dictionary = [:]
23-
23+
2424
for config in configArray {
2525
dictionary[config.identifier] = config
2626
}
2727
}
28-
28+
2929
subscript<T: LookupConfig>(key: T.Type) -> T? {
3030
get {
3131
return dictionary[ObjectIdentifier(key)] as? T

Sources/SwiftLexicalLookup/LookupResult.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import SwiftSyntax
1818
case fromScope(ScopeSyntax, withNames: [LookupName])
1919
/// File scope, names that matched lookup and name introduction
2020
/// strategy used for the lookup.
21-
case fromFileScope(SourceFileSyntax, withNames: [LookupName], nameIntroductionStrategy: FileScopeNameIntroductionStrategy)
21+
case fromFileScope(
22+
SourceFileSyntax,
23+
withNames: [LookupName],
24+
nameIntroductionStrategy: FileScopeNameIntroductionStrategy
25+
)
2226

2327
/// Associated scope.
2428
@_spi(Experimental) public var scope: ScopeSyntax? {

Sources/SwiftLexicalLookup/ScopeImplementations.swift

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ extension SyntaxProtocol {
2727
public var introducedNames: [LookupName] {
2828
introducedNames(using: .memberBlockUpToLastDecl)
2929
}
30-
30+
3131
public func introducedNames(using nameIntroductionStrategy: FileScopeNameIntroductionStrategy) -> [LookupName] {
3232
switch nameIntroductionStrategy {
3333
case .memberBlockUpToLastDecl:
3434
var encounteredNonDeclaration = false
35-
35+
3636
return statements.flatMap { codeBlockItem in
3737
let item = codeBlockItem.item
38-
38+
3939
if encounteredNonDeclaration {
4040
return LookupName.getNames(from: item, accessibleAfter: codeBlockItem.endPosition)
4141
} else {
@@ -57,15 +57,19 @@ extension SyntaxProtocol {
5757
}
5858
}
5959
}
60-
61-
public func lookup(for name: String?, at syntax: SyntaxProtocol, with configDict: LookupConfigDictionary) -> [LookupResult] {
60+
61+
public func lookup(
62+
for name: String?,
63+
at syntax: SyntaxProtocol,
64+
with configDict: LookupConfigDictionary
65+
) -> [LookupResult] {
6266
let nameIntroductionStrategy = configDict[FileScopeNameIntroductionStrategy.self] ?? .memberBlockUpToLastDecl
63-
67+
6468
let names = introducedNames(using: nameIntroductionStrategy)
6569
.filter { introducedName in
6670
introducedName.isAccessible(at: syntax) && (name == nil || introducedName.refersTo(name!))
6771
}
68-
72+
6973
return [.fromFileScope(self, withNames: names, nameIntroductionStrategy: nameIntroductionStrategy)]
7074
}
7175
}
@@ -86,26 +90,28 @@ extension SyntaxProtocol {
8690

8791
@_spi(Experimental) extension ClosureExprSyntax: ScopeSyntax {
8892
public var introducedNames: [LookupName] {
89-
let captureNames = signature?.capture?.children(viewMode: .sourceAccurate).flatMap { child in
90-
if let captureList = child.as(ClosureCaptureListSyntax.self) {
91-
captureList.children(viewMode: .sourceAccurate).flatMap { capture in
92-
LookupName.getNames(from: capture)
93+
let captureNames =
94+
signature?.capture?.children(viewMode: .sourceAccurate).flatMap { child in
95+
if let captureList = child.as(ClosureCaptureListSyntax.self) {
96+
captureList.children(viewMode: .sourceAccurate).flatMap { capture in
97+
LookupName.getNames(from: capture)
98+
}
99+
} else {
100+
LookupName.getNames(from: child)
93101
}
94-
} else {
95-
LookupName.getNames(from: child)
96-
}
97-
} ?? []
98-
99-
let parameterNames = signature?.parameterClause?.children(viewMode: .sourceAccurate).flatMap { parameter in
100-
if let parameterList = parameter.as(ClosureParameterListSyntax.self) {
101-
parameterList.children(viewMode: .sourceAccurate).flatMap { parameter in
102+
} ?? []
103+
104+
let parameterNames =
105+
signature?.parameterClause?.children(viewMode: .sourceAccurate).flatMap { parameter in
106+
if let parameterList = parameter.as(ClosureParameterListSyntax.self) {
107+
parameterList.children(viewMode: .sourceAccurate).flatMap { parameter in
108+
LookupName.getNames(from: parameter)
109+
}
110+
} else {
102111
LookupName.getNames(from: parameter)
103112
}
104-
} else {
105-
LookupName.getNames(from: parameter)
106-
}
107-
} ?? []
108-
113+
} ?? []
114+
109115
return captureNames + parameterNames
110116
}
111117
}
@@ -144,7 +150,11 @@ extension SyntaxProtocol {
144150
}
145151
}
146152

147-
public func lookup(for name: String?, at syntax: SyntaxProtocol, with configDict: LookupConfigDictionary) -> [LookupResult] {
153+
public func lookup(
154+
for name: String?,
155+
at syntax: SyntaxProtocol,
156+
with configDict: LookupConfigDictionary
157+
) -> [LookupResult] {
148158
if let elseBody, elseBody.position <= syntax.position, elseBody.endPosition >= syntax.position {
149159
lookupInParent(for: name, at: syntax, with: configDict)
150160
} else {
@@ -165,8 +175,12 @@ extension SyntaxProtocol {
165175
public var introducedNames: [LookupName] {
166176
[]
167177
}
168-
169-
public func lookup(for name: String?, at syntax: SyntaxProtocol, with configDict: LookupConfigDictionary) -> [LookupResult] {
178+
179+
public func lookup(
180+
for name: String?,
181+
at syntax: SyntaxProtocol,
182+
with configDict: LookupConfigDictionary
183+
) -> [LookupResult] {
170184
if body.position <= syntax.position && body.endPosition >= syntax.position {
171185
lookupInParent(for: name, at: self, with: configDict)
172186
} else {

Sources/SwiftLexicalLookup/ScopeSyntax.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ extension SyntaxProtocol {
6666
/// Returns `LookupResult` of all names introduced in this scope that `name`
6767
/// refers to and is accessible at given syntax node then passes lookup to the parent.
6868
/// If `name` set to `nil`, returns all available names at the given node.
69-
public func lookup(for name: String?, at syntax: SyntaxProtocol, with configDict: LookupConfigDictionary) -> [LookupResult] {
69+
public func lookup(
70+
for name: String?,
71+
at syntax: SyntaxProtocol,
72+
with configDict: LookupConfigDictionary
73+
) -> [LookupResult] {
7074
defaultLookupImplementation(for: name, at: syntax, with: configDict)
7175
}
7276

@@ -90,7 +94,7 @@ extension SyntaxProtocol {
9094
return [.fromScope(self, withNames: filteredNames)] + lookupInParent(for: name, at: syntax, with: configDict)
9195
}
9296
}
93-
97+
9498
/// Looks up in parent scope.
9599
func lookupInParent(
96100
for name: String?,

Tests/SwiftLexicalLookupTest/Assertions.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ func assertLexicalNameLookup(
172172
scope.syntaxNodeType == expectedType,
173173
"For marker \(marker), scope result type of \(scope.syntaxNodeType) doesn't match expected \(expectedType)"
174174
)
175-
case (.fromFileScope(_, withNames: _, nameIntroductionStrategy: let nameIntroductionStrategy), .fromFileScope(expectedNames: _, nameIntroductionStrategy: let expectedNameIntroductionStrategy)):
175+
case (
176+
.fromFileScope(_, withNames: _, nameIntroductionStrategy: let nameIntroductionStrategy),
177+
.fromFileScope(expectedNames: _, nameIntroductionStrategy: let expectedNameIntroductionStrategy)
178+
):
176179
XCTAssert(
177180
nameIntroductionStrategy == expectedNameIntroductionStrategy,
178181
"For marker \(marker), actual file scope name introduction strategy \(nameIntroductionStrategy) doesn't match expected \(expectedNameIntroductionStrategy)"

Tests/SwiftLexicalLookupTest/NameLookupTests.swift

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ final class testNameLookup: XCTestCase {
134134
)
135135
)
136136
}
137-
137+
138138
func testClosureCaptureLookup() {
139139
assertLexicalNameLookup(
140140
source: """
@@ -156,7 +156,7 @@ final class testNameLookup: XCTestCase {
156156
expectedResultTypes: .all(
157157
ClosureCaptureSyntax.self,
158158
except: [
159-
"1️⃣": IdentifierPatternSyntax.self,
159+
"1️⃣": IdentifierPatternSyntax.self
160160
]
161161
)
162162
)
@@ -353,7 +353,7 @@ final class testNameLookup: XCTestCase {
353353
)
354354
)
355355
}
356-
356+
357357
func testIfCaseLookup() {
358358
assertLexicalNameLookup(
359359
source: """
@@ -402,25 +402,27 @@ final class testNameLookup: XCTestCase {
402402
"7️⃣": [
403403
.fromScope(CodeBlockSyntax.self, expectedNames: ["4️⃣", "5️⃣"]),
404404
.fromScope(MemberBlockSyntax.self, expectedNames: ["1️⃣", "2️⃣", "3️⃣"]),
405-
.fromFileScope(expectedNames: ["🔟"], nameIntroductionStrategy: .memberBlockUpToLastDecl)
405+
.fromFileScope(expectedNames: ["🔟"], nameIntroductionStrategy: .memberBlockUpToLastDecl),
406406
],
407407
"0️⃣": [
408408
.fromScope(CodeBlockSyntax.self, expectedNames: ["8️⃣", "9️⃣"]),
409409
.fromScope(IfExprSyntax.self, expectedNames: ["6️⃣"]),
410410
.fromScope(CodeBlockSyntax.self, expectedNames: ["4️⃣", "5️⃣"]),
411411
.fromScope(MemberBlockSyntax.self, expectedNames: ["1️⃣", "2️⃣", "3️⃣"]),
412-
.fromFileScope(expectedNames: ["🔟"], nameIntroductionStrategy: .memberBlockUpToLastDecl)
412+
.fromFileScope(expectedNames: ["🔟"], nameIntroductionStrategy: .memberBlockUpToLastDecl),
413413
],
414414
],
415415
expectedResultTypes: .all(
416416
IdentifierPatternSyntax.self,
417-
except: ["3️⃣": FunctionDeclSyntax.self,
418-
"🔟": ClassDeclSyntax.self]
417+
except: [
418+
"3️⃣": FunctionDeclSyntax.self,
419+
"🔟": ClassDeclSyntax.self,
420+
]
419421
),
420422
useNilAsTheParameter: true
421423
)
422424
}
423-
425+
424426
func testGuardLookup() {
425427
assertLexicalNameLookup(
426428
source: """
@@ -431,7 +433,7 @@ final class testNameLookup: XCTestCase {
431433
print(4️⃣a, 5️⃣b)
432434
return
433435
}
434-
436+
435437
print(6️⃣a, 7️⃣b)
436438
}
437439
""",
@@ -446,7 +448,7 @@ final class testNameLookup: XCTestCase {
446448
)
447449
)
448450
}
449-
451+
450452
func testGuardLookupInConditions() {
451453
assertLexicalNameLookup(
452454
source: """
@@ -465,24 +467,24 @@ final class testNameLookup: XCTestCase {
465467
)
466468
)
467469
}
468-
470+
469471
func testSimpleFileScope() {
470472
assertLexicalNameLookup(
471473
source: """
472474
1️⃣class a {}
473-
475+
474476
2️⃣class b {
475477
let x = 3️⃣a + 4️⃣b + 5️⃣c + 6️⃣d
476478
}
477479
478480
let 8️⃣a = 0
479-
481+
480482
7️⃣class c {}
481-
483+
482484
if a == 0 {}
483-
485+
484486
9️⃣class d {}
485-
487+
486488
let x = 0️⃣d
487489
""",
488490
references: [
@@ -495,24 +497,24 @@ final class testNameLookup: XCTestCase {
495497
expectedResultTypes: .all(ClassDeclSyntax.self, except: ["8️⃣": IdentifierPatternSyntax.self])
496498
)
497499
}
498-
500+
499501
func testFileScopeAsMember() {
500502
assertLexicalNameLookup(
501503
source: """
502504
1️⃣class a {}
503-
505+
504506
2️⃣class b {
505507
let x = 3️⃣a + 4️⃣b + 5️⃣c + 6️⃣d
506508
}
507509
508510
let 8️⃣a = 0
509-
511+
510512
7️⃣class c {}
511-
513+
512514
if a == 0 {}
513-
515+
514516
9️⃣class d {}
515-
517+
516518
let x = 0️⃣d
517519
""",
518520
references: [
@@ -526,24 +528,24 @@ final class testNameLookup: XCTestCase {
526528
config: [FileScopeNameIntroductionStrategy.memberBlock]
527529
)
528530
}
529-
531+
530532
func testFileScopeAsCodeBlock() {
531533
assertLexicalNameLookup(
532534
source: """
533535
1️⃣class a {}
534-
536+
535537
2️⃣class b {
536538
let x = 3️⃣a + 4️⃣b + 5️⃣c + 6️⃣d
537539
}
538540
539541
let 8️⃣a = 0
540-
542+
541543
7️⃣class c {}
542-
544+
543545
if a == 0 {}
544-
546+
545547
9️⃣class d {}
546-
548+
547549
let x = 0️⃣d
548550
""",
549551
references: [

0 commit comments

Comments
 (0)