Skip to content

Commit 4a2eefc

Browse files
committed
Deserialize RegistrationOptions
1 parent 0601381 commit 4a2eefc

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

Sources/LanguageServerProtocol/SupportTypes/RegistrationOptions.swift

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ public struct TextDocumentRegistrationOptions: RegistrationOptions, Hashable {
3737
self.documentSelector = documentSelector
3838
}
3939

40+
public init(fromLSPAny value: LSPAny) {
41+
guard case let .dictionary(dict) = value else {
42+
return
43+
}
44+
45+
var documentSelector: DocumentSelector?
46+
if let value = dict["documentSelector"] {
47+
documentSelector = DocumentSelector(fromLSPArray: value)
48+
}
49+
50+
self.documentSelector = documentSelector
51+
}
52+
4053
public func encodeIntoLSPAny(dict: inout [String: LSPAny]) {
4154
guard let documentSelector = documentSelector else { return }
4255
dict["documentSelector"] = documentSelector.encodeToLSPAny()
@@ -106,6 +119,80 @@ public struct SemanticTokensRegistrationOptions: RegistrationOptions, TextDocume
106119
self.semanticTokenOptions = semanticTokenOptions
107120
}
108121

122+
public init?(fromLSPAny value: LSPAny) {
123+
guard case let .dictionary(dict) = value else {
124+
return nil
125+
}
126+
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+
}
159+
160+
guard case let .dictionary(legendDict) = dict["legend"] else {
161+
return nil
162+
}
163+
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+
)
194+
}
195+
109196
public func encodeIntoLSPAny(dict: inout [String: LSPAny]) {
110197
textDocumentRegistrationOptions.encodeIntoLSPAny(dict: &dict)
111198
let legend = semanticTokenOptions.legend

0 commit comments

Comments
 (0)