@@ -115,7 +115,7 @@ import SwiftSyntax
115
115
switch config. fileScopeHandling {
116
116
case . memberBlock:
117
117
guard config. includeMembers else { return [ ] }
118
-
118
+
119
119
let names = introducedNames ( using: . memberBlock)
120
120
. filter { lookupName in
121
121
checkIdentifier ( identifier, refersTo: lookupName, at: lookUpPosition)
@@ -150,7 +150,8 @@ import SwiftSyntax
150
150
with: config
151
151
)
152
152
153
- return ( members. isEmpty || !config. includeMembers ? [ ] : [ . fromFileScope( self , withNames: members) ] ) + sequentialNames
153
+ return ( members. isEmpty || !config. includeMembers ? [ ] : [ . fromFileScope( self , withNames: members) ] )
154
+ + sequentialNames
154
155
}
155
156
}
156
157
}
@@ -528,7 +529,7 @@ import SwiftSyntax
528
529
@_spi ( Experimental) public var scopeDebugName : String {
529
530
" AccessorDeclScope "
530
531
}
531
-
532
+
532
533
/// Returns result with matching names from
533
534
/// this scope and passes result with implicit `self`
534
535
/// to be introduced after the `subscript`
@@ -541,18 +542,24 @@ import SwiftSyntax
541
542
guard let parentAccessorBlockScope = parentScope? . as ( AccessorBlockSyntax . self) else {
542
543
return defaultLookupImplementation ( identifier, at: lookUpPosition, with: config)
543
544
}
544
-
545
+
546
+ let implicitSelf : [ LookupName ] = [ . implicit( . self ( self ) ) ]
547
+ . filter { name in
548
+ checkIdentifier ( identifier, refersTo: name, at: lookUpPosition)
549
+ }
550
+
545
551
return defaultLookupImplementation (
546
552
identifier,
547
553
at: lookUpPosition,
548
554
with: config,
549
555
propagateToParent: false
550
- ) + parentAccessorBlockScope. interleaveAccessorResultsAfterSubscriptLookup (
551
- identifier,
552
- at: lookUpPosition,
553
- with: config,
554
- resultsToInterleave: [ . fromScope( self , withNames: [ . implicit( . self ( self ) ) ] ) ]
555
556
)
557
+ + parentAccessorBlockScope. interleaveAccessorResultsAfterSubscriptLookup (
558
+ identifier,
559
+ at: lookUpPosition,
560
+ with: config,
561
+ resultsToInterleave: implicitSelf. isEmpty ? [ ] : [ . fromScope( self , withNames: implicitSelf) ]
562
+ )
556
563
}
557
564
}
558
565
@@ -606,14 +613,18 @@ import SwiftSyntax
606
613
with: config,
607
614
propagateToParent: false
608
615
) + ( filteredNamesFromLabel. isEmpty ? [ ] : [ . fromScope( self , withNames: filteredNamesFromLabel) ] )
609
- + ( config. finishInSequentialScope ? [ ] : lookupInParent ( identifier, at: lookUpPosition, with: config) )
616
+ + ( config. finishInSequentialScope ? [ ] : lookupInParent ( identifier, at: lookUpPosition, with: config) )
610
617
}
611
618
}
612
619
613
- @_spi ( Experimental) extension ProtocolDeclSyntax : ScopeSyntax {
620
+ @_spi ( Experimental) extension ProtocolDeclSyntax : ScopeSyntax , LookInMembersScopeSyntax {
614
621
/// Protocol declarations don't introduce names by themselves.
615
622
@_spi ( Experimental) public var introducedNames : [ LookupName ] {
616
- [ ]
623
+ [ . implicit( . Self( self ) ) ]
624
+ }
625
+
626
+ @_spi ( Experimental) public var lookupMembersPosition : AbsolutePosition {
627
+ name. positionAfterSkippingLeadingTrivia
617
628
}
618
629
619
630
@_spi ( Experimental) public var scopeDebugName : String {
@@ -655,7 +666,13 @@ import SwiftSyntax
655
666
)
656
667
}
657
668
658
- return results + defaultLookupImplementation( identifier, at: lookUpPosition, with: config)
669
+ return results
670
+ + defaultLookupImplementation(
671
+ identifier,
672
+ at: lookUpPosition,
673
+ with: config,
674
+ propagateToParent: false
675
+ ) + [ . lookInMembers( self ) ] + lookupInParent( identifier, at: lookUpPosition, with: config)
659
676
}
660
677
}
661
678
@@ -672,42 +689,15 @@ import SwiftSyntax
672
689
}
673
690
}
674
691
675
- @_spi ( Experimental) extension FunctionDeclSyntax : WithGenericParametersScopeSyntax {
676
- /// Function parameters introduced by this function's signature.
677
- @_spi ( Experimental) public var introducedNames : [ LookupName ] {
678
- signature. parameterClause. parameters. flatMap { parameter in
679
- LookupName . getNames ( from: parameter)
680
- } + ( parentScope? . is ( MemberBlockSyntax . self) ?? false ? [ . implicit( . self ( self ) ) ] : [ ] )
681
- }
682
-
692
+ @_spi ( Experimental) extension FunctionDeclSyntax : FunctionScopeSyntax {
683
693
@_spi ( Experimental) public var scopeDebugName : String {
684
694
" FunctionDeclScope "
685
695
}
696
+ }
686
697
687
- /// Lookup results from this function scope.
688
- /// Routes to generic parameter clause scope if exists.
689
- @_spi ( Experimental) public func lookup(
690
- _ identifier: Identifier ? ,
691
- at lookUpPosition: AbsolutePosition ,
692
- with config: LookupConfig
693
- ) -> [ LookupResult ] {
694
- var thisScopeResults : [ LookupResult ] = [ ]
695
-
696
- if !signature. range. contains ( lookUpPosition) {
697
- thisScopeResults = defaultLookupImplementation (
698
- identifier,
699
- at: position,
700
- with: config,
701
- propagateToParent: false
702
- )
703
- }
704
-
705
- return thisScopeResults
706
- + lookupThroughGenericParameterScope(
707
- identifier,
708
- at: lookUpPosition,
709
- with: config
710
- )
698
+ @_spi ( Experimental) extension InitializerDeclSyntax : FunctionScopeSyntax {
699
+ @_spi ( Experimental) public var scopeDebugName : String {
700
+ " InitializerDeclScope "
711
701
}
712
702
}
713
703
@@ -743,7 +733,7 @@ import SwiftSyntax
743
733
resultsToInterleave: [ ]
744
734
)
745
735
}
746
-
736
+
747
737
/// Lookup names in this scope and add `resultsToInterleave`
748
738
/// after results from this scope.
749
739
///
@@ -822,7 +812,7 @@ import SwiftSyntax
822
812
return lookupInParent ( identifier, at: lookUpPosition, with: config)
823
813
}
824
814
}
825
-
815
+
826
816
/// Used by children accessors to interleave
827
817
/// their results with parent `subscript` declaration scope.
828
818
func interleaveAccessorResultsAfterSubscriptLookup(
@@ -834,7 +824,7 @@ import SwiftSyntax
834
824
guard let parentSubscriptScope = parentScope? . as ( SubscriptDeclSyntax . self) else {
835
825
return lookupInParent ( identifier, at: lookUpPosition, with: config)
836
826
}
837
-
827
+
838
828
return parentSubscriptScope. interleaveResultsAfterThisSubscriptLookup (
839
829
identifier,
840
830
at: lookUpPosition,
@@ -854,3 +844,39 @@ import SwiftSyntax
854
844
" TypeAliasDeclScope "
855
845
}
856
846
}
847
+
848
+ @_spi ( Experimental) extension VariableDeclSyntax : ScopeSyntax {
849
+ /// Variable decl scope doesn't introduce any
850
+ /// names unless it is a member and is looked
851
+ /// up from inside it's accessor block.
852
+ @_spi ( Experimental) public var introducedNames : [ LookupName ] {
853
+ [ ]
854
+ }
855
+
856
+ @_spi ( Experimental) public var scopeDebugName : String {
857
+ " VariableDeclScope "
858
+ }
859
+
860
+ /// If a member and looked up from inside
861
+ /// it's accessor block, introduce implicit
862
+ /// `self` and propagate the lookup further.
863
+ @_spi ( Experimental) public func lookup(
864
+ _ identifier: Identifier ? ,
865
+ at lookUpPosition: AbsolutePosition ,
866
+ with config: LookupConfig
867
+ ) -> [ LookupResult ] {
868
+ if let parentScope,
869
+ parentScope. is ( MemberBlockSyntax . self) ,
870
+ bindings. first? . accessorBlock? . range. contains ( lookUpPosition) ?? false
871
+ {
872
+ return defaultLookupImplementation (
873
+ in: [ . implicit( . self ( self ) ) ] ,
874
+ identifier,
875
+ at: lookUpPosition,
876
+ with: config
877
+ )
878
+ } else {
879
+ return lookupInParent ( identifier, at: lookUpPosition, with: config)
880
+ }
881
+ }
882
+ }
0 commit comments