Skip to content

Commit a2c6fe8

Browse files
committed
Add suggested changes.
1 parent fcaecc7 commit a2c6fe8

File tree

9 files changed

+85
-112
lines changed

9 files changed

+85
-112
lines changed

Sources/SwiftLexicalLookup/IntroducingToSequentialParentScopeSyntax.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ protocol IntroducingToSequentialParentScopeSyntax: ScopeSyntax {
1919
/// Returns results matching lookup that should be
2020
/// interleaved with results of the sequential parent.
2121
func lookupFromSequentialParent(
22-
for identifier: Identifier?,
23-
at origin: AbsolutePosition,
22+
_ identifier: Identifier?,
23+
at lookUpPosition: AbsolutePosition,
2424
with config: LookupConfig
2525
) -> [LookupResult]
2626
}

Sources/SwiftLexicalLookup/LookupName.swift

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ import SwiftSyntax
110110
case declaration(NamedDeclSyntax)
111111
/// Name introduced implicitly by certain syntax nodes.
112112
case implicit(ImplicitDecl)
113-
/// Explicit `self` keyword.
114-
case `self`(IdentifiableSyntax, accessibleAfter: AbsolutePosition?)
115-
/// Explicit `Self` keyword.
116-
case `Self`(IdentifiableSyntax, accessibleAfter: AbsolutePosition?)
117113

118114
/// Syntax associated with this name.
119115
@_spi(Experimental) public var syntax: SyntaxProtocol {
@@ -124,8 +120,6 @@ import SwiftSyntax
124120
return syntax
125121
case .implicit(let implicitName):
126122
return implicitName.syntax
127-
case .self(let syntax, _), .Self(let syntax, _):
128-
return syntax
129123
}
130124
}
131125

@@ -138,35 +132,28 @@ import SwiftSyntax
138132
return Identifier(syntax.name)
139133
case .implicit(let kind):
140134
return kind.identifier
141-
case .self:
142-
return Identifier("self")
143-
case .Self:
144-
return Identifier("Self")
145135
}
146136
}
147137

148138
/// Point, after which the name is available in scope.
149139
/// If set to `nil`, the name is available at any point in scope.
150140
var accessibleAfter: AbsolutePosition? {
151141
switch self {
152-
case .identifier(_, let absolutePosition),
153-
.self(_, let absolutePosition),
154-
.Self(_, let absolutePosition):
142+
case .identifier(_, let absolutePosition):
155143
return absolutePosition
156144
default:
157145
return nil
158146
}
159147
}
160148

161149
/// Checks if this name was introduced before the syntax used for lookup.
162-
func isAccessible(at origin: AbsolutePosition) -> Bool {
150+
func isAccessible(at lookUpPosition: AbsolutePosition) -> Bool {
163151
guard let accessibleAfter else { return true }
164-
return accessibleAfter <= origin
152+
return accessibleAfter <= lookUpPosition
165153
}
166154

167155
/// Checks if this name refers to the looked up phrase.
168156
func refersTo(_ lookedUpIdentifier: Identifier) -> Bool {
169-
guard let identifier else { return false }
170157
return identifier == lookedUpIdentifier
171158
}
172159

@@ -229,16 +216,10 @@ import SwiftSyntax
229216
accessibleAfter: AbsolutePosition? = nil
230217
) -> [LookupName] {
231218
switch identifiable.identifier.tokenKind {
232-
case .keyword(.self):
233-
return [.self(identifiable, accessibleAfter: accessibleAfter)]
234-
case .keyword(.Self):
235-
return [.Self(identifiable, accessibleAfter: accessibleAfter)]
219+
case .wildcard:
220+
return []
236221
default:
237-
if identifiable.identifier.tokenKind != .wildcard {
238-
return [.identifier(identifiable, accessibleAfter: accessibleAfter)]
239-
} else {
240-
return []
241-
}
222+
return [.identifier(identifiable, accessibleAfter: accessibleAfter)]
242223
}
243224
}
244225

