Skip to content

Commit 8b57352

Browse files
authored
Send didSave notifications to clangd if it supports it (#280)
* Send `didSave` notifications to clangd if it supports it - Minor update to the LSP protocol for `didSave` and `colorProvider` to be more in line with the spec - Only send color requests over to clangd if it supports it Rationale for this change: clangd recently implemented didSave support, it is used to rebuild preambles when the user edits a header file referenced by an open file. Change-Id: Ie6e2198bdeccb9d1b4083806c254475baadc2d2b
1 parent cf19900 commit 8b57352

File tree

8 files changed

+28
-18
lines changed

8 files changed

+28
-18
lines changed

Sources/LanguageServerProtocol/Requests/ColorPresentationRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/// - Returns: A list of color presentations for the given document.
2424
public struct ColorPresentationRequest: TextDocumentRequest, Hashable {
2525
public static let method: String = "textDocument/colorPresentation"
26-
public typealias Response = [ColorPresentation]?
26+
public typealias Response = [ColorPresentation]
2727

2828
/// The document to request presentations for.
2929
public var textDocument: TextDocumentIdentifier

Sources/LanguageServerProtocol/Requests/DocumentColorRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/// - Returns: A list of color references for the given document.
2222
public struct DocumentColorRequest: TextDocumentRequest, Hashable {
2323
public static let method: String = "textDocument/documentColor"
24-
public typealias Response = [ColorInformation]?
24+
public typealias Response = [ColorInformation]
2525

2626
/// The document in which to search for color references.
2727
public var textDocument: TextDocumentIdentifier

Sources/LanguageServerProtocol/SupportTypes/ServerCapabilities.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,21 @@ public struct TextDocumentSyncOptions: Codable, Hashable {
189189
public struct SaveOptions: Codable, Hashable {
190190

191191
/// Whether the client should include the file content in save notifications.
192-
public var includeText: Bool
192+
public var includeText: Bool?
193193

194-
public init(includeText: Bool = false) {
194+
public init(includeText: Bool? = nil) {
195195
self.includeText = includeText
196196
}
197197
}
198198

199199
/// Whether save notifications should be sent to the server.
200-
public var save: SaveOptions?
200+
public var save: ValueOrBool<SaveOptions>?
201201

202202
public init(openClose: Bool? = true,
203203
change: TextDocumentSyncKind? = .incremental,
204204
willSave: Bool? = true,
205205
willSaveWaitUntil: Bool? = false,
206-
save: SaveOptions? = SaveOptions()) {
206+
save: ValueOrBool<SaveOptions>? = .value(SaveOptions(includeText: false))) {
207207
self.openClose = openClose
208208
self.change = change
209209
self.willSave = willSave
@@ -218,7 +218,7 @@ public struct TextDocumentSyncOptions: Codable, Hashable {
218218
self.change = try container.decodeIfPresent(TextDocumentSyncKind.self, forKey: .change)
219219
self.willSave = try container.decodeIfPresent(Bool.self, forKey: .willSave)
220220
self.willSaveWaitUntil = try container.decodeIfPresent(Bool.self, forKey: .willSaveWaitUntil)
221-
self.save = try container.decodeIfPresent(SaveOptions.self, forKey: .save)
221+
self.save = try container.decodeIfPresent(ValueOrBool<SaveOptions>.self, forKey: .save)
222222
return
223223
} catch {}
224224
do {

Sources/SourceKit/SourceKitServer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public final class SourceKitServer: LanguageServer {
9393
registerToolchainTextDocumentRequest(SourceKitServer.documentSymbolHighlight, nil)
9494
registerToolchainTextDocumentRequest(SourceKitServer.foldingRange, nil)
9595
registerToolchainTextDocumentRequest(SourceKitServer.documentSymbol, nil)
96-
registerToolchainTextDocumentRequest(SourceKitServer.documentColor, nil)
97-
registerToolchainTextDocumentRequest(SourceKitServer.colorPresentation, nil)
96+
registerToolchainTextDocumentRequest(SourceKitServer.documentColor, [])
97+
registerToolchainTextDocumentRequest(SourceKitServer.colorPresentation, [])
9898
registerToolchainTextDocumentRequest(SourceKitServer.codeAction, nil)
9999
}
100100

@@ -455,7 +455,7 @@ extension SourceKitServer {
455455
change: .incremental,
456456
willSave: true,
457457
willSaveWaitUntil: false,
458-
save: TextDocumentSyncOptions.SaveOptions(includeText: false)
458+
save: .value(TextDocumentSyncOptions.SaveOptions(includeText: false))
459459
),
460460
hoverProvider: true,
461461
completionProvider: CompletionOptions(

Sources/SourceKit/clangd/ClangLanguageServer.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ extension ClangLanguageServerShim {
101101
}
102102

103103
public func didSaveDocument(_ note: DidSaveTextDocumentNotification) {
104-
104+
if capabilities?.textDocumentSync?.save?.isSupported == true {
105+
clangd.send(note)
106+
}
105107
}
106108

107109
// MARK: - Build System Integration
@@ -167,11 +169,19 @@ extension ClangLanguageServerShim {
167169
}
168170

169171
func documentColor(_ req: Request<DocumentColorRequest>) {
170-
forwardRequest(req, to: clangd)
172+
if capabilities?.colorProvider?.isSupported == true {
173+
forwardRequest(req, to: clangd)
174+
} else {
175+
req.reply(.success([]))
176+
}
171177
}
172178

173179
func colorPresentation(_ req: Request<ColorPresentationRequest>) {
174-
forwardRequest(req, to: clangd)
180+
if capabilities?.colorProvider?.isSupported == true {
181+
forwardRequest(req, to: clangd)
182+
} else {
183+
req.reply(.success([]))
184+
}
175185
}
176186

177187
func codeAction(_ req: Request<CodeActionRequest>) {

Sources/SourceKit/sourcekitd/SwiftLanguageServer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ extension SwiftLanguageServer {
206206
change: .incremental,
207207
willSave: true,
208208
willSaveWaitUntil: false,
209-
save: TextDocumentSyncOptions.SaveOptions(includeText: false)),
209+
save: .value(TextDocumentSyncOptions.SaveOptions(includeText: false))),
210210
hoverProvider: true,
211211
completionProvider: CompletionOptions(
212212
resolveProvider: false,
@@ -758,7 +758,7 @@ extension SwiftLanguageServer {
758758
queue.async {
759759
guard let snapshot = self.documentManager.latestSnapshot(req.params.textDocument.uri) else {
760760
log("failed to find snapshot for url \(req.params.textDocument.uri)")
761-
req.reply(nil)
761+
req.reply([])
762762
return
763763
}
764764

Tests/LanguageServerProtocolJSONRPCTests/CodingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ final class CodingTests: XCTestCase {
7676
change: .incremental,
7777
willSave: true,
7878
willSaveWaitUntil: false,
79-
save: TextDocumentSyncOptions.SaveOptions(includeText: false)),
79+
save: .value(TextDocumentSyncOptions.SaveOptions(includeText: false))),
8080
completionProvider: CompletionOptions(
8181
resolveProvider: false,
8282
triggerCharacters: ["."]))), id: .number(2), json: """

Tests/SourceKitTests/DocumentColorTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ final class DocumentColorTests: XCTestCase {
5252
text: text)))
5353

5454
let request = DocumentColorRequest(textDocument: TextDocumentIdentifier(url))
55-
return try! sk.sendSync(request)!
55+
return try! sk.sendSync(request)
5656
}
5757

5858
func performColorPresentationRequest(text: String, color: Color, range: Range<Position>) -> [ColorPresentation] {
@@ -68,7 +68,7 @@ final class DocumentColorTests: XCTestCase {
6868
textDocument: TextDocumentIdentifier(url),
6969
color: color,
7070
range: range)
71-
return try! sk.sendSync(request)!
71+
return try! sk.sendSync(request)
7272
}
7373

7474
func testEmptyText() {

0 commit comments

Comments
 (0)