@@ -23,9 +23,9 @@ public struct SyntaxHighlightingToken: Hashable {
23
23
}
24
24
}
25
25
/// The token type.
26
- public var kind : Kind
26
+ public var kind : SemanticTokenTypes
27
27
/// Additional metadata about the token.
28
- public var modifiers : Modifiers
28
+ public var modifiers : SemanticTokenModifiers
29
29
30
30
/// The (inclusive) start position of the token.
31
31
public var start : Position { range. lowerBound }
@@ -34,159 +34,18 @@ public struct SyntaxHighlightingToken: Hashable {
34
34
/// The length of the token in UTF-16 code units.
35
35
public var utf16length : Int { end. utf16index - start. utf16index }
36
36
37
- public init ( range: Range < Position > , kind: Kind , modifiers: Modifiers = [ ] ) {
37
+ public init ( range: Range < Position > , kind: SemanticTokenTypes , modifiers: SemanticTokenModifiers = [ ] ) {
38
38
assert ( range. lowerBound. line == range. upperBound. line)
39
39
40
40
self . range = range
41
41
self . kind = kind
42
42
self . modifiers = modifiers
43
43
}
44
44
45
- public init ( start: Position , utf16length: Int , kind: Kind , modifiers: Modifiers = [ ] ) {
45
+ public init ( start: Position , utf16length: Int , kind: SemanticTokenTypes , modifiers: SemanticTokenModifiers = [ ] ) {
46
46
let range = start..< Position ( line: start. line, utf16index: start. utf16index + utf16length)
47
47
self . init ( range: range, kind: kind, modifiers: modifiers)
48
48
}
49
-
50
- /// The token type.
51
- ///
52
- /// Represented using an int to make the conversion to
53
- /// LSP tokens efficient. The order of this enum does not have to be
54
- /// stable, since we provide a `SemanticTokensLegend` during initialization.
55
- /// It is, however, important that the values are numbered from 0 due to
56
- /// the way the kinds are encoded in LSP.
57
- /// Also note that we intentionally use an enum here instead of e.g. a
58
- /// `RawRepresentable` struct, since we want to have a conversion to
59
- /// strings for known kinds and since these kinds are only provided by the
60
- /// server, i.e. there is no need to handle cases where unknown kinds
61
- /// have to be decoded.
62
- public enum Kind : UInt32 , CaseIterable , Hashable {
63
- case namespace = 0
64
- case type
65
- case actor
66
- case `class`
67
- case `enum`
68
- case interface
69
- case `struct`
70
- case typeParameter
71
- case parameter
72
- case variable
73
- case property
74
- case enumMember
75
- case event
76
- case function
77
- case method
78
- case macro
79
- case keyword
80
- case modifier
81
- case comment
82
- case string
83
- case number
84
- case regexp
85
- case `operator`
86
- case decorator
87
- /// **(LSP Extension)**
88
- case identifier
89
-
90
- /// The name of the token type used by LSP.
91
- var lspName : String {
92
- switch self {
93
- case . namespace: return " namespace "
94
- case . type: return " type "
95
- case . actor : return " class " // LSP doesn’t know about actors. Display actors as classes.
96
- case . class: return " class "
97
- case . enum: return " enum "
98
- case . interface: return " interface "
99
- case . struct: return " struct "
100
- case . typeParameter: return " typeParameter "
101
- case . parameter: return " parameter "
102
- case . variable: return " variable "
103
- case . property: return " property "
104
- case . enumMember: return " enumMember "
105
- case . event: return " event "
106
- case . function: return " function "
107
- case . method: return " method "
108
- case . macro: return " macro "
109
- case . keyword: return " keyword "
110
- case . modifier: return " modifier "
111
- case . comment: return " comment "
112
- case . string: return " string "
113
- case . number: return " number "
114
- case . regexp: return " regexp "
115
- case . operator: return " operator "
116
- case . decorator: return " decorator "
117
- case . identifier: return " identifier "
118
- }
119
- }
120
-
121
- /// **Public for testing**
122
- public var _lspName : String {
123
- lspName
124
- }
125
- }
126
-
127
- /// Additional metadata about a token.
128
- ///
129
- /// Similar to `Kind`, the raw values do not actually have
130
- /// to be stable, do note however that the bit indices should
131
- /// be numbered starting at 0 and that the ordering should
132
- /// correspond to `allModifiers`.
133
- public struct Modifiers : OptionSet , Hashable {
134
- public static let declaration = Self ( rawValue: 1 << 0 )
135
- public static let definition = Self ( rawValue: 1 << 1 )
136
- public static let readonly = Self ( rawValue: 1 << 2 )
137
- public static let `static` = Self ( rawValue: 1 << 3 )
138
- public static let deprecated = Self ( rawValue: 1 << 4 )
139
- public static let abstract = Self ( rawValue: 1 << 5 )
140
- public static let async = Self ( rawValue: 1 << 6 )
141
- public static let modification = Self ( rawValue: 1 << 7 )
142
- public static let documentation = Self ( rawValue: 1 << 8 )
143
- public static let defaultLibrary = Self ( rawValue: 1 << 9 )
144
-
145
- /// All available modifiers, in ascending order of the bit index
146
- /// they are represented with (starting at the rightmost bit).
147
- public static let allModifiers : [ Self ] = [
148
- . declaration,
149
- . definition,
150
- . readonly,
151
- . static,
152
- . deprecated,
153
- . abstract,
154
- . async,
155
- . modification,
156
- . documentation,
157
- . defaultLibrary,
158
- ]
159
-
160
- public let rawValue : UInt32
161
-
162
- /// The name of the modifier used by LSP, if this
163
- /// is a single modifier. Note that every modifier
164
- /// in `allModifiers` must have an associated `lspName`.
165
- var lspName : String ? {
166
- switch self {
167
- case . declaration: return " declaration "
168
- case . definition: return " definition "
169
- case . readonly: return " readonly "
170
- case . static: return " static "
171
- case . deprecated: return " deprecated "
172
- case . abstract: return " abstract "
173
- case . async : return " async "
174
- case . modification: return " modification "
175
- case . documentation: return " documentation "
176
- case . defaultLibrary: return " defaultLibrary "
177
- default : return nil
178
- }
179
- }
180
-
181
- /// **Public for testing**
182
- public var _lspName : String ? {
183
- lspName
184
- }
185
-
186
- public init ( rawValue: UInt32 ) {
187
- self . rawValue = rawValue
188
- }
189
- }
190
49
}
191
50
192
51
extension Array where Element == SyntaxHighlightingToken {
@@ -214,7 +73,7 @@ extension Array where Element == SyntaxHighlightingToken {
214
73
UInt32 ( lineDelta) ,
215
74
UInt32 ( charDelta) ,
216
75
UInt32 ( token. utf16length) ,
217
- token. kind. rawValue ,
76
+ token. kind. tokenType ,
218
77
token. modifiers. rawValue,
219
78
]
220
79
}
@@ -230,3 +89,24 @@ extension Array where Element == SyntaxHighlightingToken {
230
89
return filter { !otherRanges. contains ( $0. range) } + other
231
90
}
232
91
}
92
+
93
+ extension SemanticTokenTypes {
94
+ /// **(LSP Extension)**
95
+ public static let identifier = Self ( " identifier " )
96
+
97
+ // LSP doesn’t know about actors. Display actors as classes.
98
+ public static let actor = Self ( " class " )
99
+
100
+ /// All tokens supported by sourcekit-lsp
101
+ public static let all : [ Self ] = predefined + [ . identifier, . actor ]
102
+
103
+ /// Token types are looked up by index
104
+ public var tokenType : UInt32 {
105
+ UInt32 ( Self . all. firstIndex ( of: self ) !)
106
+ }
107
+ }
108
+
109
+ extension SemanticTokenModifiers {
110
+ /// All tokens supported by sourcekit-lsp
111
+ public static let all : [ Self ] = predefined
112
+ }
0 commit comments