Skip to content

Commit bcea952

Browse files
authored
Merge pull request #1287 from kimdv/kimdv/1182-conditional-compile-directives-if-should-support-code-folding
Add folding operator for `IfConfigClauseSyntax`
2 parents 490871b + b30f595 commit bcea952

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

Sources/SourceKitLSP/Swift/FoldingRange.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ fileprivate final class FoldingRangeFinder: SyntaxAnyVisitor {
164164
return .visitChildren
165165
}
166166

167+
override func visit(_ node: IfConfigClauseSyntax) -> SyntaxVisitorContinueKind {
168+
guard let closePound = node.lastToken(viewMode: .sourceAccurate)?.nextToken(viewMode: .sourceAccurate) else {
169+
return .visitChildren
170+
}
171+
172+
return self.addFoldingRange(
173+
start: node.poundKeyword.positionAfterSkippingLeadingTrivia,
174+
end: closePound.positionAfterSkippingLeadingTrivia
175+
)
176+
}
177+
167178
override func visit(_ node: SubscriptCallExprSyntax) -> SyntaxVisitorContinueKind {
168179
return self.addFoldingRange(
169180
start: node.leftSquare.endPositionBeforeTrailingTrivia,

Tests/SourceKitLSPTests/FoldingRangeTests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,52 @@ final class FoldingRangeTests: XCTestCase {
357357
expectedRanges: [FoldingRangeSpec(from: "1️⃣", to: "2️⃣")]
358358
)
359359
}
360+
361+
func testFoldArgumentsForConditionalIfCompileDirectives() async throws {
362+
try await assertFoldingRanges(
363+
markedSource: """
364+
1️⃣#if DEBUG
365+
let foo = "x"
366+
2️⃣#endif
367+
""",
368+
expectedRanges: [
369+
FoldingRangeSpec(from: "1️⃣", to: "2️⃣")
370+
]
371+
)
372+
}
373+
374+
func testFoldArgumentsForConditionalElseIfCompileDirectives() async throws {
375+
try await assertFoldingRanges(
376+
markedSource: """
377+
1️⃣#if DEBUG
378+
let foo = "x"
379+
2️⃣#elseif TEST
380+
let foo = "y"
381+
3️⃣#else
382+
let foo = "z"
383+
4️⃣#endif
384+
""",
385+
expectedRanges: [
386+
FoldingRangeSpec(from: "1️⃣", to: "2️⃣"),
387+
FoldingRangeSpec(from: "2️⃣", to: "3️⃣"),
388+
FoldingRangeSpec(from: "3️⃣", to: "4️⃣"),
389+
]
390+
)
391+
}
392+
393+
func testFoldArgumentsForConditionalElseCompileDirectives() async throws {
394+
try await assertFoldingRanges(
395+
markedSource: """
396+
1️⃣#if DEBUG
397+
let foo = "x"
398+
2️⃣#else
399+
let foo = "y"
400+
3️⃣#endif
401+
""",
402+
expectedRanges: [
403+
FoldingRangeSpec(from: "1️⃣", to: "2️⃣"),
404+
FoldingRangeSpec(from: "2️⃣", to: "3️⃣"),
405+
]
406+
)
407+
}
360408
}

0 commit comments

Comments
 (0)