Skip to content

Commit 9d7c597

Browse files
committed
Convert lit-based syntax classification tests to XCTests
1 parent e04c5c1 commit 9d7c597

File tree

4 files changed

+426
-3
lines changed

4 files changed

+426
-3
lines changed

Sources/SwiftIDEUtils/SyntaxClassification.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extension SyntaxClassification {
6666
internal static func classify(_ keyPath: AnyKeyPath) -> (SyntaxClassification, Bool)? {
6767
switch keyPath {
6868
case \AttributeSyntax.attributeName:
69-
return (.attribute, false)
69+
return (.attribute, true)
7070
case \PlatformVersionItemSyntax.availabilityVersionRestriction:
7171
return (.keyword, false)
7272
case \AvailabilityVersionRestrictionSyntax.platform:

Sources/SwiftIDEUtils/SyntaxClassifier.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,37 @@ private struct ClassificationVisitor {
196196

197197
for case (let index, let child?) in children.enumerated() {
198198

199-
let classification: (SyntaxClassification, Bool)?
199+
let classification: (classification: SyntaxClassification, force: Bool)?
200200
if case .layout(let layout) = descriptor.node.kind.syntaxNodeType.structure {
201201
classification = SyntaxClassification.classify(layout[index])
202202
} else {
203203
classification = nil
204204
}
205+
206+
if let classification, classification.force {
207+
// Leading trivia.
208+
report(range: SyntaxClassifiedRange(kind: .none, range: ByteSourceRange(offset: byteOffset, length: child.leadingTriviaByteLength)))
209+
// Layout node text.
210+
let range = SyntaxClassifiedRange(
211+
kind: classification.classification,
212+
range: ByteSourceRange(
213+
offset: byteOffset + child.leadingTriviaByteLength,
214+
length: child.byteLength - child.leadingTriviaByteLength - child.trailingTriviaByteLength
215+
)
216+
)
217+
report(range: range)
218+
// Trailing trivia.
219+
report(
220+
range: SyntaxClassifiedRange(
221+
kind: .none,
222+
range: ByteSourceRange(offset: byteOffset + child.byteLength - child.trailingTriviaByteLength, length: child.trailingTriviaByteLength)
223+
)
224+
)
225+
226+
byteOffset += child.byteLength
227+
continue
228+
}
229+
205230
let result = visit(
206231
.init(
207232
node: child,

Tests/SwiftIDEUtilsTest/Assertions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func assertClassification(
7171
line: spec.line
7272
)
7373

74-
lastRangeUpperBound = source.index(source.startIndex, offsetBy: range.endOffset)
74+
lastRangeUpperBound = source.utf8.index(source.utf8.startIndex, offsetBy: range.endOffset)
7575
}
7676
}
7777

0 commit comments

Comments
 (0)