@@ -47,146 +47,8 @@ public struct SyntaxHighlightingToken: Hashable {
47
47
self . init ( range: range, kind: kind, modifiers: modifiers)
48
48
}
49
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
- }
50
+ public typealias Kind = SemanticTokenTypes
51
+ public typealias Modifiers = SemanticTokenModifiers
190
52
}
191
53
192
54
extension Array where Element == SyntaxHighlightingToken {
@@ -214,7 +76,7 @@ extension Array where Element == SyntaxHighlightingToken {
214
76
UInt32 ( lineDelta) ,
215
77
UInt32 ( charDelta) ,
216
78
UInt32 ( token. utf16length) ,
217
- token. kind. rawValue ,
79
+ token. kind. tokenType ,
218
80
token. modifiers. rawValue,
219
81
]
220
82
}
@@ -230,3 +92,24 @@ extension Array where Element == SyntaxHighlightingToken {
230
92
return filter { !otherRanges. contains ( $0. range) } + other
231
93
}
232
94
}
95
+
96
+ extension SemanticTokenTypes {
97
+ /// **(LSP Extension)**
98
+ public static let identifier = Self ( " identifier " )
99
+
100
+ // LSP doesn’t know about actors. Display actors as classes.
101
+ public static let actor = Self ( " class " )
102
+
103
+ /// All tokens supported by sourcekit-lsp
104
+ public static let all : [ Self ] = predefined + [ . identifier, . actor ]
105
+
106
+ /// Token types are looked up by index
107
+ public var tokenType : UInt32 {
108
+ UInt32 ( Self . all. firstIndex ( of: self ) !)
109
+ }
110
+ }
111
+
112
+ extension SemanticTokenModifiers {
113
+ /// All tokens supported by sourcekit-lsp
114
+ public static let all : [ Self ] = predefined
115
+ }
0 commit comments