Skip to content

Commit cc4cfac

Browse files
committed
Support folding of collection literals
1 parent fc3c519 commit cc4cfac

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

Sources/SourceKitLSP/Swift/FoldingRange.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ fileprivate final class FoldingRangeFinder: SyntaxAnyVisitor {
134134
return .visitChildren
135135
}
136136

137+
override func visit(_ node: ArrayExprSyntax) -> SyntaxVisitorContinueKind {
138+
return self.addFoldingRange(
139+
start: node.leftSquare.endPositionBeforeTrailingTrivia.utf8Offset,
140+
end: node.rightSquare.positionAfterSkippingLeadingTrivia.utf8Offset
141+
)
142+
}
143+
144+
override func visit(_ node: DictionaryExprSyntax) -> SyntaxVisitorContinueKind {
145+
return self.addFoldingRange(
146+
start: node.leftSquare.endPositionBeforeTrailingTrivia.utf8Offset,
147+
end: node.rightSquare.positionAfterSkippingLeadingTrivia.utf8Offset
148+
)
149+
}
150+
137151
override func visit(_ node: FunctionCallExprSyntax) -> SyntaxVisitorContinueKind {
138152
return self.addFoldingRange(
139153
start: node.arguments.position.utf8Offset,

Tests/SourceKitLSPTests/FoldingRangeTests.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ func assertFoldingRanges(
6666
Expected \(expectedRanges.count) ranges but got \(foldingRanges.count)
6767
6868
\(foldingRanges)
69-
"""
69+
""",
70+
file: file,
71+
line: line
7072
)
7173
return
7274
}
@@ -258,7 +260,27 @@ final class FoldingRangeTests: XCTestCase {
258260
)
259261
}
260262

261-
func testFoldCollections() {
262-
// let testClient
263+
func testFoldCollections() async throws {
264+
try await assertFoldingRanges(
265+
markedSource: """
266+
let x = [1️⃣1, 2, 32️⃣]
267+
""",
268+
expectedRanges: [
269+
FoldingRangeSpec(from: "1️⃣", to: "2️⃣")
270+
]
271+
)
272+
273+
try await assertFoldingRanges(
274+
markedSource: """
275+
let x = [1️⃣
276+
1: "one",
277+
2: "two",
278+
3: "three"
279+
2️⃣]
280+
""",
281+
expectedRanges: [
282+
FoldingRangeSpec(from: "1️⃣", to: "2️⃣")
283+
]
284+
)
263285
}
264286
}

0 commit comments

Comments
 (0)