Skip to content

Commit 6dafe87

Browse files
committed
Fix JSON encoding for TextDocumentEdit and VersionedTextDocumentIdentifier
1 parent 3dd5045 commit 6dafe87

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Sources/LanguageServerProtocol/SupportTypes/TextDocumentEdit.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,31 @@ public struct TextDocumentEdit: Hashable, Codable, Sendable {
5252
self.textDocument = textDocument
5353
self.edits = edits
5454
}
55+
56+
public enum CodingKeys: String, CodingKey {
57+
case kind
58+
case textDocument
59+
case edits
60+
}
61+
62+
public func encode(to encoder: Encoder) throws {
63+
var container = encoder.container(keyedBy: CodingKeys.self)
64+
try container.encode("textDocumentEdit", forKey: .kind)
65+
try container.encode(self.textDocument, forKey: .textDocument)
66+
try container.encode(self.edits, forKey: .edits)
67+
}
68+
69+
public init(from decoder: Decoder) throws {
70+
let container = try decoder.container(keyedBy: CodingKeys.self)
71+
let kind = try container.decode(String.self, forKey: .kind)
72+
guard kind == "textDocumentEdit" else {
73+
throw DecodingError.dataCorruptedError(
74+
forKey: .kind,
75+
in: container,
76+
debugDescription: "Kind of TextDocumentEdit is not 'textDocumentEdit'"
77+
)
78+
}
79+
self.textDocument = try container.decode(OptionalVersionedTextDocumentIdentifier.self, forKey: .textDocument)
80+
self.edits = try container.decode([Edit].self, forKey: .edits)
81+
}
5582
}

Sources/LanguageServerProtocol/SupportTypes/VersionedTextDocumentIdentifier.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,18 @@ public struct OptionalVersionedTextDocumentIdentifier: Hashable, Codable, Sendab
5353
self.uri = uri
5454
self.version = version
5555
}
56+
57+
enum CodingKeys: CodingKey {
58+
case uri
59+
case version
60+
}
61+
62+
public func encode(to encoder: any Encoder) throws {
63+
var container = encoder.container(keyedBy: CodingKeys.self)
64+
try container.encode(self.uri, forKey: .uri)
65+
66+
// Note: we use encode(_:forKey:) here instead of encodeIf(_:forKey:)
67+
// because VSCode will drop requests without the explicit 'null'.
68+
try container.encode(self.version, forKey: .version)
69+
}
5670
}

0 commit comments

Comments
 (0)