Skip to content

Commit 6e63212

Browse files
committed
Validate ClangWorkspaceSettings before preferring it over unknown WorkspaceSettingsChanges
Basically everything will be decoded as ClangWorkspaceSettings, even if it has nothing to do wit hit, yielding for example: ``` clangd(LanguageServerProtocol.ClangWorkspaceSettings(compilationDatabasePath: nil, compilationDatabaseChanges: nil)) ``` This breaks at least my case of using WorkspaceSettingsChange. This patch adds a - maybe bandaid - fix for that. Furthermore I removed one Test, as it - AFAIU - broke the invariant that none of the attributes of ClangWorkspaceSettings may be nil.
1 parent 6cff70a commit 6e63212

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

Sources/LanguageServerProtocol/SupportTypes/WorkspaceSettings.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ public enum WorkspaceSettingsChange: Codable, Hashable {
1919
case unknown(LSPAny)
2020

2121
public init(from decoder: Decoder) throws {
22-
// FIXME: doing trial deserialization only works if we have at least one non-optional unique
23-
// key, which we don't yet. For now, assume that if we add another kind of workspace settings
24-
// it will rectify this issue.
25-
if let settings = try? ClangWorkspaceSettings(from: decoder) {
22+
if let settings = try? ClangWorkspaceSettings(from: decoder), settings.isValid {
2623
self = .clangd(settings)
2724
} else {
2825
let settings = try LSPAny(from: decoder)
@@ -59,6 +56,13 @@ public struct ClangWorkspaceSettings: Codable, Hashable {
5956
self.compilationDatabasePath = compilationDatabasePath
6057
self.compilationDatabaseChanges = compilationDatabaseChanges
6158
}
59+
60+
var isValid: Bool {
61+
switch (compilationDatabasePath, compilationDatabaseChanges) {
62+
case (nil, .some), (.some, nil): return true
63+
default: return false
64+
}
65+
}
6266
}
6367

6468
/// A single compile command for use in a clangd workspace settings.

Tests/LanguageServerProtocolTests/CodingTests.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,6 @@ final class CodingTests: XCTestCase {
173173
"""
174174
)
175175

176-
checkCoding(
177-
WorkspaceSettingsChange.clangd(ClangWorkspaceSettings(compilationDatabasePath: nil)),
178-
json: "{\n\n}"
179-
)
180176
checkCoding(
181177
WorkspaceSettingsChange.clangd(ClangWorkspaceSettings(compilationDatabasePath: "foo")),
182178
json: """
@@ -186,14 +182,13 @@ final class CodingTests: XCTestCase {
186182
"""
187183
)
188184

189-
// FIXME: should probably be "unknown"; see comment in WorkspaceSettingsChange decoder.
190185
checkDecoding(
191186
json: """
192187
{
193188
"hi": "there"
194189
}
195190
""",
196-
expected: WorkspaceSettingsChange.clangd(ClangWorkspaceSettings(compilationDatabasePath: nil))
191+
expected: WorkspaceSettingsChange.unknown(LSPAny.dictionary(["hi": LSPAny.string("there")]))
197192
)
198193

199194
// experimental can be anything

0 commit comments

Comments
 (0)