@@ -32,9 +32,6 @@ protocol Scope {
32
32
33
33
/// Syntax node that introduces this protocol.
34
34
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 ]
38
35
}
39
36
40
37
extension Scope {
@@ -46,33 +43,41 @@ extension Scope {
46
43
return syntax? . scope
47
44
}
48
45
}
49
-
46
+
50
47
// MARK: - lookupLabeledStmts
51
-
48
+
52
49
/// Given syntax node position, returns all available labeled statements.
53
50
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
+ )
55
55
}
56
-
56
+
57
57
// MARK: - lookupFallthroughSourceAndDest
58
-
58
+
59
59
/// Given syntax node position, returns the current switch case and it's fallthrough destination.
60
60
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 {
62
67
return ( nil , nil )
63
68
}
64
-
69
+
65
70
let nextSwitchCase = lookupNextSwitchCase ( at: originalSwitchCase)
66
-
71
+
67
72
return ( originalSwitchCase, nextSwitchCase)
68
73
}
69
-
74
+
70
75
/// Given a switch case, returns the case that follows according to the parent.
71
76
private func lookupNextSwitchCase( at switchCaseSyntax: SwitchCaseSyntax ) -> SwitchCaseSyntax ? {
72
77
guard let switchCaseListSyntax = switchCaseSyntax. parent? . as ( SwitchCaseListSyntax . self) else { return nil }
73
-
78
+
74
79
var visitedOriginalCase = false
75
-
80
+
76
81
for child in switchCaseListSyntax. children ( viewMode: . sourceAccurate) {
77
82
if let thisCase = child. as ( SwitchCaseSyntax . self) {
78
83
if thisCase. id == switchCaseSyntax. id {
@@ -82,21 +87,21 @@ extension Scope {
82
87
}
83
88
}
84
89
}
85
-
90
+
86
91
return nil
87
92
}
88
-
93
+
89
94
// MARK: - lookupCatchNode
90
-
95
+
91
96
/// Given syntax node position, returns the closest ancestor catch node.
92
97
func lookupCatchNode( at syntax: Syntax ) -> Syntax ? {
93
98
return lookupCatchNodeHelper ( at: syntax, traversedCatchClause: false )
94
99
}
95
-
100
+
96
101
/// Given syntax node location, finds where an error could be caught. If set to `true`, `traverseCatchClause`lookup will skip the next do statement.
97
102
private func lookupCatchNodeHelper( at syntax: Syntax ? , traversedCatchClause: Bool ) -> Syntax ? {
98
103
guard let syntax else { return nil }
99
-
104
+
100
105
switch syntax. as ( SyntaxEnum . self) {
101
106
case . doStmt:
102
107
if traversedCatchClause {
@@ -118,41 +123,63 @@ extension Scope {
118
123
return lookupCatchNodeHelper ( at: syntax. parent, traversedCatchClause: traversedCatchClause)
119
124
}
120
125
}
121
-
126
+
122
127
/// 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 ? {
125
132
walkParentTreeUpToFunctionBoundary ( at: syntax, collect: collect, stopWithFirstMatch: true ) . first
126
133
}
127
-
134
+
128
135
/// 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
139
153
)
140
154
}
141
-
155
+
142
156
/// 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 ] {
147
163
guard let syntax, !stopAt. contains ( where: { syntax. is ( $0) } ) else { return [ ] }
148
164
if let matchedSyntax = syntax. as ( T . self) {
149
165
if stopWithFirstMatch {
150
166
return [ matchedSyntax]
151
167
} 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
+ )
153
175
}
154
176
} 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
+ )
156
183
}
157
184
}
158
185
}
0 commit comments