@@ -191,6 +191,21 @@ import SwiftSyntax
191191 @_spi ( Experimental) public var scopeDebugName : String {
192192 " ForStmtScope "
193193 }
194+
195+ /// Returns results with names matching lookup.
196+ /// Doesn't include names introduced at this scope
197+ /// if lookup started inside it's `pattern` or `sequence`.
198+ @_spi ( Experimental) public func lookup(
199+ _ identifier: Identifier ? ,
200+ at lookUpPosition: AbsolutePosition ,
201+ with config: LookupConfig
202+ ) -> [ LookupResult ] {
203+ if pattern. range. contains ( lookUpPosition) || sequence. range. contains ( lookUpPosition) {
204+ return lookupInParent ( identifier, at: lookUpPosition, with: config)
205+ } else {
206+ return defaultLookupImplementation ( identifier, at: lookUpPosition, with: config)
207+ }
208+ }
194209}
195210
196211@_spi ( Experimental) extension ClosureExprSyntax : SequentialScopeSyntax {
@@ -249,8 +264,14 @@ import SwiftSyntax
249264 at lookUpPosition: AbsolutePosition ,
250265 with config: LookupConfig
251266 ) -> [ LookupResult ] {
252- let filteredSignatureNames = introducedNamesInSignature. filter { name in
253- checkIdentifier ( identifier, refersTo: name, at: lookUpPosition)
267+ let filteredSignatureNames : [ LookupName ]
268+
269+ if let signature, signature. range. contains ( lookUpPosition) {
270+ filteredSignatureNames = [ ]
271+ } else {
272+ filteredSignatureNames = introducedNamesInSignature. filter { name in
273+ checkIdentifier ( identifier, refersTo: name, at: lookUpPosition)
274+ }
254275 }
255276
256277 return sequentialLookup (
@@ -267,8 +288,8 @@ import SwiftSyntax
267288@_spi ( Experimental) extension WhileStmtSyntax : ScopeSyntax {
268289 /// Names introduced by the `while` loop by its conditions.
269290 @_spi ( Experimental) public var introducedNames : [ LookupName ] {
270- conditions. flatMap { element in
271- LookupName . getNames ( from: element. condition)
291+ conditions. reversed ( ) . flatMap { element in
292+ LookupName . getNames ( from: element. condition, accessibleAfter : element . endPositionBeforeTrailingTrivia )
272293 }
273294 }
274295
@@ -486,18 +507,26 @@ import SwiftSyntax
486507@_spi ( Experimental) extension AccessorDeclSyntax : ScopeSyntax {
487508 /// Implicit and/or explicit names introduced within the accessor.
488509 @_spi ( Experimental) public var introducedNames : [ LookupName ] {
510+ let names : [ LookupName ]
511+
489512 if let parameters {
490- return LookupName . getNames ( from: parameters)
513+ names = LookupName . getNames ( from: parameters)
491514 } else {
492515 switch accessorSpecifier. tokenKind {
493516 case . keyword( . set) , . keyword( . willSet) :
494- return [ . implicit( . newValue( self ) ) ]
517+ names = [ . implicit( . newValue( self ) ) ]
495518 case . keyword( . didSet) :
496- return [ . implicit( . oldValue( self ) ) ]
519+ names = [ . implicit( . oldValue( self ) ) ]
497520 default :
498- return [ ]
521+ names = [ ]
499522 }
500523 }
524+
525+ if let parentScope, parentScope. is ( AccessorBlockSyntax . self) {
526+ return names + [ . implicit( . self ( self ) ) ]
527+ } else {
528+ return names
529+ }
501530 }
502531
503532 @_spi ( Experimental) public var scopeDebugName : String {
@@ -516,17 +545,47 @@ import SwiftSyntax
516545 }
517546}
518547
519- @_spi ( Experimental) extension SwitchCaseSyntax : ScopeSyntax {
548+ @_spi ( Experimental) extension SwitchCaseSyntax : SequentialScopeSyntax {
520549 /// Names introduced within `case` items.
521- @ _spi ( Experimental ) public var introducedNames : [ LookupName ] {
550+ var namesFromLabel : [ LookupName ] {
522551 label. as ( SwitchCaseLabelSyntax . self) ? . caseItems. flatMap { child in
523552 LookupName . getNames ( from: child. pattern)
524553 } ?? [ ]
525554 }
526555
556+ /// Names introduced within `case` items
557+ /// as well as sequential names from inside this case.
558+ @_spi ( Experimental) public var introducedNames : [ LookupName ] {
559+ statements. flatMap { codeBlockItem in
560+ LookupName . getNames ( from: codeBlockItem. item, accessibleAfter: codeBlockItem. endPosition)
561+ } + namesFromLabel
562+ }
563+
527564 @_spi ( Experimental) public var scopeDebugName : String {
528565 " SwitchCaseScope "
529566 }
567+
568+ /// Returns results with names matching lookup.
569+ /// Includes names introduced in it's label and sequentially
570+ /// introduced names from inside this case.
571+ @_spi ( Experimental) public func lookup(
572+ _ identifier: Identifier ? ,
573+ at lookUpPosition: AbsolutePosition ,
574+ with config: LookupConfig
575+ ) -> [ LookupResult ] {
576+ let filteredNamesFromLabel = namesFromLabel. filter { name in
577+ checkIdentifier ( identifier, refersTo: name, at: lookUpPosition)
578+ }
579+
580+ return sequentialLookup (
581+ in: statements,
582+ identifier,
583+ at: lookUpPosition,
584+ with: config,
585+ propagateToParent: false
586+ ) + ( filteredNamesFromLabel. isEmpty ? [ ] : [ . fromScope( self , withNames: filteredNamesFromLabel) ] )
587+ + lookupInParent( identifier, at: lookUpPosition, with: config)
588+ }
530589}
531590
532591@_spi ( Experimental) extension ProtocolDeclSyntax : ScopeSyntax {
@@ -631,16 +690,83 @@ import SwiftSyntax
631690}
632691
633692@_spi ( Experimental) extension SubscriptDeclSyntax : WithGenericParametersScopeSyntax {
634- /// Parameters introduced by this subscript.
693+ /// Parameters introduced by this subscript and possibly `self` keyword .
635694 @_spi ( Experimental) public var introducedNames : [ LookupName ] {
636- parameterClause. parameters. flatMap { parameter in
695+ let parameters = parameterClause. parameters. flatMap { parameter in
637696 LookupName . getNames ( from: parameter)
638697 }
698+
699+ if let accessorBlock, case . getter = accessorBlock. accessors {
700+ return parameters + [ . implicit( . self ( self ) ) ]
701+ } else {
702+ return parameters
703+ }
639704 }
640705
641706 @_spi ( Experimental) public var scopeDebugName : String {
642707 " SubscriptDeclScope "
643708 }
709+
710+ /// Lookup results from this subscript scope.
711+ /// Routes to generic parameter clause scope if exists.
712+ @_spi ( Experimental) public func lookup(
713+ _ identifier: Identifier ? ,
714+ at lookUpPosition: AbsolutePosition ,
715+ with config: LookupConfig
716+ ) -> [ LookupResult ] {
717+ var thisScopeResults : [ LookupResult ] = [ ]
718+
719+ if !parameterClause. range. contains ( lookUpPosition) && !returnClause. range. contains ( lookUpPosition) {
720+ thisScopeResults = defaultLookupImplementation (
721+ identifier,
722+ at: position,
723+ with: config,
724+ propagateToParent: false
725+ )
726+ }
727+
728+ return thisScopeResults
729+ + lookupThroughGenericParameterScope(
730+ identifier,
731+ at: lookUpPosition,
732+ with: config
733+ )
734+ }
735+ }
736+
737+ @_spi ( Experimental) extension AccessorBlockSyntax : SequentialScopeSyntax {
738+ /// Names from the accessors or
739+ /// getters of this accessor block scope.
740+ @_spi ( Experimental) public var introducedNames : [ LookupName ] {
741+ switch accessors {
742+ case . getter( let codeBlockItems) :
743+ return codeBlockItems. flatMap { codeBlockItem in
744+ LookupName . getNames ( from: codeBlockItem. item)
745+ }
746+ case . accessors:
747+ return [ ]
748+ }
749+ }
750+
751+ @_spi ( Experimental) public var scopeDebugName : String {
752+ " AccessorBlockScope "
753+ }
754+
755+ /// Names introduced in this accessir block scope.
756+ /// If `accessor` is of `.getter` kind, introduced
757+ /// it's items sequentially. Otherwise, propagate to parent.
758+ @_spi ( Experimental) public func lookup(
759+ _ identifier: Identifier ? ,
760+ at lookUpPosition: AbsolutePosition ,
761+ with config: LookupConfig
762+ ) -> [ LookupResult ] {
763+ switch accessors {
764+ case . getter( let codeBlockItems) :
765+ return sequentialLookup ( in: codeBlockItems, identifier, at: lookUpPosition, with: config)
766+ case . accessors:
767+ return lookupInParent ( identifier, at: lookUpPosition, with: config)
768+ }
769+ }
644770}
645771
646772@_spi ( Experimental) extension TypeAliasDeclSyntax : WithGenericParametersScopeSyntax {
0 commit comments