@@ -15,9 +15,10 @@ import SwiftSyntax
1515extension SyntaxProtocol {
1616 /// Returns all names that `for` refers to at this syntax node.
1717 ///
18- /// - Returns: An array of names referred to by `for` at this syntax node,
19- /// ordered by visibility. The order is from the innermost to the outermost
20- /// scope, and within each scope, names are ordered by their introduction
18+ /// - Returns: An array of `LookupResult` for name `for` at this syntax node,
19+ /// ordered by visibility. If set to `nil`, returns all available names ordered by visibility.
20+ /// The order is from the innermost to the outermost scope,
21+ /// and within each result, names are ordered by their introduction
2122 /// in the source code.
2223 ///
2324 /// Example usage:
@@ -41,40 +42,50 @@ extension SyntaxProtocol {
4142 /// declaration, followed by the first function name, and then the second function name,
4243 /// in this exact order. The constant declaration within the function body is omitted
4344 /// due to the ordering rules that prioritize visibility within the function body.
44- @_spi ( Experimental) public func lookup( for name: String ) -> [ LookupName ] {
45+ @_spi ( Experimental) public func lookup( for name: String ? ) -> [ LookupResult ] {
4546 scope? . lookup ( for: name, at: self ) ?? [ ]
4647 }
4748}
4849
49- protocol ScopeSyntax : SyntaxProtocol {
50+ @ _spi ( Experimental ) public protocol ScopeSyntax : SyntaxProtocol {
5051 /// Parent of this scope, or `nil` if it is the root.
5152 var parentScope : ScopeSyntax ? { get }
5253 /// Names found in this scope. Ordered from first to last introduced.
5354 var introducedNames : [ LookupName ] { get }
5455 /// Finds all declarations `name` refers to. `at` specifies the node lookup was triggered with.
55- func lookup( for name: String , at syntax: SyntaxProtocol ) -> [ LookupName ]
56+ /// If `name` set to `nil`, returns all available names at the given node.
57+ func lookup( for name: String ? , at syntax: SyntaxProtocol ) -> [ LookupResult ]
5658}
5759
58- extension ScopeSyntax {
59- var parentScope : ScopeSyntax ? {
60+ @ _spi ( Experimental ) extension ScopeSyntax {
61+ public var parentScope : ScopeSyntax ? {
6062 self . parent? . scope
6163 }
62-
63- /// Returns all names introduced in this scope that `name` refers to and
64- /// is accessible at given syntax node then passes lookup to the parent.
65- func lookup( for name: String , at syntax: SyntaxProtocol ) -> [ LookupName ] {
64+
65+ /// Returns `LookupResult` of all names introduced in this scope that `name`
66+ /// refers to and is accessible at given syntax node then passes lookup to the parent.
67+ /// If `name` set to `nil`, returns all available names at the given node.
68+ public func lookup( for name: String ? , at syntax: SyntaxProtocol ) -> [ LookupResult ] {
6669 defaultLookupImplementation ( for: name, at: syntax)
6770 }
6871
69- /// Returns all names introduced in this scope that `name` refers to and
70- /// is accessible at given syntax node then passes lookup to the parent.
71- func defaultLookupImplementation(
72- for name: String ,
72+ /// Returns `LookupResult` of all names introduced in this scope that `name`
73+ /// refers to and is accessible at given syntax node then passes lookup to the parent.
74+ /// If `name` set to `nil`, returns all available names at the given node.
75+ public func defaultLookupImplementation(
76+ for name: String ? ,
7377 at syntax: SyntaxProtocol
74- ) -> [ LookupName ] {
75- introducedNames
78+ ) -> [ LookupResult ] {
79+ let filteredNames =
80+ introducedNames
7681 . filter { introducedName in
77- introducedName. isAccessible ( at: syntax) && introducedName. refersTo ( name)
78- } + ( parentScope? . lookup ( for: name, at: syntax) ?? [ ] )
82+ introducedName. isAccessible ( at: syntax) && ( name == nil || introducedName. refersTo ( name!) )
83+ }
84+
85+ if filteredNames. isEmpty {
86+ return parentScope? . lookup ( for: name, at: syntax) ?? [ ]
87+ } else {
88+ return [ . fromScope( self , withNames: filteredNames) ] + ( parentScope? . lookup ( for: name, at: syntax) ?? [ ] )
89+ }
7990 }
8091}
0 commit comments