@@ -14,12 +14,12 @@ import LSPLogging
14
14
import LanguageServerProtocol
15
15
import SourceKitD
16
16
17
- /// A ranged token in the document used for syntax highlighting.
17
+ /// A wrapper around an array of syntax highlighting tokens .
18
18
public struct SyntaxHighlightingTokens {
19
- public var members : [ SyntaxHighlightingToken ]
19
+ public var tokens : [ SyntaxHighlightingToken ]
20
20
21
- public init ( members : [ SyntaxHighlightingToken ] ) {
22
- self . members = members
21
+ public init ( tokens : [ SyntaxHighlightingToken ] ) {
22
+ self . tokens = tokens
23
23
}
24
24
25
25
/// The LSP representation of syntax highlighting tokens. Note that this
@@ -29,26 +29,26 @@ public struct SyntaxHighlightingTokens {
29
29
var rawTokens : [ UInt32 ] = [ ]
30
30
rawTokens. reserveCapacity ( count * 5 )
31
31
32
- for token in self . members {
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
- ]
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
52
}
53
53
54
54
return rawTokens
@@ -58,12 +58,49 @@ public struct SyntaxHighlightingTokens {
58
58
/// preferring the given array's tokens if duplicate ranges are
59
59
/// found.
60
60
public func mergingTokens( with other: SyntaxHighlightingTokens ) -> SyntaxHighlightingTokens {
61
- let otherRanges = Set ( other. members . map ( \. range) )
62
- return SyntaxHighlightingTokens ( members : members . filter { !otherRanges. contains ( $0. range) } + other. members )
61
+ let otherRanges = Set ( other. tokens . map ( \. range) )
62
+ return SyntaxHighlightingTokens ( tokens : tokens . filter { !otherRanges. contains ( $0. range) } + other. tokens )
63
63
}
64
64
65
65
/// Sorts the tokens in this array by their start position.
66
66
public func sorted( _ areInIncreasingOrder: ( SyntaxHighlightingToken , SyntaxHighlightingToken ) -> Bool ) -> SyntaxHighlightingTokens {
67
- SyntaxHighlightingTokens ( members : members . sorted ( by: areInIncreasingOrder) )
67
+ SyntaxHighlightingTokens ( tokens : tokens . sorted ( by: areInIncreasingOrder) )
68
68
}
69
69
}
70
+
71
+ 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
+ }
105
+ }
106
+ }
0 commit comments