Skip to content

Commit 7fdb145

Browse files
committed
Introduce LookupConfigDictionary for retrieving lookup configurations.
1 parent 1cb4a9f commit 7fdb145

File tree

5 files changed

+61
-20
lines changed

5 files changed

+61
-20
lines changed

Sources/SwiftLexicalLookup/LookupConfig.swift renamed to Sources/SwiftLexicalLookup/Configurations/LookupConfig.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ import Foundation
1414

1515
/// Used to customize lookup behavior.
1616
@_spi(Experimental) public protocol LookupConfig {}
17+
18+
extension LookupConfig {
19+
var identifier: ObjectIdentifier {
20+
ObjectIdentifier(Self.self)
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
/// Stores and provides easy access for lookup configuration.
16+
@_spi(Experimental) public struct LookupConfigDictionary {
17+
private var dictionary: [ObjectIdentifier: LookupConfig]
18+
19+
/// Creates a new lookup configuration dictionary
20+
/// from a given array of configurations.
21+
init(from configArray: [LookupConfig]) {
22+
dictionary = [:]
23+
24+
for config in configArray {
25+
dictionary[config.identifier] = config
26+
}
27+
}
28+
29+
subscript<T: LookupConfig>(key: T.Type) -> T? {
30+
get {
31+
return dictionary[ObjectIdentifier(key)] as? T
32+
}
33+
set {
34+
dictionary[ObjectIdentifier(key)] = newValue
35+
}
36+
}
37+
}

Sources/SwiftLexicalLookup/ScopeImplementations.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ extension SyntaxProtocol {
5858
}
5959
}
6060

61-
public func lookup(for name: String?, at syntax: SyntaxProtocol, with config: [LookupConfig]) -> [LookupResult] {
62-
let nameIntroductionStrategy = config.first {
63-
$0 is FileScopeNameIntroductionStrategy
64-
} as? FileScopeNameIntroductionStrategy ?? .memberBlockUpToLastDecl
61+
public func lookup(for name: String?, at syntax: SyntaxProtocol, with configDict: LookupConfigDictionary) -> [LookupResult] {
62+
let nameIntroductionStrategy = configDict[FileScopeNameIntroductionStrategy.self] ?? .memberBlockUpToLastDecl
6563

6664
let names = introducedNames(using: nameIntroductionStrategy)
6765
.filter { introducedName in
@@ -146,11 +144,11 @@ extension SyntaxProtocol {
146144
}
147145
}
148146

149-
public func lookup(for name: String?, at syntax: SyntaxProtocol, with config: [LookupConfig]) -> [LookupResult] {
147+
public func lookup(for name: String?, at syntax: SyntaxProtocol, with configDict: LookupConfigDictionary) -> [LookupResult] {
150148
if let elseBody, elseBody.position <= syntax.position, elseBody.endPosition >= syntax.position {
151-
lookupInParent(for: name, at: syntax, with: config)
149+
lookupInParent(for: name, at: syntax, with: configDict)
152150
} else {
153-
defaultLookupImplementation(for: name, at: syntax, with: config)
151+
defaultLookupImplementation(for: name, at: syntax, with: configDict)
154152
}
155153
}
156154
}
@@ -168,11 +166,11 @@ extension SyntaxProtocol {
168166
[]
169167
}
170168

171-
public func lookup(for name: String?, at syntax: SyntaxProtocol, with config: [LookupConfig]) -> [LookupResult] {
169+
public func lookup(for name: String?, at syntax: SyntaxProtocol, with configDict: LookupConfigDictionary) -> [LookupResult] {
172170
if body.position <= syntax.position && body.endPosition >= syntax.position {
173-
lookupInParent(for: name, at: self, with: config)
171+
lookupInParent(for: name, at: self, with: configDict)
174172
} else {
175-
defaultLookupImplementation(for: name, at: syntax, with: config)
173+
defaultLookupImplementation(for: name, at: syntax, with: configDict)
176174
}
177175
}
178176
}

Sources/SwiftLexicalLookup/ScopeSyntax.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ extension SyntaxProtocol {
4343
/// declaration, followed by the first function name, and then the second function name,
4444
/// in this exact order. The constant declaration within the function body is omitted
4545
/// due to the ordering rules that prioritize visibility within the function body.
46-
@_spi(Experimental) public func lookup(for name: String?, with config: [LookupConfig] = []) -> [LookupResult] {
47-
scope?.lookup(for: name, at: self, with: config) ?? []
46+
@_spi(Experimental) public func lookup(for name: String?, with configArr: [LookupConfig] = []) -> [LookupResult] {
47+
scope?.lookup(for: name, at: self, with: LookupConfigDictionary(from: configArr)) ?? []
4848
}
4949
}
5050

@@ -55,7 +55,7 @@ extension SyntaxProtocol {
5555
var introducedNames: [LookupName] { get }
5656
/// Finds all declarations `name` refers to. `at` specifies the node lookup was triggered with.
5757
/// If `name` set to `nil`, returns all available names at the given node.
58-
func lookup(for name: String?, at syntax: SyntaxProtocol, with config: [LookupConfig]) -> [LookupResult]
58+
func lookup(for name: String?, at syntax: SyntaxProtocol, with configDict: LookupConfigDictionary) -> [LookupResult]
5959
}
6060

6161
@_spi(Experimental) extension ScopeSyntax {
@@ -66,8 +66,8 @@ 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 config: [LookupConfig]) -> [LookupResult] {
70-
defaultLookupImplementation(for: name, at: syntax, with: config)
69+
public func lookup(for name: String?, at syntax: SyntaxProtocol, with configDict: LookupConfigDictionary) -> [LookupResult] {
70+
defaultLookupImplementation(for: name, at: syntax, with: configDict)
7171
}
7272

7373
/// Returns `LookupResult` of all names introduced in this scope that `name`
@@ -76,7 +76,7 @@ extension SyntaxProtocol {
7676
func defaultLookupImplementation(
7777
for name: String?,
7878
at syntax: SyntaxProtocol,
79-
with config: [LookupConfig]
79+
with configDict: LookupConfigDictionary
8080
) -> [LookupResult] {
8181
let filteredNames =
8282
introducedNames
@@ -85,18 +85,18 @@ extension SyntaxProtocol {
8585
}
8686

8787
if filteredNames.isEmpty {
88-
return lookupInParent(for: name, at: syntax, with: config)
88+
return lookupInParent(for: name, at: syntax, with: configDict)
8989
} else {
90-
return [.fromScope(self, withNames: filteredNames)] + lookupInParent(for: name, at: syntax, with: config)
90+
return [.fromScope(self, withNames: filteredNames)] + lookupInParent(for: name, at: syntax, with: configDict)
9191
}
9292
}
9393

9494
/// Looks up in parent scope.
9595
func lookupInParent(
9696
for name: String?,
9797
at syntax: SyntaxProtocol,
98-
with config: [LookupConfig]
98+
with configDict: LookupConfigDictionary
9999
) -> [LookupResult] {
100-
parentScope?.lookup(for: name, at: syntax, with: config) ?? []
100+
parentScope?.lookup(for: name, at: syntax, with: configDict) ?? []
101101
}
102102
}

0 commit comments

Comments
 (0)