Sources/SwiftLexicalLookup/ScopeImplementations.swift

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ import SwiftSyntax
9797
/// - for `memberBlock` - a, b, c, d, e, f
9898
/// - for `codeBlock` - a
9999
@_spi(Experimental) public func lookup(
100-
for identifier: Identifier?,
101-
at origin: AbsolutePosition,
100+
_ identifier: Identifier?,
101+
at lookUpPosition: AbsolutePosition,
102102
with config: LookupConfig
103103
) -> [LookupResult] {
104104
switch config.fileScopeHandling {
105105
case .memberBlock:
106106
let names = introducedNames(using: .memberBlock)
107107
.filter { lookupName in
108-
checkName(identifier, refersTo: lookupName, at: origin)
108+
checkName(identifier, refersTo: lookupName, at: lookUpPosition)
109109
}
110110

111111
return names.isEmpty ? [] : [.fromFileScope(self, withNames: names)]
@@ -119,22 +119,19 @@ import SwiftSyntax
119119

120120
if encounteredNonDeclaration {
121121
sequentialItems.append(codeBlockItem)
122+
} else if item.is(DeclSyntax.self) {
123+
let foundNames = LookupName.getNames(from: item)
124+
members.append(contentsOf: foundNames.filter { checkName(identifier, refersTo: $0, at: lookUpPosition) })
122125
} else {
123-
if item.is(DeclSyntax.self) {
124-
let foundNames = LookupName.getNames(from: item)
125-
126-
members.append(contentsOf: foundNames.filter { checkName(identifier, refersTo: $0, at: origin) })
127-
} else {
128-
encounteredNonDeclaration = true
129-
sequentialItems.append(codeBlockItem)
130-
}
126+
encounteredNonDeclaration = true
127+
sequentialItems.append(codeBlockItem)
131128
}
132129
}
133130

134131
let sequentialNames = sequentialLookup(
135132
in: sequentialItems,
136-
for: identifier,
137-
at: origin,
133+
identifier,
134+
at: lookUpPosition,
138135
with: config,
139136
createResultsForThisScopeWith: { .fromFileScope(self, withNames: $0) }
140137
)
@@ -154,14 +151,14 @@ import SwiftSyntax
154151
}
155152

