Skip to content

Commit d6101a1

Browse files
authored
Merge pull request #848 from ahoppen/ahoppen/minor-cleanups
Fix a couple `FIXME: (async)`
2 parents 6206585 + abf456a commit d6101a1

File tree

10 files changed

+57
-131
lines changed

10 files changed

+57
-131
lines changed

Sources/SKCore/BuildSystemManager.swift

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,7 @@ extension BuildSystemManager {
254254
}
255255

256256
extension BuildSystemManager: BuildSystemDelegate {
257-
// FIXME: (async) Make this method isolated once `BuildSystemDelegate` has ben asyncified
258-
public nonisolated func fileBuildSettingsChanged(_ changes: Set<DocumentURI>) {
259-
Task {
260-
await fileBuildSettingsChangedImpl(changes)
261-
}
262-
}
263-
264-
public func fileBuildSettingsChangedImpl(_ changedFiles: Set<DocumentURI>) async {
257+
public func fileBuildSettingsChanged(_ changedFiles: Set<DocumentURI>) async {
265258
let changedWatchedFiles = changedFiles.flatMap({ mainFile in
266259
self.watchedFiles.filter { $1.mainFile == mainFile }.keys
267260
})
@@ -271,14 +264,7 @@ extension BuildSystemManager: BuildSystemDelegate {
271264
}
272265
}
273266

274-
// FIXME: (async) Make this method isolated once `BuildSystemDelegate` has ben asyncified
275-
public nonisolated func filesDependenciesUpdated(_ changedFiles: Set<DocumentURI>) {
276-
Task {
277-
await filesDependenciesUpdatedImpl(changedFiles)
278-
}
279-
}
280-
281-
public func filesDependenciesUpdatedImpl(_ changedFiles: Set<DocumentURI>) async {
267+
public func filesDependenciesUpdated(_ changedFiles: Set<DocumentURI>) async {
282268
// Empty changes --> assume everything has changed.
283269
guard !changedFiles.isEmpty else {
284270
if let delegate = self._delegate {
@@ -295,43 +281,22 @@ extension BuildSystemManager: BuildSystemDelegate {
295281
}
296282
}
297283

298-
// FIXME: (async) Make this method isolated once `BuildSystemDelegate` has ben asyncified
299-
public nonisolated func buildTargetsChanged(_ changes: [BuildTargetEvent]) {
300-
Task {
301-
await buildTargetsChangedImpl(changes)
302-
}
303-
}
304-
305-
public func buildTargetsChangedImpl(_ changes: [BuildTargetEvent]) async {
284+
public func buildTargetsChanged(_ changes: [BuildTargetEvent]) async {
306285
if let delegate = self._delegate {
307286
await delegate.buildTargetsChanged(changes)
308287
}
309288
}
310289

311-
// FIXME: (async) Make this method isolated once `BuildSystemDelegate` has ben asyncified
312-
public nonisolated func fileHandlingCapabilityChanged() {
313-
Task {
314-
await fileHandlingCapabilityChangedImpl()
315-
}
316-
}
317-
318-
public func fileHandlingCapabilityChangedImpl() async {
290+
public func fileHandlingCapabilityChanged() async {
319291
if let delegate = self._delegate {
320292
await delegate.fileHandlingCapabilityChanged()
321293
}
322294
}
323295
}
324296

325297
extension BuildSystemManager: MainFilesDelegate {
326-
// FIXME: (async) Make this method isolated once `MainFilesDelegate` has ben asyncified
327-
public nonisolated func mainFilesChanged() {
328-
Task {
329-
await mainFilesChangedImpl()
330-
}
331-
}
332-
333298
// FIXME: Consider debouncing/limiting this, seems to trigger often during a build.
334-
public func mainFilesChangedImpl() async {
299+
public func mainFilesChanged() async {
335300
let origWatched = self.watchedFiles
336301
self.watchedFiles = [:]
337302
var buildSettingsChanges = Set<DocumentURI>()

Sources/SKCore/CompilationDatabaseBuildSystem.swift

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,16 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
8787

8888
public var indexPrefixMappings: [PathPrefixMapping] { return [] }
8989

90-
public func buildSettings(for document: DocumentURI, language: Language) async throws -> FileBuildSettings? {
91-
return self.settings(for: document)
90+
public func buildSettings(for document: DocumentURI, language: Language) async -> FileBuildSettings? {
91+
guard let url = document.fileURL else {
92+
// We can't determine build settings for non-file URIs.
93+
return nil
94+
}
95+
guard let db = database(for: url),
96+
let cmd = db[url].first else { return nil }
97+
return FileBuildSettings(
98+
compilerArguments: Array(cmd.commandLine.dropFirst()),
99+
workingDirectory: cmd.directory)
92100
}
93101

94102
public func registerForChangeNotifications(for uri: DocumentURI, language: Language) async {
@@ -168,22 +176,3 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
168176
}
169177
}
170178
}
171-
172-
extension CompilationDatabaseBuildSystem {
173-
private func settings(for uri: DocumentURI) -> FileBuildSettings? {
174-
guard let url = uri.fileURL else {
175-
// We can't determine build settings for non-file URIs.
176-
return nil
177-
}
178-
guard let db = database(for: url),
179-
let cmd = db[url].first else { return nil }
180-
return FileBuildSettings(
181-
compilerArguments: Array(cmd.commandLine.dropFirst()),
182-
workingDirectory: cmd.directory)
183-
}
184-
185-
/// Exposed for *testing*.
186-
public func _settings(for uri: DocumentURI) -> FileBuildSettings? {
187-
return self.settings(for: uri)
188-
}
189-
}

Sources/SKCore/MainFilesProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ public protocol MainFilesProvider: AnyObject {
3030
public protocol MainFilesDelegate: AnyObject {
3131

3232
/// The mapping from files to main files (may have) changed.
33-
func mainFilesChanged()
33+
func mainFilesChanged() async
3434
}

Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public actor SwiftPMWorkspace {
8383
var watchedFiles: [DocumentURI: Language] = [:]
8484

8585
/// This callback is informed when `reloadPackage` starts and ends executing.
86-
var reloadPackageStatusCallback: (ReloadPackageStatus) -> Void
86+
var reloadPackageStatusCallback: (ReloadPackageStatus) async -> Void
8787

8888

8989
/// Creates a build system using the Swift Package Manager, if this workspace is a package.
@@ -99,7 +99,7 @@ public actor SwiftPMWorkspace {
9999
toolchainRegistry: ToolchainRegistry,
100100
fileSystem: FileSystem = localFileSystem,
101101
buildSetup: BuildSetup,
102-
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) -> Void = { _ in }
102+
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) async -> Void = { _ in }
103103
) async throws {
104104
self.workspacePath = workspacePath
105105
self.fileSystem = fileSystem
@@ -164,7 +164,7 @@ public actor SwiftPMWorkspace {
164164
url: URL,
165165
toolchainRegistry: ToolchainRegistry,
166166
buildSetup: BuildSetup,
167-
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) -> Void
167+
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) async -> Void
168168
) async {
169169
do {
170170
try await self.init(
@@ -189,11 +189,7 @@ extension SwiftPMWorkspace {
189189
/// (Re-)load the package settings by parsing the manifest and resolving all the targets and
190190
/// dependencies.
191191
func reloadPackage() async throws {
192-
reloadPackageStatusCallback(.start)
193-
defer {
194-
reloadPackageStatusCallback(.end)
195-
}
196-
192+
await reloadPackageStatusCallback(.start)
197193

198194
let observabilitySystem = ObservabilitySystem({ scope, diagnostic in
199195
log(diagnostic.description, level: diagnostic.severity.asLogLevel)
@@ -241,10 +237,14 @@ extension SwiftPMWorkspace {
241237
return td
242238
})
243239

244-
guard let delegate = self.delegate else { return }
240+
guard let delegate = self.delegate else {
241+
await reloadPackageStatusCallback(.end)
242+
return
243+
}
245244
let changedFiles = Set<DocumentURI>(self.watchedFiles.keys)
246245
await delegate.fileBuildSettingsChanged(changedFiles)
247246
await delegate.fileHandlingCapabilityChanged()
247+
await reloadPackageStatusCallback(.end)
248248
}
249249
}
250250

Sources/SourceKitLSP/SourceKitIndexDelegate.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public final class SourceKitIndexDelegate: IndexDelegate {
5656
/// *Must be called on queue*.
5757
func _indexChanged() {
5858
for delegate in mainFilesDelegates {
59-
delegate.mainFilesChanged()
59+
Task {
60+
await delegate.mainFilesChanged()
61+
}
6062
}
6163
}
6264

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,7 @@ extension SourceKitServer: MessageHandler {
648648
// MARK: - Build System Delegate
649649

650650
extension SourceKitServer: BuildSystemDelegate {
651-
// FIXME: (async) Make this method isolated once `BuildSystemDelegate` has been asyncified
652-
public nonisolated func buildTargetsChanged(_ changes: [BuildTargetEvent]) {
651+
public func buildTargetsChanged(_ changes: [BuildTargetEvent]) {
653652
// TODO: do something with these changes once build target support is in place
654653
}
655654

@@ -664,19 +663,11 @@ extension SourceKitServer: BuildSystemDelegate {
664663
return documentManager.openDocuments.intersection(changes)
665664
}
666665

667-
// FIXME: (async) Make this method isolated once `BuildSystemDelegate` has been asyncified
668-
/// Non-async variant that executes `fileBuildSettingsChangedImpl` in a new task.
669-
public nonisolated func fileBuildSettingsChanged(_ changedFiles: Set<DocumentURI>) {
670-
Task {
671-
await self.fileBuildSettingsChangedImpl(changedFiles)
672-
}
673-
}
674-
675666
/// Handle a build settings change notification from the `BuildSystem`.
676667
/// This has two primary cases:
677668
/// - Initial settings reported for a given file, now we can fully open it
678669
/// - Changed settings for an already open file
679-
public func fileBuildSettingsChangedImpl(_ changedFiles: Set<DocumentURI>) async {
670+
public func fileBuildSettingsChanged(_ changedFiles: Set<DocumentURI>) async {
680671
for uri in changedFiles {
681672
guard self.documentManager.openDocuments.contains(uri) else {
682673
continue
@@ -690,17 +681,10 @@ extension SourceKitServer: BuildSystemDelegate {
690681
}
691682
}
692683

693-
// FIXME: (async) Make this method isolated once `BuildSystemDelegate` has been asyncified
694-
public nonisolated func filesDependenciesUpdated(_ changedFiles: Set<DocumentURI>) {
695-
Task {
696-
await filesDependenciesUpdatedImpl(changedFiles)
697-
}
698-
}
699-
700684
/// Handle a dependencies updated notification from the `BuildSystem`.
701685
/// We inform the respective language services as long as the given file is open
702686
/// (not queued for opening).
703-
public func filesDependenciesUpdatedImpl(_ changedFiles: Set<DocumentURI>) async {
687+
public func filesDependenciesUpdated(_ changedFiles: Set<DocumentURI>) async {
704688
// Split the changedFiles into the workspaces they belong to.
705689
// Then invoke affectedOpenDocumentsForChangeSet for each workspace with its affected files.
706690
let changedFilesAndWorkspace = await changedFiles.asyncMap {
@@ -720,14 +704,7 @@ extension SourceKitServer: BuildSystemDelegate {
720704
}
721705
}
722706

723-
// FIXME: (async) Make this method isolated once `BuildSystemDelegate` has been asyncified
724-
public nonisolated func fileHandlingCapabilityChanged() {
725-
Task {
726-
await fileHandlingCapabilityChangedImpl()
727-
}
728-
}
729-
730-
public func fileHandlingCapabilityChangedImpl() {
707+
public func fileHandlingCapabilityChanged() {
731708
self.uriToWorkspaceCache = [:]
732709
}
733710
}
@@ -756,18 +733,11 @@ extension SourceKitServer {
756733
// Client doesn’t support work done progress
757734
return
758735
}
759-
// FIXME: (async) This can cause out-of-order notifications to be sent to the editor
760-
// if the scheduled tasks change order.
761-
// Make `reloadPackageStatusCallback` async and shift the responsibility for
762-
// guaranteeing in-order calls to `reloadPackageStatusCallback` to
763-
// `SwiftPMWorkspace.reloadPackage` once that method is async.
764-
Task {
765-
switch status {
766-
case .start:
767-
await self.packageLoadingWorkDoneProgress.startProgress(server: self)
768-
case .end:
769-
await self.packageLoadingWorkDoneProgress.endProgress(server: self)
770-
}
736+
switch status {
737+
case .start:
738+
await self.packageLoadingWorkDoneProgress.startProgress(server: self)
739+
case .end:
740+
await self.packageLoadingWorkDoneProgress.endProgress(server: self)
771741
}
772742
}
773743
)

Sources/SourceKitLSP/Workspace.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public final class Workspace {
9999
toolchainRegistry: ToolchainRegistry,
100100
buildSetup: BuildSetup,
101101
indexOptions: IndexOptions = IndexOptions(),
102-
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) -> Void
102+
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) async -> Void
103103
) async throws {
104104
var buildSystem: BuildSystem? = nil
105105
if let rootUrl = rootUri.fileURL, let rootPath = try? AbsolutePath(validating: rootUrl.path) {

0 commit comments

Comments
 (0)