@@ -14,17 +14,8 @@ import Foundation
14
14
15
15
/// Protocol for capability registration options, which must be encodable to
16
16
/// `LSPAny` so they can be included in a `Registration`.
17
- public protocol RegistrationOptions : Hashable {
18
- func encodeIntoLSPAny( dict: inout [ String : LSPAny ] )
19
- }
17
+ public protocol RegistrationOptions : Hashable , LSPAnyCodable {
20
18
21
- fileprivate func encode( strings: [ String ] ) -> LSPAny {
22
- var values = [ LSPAny] ( )
23
- values. reserveCapacity ( strings. count)
24
- for str in strings {
25
- values. append ( . string( str) )
26
- }
27
- return . array( values)
28
19
}
29
20
30
21
/// General text document registration options.
@@ -37,22 +28,20 @@ public struct TextDocumentRegistrationOptions: RegistrationOptions, Hashable {
37
28
self . documentSelector = documentSelector
38
29
}
39
30
40
- public init ( fromLSPAny value: LSPAny ) {
41
- guard case let . dictionary( dict) = value else {
42
- return
43
- }
44
-
31
+ public init ? ( fromLSPDictionary dictionary: [ String : LSPAny ] ) {
45
32
var documentSelector : DocumentSelector ?
46
- if let value = dict [ " documentSelector " ] {
33
+
34
+ if let value = dictionary [ " documentSelector " ] {
47
35
documentSelector = DocumentSelector ( fromLSPArray: value)
48
36
}
49
37
50
38
self . documentSelector = documentSelector
51
39
}
52
40
53
- public func encodeIntoLSPAny( dict: inout [ String : LSPAny ] ) {
54
- guard let documentSelector = documentSelector else { return }
55
- dict [ " documentSelector " ] = documentSelector. encodeToLSPAny ( )
41
+ public func encodeToLSPAny( ) -> LSPAny {
42
+ guard let documentSelector = documentSelector else { return . null }
43
+
44
+ return . dictionary( [ " documentSelector " : documentSelector. encodeToLSPAny ( ) ] )
56
45
}
57
46
}
58
47
@@ -72,18 +61,32 @@ public struct CompletionRegistrationOptions: RegistrationOptions, TextDocumentRe
72
61
self . completionOptions = completionOptions
73
62
}
74
63
75
- public func encodeIntoLSPAny( dict: inout [ String : LSPAny ] ) {
76
- textDocumentRegistrationOptions. encodeIntoLSPAny ( dict: & dict)
64
+ public init ? ( fromLSPDictionary dictionary: [ String : LSPAny ] ) {
65
+ guard let completionOptions = CompletionOptions ( fromLSPDictionary: dictionary) else {
66
+ return nil
67
+ }
77
68
78
- if let resolveProvider = completionOptions. resolveProvider {
79
- dict [ " resolveProvider " ] = . bool( resolveProvider)
69
+ self . completionOptions = completionOptions
70
+
71
+ guard let textDocumentRegistrationOptions = TextDocumentRegistrationOptions ( fromLSPDictionary: dictionary) else {
72
+ return nil
80
73
}
81
- if let triggerCharacters = completionOptions. triggerCharacters {
82
- dict [ " triggerCharacters " ] = encode ( strings: triggerCharacters)
74
+
75
+ self . textDocumentRegistrationOptions = textDocumentRegistrationOptions
76
+ }
77
+
78
+ public func encodeToLSPAny( ) -> LSPAny {
79
+ var dict : [ String : LSPAny ] = [ : ]
80
+
81
+ if case . dictionary( let dictionary) = completionOptions. encodeToLSPAny ( ) {
82
+ dict. merge ( dictionary) { ( current, _) in current }
83
83
}
84
- if let allCommitCharacters = completionOptions. allCommitCharacters {
85
- dict [ " allCommitCharacters " ] = encode ( strings: allCommitCharacters)
84
+
85
+ if case . dictionary( let dictionary) = textDocumentRegistrationOptions. encodeToLSPAny ( ) {
86
+ dict. merge ( dictionary) { ( current, _) in current }
86
87
}
88
+
89
+ return . dictionary( dict)
87
90
}
88
91
}
89
92
@@ -98,8 +101,19 @@ public struct FoldingRangeRegistrationOptions: RegistrationOptions, TextDocument
98
101
self . foldingRangeOptions = foldingRangeOptions
99
102
}
100
103
101
- public func encodeIntoLSPAny( dict: inout [ String : LSPAny ] ) {
102
- textDocumentRegistrationOptions. encodeIntoLSPAny ( dict: & dict)
104
+ public init ? ( fromLSPDictionary dictionary: [ String : LSPAny ] ) {
105
+ guard let textDocumentRegistrationOptions = TextDocumentRegistrationOptions ( fromLSPDictionary: dictionary) else {
106
+ return nil
107
+ }
108
+
109
+ self . textDocumentRegistrationOptions = textDocumentRegistrationOptions
110
+
111
+ /// Currently empty in the spec.
112
+ self . foldingRangeOptions = FoldingRangeOptions ( )
113
+ }
114
+
115
+ public func encodeToLSPAny( ) -> LSPAny {
116
+ textDocumentRegistrationOptions. encodeToLSPAny ( )
103
117
// foldingRangeOptions is currently empty.
104
118
}
105
119
}
@@ -119,108 +133,32 @@ public struct SemanticTokensRegistrationOptions: RegistrationOptions, TextDocume
119
133
self . semanticTokenOptions = semanticTokenOptions
120
134
}
121
135
122
- public init ? ( fromLSPAny value : LSPAny ) {
123
- guard case let . dictionary ( dict ) = value else {
136
+ public init ? ( fromLSPDictionary dictionary : [ String : LSPAny ] ) {
137
+ guard let textDocumentRegistrationOptions = TextDocumentRegistrationOptions ( fromLSPDictionary : dictionary ) else {
124
138
return nil
125
139
}
126
140
127
- textDocumentRegistrationOptions = TextDocumentRegistrationOptions ( fromLSPAny: value)
128
-
129
- var range : ValueOrBool < SemanticTokensOptions . SemanticTokensRangeOptions > ?
130
- if let rangeLSPAny = dict [ " range " ] {
131
- switch rangeLSPAny {
132
- case let . bool( value) :
133
- range = . bool( value)
134
- case . dictionary:
135
- range = . value( SemanticTokensOptions . SemanticTokensRangeOptions ( ) )
136
- default :
137
- break
138
- }
139
- }
140
-
141
- var full : ValueOrBool < SemanticTokensOptions . SemanticTokensFullOptions > ?
142
- if let fullLSPAny = dict [ " full " ] {
143
- switch fullLSPAny {
144
- case let . bool( value) :
145
- full = . bool( value)
146
- case let . dictionary( optionsDict) :
147
- var delta : Bool ?
148
- if let value = optionsDict [ " delta " ] ,
149
- case let . bool( deltaValue) = value
150
- {
151
- delta = deltaValue
152
- }
153
-
154
- full = . value( SemanticTokensOptions . SemanticTokensFullOptions ( delta: delta) )
155
- default :
156
- break
157
- }
158
- }
141
+ self . textDocumentRegistrationOptions = textDocumentRegistrationOptions
159
142
160
- guard case let . dictionary ( legendDict ) = dict [ " legend " ] else {
143
+ guard let semanticTokenOptions = SemanticTokensOptions ( fromLSPDictionary : dictionary ) else {
161
144
return nil
162
145
}
163
146
164
- var tokenTypes : [ String ] = [ ]
165
- if case let . array( types) = legendDict [ " tokenTypes " ] {
166
- tokenTypes = types. compactMap {
167
- if case let . string( string) = $0 {
168
- return string
169
- }
170
- return nil
171
- }
172
- }
173
-
174
- var tokenModifiers : [ String ] = [ ]
175
- if case let . array( types) = legendDict [ " tokenModifiers " ] {
176
- tokenModifiers = types. compactMap {
177
- if case let . string( string) = $0 {
178
- return string
179
- }
180
- return nil
181
- }
182
- }
183
-
184
- let legend = SemanticTokensLegend (
185
- tokenTypes: tokenTypes,
186
- tokenModifiers: tokenModifiers
187
- )
188
-
189
- semanticTokenOptions = SemanticTokensOptions (
190
- legend: legend,
191
- range: range,
192
- full: full
193
- )
147
+ self . semanticTokenOptions = semanticTokenOptions
194
148
}
195
149
196
- public func encodeIntoLSPAny( dict: inout [ String : LSPAny ] ) {
197
- textDocumentRegistrationOptions. encodeIntoLSPAny ( dict: & dict)
198
- let legend = semanticTokenOptions. legend
199
- dict [ " legend " ] = . dictionary( [
200
- " tokenTypes " : encode ( strings: legend. tokenTypes) ,
201
- " tokenModifiers " : encode ( strings: legend. tokenModifiers) ,
202
- ] )
203
- if let range = semanticTokenOptions. range {
204
- let encodedRange : LSPAny
205
- switch range {
206
- case . bool( let value) : encodedRange = . bool( value)
207
- case . value( _) : encodedRange = . dictionary( [ : ] )
208
- }
209
- dict [ " range " ] = encodedRange
150
+ public func encodeToLSPAny( ) -> LSPAny {
151
+ var dict : [ String : LSPAny ] = [ : ]
152
+
153
+ if case . dictionary( let dictionary) = textDocumentRegistrationOptions. encodeToLSPAny ( ) {
154
+ dict. merge ( dictionary) { ( current, _) in current }
210
155
}
211
- if let full = semanticTokenOptions. full {
212
- let encodedFull : LSPAny
213
- switch full {
214
- case . bool( let value) : encodedFull = . bool( value)
215
- case . value( let fullOptions) :
216
- var encodedOptions : [ String : LSPAny ] = [ : ]
217
- if let delta = fullOptions. delta {
218
- encodedOptions [ " delta " ] = . bool( delta)
219
- }
220
- encodedFull = . dictionary( encodedOptions)
221
- }
222
- dict [ " full " ] = encodedFull
156
+
157
+ if case . dictionary( let dictionary) = semanticTokenOptions. encodeToLSPAny ( ) {
158
+ dict. merge ( dictionary) { ( current, _) in current }
223
159
}
160
+
161
+ return . dictionary( dict)
224
162
}
225
163
}
226
164
@@ -236,11 +174,32 @@ public struct InlayHintRegistrationOptions: RegistrationOptions, TextDocumentReg
236
174
self . inlayHintOptions = inlayHintOptions
237
175
}
238
176
239
- public func encodeIntoLSPAny( dict: inout [ String : LSPAny ] ) {
240
- textDocumentRegistrationOptions. encodeIntoLSPAny ( dict: & dict)
177
+ public init ? ( fromLSPDictionary dictionary: [ String : LSPAny ] ) {
178
+ self . inlayHintOptions = InlayHintOptions ( )
179
+
180
+ if case . bool( let resolveProvider) = dictionary [ " resolveProvider " ] {
181
+ self . inlayHintOptions. resolveProvider = resolveProvider
182
+ }
183
+
184
+ guard let textDocumentRegistrationOptions = TextDocumentRegistrationOptions ( fromLSPDictionary: dictionary) else {
185
+ return nil
186
+ }
187
+
188
+ self . textDocumentRegistrationOptions = textDocumentRegistrationOptions
189
+ }
190
+
191
+ public func encodeToLSPAny( ) -> LSPAny {
192
+ var dict : [ String : LSPAny ] = [ : ]
193
+
241
194
if let resolveProvider = inlayHintOptions. resolveProvider {
242
195
dict [ " resolveProvider " ] = . bool( resolveProvider)
243
196
}
197
+
198
+ if case . dictionary( let dictionary) = textDocumentRegistrationOptions. encodeToLSPAny ( ) {
199
+ dict. merge ( dictionary) { ( current, _) in current }
200
+ }
201
+
202
+ return . dictionary( dict)
244
203
}
245
204
}
246
205
@@ -257,9 +216,29 @@ public struct DiagnosticRegistrationOptions: RegistrationOptions, TextDocumentRe
257
216
self . diagnosticOptions = diagnosticOptions
258
217
}
259
218
260
- public func encodeIntoLSPAny( dict: inout [ String : LSPAny ] ) {
261
- textDocumentRegistrationOptions. encodeIntoLSPAny ( dict: & dict)
262
- diagnosticOptions. encodeIntoLSPAny ( dict: & dict)
219
+ public init ? ( fromLSPDictionary dictionary: [ String : LSPAny ] ) {
220
+ guard let textDocumentRegistrationOptions = TextDocumentRegistrationOptions ( fromLSPDictionary: dictionary) else {
221
+ return nil
222
+ }
223
+
224
+ self . textDocumentRegistrationOptions = textDocumentRegistrationOptions
225
+
226
+ guard let diagnosticOptions = DiagnosticOptions ( fromLSPDictionary: dictionary) else {
227
+ return nil
228
+ }
229
+ self . diagnosticOptions = diagnosticOptions
230
+ }
231
+
232
+ public func encodeToLSPAny( ) -> LSPAny {
233
+ var dict : [ String : LSPAny ] = [ : ]
234
+ if case . dictionary( let dictionary) = textDocumentRegistrationOptions. encodeToLSPAny ( ) {
235
+ dict. merge ( dictionary) { ( current, _) in current }
236
+ }
237
+
238
+ if case . dictionary( let dictionary) = diagnosticOptions. encodeToLSPAny ( ) {
239
+ dict. merge ( dictionary) { ( current, _) in current }
240
+ }
241
+ return . dictionary( dict)
263
242
}
264
243
}
265
244
@@ -272,8 +251,18 @@ public struct DidChangeWatchedFilesRegistrationOptions: RegistrationOptions {
272
251
self . watchers = watchers
273
252
}
274
253
275
- public func encodeIntoLSPAny( dict: inout [ String : LSPAny ] ) {
276
- dict [ " watchers " ] = watchers. encodeToLSPAny ( )
254
+ public init ? ( fromLSPDictionary dictionary: [ String : LSPAny ] ) {
255
+ guard let watchersArray = dictionary [ " watchers " ] ,
256
+ let watchers = [ FileSystemWatcher] ( fromLSPArray: watchersArray)
257
+ else {
258
+ return nil
259
+ }
260
+
261
+ self . watchers = watchers
262
+ }
263
+
264
+ public func encodeToLSPAny( ) -> LSPAny {
265
+ . dictionary( [ " watchers " : watchers. encodeToLSPAny ( ) ] )
277
266
}
278
267
}
279
268
@@ -286,7 +275,17 @@ public struct ExecuteCommandRegistrationOptions: RegistrationOptions {
286
275
self . commands = commands
287
276
}
288
277
289
- public func encodeIntoLSPAny( dict: inout [ String : LSPAny ] ) {
290
- dict [ " commands " ] = encode ( strings: commands)
278
+ public init ? ( fromLSPDictionary dictionary: [ String : LSPAny ] ) {
279
+ guard let commandsArray = dictionary [ " commands " ] ,
280
+ let commands = [ String] ( fromLSPArray: commandsArray)
281
+ else {
282
+ return nil
283
+ }
284
+
285
+ self . commands = commands
286
+ }
287
+
288
+ public func encodeToLSPAny( ) -> LSPAny {
289
+ . dictionary( [ " commands " : commands. encodeToLSPAny ( ) ] )
291
290
}
292
291
}
0 commit comments