Skip to content

Commit ad6358a

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

File tree

6 files changed

+508
-6
lines changed

6 files changed

+508
-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: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,49 @@ 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+
if let triviaPieces = child.leadingTriviaPieces {
209+
for triviaPiece in triviaPieces {
210+
let range = triviaPiece.classify(offset: byteOffset)
211+
report(range: range)
212+
byteOffset += triviaPiece.byteLength
213+
}
214+
}
215+
child.leadingTriviaPieces?.compactMap { $0 }.forEach {
216+
let range = $0.classify(offset: byteOffset)
217+
report(range: range)
218+
}
219+
// Layout node text.
220+
let layoutNodeTextLength = child.byteLength - child.leadingTriviaByteLength - child.trailingTriviaByteLength
221+
let range = SyntaxClassifiedRange(
222+
kind: classification.classification,
223+
range: ByteSourceRange(
224+
offset: byteOffset,
225+
length: layoutNodeTextLength
226+
)
227+
)
228+
report(range: range)
229+
byteOffset += layoutNodeTextLength
230+
231+
// Trailing trivia.
232+
if let triviaPieces = child.trailingTriviaPieces {
233+
for triviaPiece in triviaPieces {
234+
let range = triviaPiece.classify(offset: byteOffset)
235+
report(range: range)
236+
byteOffset += triviaPiece.byteLength
237+
}
238+
}
239+
continue
240+
}
241+
205242
let result = visit(
206243
.init(
207244
node: child,

Sources/SwiftSyntax/Raw/RawSyntax.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,16 @@ extension RawSyntax {
505505
lastToken(viewMode: .sourceAccurate)?.trailingTriviaByteLength ?? 0
506506
}
507507

508+
@_spi(RawSyntax)
509+
public var leadingTriviaPieces: [RawTriviaPiece]? {
510+
firstToken(viewMode: .sourceAccurate)?.leadingRawTriviaPieces
511+
}
512+
513+
@_spi(RawSyntax)
514+
public var trailingTriviaPieces: [RawTriviaPiece]? {
515+
lastToken(viewMode: .sourceAccurate)?.trailingRawTriviaPieces
516+
}
517+
508518
/// The length of this node’s content, without the first leading and the last
509519
/// trailing trivia. Intermediate trivia inside a layout node is included in
510520
/// this.

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)