Skip to content

Commit fc3c519

Browse files
committed
Consider trivia symetrically when folding braced blocks
1 parent 1432390 commit fc3c519

File tree

2 files changed

+10
-35
lines changed

2 files changed

+10
-35
lines changed

Sources/SourceKitLSP/Swift/FoldingRange.swift

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import LSPLogging
1414
import LanguageServerProtocol
1515
import SwiftSyntax
1616

17-
fileprivate final class FoldingRangeFinder: SyntaxVisitor {
17+
fileprivate final class FoldingRangeFinder: SyntaxAnyVisitor {
1818
private let snapshot: DocumentSnapshot
1919
/// Some ranges might occur multiple times.
2020
/// E.g. for `print("hi")`, `"hi"` is both the range of all call arguments and the range the first argument in the call.
@@ -124,39 +124,14 @@ fileprivate final class FoldingRangeFinder: SyntaxVisitor {
124124
}
125125
}
126126

127-
override func visit(_ node: CodeBlockSyntax) -> SyntaxVisitorContinueKind {
128-
return self.addFoldingRange(
129-
start: node.statements.position.utf8Offset,
130-
end: node.rightBrace.positionAfterSkippingLeadingTrivia.utf8Offset
131-
)
132-
}
133-
134-
override func visit(_ node: MemberBlockSyntax) -> SyntaxVisitorContinueKind {
135-
return self.addFoldingRange(
136-
start: node.members.position.utf8Offset,
137-
end: node.rightBrace.positionAfterSkippingLeadingTrivia.utf8Offset
138-
)
139-
}
140-
141-
override func visit(_ node: ClosureExprSyntax) -> SyntaxVisitorContinueKind {
142-
return self.addFoldingRange(
143-
start: node.statements.position.utf8Offset,
144-
end: node.rightBrace.positionAfterSkippingLeadingTrivia.utf8Offset
145-
)
146-
}
147-
148-
override func visit(_ node: AccessorBlockSyntax) -> SyntaxVisitorContinueKind {
149-
return self.addFoldingRange(
150-
start: node.accessors.position.utf8Offset,
151-
end: node.rightBrace.positionAfterSkippingLeadingTrivia.utf8Offset
152-
)
153-
}
154-
155-
override func visit(_ node: SwitchExprSyntax) -> SyntaxVisitorContinueKind {
156-
return self.addFoldingRange(
157-
start: node.cases.position.utf8Offset,
158-
end: node.rightBrace.positionAfterSkippingLeadingTrivia.utf8Offset
159-
)
127+
override func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
128+
if let braced = node.asProtocol(BracedSyntax.self) {
129+
return self.addFoldingRange(
130+
start: braced.leftBrace.endPositionBeforeTrailingTrivia.utf8Offset,
131+
end: braced.rightBrace.positionAfterSkippingLeadingTrivia.utf8Offset
132+
)
133+
}
134+
return .visitChildren
160135
}
161136

162137
override func visit(_ node: FunctionCallExprSyntax) -> SyntaxVisitorContinueKind {

Tests/SourceKitLSPTests/FoldingRangeTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ final class FoldingRangeTests: XCTestCase {
233233
func testFoldGuardBody() async throws {
234234
try await assertFoldingRanges(
235235
markedSource: """
236-
guard a > 0 else { 1️⃣return 2️⃣}
236+
guard a > 0 else {1️⃣ return 2️⃣}
237237
"""
238238
,
239239
expectedRanges: [

0 commit comments

Comments
 (0)