@@ -191,6 +191,21 @@ import SwiftSyntax
191
191
@_spi ( Experimental) public var scopeDebugName : String {
192
192
" ForStmtScope "
193
193
}
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
+ }
194
209
}
195
210
196
211
@_spi ( Experimental) extension ClosureExprSyntax : SequentialScopeSyntax {
@@ -249,8 +264,14 @@ import SwiftSyntax
249
264
at lookUpPosition: AbsolutePosition ,
250
265
with config: LookupConfig
251
266
) -> [ 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
+ }
254
275
}
255
276
256
277
return sequentialLookup (
@@ -267,8 +288,8 @@ import SwiftSyntax
267
288
@_spi ( Experimental) extension WhileStmtSyntax : ScopeSyntax {
268
289
/// Names introduced by the `while` loop by its conditions.
269
290
@_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 )
272
293
}
273
294
}
274
295
@@ -486,18 +507,26 @@ import SwiftSyntax
486
507
@_spi ( Experimental) extension AccessorDeclSyntax : ScopeSyntax {
487
508
/// Implicit and/or explicit names introduced within the accessor.
488
509
@_spi ( Experimental) public var introducedNames : [ LookupName ] {
510
+ let names : [ LookupName ]
511
+
489
512
if let parameters {
490
- return LookupName . getNames ( from: parameters)
513
+ names = LookupName . getNames ( from: parameters)
491
514
} else {
492
515
switch accessorSpecifier. tokenKind {
493
516
case . keyword( . set) , . keyword( . willSet) :
494
- return [ . implicit( . newValue( self ) ) ]
517
+ names = [ . implicit( . newValue( self ) ) ]
495
518
case . keyword( . didSet) :
496
- return [ . implicit( . oldValue( self ) ) ]
519
+ names = [ . implicit( . oldValue( self ) ) ]
497
520
default :
498
- return [ ]
521
+ names = [ ]
499
522
}
500
523
}
524
+
525
+ if let parentScope, parentScope. is ( AccessorBlockSyntax . self) {
526
+ return names + [ . implicit( . self ( self ) ) ]
527
+ } else {
528
+ return names
529
+ }
501
530
}
502
531
503
532
@_spi ( Experimental) public var scopeDebugName : String {
@@ -516,17 +545,47 @@ import SwiftSyntax
516
545
}
517
546
}
518
547
519
- @_spi ( Experimental) extension SwitchCaseSyntax : ScopeSyntax {
548
+ @_spi ( Experimental) extension SwitchCaseSyntax : SequentialScopeSyntax {
520
549
/// Names introduced within `case` items.
521
- @ _spi ( Experimental ) public var introducedNames : [ LookupName ] {
550
+ var namesFromLabel : [ LookupName ] {
522
551
label. as ( SwitchCaseLabelSyntax . self) ? . caseItems. flatMap { child in
523
552
LookupName . getNames ( from: child. pattern)
524
553
} ?? [ ]
525
554
}
526
555
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
+
527
564
@_spi ( Experimental) public var scopeDebugName : String {
528
565
" SwitchCaseScope "
529
566
}
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
+ }
530
589
}
531
590
532
591
@_spi ( Experimental) extension ProtocolDeclSyntax : ScopeSyntax {
@@ -631,16 +690,83 @@ import SwiftSyntax
631
690
}
632
691
633
692
@_spi ( Experimental) extension SubscriptDeclSyntax : WithGenericParametersScopeSyntax {
634
- /// Parameters introduced by this subscript.
693
+ /// Parameters introduced by this subscript and possibly `self` keyword .
635
694
@_spi ( Experimental) public var introducedNames : [ LookupName ] {
636
- parameterClause. parameters. flatMap { parameter in
695
+ let parameters = parameterClause. parameters. flatMap { parameter in
637
696
LookupName . getNames ( from: parameter)
638
697
}
698
+
699
+ if let accessorBlock, case . getter = accessorBlock. accessors {
700
+ return parameters + [ . implicit( . self ( self ) ) ]
701
+ } else {
702
+ return parameters
703
+ }
639
704
}
640
705
641
706
@_spi ( Experimental) public var scopeDebugName : String {
642
707
" SubscriptDeclScope "
643
708
}
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
+ }
644
770
}
645
771
646
772
@_spi ( Experimental) extension TypeAliasDeclSyntax : WithGenericParametersScopeSyntax {
0 commit comments