diff --git a/Sources/BuildServerProtocol/CMakeLists.txt b/Sources/BuildServerProtocol/CMakeLists.txt index 53294187..2d1fb40d 100644 --- a/Sources/BuildServerProtocol/CMakeLists.txt +++ b/Sources/BuildServerProtocol/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(BuildServerProtocol STATIC BuildTargets.swift + DidChangeWatchedFilesNotification.swift FileOptions.swift InitializeBuild.swift Messages.swift diff --git a/Sources/BuildServerProtocol/DidChangeWatchedFilesNotification.swift b/Sources/BuildServerProtocol/DidChangeWatchedFilesNotification.swift new file mode 100644 index 00000000..1e0525cf --- /dev/null +++ b/Sources/BuildServerProtocol/DidChangeWatchedFilesNotification.swift @@ -0,0 +1,16 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import LanguageServerProtocol + +/// Notification sent from SourceKit-LSP to the build system to indicate that files within the project have been modified. +public typealias DidChangeWatchedFilesNotification = LanguageServerProtocol.DidChangeWatchedFilesNotification diff --git a/Sources/BuildSystemIntegration/BuildServerBuildSystem.swift b/Sources/BuildSystemIntegration/BuildServerBuildSystem.swift index 1eeceb68..3d333f9e 100644 --- a/Sources/BuildSystemIntegration/BuildServerBuildSystem.swift +++ b/Sources/BuildSystemIntegration/BuildServerBuildSystem.swift @@ -332,7 +332,7 @@ extension BuildServerBuildSystem: BuiltInBuildSystem { } } - package func filesDidChange(_ events: [FileEvent]) {} + package func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) {} package func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability { guard diff --git a/Sources/BuildSystemIntegration/BuildSystem.swift b/Sources/BuildSystemIntegration/BuildSystem.swift index fa4ac9bf..4ea28c12 100644 --- a/Sources/BuildSystemIntegration/BuildSystem.swift +++ b/Sources/BuildSystemIntegration/BuildSystem.swift @@ -199,7 +199,7 @@ package protocol BuiltInBuildSystem: AnyObject, Sendable { func unregisterForChangeNotifications(for: DocumentURI) async /// Called when files in the project change. - func filesDidChange(_ events: [FileEvent]) async + func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async func fileHandlingCapability(for uri: DocumentURI) async -> FileHandlingCapability diff --git a/Sources/BuildSystemIntegration/BuildSystemManager.swift b/Sources/BuildSystemIntegration/BuildSystemManager.swift index 689e5613..53a0349f 100644 --- a/Sources/BuildSystemIntegration/BuildSystemManager.swift +++ b/Sources/BuildSystemIntegration/BuildSystemManager.swift @@ -101,7 +101,7 @@ package actor BuildSystemManager: BuiltInBuildSystemAdapterDelegate { } package func filesDidChange(_ events: [FileEvent]) async { - await self.buildSystem?.underlyingBuildSystem.filesDidChange(events) + await self.buildSystem?.send(BuildServerProtocol.DidChangeWatchedFilesNotification(changes: events)) } /// Implementation of `MessageHandler`, handling notifications from the build system. diff --git a/Sources/BuildSystemIntegration/BuiltInBuildSystemAdapter.swift b/Sources/BuildSystemIntegration/BuiltInBuildSystemAdapter.swift index 52d323ac..340e3327 100644 --- a/Sources/BuildSystemIntegration/BuiltInBuildSystemAdapter.swift +++ b/Sources/BuildSystemIntegration/BuiltInBuildSystemAdapter.swift @@ -79,6 +79,8 @@ actor BuiltInBuildSystemAdapter: BuiltInBuildSystemMessageHandler { // sent. We can only do this once all requests to the build system have been migrated and we can implement proper // dependency management between the BSP messages switch notification { + case let notification as DidChangeWatchedFilesNotification: + await self.underlyingBuildSystem.didChangeWatchedFiles(notification: notification) default: logger.error("Ignoring unknown notification \(type(of: notification).method) from SourceKit-LSP") } diff --git a/Sources/BuildSystemIntegration/CompilationDatabaseBuildSystem.swift b/Sources/BuildSystemIntegration/CompilationDatabaseBuildSystem.swift index c9cd0c23..b1cb034b 100644 --- a/Sources/BuildSystemIntegration/CompilationDatabaseBuildSystem.swift +++ b/Sources/BuildSystemIntegration/CompilationDatabaseBuildSystem.swift @@ -212,8 +212,8 @@ extension CompilationDatabaseBuildSystem: BuiltInBuildSystem { } } - package func filesDidChange(_ events: [FileEvent]) async { - if events.contains(where: { self.fileEventShouldTriggerCompilationDatabaseReload(event: $0) }) { + package func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async { + if notification.changes.contains(where: { self.fileEventShouldTriggerCompilationDatabaseReload(event: $0) }) { await self.reloadCompilationDatabase() } } diff --git a/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift b/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift index 6dd0a354..ed442801 100644 --- a/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift +++ b/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift @@ -746,8 +746,8 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem { } } - package func filesDidChange(_ events: [FileEvent]) async { - if events.contains(where: { self.fileEventShouldTriggerPackageReload(event: $0) }) { + package func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async { + if notification.changes.contains(where: { self.fileEventShouldTriggerPackageReload(event: $0) }) { logger.log("Reloading package because of file change") await orLog("Reloading package") { try await self.schedulePackageReload().value @@ -757,7 +757,7 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem { var filesWithUpdatedDependencies: Set = [] // If a Swift file within a target is updated, reload all the other files within the target since they might be // referring to a function in the updated file. - for event in events { + for event in notification.changes { guard event.uri.fileURL?.pathExtension == "swift", let targets = fileToTargets[event.uri] else { continue } @@ -775,7 +775,7 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem { // directory outside the source tree. // If we have background indexing enabled, this is not necessary because we call `fileDependenciesUpdated` when // preparation of a target finishes. - if !isForIndexBuild, events.contains(where: { $0.uri.fileURL?.pathExtension == "swiftmodule" }) { + if !isForIndexBuild, notification.changes.contains(where: { $0.uri.fileURL?.pathExtension == "swiftmodule" }) { filesWithUpdatedDependencies.formUnion(self.fileToTargets.keys) } await self.fileDependenciesUpdatedDebouncer.scheduleCall(filesWithUpdatedDependencies) diff --git a/Tests/BuildSystemIntegrationTests/BuildSystemManagerTests.swift b/Tests/BuildSystemIntegrationTests/BuildSystemManagerTests.swift index d009266c..1647aac2 100644 --- a/Tests/BuildSystemIntegrationTests/BuildSystemManagerTests.swift +++ b/Tests/BuildSystemIntegrationTests/BuildSystemManagerTests.swift @@ -509,7 +509,7 @@ class ManualBuildSystem: BuiltInBuildSystem { var indexStorePath: AbsolutePath? { nil } var indexDatabasePath: AbsolutePath? { nil } - func filesDidChange(_ events: [FileEvent]) {} + func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) {} package func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability { if map[uri] != nil { diff --git a/Tests/SourceKitLSPTests/BuildSystemTests.swift b/Tests/SourceKitLSPTests/BuildSystemTests.swift index 124c67a7..39c77314 100644 --- a/Tests/SourceKitLSPTests/BuildSystemTests.swift +++ b/Tests/SourceKitLSPTests/BuildSystemTests.swift @@ -99,7 +99,7 @@ actor TestBuildSystem: BuiltInBuildSystem { watchedFiles.remove(uri) } - func filesDidChange(_ events: [FileEvent]) {} + func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async {} func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability { if buildSettingsByFile[uri] != nil { diff --git a/Tests/SourceKitLSPTests/CompilationDatabaseTests.swift b/Tests/SourceKitLSPTests/CompilationDatabaseTests.swift index e0d7909c..a9e7a4d4 100644 --- a/Tests/SourceKitLSPTests/CompilationDatabaseTests.swift +++ b/Tests/SourceKitLSPTests/CompilationDatabaseTests.swift @@ -38,7 +38,7 @@ final class CompilationDatabaseTests: XCTestCase { let (mainUri, positions) = try project.openDocument("main.cpp") - // Do a sanity check and verify that we get the expected result from a hover response before modifing the compile commands. + // Do a sanity check and verify that we get the expected result from a hover response before modifying the compile commands. let highlightRequest = DocumentHighlightRequest( textDocument: TextDocumentIdentifier(mainUri),