@@ -18,8 +18,11 @@ fileprivate extension SyntaxProtocol {
1818 var curData = Syntax ( self )
1919 repeat {
2020 guard let parent = curData. parent else { break }
21- contextualClassif = SyntaxClassification . classify ( parentKind: parent. raw. kind,
22- indexInParent: curData. indexInParent, childKind: raw. kind)
21+ contextualClassif = SyntaxClassification . classify (
22+ parentKind: parent. raw. kind,
23+ indexInParent: curData. indexInParent,
24+ childKind: raw. kind
25+ )
2326 curData = parent
2427 } while contextualClassif == nil
2528 return contextualClassif
@@ -33,19 +36,21 @@ extension TokenSyntax {
3336 let relativeOffset = leadingTriviaLength. utf8Length
3437 let absoluteOffset = position. utf8Offset + relativeOffset
3538 return TokenKindAndText ( kind: rawTokenKind, text: tokenView. rawText) . classify (
36- offset: absoluteOffset, contextualClassification: contextualClassification)
39+ offset: absoluteOffset,
40+ contextualClassification: contextualClassification
41+ )
3742 }
3843}
3944
4045extension RawTriviaPiece {
4146 func classify( offset: Int ) -> SyntaxClassifiedRange {
4247 let range = ByteSourceRange ( offset: offset, length: byteLength)
4348 switch self {
44- case . lineComment: return . init( kind: . lineComment, range: range)
45- case . blockComment: return . init( kind: . blockComment, range: range)
46- case . docLineComment: return . init( kind: . docLineComment, range: range)
47- case . docBlockComment: return . init( kind: . docBlockComment, range: range)
48- default : return . init( kind: . none, range: range)
49+ case . lineComment: return . init( kind: . lineComment, range: range)
50+ case . blockComment: return . init( kind: . blockComment, range: range)
51+ case . docLineComment: return . init( kind: . docLineComment, range: range)
52+ case . docBlockComment: return . init( kind: . docBlockComment, range: range)
53+ default : return . init( kind: . none, range: range)
4954 }
5055 }
5156}
@@ -55,7 +60,8 @@ fileprivate struct TokenKindAndText {
5560 let text : SyntaxText
5661
5762 func classify(
58- offset: Int , contextualClassification: ( SyntaxClassification , Bool ) ?
63+ offset: Int ,
64+ contextualClassification: ( SyntaxClassification , Bool ) ?
5965 ) -> SyntaxClassifiedRange {
6066 let range = ByteSourceRange ( offset: offset, length: text. count)
6167
@@ -73,8 +79,9 @@ fileprivate struct TokenKindAndText {
7379 return . stringLiteral
7480 }
7581 if kind == . identifier,
76- text. hasPrefix ( " <# " ) ,
77- text. hasSuffix ( " #> " ) {
82+ text. hasPrefix ( " <# " ) ,
83+ text. hasSuffix ( " #> " )
84+ {
7885 return . editorPlaceholder
7986 }
8087 return kind. classification
@@ -114,17 +121,21 @@ private struct ClassificationVisitor {
114121 init ( node: Syntax , relativeClassificationRange: ByteSourceRange ) {
115122 let range = ByteSourceRange (
116123 offset: node. position. utf8Offset + relativeClassificationRange. offset,
117- length: relativeClassificationRange. length)
124+ length: relativeClassificationRange. length
125+ )
118126 self . targetRange = range
119127 self . classifications = [ ]
120128
121129 // `withExtendedLifetime` to make sure `SyntaxArena` for the node alive
122130 // during the visit.
123131 withExtendedLifetime ( node) {
124- _ = self . visit ( Descriptor (
125- node: node. raw,
126- byteOffset: node. position. utf8Offset,
127- contextualClassification: node. contextualClassification) )
132+ _ = self . visit (
133+ Descriptor (
134+ node: node. raw,
135+ byteOffset: node. position. utf8Offset,
136+ contextualClassification: node. contextualClassification
137+ )
138+ )
128139 }
129140 }
130141
@@ -135,15 +146,19 @@ private struct ClassificationVisitor {
135146
136147 // Merge consecutive classified ranges of the same kind.
137148 if let last = classifications. last,
138- last. kind == range. kind,
139- last. endOffset == range. offset {
149+ last. kind == range. kind,
150+ last. endOffset == range. offset
151+ {
140152 classifications [ classifications. count - 1 ] . range = ByteSourceRange (
141- offset: last. offset, length: last. length + range. length)
153+ offset: last. offset,
154+ length: last. length + range. length
155+ )
142156 return
143157 }
144158
145159 guard range. offset <= targetRange. endOffset,
146- range. endOffset >= targetRange. offset else {
160+ range. endOffset >= targetRange. offset
161+ else {
147162 return
148163 }
149164 classifications. append ( range)
@@ -186,11 +201,17 @@ private struct ClassificationVisitor {
186201 for case (let index, let child? ) in children. enumerated ( ) {
187202
188203 let classficiation = SyntaxClassification . classify (
189- parentKind: descriptor. node. kind, indexInParent: index, childKind: child. kind)
190- let result = visit ( . init(
191- node: child,
192- byteOffset: byteOffset,
193- contextualClassification: classficiation ?? descriptor. contextualClassification) )
204+ parentKind: descriptor. node. kind,
205+ indexInParent: index,
206+ childKind: child. kind
207+ )
208+ let result = visit (
209+ . init(
210+ node: child,
211+ byteOffset: byteOffset,
212+ contextualClassification: classficiation ?? descriptor. contextualClassification
213+ )
214+ )
194215 if result == . break {
195216 return . break
196217 }
0 commit comments