Skip to content

Commit 54d3de5

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

File tree

5 files changed

+426
-6
lines changed

5 files changed

+426
-6
lines changed

Sources/SwiftIDEUtils/SyntaxClassification.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public enum SyntaxClassification {
3939
case lineComment
4040
/// The token should not receive syntax coloring.
4141
case none
42-
/// An image, color, etc. literal.
43-
case objectLiteral
4442
/// An identifier referring to an operator.
4543
case operatorIdentifier
4644
/// A `#` token like `#warning`.
@@ -66,7 +64,7 @@ extension SyntaxClassification {
6664
internal static func classify(_ keyPath: AnyKeyPath) -> (SyntaxClassification, Bool)? {
6765
switch keyPath {
6866
case \AttributeSyntax.attributeName:
69-
return (.attribute, false)
67+
return (.attribute, true)
7068
case \PlatformVersionItemSyntax.availabilityVersionRestriction:
7169
return (.keyword, false)
7270
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,

Sources/lit-test-helper/ClassifiedSyntaxTreePrinter.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ extension SyntaxClassification {
3030
case .poundDirective: return "#kw"
3131
case .buildConfigId: return "#id"
3232
case .attribute: return "attr-builtin"
33-
case .objectLiteral: return "object-literal"
3433
case .editorPlaceholder: return "placeholder"
3534
case .lineComment: return "comment-line"
3635
case .blockComment: return "comment-block"

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)