Skip to content

Send didSave notifications to clangd if it supports it #280

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 2 commits into from
Jun 3, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// - Returns: A list of color presentations for the given document.
public struct ColorPresentationRequest: TextDocumentRequest, Hashable {
public static let method: String = "textDocument/colorPresentation"
public typealias Response = [ColorPresentation]?
public typealias Response = [ColorPresentation]

/// The document to request presentations for.
public var textDocument: TextDocumentIdentifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// - Returns: A list of color references for the given document.
public struct DocumentColorRequest: TextDocumentRequest, Hashable {
public static let method: String = "textDocument/documentColor"
public typealias Response = [ColorInformation]?
public typealias Response = [ColorInformation]

/// The document in which to search for color references.
public var textDocument: TextDocumentIdentifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,21 @@ public struct TextDocumentSyncOptions: Codable, Hashable {
public struct SaveOptions: Codable, Hashable {

/// Whether the client should include the file content in save notifications.
public var includeText: Bool
public var includeText: Bool?

public init(includeText: Bool = false) {
public init(includeText: Bool? = nil) {
self.includeText = includeText
}
}

/// Whether save notifications should be sent to the server.
public var save: SaveOptions?
public var save: ValueOrBool<SaveOptions>?

public init(openClose: Bool? = true,
change: TextDocumentSyncKind? = .incremental,
willSave: Bool? = true,
willSaveWaitUntil: Bool? = false,
save: SaveOptions? = SaveOptions()) {
save: ValueOrBool<SaveOptions>? = .value(SaveOptions(includeText: false))) {
self.openClose = openClose
self.change = change
self.willSave = willSave
Expand All @@ -218,7 +218,7 @@ public struct TextDocumentSyncOptions: Codable, Hashable {
self.change = try container.decodeIfPresent(TextDocumentSyncKind.self, forKey: .change)
self.willSave = try container.decodeIfPresent(Bool.self, forKey: .willSave)
self.willSaveWaitUntil = try container.decodeIfPresent(Bool.self, forKey: .willSaveWaitUntil)
self.save = try container.decodeIfPresent(SaveOptions.self, forKey: .save)
self.save = try container.decodeIfPresent(ValueOrBool<SaveOptions>.self, forKey: .save)
return
} catch {}
do {
Expand Down
6 changes: 3 additions & 3 deletions Sources/SourceKit/SourceKitServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public final class SourceKitServer: LanguageServer {
registerToolchainTextDocumentRequest(SourceKitServer.documentSymbolHighlight, nil)
registerToolchainTextDocumentRequest(SourceKitServer.foldingRange, nil)
registerToolchainTextDocumentRequest(SourceKitServer.documentSymbol, nil)
registerToolchainTextDocumentRequest(SourceKitServer.documentColor, nil)
registerToolchainTextDocumentRequest(SourceKitServer.colorPresentation, nil)
registerToolchainTextDocumentRequest(SourceKitServer.documentColor, [])
registerToolchainTextDocumentRequest(SourceKitServer.colorPresentation, [])
registerToolchainTextDocumentRequest(SourceKitServer.codeAction, nil)
}

Expand Down Expand Up @@ -455,7 +455,7 @@ extension SourceKitServer {
change: .incremental,
willSave: true,
willSaveWaitUntil: false,
save: TextDocumentSyncOptions.SaveOptions(includeText: false)
save: .value(TextDocumentSyncOptions.SaveOptions(includeText: false))
),
hoverProvider: true,
completionProvider: CompletionOptions(
Expand Down
16 changes: 13 additions & 3 deletions Sources/SourceKit/clangd/ClangLanguageServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ extension ClangLanguageServerShim {
}

public func didSaveDocument(_ note: DidSaveTextDocumentNotification) {

if capabilities?.textDocumentSync?.save?.isSupported == true {
clangd.send(note)
}
}

// MARK: - Build System Integration
Expand Down Expand Up @@ -167,11 +169,19 @@ extension ClangLanguageServerShim {
}

func documentColor(_ req: Request<DocumentColorRequest>) {
forwardRequest(req, to: clangd)
if capabilities?.colorProvider?.isSupported == true {
forwardRequest(req, to: clangd)
} else {
req.reply(.success([]))
}
}

func colorPresentation(_ req: Request<ColorPresentationRequest>) {
forwardRequest(req, to: clangd)
if capabilities?.colorProvider?.isSupported == true {
forwardRequest(req, to: clangd)
} else {
req.reply(.success([]))
}
}

func codeAction(_ req: Request<CodeActionRequest>) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/SourceKit/sourcekitd/SwiftLanguageServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ extension SwiftLanguageServer {
change: .incremental,
willSave: true,
willSaveWaitUntil: false,
save: TextDocumentSyncOptions.SaveOptions(includeText: false)),
save: .value(TextDocumentSyncOptions.SaveOptions(includeText: false))),
hoverProvider: true,
completionProvider: CompletionOptions(
resolveProvider: false,
Expand Down Expand Up @@ -758,7 +758,7 @@ extension SwiftLanguageServer {
queue.async {
guard let snapshot = self.documentManager.latestSnapshot(req.params.textDocument.uri) else {
log("failed to find snapshot for url \(req.params.textDocument.uri)")
req.reply(nil)
req.reply([])
return
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/LanguageServerProtocolJSONRPCTests/CodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ final class CodingTests: XCTestCase {
change: .incremental,
willSave: true,
willSaveWaitUntil: false,
save: TextDocumentSyncOptions.SaveOptions(includeText: false)),
save: .value(TextDocumentSyncOptions.SaveOptions(includeText: false))),
completionProvider: CompletionOptions(
resolveProvider: false,
triggerCharacters: ["."]))), id: .number(2), json: """
Expand Down
4 changes: 2 additions & 2 deletions Tests/SourceKitTests/DocumentColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class DocumentColorTests: XCTestCase {
text: text)))

let request = DocumentColorRequest(textDocument: TextDocumentIdentifier(url))
return try! sk.sendSync(request)!
return try! sk.sendSync(request)
}

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

func testEmptyText() {
Expand Down