@@ -51,27 +51,27 @@ fileprivate final class FoldingRangeFinder: SyntaxAnyVisitor {
51
51
52
52
private func addTrivia( from node: TokenSyntax , _ trivia: Trivia ) {
53
53
let pieces = trivia. pieces
54
- var start = node. position. utf8Offset
54
+ var start = node. position
55
55
/// The index of the trivia piece we are currently inspecting.
56
56
var index = 0
57
57
58
58
while index < pieces. count {
59
59
let piece = pieces [ index]
60
60
defer {
61
- start += pieces [ index] . sourceLength. utf8Length
61
+ start = start . advanced ( by : pieces [ index] . sourceLength. utf8Length)
62
62
index += 1
63
63
}
64
64
switch piece {
65
65
case . blockComment:
66
66
_ = self . addFoldingRange (
67
67
start: start,
68
- end: start + piece. sourceLength. utf8Length,
68
+ end: start. advanced ( by : piece. sourceLength. utf8Length) ,
69
69
kind: . comment
70
70
)
71
71
case . docBlockComment:
72
72
_ = self . addFoldingRange (
73
73
start: start,
74
- end: start + piece. sourceLength. utf8Length,
74
+ end: start. advanced ( by : piece. sourceLength. utf8Length) ,
75
75
kind: . comment
76
76
)
77
77
case . lineComment, . docLineComment:
@@ -89,7 +89,7 @@ fileprivate final class FoldingRangeFinder: SyntaxAnyVisitor {
89
89
let piece = pieces [ lookaheadIndex]
90
90
defer {
91
91
lookaheadIndex += 1
92
- lookaheadStart += piece. sourceLength. utf8Length
92
+ lookaheadStart = lookaheadStart . advanced ( by : piece. sourceLength. utf8Length)
93
93
}
94
94
switch piece {
95
95
case . newlines( let count) , . carriageReturns( let count) , . carriageReturnLineFeeds( let count) :
@@ -115,7 +115,7 @@ fileprivate final class FoldingRangeFinder: SyntaxAnyVisitor {
115
115
}
116
116
_ = self . addFoldingRange (
117
117
start: lineCommentBlockStart,
118
- end: start + pieces[ index] . sourceLength. utf8Length,
118
+ end: start. advanced ( by : pieces [ index] . sourceLength. utf8Length) ,
119
119
kind: . comment
120
120
)
121
121
default :
@@ -127,61 +127,76 @@ fileprivate final class FoldingRangeFinder: SyntaxAnyVisitor {
127
127
override func visitAny( _ node: Syntax ) -> SyntaxVisitorContinueKind {
128
128
if let braced = node. asProtocol ( BracedSyntax . self) {
129
129
return self . addFoldingRange (
130
- start: braced. leftBrace. endPositionBeforeTrailingTrivia. utf8Offset ,
131
- end: braced. rightBrace. positionAfterSkippingLeadingTrivia. utf8Offset
130
+ start: braced. leftBrace. endPositionBeforeTrailingTrivia,
131
+ end: braced. rightBrace. positionAfterSkippingLeadingTrivia
132
132
)
133
133
}
134
134
return . visitChildren
135
135
}
136
136
137
137
override func visit( _ node: ArrayExprSyntax ) -> SyntaxVisitorContinueKind {
138
138
return self . addFoldingRange (
139
- start: node. leftSquare. endPositionBeforeTrailingTrivia. utf8Offset ,
140
- end: node. rightSquare. positionAfterSkippingLeadingTrivia. utf8Offset
139
+ start: node. leftSquare. endPositionBeforeTrailingTrivia,
140
+ end: node. rightSquare. positionAfterSkippingLeadingTrivia
141
141
)
142
142
}
143
143
144
144
override func visit( _ node: DictionaryExprSyntax ) -> SyntaxVisitorContinueKind {
145
145
return self . addFoldingRange (
146
- start: node. leftSquare. endPositionBeforeTrailingTrivia. utf8Offset ,
147
- end: node. rightSquare. positionAfterSkippingLeadingTrivia. utf8Offset
146
+ start: node. leftSquare. endPositionBeforeTrailingTrivia,
147
+ end: node. rightSquare. positionAfterSkippingLeadingTrivia
148
148
)
149
149
}
150
150
151
151
override func visit( _ node: FunctionCallExprSyntax ) -> SyntaxVisitorContinueKind {
152
- return self . addFoldingRange (
153
- start: node. arguments. position. utf8Offset,
154
- end: node. arguments. endPosition. utf8Offset
155
- )
152
+ let start = node. leftParen? . endPositionBeforeTrailingTrivia ?? node. arguments. position
153
+ let end =
154
+ if !node. additionalTrailingClosures. isEmpty {
155
+ node. additionalTrailingClosures. endPositionBeforeTrailingTrivia
156
+ } else if let trailingClosure = node. trailingClosure {
157
+ trailingClosure. endPositionBeforeTrailingTrivia
158
+ } else if let rightParen = node. rightParen {
159
+ rightParen. positionAfterSkippingLeadingTrivia
160
+ } else {
161
+ // Should never happen because the call should have either a trailing closure or a closing ')'
162
+ node. arguments. endPositionBeforeTrailingTrivia
163
+ }
164
+ return self . addFoldingRange ( start: start, end: end)
156
165
}
157
166
158
167
override func visit( _ node: SubscriptCallExprSyntax ) -> SyntaxVisitorContinueKind {
159
168
return self . addFoldingRange (
160
- start: node. arguments . position . utf8Offset ,
161
- end: node. arguments . endPosition . utf8Offset
169
+ start: node. leftSquare . endPositionBeforeTrailingTrivia ,
170
+ end: node. rightSquare . positionAfterSkippingLeadingTrivia
162
171
)
163
172
}
164
173
165
174
override func visit( _ node: SwitchCaseSyntax ) -> SyntaxVisitorContinueKind {
166
175
return self . addFoldingRange (
167
- start: node. label. endPositionBeforeTrailingTrivia. utf8Offset ,
168
- end: node. statements. endPosition. utf8Offset
176
+ start: node. label. endPositionBeforeTrailingTrivia,
177
+ end: node. statements. endPosition
169
178
)
170
179
}
171
180
172
181
__consuming func finalize( ) -> Set < FoldingRange > {
173
182
return self . ranges
174
183
}
175
184
176
- private func addFoldingRange( start: Int , end: Int , kind: FoldingRangeKind ? = nil ) -> SyntaxVisitorContinueKind {
185
+ private func addFoldingRange(
186
+ start: AbsolutePosition ,
187
+ end: AbsolutePosition ,
188
+ kind: FoldingRangeKind ? = nil
189
+ ) -> SyntaxVisitorContinueKind {
177
190
if let limit = self . rangeLimit, self . ranges. count >= limit {
178
191
return . skipChildren
179
192
}
180
193
181
- guard let start: Position = snapshot. positionOf ( utf8Offset: start) ,
182
- let end: Position = snapshot. positionOf ( utf8Offset: end)
194
+ guard let start: Position = snapshot. positionOf ( utf8Offset: start. utf8Offset ) ,
195
+ let end: Position = snapshot. positionOf ( utf8Offset: end. utf8Offset )
183
196
else {
184
- logger. error ( " folding range failed to retrieve position of \( self . snapshot. uri. forLogging) : \( start) - \( end) " )
197
+ logger. error (
198
+ " folding range failed to retrieve position of \( self . snapshot. uri. forLogging) : \( start. utf8Offset) - \( end. utf8Offset) "
199
+ )
185
200
return . visitChildren
186
201
}
187
202
let range : FoldingRange
0 commit comments