Skip to content

Fix decoding Array of LSPAnyCodable #1011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions Sources/LanguageServerProtocol/SupportTypes/LSPAny.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,50 @@ extension Array: LSPAnyCodable where Element: LSPAnyCodable {
guard case .array(let array) = array else {
return nil
}

var result = [Element]()
for case .dictionary(let editDict) in array {
guard let element = Element.init(fromLSPDictionary: editDict) else {
for element in array {
switch element {
case .dictionary(let dict):
if let value = Element(fromLSPDictionary: dict) {
result.append(value)
} else {
return nil
}
case .array(let value):
if let value = value as? [Element] {
result.append(contentsOf: value)
} else {
return nil
}
case .string(let value):
if let value = value as? Element {
result.append(value)
} else {
return nil
}
case .int(let value):
if let value = value as? Element {
result.append(value)
} else {
return nil
}
case .double(let value):
if let value = value as? Element {
result.append(value)
} else {
return nil
}
case .bool(let value):
if let value = value as? Element {
result.append(value)
} else {
return nil
}
case .null:
// null is not expected for non-optional Element
return nil
}
result.append(element)
}
self = result
}
Expand Down
19 changes: 15 additions & 4 deletions Tests/SourceKitLSPTests/SemanticTokensTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,22 @@ final class SemanticTokensTests: XCTestCase {

let registerCapabilityExpectation = expectation(description: "\(#function) - register semantic tokens capability")
testClient.handleNextRequest { (req: RegisterCapabilityRequest) -> VoidResponse in
XCTAssert(
req.registrations.contains { reg in
reg.method == SemanticTokensRegistrationOptions.method
}
let capabilityRegistration = req.registrations.first { reg in
reg.method == SemanticTokensRegistrationOptions.method
}

guard case .dictionary(let registerOptionsDict) = capabilityRegistration?.registerOptions,
let registerOptions = SemanticTokensRegistrationOptions(fromLSPDictionary: registerOptionsDict)
else {
XCTFail("Expected semantic tokens registration options dictionary")
return VoidResponse()
}

XCTAssertFalse(
registerOptions.semanticTokenOptions.legend.tokenTypes.isEmpty,
"Expected semantic tokens legend"
)

registerCapabilityExpectation.fulfill()
return VoidResponse()
}
Expand Down