@@ -16,91 +16,93 @@ import SourceKitD
16
16
17
17
/// A wrapper around an array of syntax highlighting tokens.
18
18
public struct SyntaxHighlightingTokens {
19
- public var tokens : [ SyntaxHighlightingToken ]
20
-
21
- public init ( tokens: [ SyntaxHighlightingToken ] ) {
22
- self . tokens = tokens
23
- }
24
-
25
- /// The LSP representation of syntax highlighting tokens. Note that this
26
- /// requires the tokens in this array to be sorted.
27
- public var lspEncoded : [ UInt32 ] {
28
- var previous = Position ( line: 0 , utf16index: 0 )
29
- var rawTokens : [ UInt32 ] = [ ]
30
- rawTokens. reserveCapacity ( count * 5 )
31
-
32
- for token in self . tokens {
33
- let lineDelta = token. start. line - previous. line
34
- let charDelta =
35
- token. start. utf16index - (
36
- // The character delta is relative to the previous token's start
37
- // only if the token is on the previous token's line.
38
- previous. line == token. start. line ? previous. utf16index : 0 )
39
-
40
- // We assert that the tokens are actually sorted
41
- assert ( lineDelta >= 0 )
42
- assert ( charDelta >= 0 )
43
-
44
- previous = token. start
45
- rawTokens += [
46
- UInt32 ( lineDelta) ,
47
- UInt32 ( charDelta) ,
48
- UInt32 ( token. utf16length) ,
49
- token. kind. tokenType,
50
- token. modifiers. rawValue,
51
- ]
52
- }
53
-
54
- return rawTokens
19
+ public var tokens : [ SyntaxHighlightingToken ]
20
+
21
+ public init ( tokens: [ SyntaxHighlightingToken ] ) {
22
+ self . tokens = tokens
23
+ }
24
+
25
+ /// The LSP representation of syntax highlighting tokens. Note that this
26
+ /// requires the tokens in this array to be sorted.
27
+ public var lspEncoded : [ UInt32 ] {
28
+ var previous = Position ( line: 0 , utf16index: 0 )
29
+ var rawTokens : [ UInt32 ] = [ ]
30
+ rawTokens. reserveCapacity ( count * 5 )
31
+
32
+ for token in self . tokens {
33
+ let lineDelta = token. start. line - previous. line
34
+ let charDelta =
35
+ token. start. utf16index - (
36
+ // The character delta is relative to the previous token's start
37
+ // only if the token is on the previous token's line.
38
+ previous. line == token. start. line ? previous. utf16index : 0 )
39
+
40
+ // We assert that the tokens are actually sorted
41
+ assert ( lineDelta >= 0 )
42
+ assert ( charDelta >= 0 )
43
+
44
+ previous = token. start
45
+ rawTokens += [
46
+ UInt32 ( lineDelta) ,
47
+ UInt32 ( charDelta) ,
48
+ UInt32 ( token. utf16length) ,
49
+ token. kind. tokenType,
50
+ token. modifiers. rawValue,
51
+ ]
55
52
}
56
53
57
- /// Merges the tokens in this array into a new token array,
58
- /// preferring the given array's tokens if duplicate ranges are
59
- /// found.
60
- public func mergingTokens( with other: SyntaxHighlightingTokens ) -> SyntaxHighlightingTokens {
61
- let otherRanges = Set ( other. tokens. map ( \. range) )
62
- return SyntaxHighlightingTokens ( tokens: tokens. filter { !otherRanges. contains ( $0. range) } + other. tokens)
63
- }
64
-
65
- /// Sorts the tokens in this array by their start position.
66
- public func sorted( _ areInIncreasingOrder: ( SyntaxHighlightingToken , SyntaxHighlightingToken ) -> Bool ) -> SyntaxHighlightingTokens {
67
- SyntaxHighlightingTokens ( tokens: tokens. sorted ( by: areInIncreasingOrder) )
68
- }
54
+ return rawTokens
55
+ }
56
+
57
+ /// Merges the tokens in this array into a new token array,
58
+ /// preferring the given array's tokens if duplicate ranges are
59
+ /// found.
60
+ public func mergingTokens( with other: SyntaxHighlightingTokens ) -> SyntaxHighlightingTokens {
61
+ let otherRanges = Set ( other. tokens. map ( \. range) )
62
+ return SyntaxHighlightingTokens ( tokens: tokens. filter { !otherRanges. contains ( $0. range) } + other. tokens)
63
+ }
64
+
65
+ /// Sorts the tokens in this array by their start position.
66
+ public func sorted( _ areInIncreasingOrder: ( SyntaxHighlightingToken , SyntaxHighlightingToken ) -> Bool )
67
+ -> SyntaxHighlightingTokens
68
+ {
69
+ SyntaxHighlightingTokens ( tokens: tokens. sorted ( by: areInIncreasingOrder) )
70
+ }
69
71
}
70
72
71
73
extension SyntaxHighlightingTokens {
72
- /// Decodes the LSP representation of syntax highlighting tokens
73
- public init ( lspEncodedTokens rawTokens: [ UInt32 ] ) {
74
- self . init ( tokens: [ ] )
75
- assert ( rawTokens. count. isMultiple ( of: 5 ) )
76
- reserveCapacity ( rawTokens. count / 5 )
77
-
78
- var current = Position ( line: 0 , utf16index: 0 )
79
-
80
- for i in stride ( from: 0 , to: rawTokens. count, by: 5 ) {
81
- let lineDelta = Int ( rawTokens [ i] )
82
- let charDelta = Int ( rawTokens [ i + 1 ] )
83
- let length = Int ( rawTokens [ i + 2 ] )
84
- let rawKind = rawTokens [ i + 3 ]
85
- let rawModifiers = rawTokens [ i + 4 ]
86
-
87
- current. line += lineDelta
88
-
89
- if lineDelta == 0 {
90
- current. utf16index += charDelta
91
- } else {
92
- current. utf16index = charDelta
93
- }
94
-
95
- let kind = SemanticTokenTypes . all [ Int ( rawKind) ]
96
- let modifiers = SemanticTokenModifiers ( rawValue: rawModifiers)
97
-
98
- self . tokens += SyntaxHighlightingToken (
99
- start: current,
100
- utf16length: length,
101
- kind: kind,
102
- modifiers: modifiers
103
- )
104
- }
74
+ /// Decodes the LSP representation of syntax highlighting tokens
75
+ public init ( lspEncodedTokens rawTokens: [ UInt32 ] ) {
76
+ self . init ( tokens: [ ] )
77
+ assert ( rawTokens. count. isMultiple ( of: 5 ) )
78
+ reserveCapacity ( rawTokens. count / 5 )
79
+
80
+ var current = Position ( line: 0 , utf16index: 0 )
81
+
82
+ for i in stride ( from: 0 , to: rawTokens. count, by: 5 ) {
83
+ let lineDelta = Int ( rawTokens [ i] )
84
+ let charDelta = Int ( rawTokens [ i + 1 ] )
85
+ let length = Int ( rawTokens [ i + 2 ] )
86
+ let rawKind = rawTokens [ i + 3 ]
87
+ let rawModifiers = rawTokens [ i + 4 ]
88
+
89
+ current. line += lineDelta
90
+
91
+ if lineDelta == 0 {
92
+ current. utf16index += charDelta
93
+ } else {
94
+ current. utf16index = charDelta
95
+ }
96
+
97
+ let kind = SemanticTokenTypes . all [ Int ( rawKind) ]
98
+ let modifiers = SemanticTokenModifiers ( rawValue: rawModifiers)
99
+
100
+ self . tokens += SyntaxHighlightingToken (
101
+ start: current,
102
+ utf16length: length,
103
+ kind: kind,
104
+ modifiers: modifiers
105
+ )
105
106
}
106
- }
107
+ }
108
+ }
0 commit comments