@@ -32,9 +32,6 @@ protocol Scope {
3232
3333 /// Syntax node that introduces this protocol.
3434 var sourceSyntax : SyntaxProtocol { get }
35-
36- /// Returns the declaration `name` refers to at a particular syntax node location.
37- func getDeclarationsFor( name: String , at syntax: SyntaxProtocol ) -> [ Syntax ]
3835}
3936
4037extension Scope {
@@ -46,33 +43,41 @@ extension Scope {
4643 return syntax? . scope
4744 }
4845 }
49-
46+
5047 // MARK: - lookupLabeledStmts
51-
48+
5249 /// Given syntax node position, returns all available labeled statements.
5350 func lookupLabeledStmts( at syntax: SyntaxProtocol ) -> [ LabeledStmtSyntax ] {
54- return walkParentTreeUpToFunctionBoundary ( at: syntax. parent, collect: LabeledStmtSyntax . self)
51+ return walkParentTreeUpToFunctionBoundary (
52+ at: syntax. parent,
53+ collect: LabeledStmtSyntax . self
54+ )
5555 }
56-
56+
5757 // MARK: - lookupFallthroughSourceAndDest
58-
58+
5959 /// Given syntax node position, returns the current switch case and it's fallthrough destination.
6060 func lookupFallthroughSourceAndDestination( at syntax: SyntaxProtocol ) -> ( SwitchCaseSyntax ? , SwitchCaseSyntax ? ) {
61- guard let originalSwitchCase = walkParentTreeUpToFunctionBoundary ( at: Syntax ( syntax) , collect: SwitchCaseSyntax . self) else {
61+ guard
62+ let originalSwitchCase = walkParentTreeUpToFunctionBoundary (
63+ at: Syntax ( syntax) ,
64+ collect: SwitchCaseSyntax . self
65+ )
66+ else {
6267 return ( nil , nil )
6368 }
64-
69+
6570 let nextSwitchCase = lookupNextSwitchCase ( at: originalSwitchCase)
66-
71+
6772 return ( originalSwitchCase, nextSwitchCase)
6873 }
69-
74+
7075 /// Given a switch case, returns the case that follows according to the parent.
7176 private func lookupNextSwitchCase( at switchCaseSyntax: SwitchCaseSyntax ) -> SwitchCaseSyntax ? {
7277 guard let switchCaseListSyntax = switchCaseSyntax. parent? . as ( SwitchCaseListSyntax . self) else { return nil }
73-
78+
7479 var visitedOriginalCase = false
75-
80+
7681 for child in switchCaseListSyntax. children ( viewMode: . sourceAccurate) {
7782 if let thisCase = child. as ( SwitchCaseSyntax . self) {
7883 if thisCase. id == switchCaseSyntax. id {
@@ -82,21 +87,21 @@ extension Scope {
8287 }
8388 }
8489 }
85-
90+
8691 return nil
8792 }
88-
93+
8994 // MARK: - lookupCatchNode
90-
95+
9196 /// Given syntax node position, returns the closest ancestor catch node.
9297 func lookupCatchNode( at syntax: Syntax ) -> Syntax ? {
9398 return lookupCatchNodeHelper ( at: syntax, traversedCatchClause: false )
9499 }
95-
100+
96101 /// Given syntax node location, finds where an error could be caught. If set to `true`, `traverseCatchClause`lookup will skip the next do statement.
97102 private func lookupCatchNodeHelper( at syntax: Syntax ? , traversedCatchClause: Bool ) -> Syntax ? {
98103 guard let syntax else { return nil }
99-
104+
100105 switch syntax. as ( SyntaxEnum . self) {
101106 case . doStmt:
102107 if traversedCatchClause {
@@ -118,41 +123,63 @@ extension Scope {
118123 return lookupCatchNodeHelper ( at: syntax. parent, traversedCatchClause: traversedCatchClause)
119124 }
120125 }
121-
126+
122127 /// Callect the first syntax node matching the collection type up to a function boundary.
123- func walkParentTreeUpToFunctionBoundary< T: SyntaxProtocol > ( at syntax: Syntax ? ,
124- collect: T . Type ) -> T ? {
128+ func walkParentTreeUpToFunctionBoundary< T: SyntaxProtocol > (
129+ at syntax: Syntax ? ,
130+ collect: T . Type
131+ ) -> T ? {
125132 walkParentTreeUpToFunctionBoundary ( at: syntax, collect: collect, stopWithFirstMatch: true ) . first
126133 }
127-
134+
128135 /// Callect syntax nodes matching the collection type up to a function boundary.
129- func walkParentTreeUpToFunctionBoundary< T: SyntaxProtocol > ( at syntax: Syntax ? ,
130- collect: T . Type ,
131- stopWithFirstMatch: Bool = false ) -> [ T ] {
132- walkParentTree ( upTo: [ MemberBlockSyntax . self,
133- FunctionDeclSyntax . self,
134- InitializerDeclSyntax . self,
135- ClosureExprSyntax . self] ,
136- at: syntax,
137- collect: collect,
138- stopWithFirstMatch: stopWithFirstMatch
136+ func walkParentTreeUpToFunctionBoundary< T: SyntaxProtocol > (
137+ at syntax: Syntax ? ,
138+ collect: T . Type ,
139+ stopWithFirstMatch: Bool = false
140+ ) -> [ T ] {
141+ walkParentTree (
142+ upTo: [
143+ MemberBlockSyntax . self,
144+ FunctionDeclSyntax . self,
145+ InitializerDeclSyntax . self,
146+ DeinitializerDeclSyntax . self,
147+ AccessorDeclSyntax . self,
148+ ClosureExprSyntax . self,
149+ ] ,
150+ at: syntax,
151+ collect: collect,
152+ stopWithFirstMatch: stopWithFirstMatch
139153 )
140154 }
141-
155+
142156 /// Callect syntax nodes matching the collection type up until encountering one of the specified syntax nodes.
143- func walkParentTree< T: SyntaxProtocol > ( upTo stopAt: [ SyntaxProtocol . Type ] ,
144- at syntax: Syntax ? ,
145- collect: T . Type ,
146- stopWithFirstMatch: Bool = false ) -> [ T ] {
157+ func walkParentTree< T: SyntaxProtocol > (
158+ upTo stopAt: [ SyntaxProtocol . Type ] ,
159+ at syntax: Syntax ? ,
160+ collect: T . Type ,
161+ stopWithFirstMatch: Bool = false
162+ ) -> [ T ] {
147163 guard let syntax, !stopAt. contains ( where: { syntax. is ( $0) } ) else { return [ ] }
148164 if let matchedSyntax = syntax. as ( T . self) {
149165 if stopWithFirstMatch {
150166 return [ matchedSyntax]
151167 } else {
152- return [ matchedSyntax] + walkParentTree( upTo: stopAt, at: syntax. parent, collect: collect, stopWithFirstMatch: stopWithFirstMatch)
168+ return [ matchedSyntax]
169+ + walkParentTree(
170+ upTo: stopAt,
171+ at: syntax. parent,
172+ collect: collect,
173+ stopWithFirstMatch: stopWithFirstMatch
174+ )
153175 }
154176 } else {
155- return walkParentTree ( upTo: stopAt, at: syntax. parent, collect: collect, stopWithFirstMatch: stopWithFirstMatch)
177+ return walkParentTree (
178+ upTo: stopAt,
179+ at: syntax. parent,
180+ collect: collect,
181+ stopWithFirstMatch: stopWithFirstMatch
182+ )
156183 }
157184 }
158185}
0 commit comments