156153
@_spi(Experimental) public func lookup(
157-
for identifier: Identifier?,
158-
at origin: AbsolutePosition,
154+
_ identifier: Identifier?,
155+
at lookUpPosition: AbsolutePosition,
159156
with config: LookupConfig
160157
) -> [LookupResult] {
161158
sequentialLookup(
162159
in: statements,
163-
for: identifier,
164-
at: origin,
160+
identifier,
161+
at: lookUpPosition,
165162
with: config,
166163
createResultsForThisScopeWith: { .fromScope(self, withNames: $0) }
167164
)
@@ -274,14 +271,14 @@ import SwiftSyntax
274271
/// }
275272
/// ```
276273
@_spi(Experimental) public func lookup(
277-
for identifier: Identifier?,
278-
at origin: AbsolutePosition,
274+
_ identifier: Identifier?,
275+
at lookUpPosition: AbsolutePosition,
279276
with config: LookupConfig
280277
) -> [LookupResult] {
281-
if let elseBody, elseBody.position <= origin, elseBody.endPosition >= origin {
282-
return lookupInParent(for: identifier, at: origin, with: config)
278+
if let elseBody, elseBody.range.contains(lookUpPosition) {
279+
return lookupInParent(identifier, at: lookUpPosition, with: config)
283280
} else {
284-
return defaultLookupImplementation(for: identifier, at: origin, with: config)
281+
return defaultLookupImplementation(identifier, at: lookUpPosition, with: config)
285282
}
286283
}
287284
}
@@ -319,15 +316,14 @@ import SwiftSyntax
319316
/// // a is visible here
320317
/// ```
321318
func lookupFromSequentialParent(
322-
for identifier: Identifier?,
323-
at origin: AbsolutePosition,
319+
_ identifier: Identifier?,
320+
at lookUpPosition: AbsolutePosition,
324321
with config: LookupConfig
325322
) -> [LookupResult] {
326-
guard body.position > origin || body.endPosition < origin
327-
else { return [] }
323+
guard !body.range.contains(lookUpPosition) else { return [] }
328324

329325
let names = namesIntroducedToSequentialParent.filter { introducedName in
330-
checkName(identifier, refersTo: introducedName, at: origin)
326+
checkName(identifier, refersTo: introducedName, at: lookUpPosition)
331327
}
332328

333329
return names.isEmpty ? [] : [.fromScope(self, withNames: names)]

Sources/SwiftLexicalLookup/ScopeSyntax.swift

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ extension SyntaxProtocol {
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.
4646
@_spi(Experimental) public func lookup(
47-
for identifier: Identifier?,
47+
_ identifier: Identifier?,
4848
with config: LookupConfig = LookupConfig()
4949
) -> [LookupResult] {
50-
scope?.lookup(for: identifier, at: self.position, with: config) ?? []
50+
scope?.lookup(identifier, at: self.position, with: config) ?? []
5151
}
5252
}
5353

@@ -58,10 +58,9 @@ extension SyntaxProtocol {
5858
var introducedNames: [LookupName] { get }
5959
/// Finds all declarations `identifier` refers to. `syntax` specifies the node lookup was triggered with.
6060
/// If `identifier` set to `nil`, returns all available names at the given node.
61-
/// `state` represents lookup state passed between lookup methods.
6261
func lookup(
63-
for identifier: Identifier?,
64-
at origin: AbsolutePosition,
62+
_ identifier: Identifier?,
63+
at lookUpPosition: AbsolutePosition,
6564
with config: LookupConfig
6665
) -> [LookupResult]
6766
}
@@ -74,47 +73,47 @@ extension SyntaxProtocol {
7473
/// Returns `LookupResult` of all names introduced in this scope that `identifier`
7574
/// refers to and is accessible at given syntax node then passes lookup to the parent.
7675
/// If `identifier` set to `nil`, returns all available names at the given node.
77-
/// `state` represents lookup state passed between lookup methods.
7876
@_spi(Experimental) public func lookup(
79-
for identifier: Identifier?,
80-
at origin: AbsolutePosition,
77+
_ identifier: Identifier?,
78+
at lookUpPosition: AbsolutePosition,
8179
with config: LookupConfig
8280
) -> [LookupResult] {
83-
defaultLookupImplementation(for: identifier, at: origin, with: config)
81+
defaultLookupImplementation(identifier, at: lookUpPosition, with: config)
8482
}
8583

8684
/// Returns `LookupResult` of all names introduced in this scope that `identifier`
8785
/// refers to and is accessible at given syntax node then passes lookup to the parent.
8886
/// If `identifier` set to `nil`, returns all available names at the given node.
8987
func defaultLookupImplementation(
90-
for identifier: Identifier?,
91-
at origin: AbsolutePosition,
88+
_ identifier: Identifier?,
89+
at lookUpPosition: AbsolutePosition,
9290
with config: LookupConfig
9391
) -> [LookupResult] {
9492
let filteredNames =
9593
introducedNames
9694
.filter { introducedName in
97-
checkName(identifier, refersTo: introducedName, at: origin)
95+
checkName(identifier, refersTo: introducedName, at: lookUpPosition)
9896
}
9997

10098
if filteredNames.isEmpty {
101-
return lookupInParent(for: identifier, at: origin, with: config)
99+
return lookupInParent(identifier, at: lookUpPosition, with: config)
102100
} else {
103101
return [.fromScope(self, withNames: filteredNames)]
104-
+ lookupInParent(for: identifier, at: origin, with: config)
102+
+ lookupInParent(identifier, at: lookUpPosition, with: config)
105103
}
106104
}
107105

108106
/// Looks up in parent scope.
109107
func lookupInParent(
110-
for identifier: Identifier?,
111-
at origin: AbsolutePosition,
108+
_ identifier: Identifier?,
109+
at lookUpPosition: AbsolutePosition,
112110
with config: LookupConfig
113111
) -> [LookupResult] {
114-
parentScope?.lookup(for: identifier, at: origin, with: config) ?? []
112+
parentScope?.lookup(identifier, at: lookUpPosition, with: config) ?? []
115113
}
116114

117-
func checkName(_ name: Identifier?, refersTo introducedName: LookupName, at origin: AbsolutePosition) -> Bool {
118-
introducedName.isAccessible(at: origin) && (name == nil || introducedName.refersTo(name!))
115+
func checkName(_ name: Identifier?, refersTo introducedName: LookupName, at lookUpPosition: AbsolutePosition) -> Bool
116+
{
117+
introducedName.isAccessible(at: lookUpPosition) && (name == nil || introducedName.refersTo(name!))
119118
}
120119
}

Sources/SwiftLexicalLookup/SequentialScopeSyntax.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@ extension SequentialScopeSyntax {
4040
/// code block scope in this exact order.
4141
func sequentialLookup(
4242
in codeBlockItems: some Collection<CodeBlockItemSyntax>,
43-
for identifier: Identifier?,
44-
at origin: AbsolutePosition,
43+
_ identifier: Identifier?,
44+
at lookUpPosition: AbsolutePosition,
4545
with config: LookupConfig,
4646
createResultsForThisScopeWith getResults: ([LookupName]) -> (LookupResult)
4747
) -> [LookupResult] {
48+
// Sequential scope needs to ensure all type declarations are
49+
// available in the whole scope (first loop) and
50+
// then that results from IntroducingToSequentialParentScopeSyntax
51+
// are properly interleaved with the results produced by this scope.
4852
var results: [LookupResult] = []
53+
// We need to use currentChunk because we
54+
// can't add the names directly to results
55+
// as we need to partition them based on results
56+
// obtained from IntroducingToSequentialParentScopeSyntax
4957
var currentChunk: [LookupName] = []
5058
var itemsWithoutNamedDecl: [CodeBlockItemSyntax] = []
5159

@@ -55,23 +63,23 @@ extension SequentialScopeSyntax {
5563
from: codeBlockItem.item,
5664
accessibleAfter: codeBlockItem.endPosition
5765
).filter { introducedName in
58-
checkName(identifier, refersTo: introducedName, at: origin)
66+
checkName(identifier, refersTo: introducedName, at: lookUpPosition)
5967
}
6068
} else {
6169
itemsWithoutNamedDecl.append(codeBlockItem)
6270
}
6371
}
6472

6573
for codeBlockItem in itemsWithoutNamedDecl {
66-
guard codeBlockItem.position < origin else { break }
74+
guard codeBlockItem.position <= lookUpPosition else { break }
6775

6876
if let introducingToParentScope = Syntax(codeBlockItem.item).asProtocol(SyntaxProtocol.self)
6977
as? IntroducingToSequentialParentScopeSyntax
7078
{
7179
// Get results from encountered scope.
7280
let introducedResults = introducingToParentScope.lookupFromSequentialParent(
73-
for: identifier,
74-
at: origin,
81+
identifier,
82+
at: lookUpPosition,
7583
with: config
7684
)
7785

@@ -91,7 +99,7 @@ extension SequentialScopeSyntax {
9199
from: codeBlockItem.item,
92100
accessibleAfter: codeBlockItem.endPosition
93101
).filter { introducedName in
94-
checkName(identifier, refersTo: introducedName, at: origin)
102+
checkName(identifier, refersTo: introducedName, at: lookUpPosition)
95103
}
96104
}
97105
}
@@ -101,6 +109,6 @@ extension SequentialScopeSyntax {
101109
results.append(getResults(currentChunk))
102110
}
103111

104-
return results.reversed() + lookupInParent(for: identifier, at: origin, with: config)
112+
return results.reversed() + lookupInParent(identifier, at: lookUpPosition, with: config)
105113
}
106114
}

0 commit comments

Comments
 (0)