Skip to content

Commit 3fc03e9

Browse files
committed
Debounced the Reloading Package work done progress
1 parent 5bf6233 commit 3fc03e9

File tree

7 files changed

+45
-149
lines changed

7 files changed

+45
-149
lines changed

Sources/SourceKitLSP/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ add_library(SourceKitLSP STATIC
1919
TestDiscovery.swift
2020
TextEdit+IsNoop.swift
2121
WorkDoneProgressManager.swift
22-
WorkDoneProgressState.swift
2322
Workspace.swift
2423
)
2524
target_sources(SourceKitLSP PRIVATE

Sources/SourceKitLSP/IndexProgressManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ actor IndexProgressManager {
127127
} else {
128128
workDoneProgress = await WorkDoneProgressManager(
129129
server: sourceKitLSPServer,
130-
initialDebounce: sourceKitLSPServer.options.indexProgressDebounceDuration,
130+
initialDebounce: sourceKitLSPServer.options.workDoneProgressDebounceDuration,
131131
title: "Indexing",
132132
message: message,
133133
percentage: percentage

Sources/SourceKitLSP/SourceKitLSPServer+Options.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ extension SourceKitLSPServer {
4848
/// notification when running unit tests.
4949
public var swiftPublishDiagnosticsDebounceDuration: TimeInterval
5050

51-
/// When an index task is started, how many milliseconds to wait before sending a work done progress to the client.
52-
// This prevents flickering of the work done progress in the client for short-lived index tasks
53-
public var indexProgressDebounceDuration: Duration
51+
/// When a task is started that should be displayed to the client as a work done progress, how many milliseconds to
52+
/// wait before actually starting the work done progress. This prevents flickering of the work done progress in the
53+
/// client for short-lived index tasks which end within this duration.
54+
public var workDoneProgressDebounceDuration: Duration
5455

5556
/// Experimental features that are enabled.
5657
public var experimentalFeatures: Set<ExperimentalFeature>
@@ -65,7 +66,7 @@ extension SourceKitLSPServer {
6566
completionOptions: SKCompletionOptions = .init(),
6667
generatedInterfacesPath: AbsolutePath = defaultDirectoryForGeneratedInterfaces,
6768
swiftPublishDiagnosticsDebounceDuration: TimeInterval = 2, /* 2s */
68-
indexProgressDebounceDuration: Duration = .seconds(0),
69+
workDoneProgressDebounceDuration: Duration = .seconds(0),
6970
experimentalFeatures: Set<ExperimentalFeature> = [],
7071
indexTestHooks: IndexTestHooks = IndexTestHooks()
7172
) {
@@ -77,7 +78,7 @@ extension SourceKitLSPServer {
7778
self.generatedInterfacesPath = generatedInterfacesPath
7879
self.swiftPublishDiagnosticsDebounceDuration = swiftPublishDiagnosticsDebounceDuration
7980
self.experimentalFeatures = experimentalFeatures
80-
self.indexProgressDebounceDuration = indexProgressDebounceDuration
81+
self.workDoneProgressDebounceDuration = workDoneProgressDebounceDuration
8182
self.indexTestHooks = indexTestHooks
8283
}
8384
}

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ public actor SourceKitLSPServer {
127127
/// initializer.
128128
private nonisolated(unsafe) var indexProgressManager: IndexProgressManager!
129129

130-
private var packageLoadingWorkDoneProgress = WorkDoneProgressState(
131-
"SourceKitLSP.SourceKitLSPServer.reloadPackage",
132-
title: "SourceKit-LSP: Reloading Package"
133-
)
130+
/// Number of workspaces that are currently reloading swift package. When this is not 0, a
131+
/// `packageLoadingWorkDoneProgress` is created to show a work done progress indicator in the client.
132+
private var inProgressPackageLoadingOperations = 0
133+
private var packageLoadingWorkDoneProgress: WorkDoneProgressManager?
134134

135135
/// **Public for testing**
136136
public var _documentManager: DocumentManager {
@@ -898,6 +898,27 @@ extension SourceKitLSPServer {
898898
)
899899
}
900900

901+
private func reloadPackageStatusCallback(_ status: ReloadPackageStatus) async {
902+
switch status {
903+
case .start:
904+
inProgressPackageLoadingOperations += 1
905+
if let capabilityRegistry, packageLoadingWorkDoneProgress == nil {
906+
packageLoadingWorkDoneProgress = WorkDoneProgressManager(
907+
server: self,
908+
capabilityRegistry: capabilityRegistry,
909+
initialDebounce: options.workDoneProgressDebounceDuration,
910+
title: "SourceKit-LSP: Reloading Package"
911+
)
912+
}
913+
case .end:
914+
inProgressPackageLoadingOperations -= 1
915+
if inProgressPackageLoadingOperations == 0, let packageLoadingWorkDoneProgress {
916+
self.packageLoadingWorkDoneProgress = nil
917+
await packageLoadingWorkDoneProgress.end()
918+
}
919+
}
920+
}
921+
901922
/// Creates a workspace at the given `uri`.
902923
///
903924
/// If the build system that was determined for the workspace does not satisfy `condition`, `nil` is returned.
@@ -916,13 +937,7 @@ extension SourceKitLSPServer {
916937
options: options,
917938
toolchainRegistry: toolchainRegistry,
918939
reloadPackageStatusCallback: { [weak self] status in
919-
guard let self else { return }
920-
switch status {
921-
case .start:
922-
await self.packageLoadingWorkDoneProgress.startProgress(server: self)
923-
case .end:
924-
await self.packageLoadingWorkDoneProgress.endProgress(server: self)
925-
}
940+
await self?.reloadPackageStatusCallback(status)
926941
}
927942
)
928943
guard await condition(buildSystem) else {

Sources/SourceKitLSP/WorkDoneProgressManager.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ final actor WorkDoneProgressManager {
6161
guard let capabilityRegistry = await server.capabilityRegistry else {
6262
return nil
6363
}
64-
self.init(server: server, capabilityRegistry: capabilityRegistry, title: title, message: message)
64+
self.init(
65+
server: server,
66+
capabilityRegistry: capabilityRegistry,
67+
initialDebounce: initialDebounce,
68+
title: title,
69+
message: message,
70+
percentage: percentage
71+
)
6572
}
6673

6774
init?(
@@ -109,7 +116,7 @@ final actor WorkDoneProgressManager {
109116
)
110117
)
111118
} else {
112-
let token = ProgressToken.string("WorkDoneProgress-\(UUID())")
119+
let token = ProgressToken.string(UUID().uuidString)
113120
do {
114121
_ = try await server.client.send(CreateWorkDoneProgressRequest(token: token))
115122
} catch {

Sources/SourceKitLSP/WorkDoneProgressState.swift

Lines changed: 0 additions & 126 deletions
This file was deleted.

Sources/sourcekit-lsp/SourceKitLSP.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ struct SourceKitLSP: AsyncParsableCommand {
207207
@Option(help: .hidden)
208208
var completionMaxResults = 200
209209

210-
@Option(help: .hidden)
211-
var indexProgressDebounceDuration: Int = 1_000
210+
@Option(name: .customLong("progress-debounce-duration"), help: .hidden)
211+
var workDoneProgressDebounceDuration: Int = 1_000
212212

213213
func mapOptions() -> SourceKitLSPServer.Options {
214214
var serverOptions = SourceKitLSPServer.Options()
@@ -228,7 +228,7 @@ struct SourceKitLSP: AsyncParsableCommand {
228228
serverOptions.generatedInterfacesPath = generatedInterfacesPath
229229
serverOptions.experimentalFeatures = Set(experimentalFeatures)
230230
serverOptions.completionOptions.maxResults = completionMaxResults
231-
serverOptions.indexProgressDebounceDuration = .milliseconds(indexProgressDebounceDuration)
231+
serverOptions.workDoneProgressDebounceDuration = .milliseconds(workDoneProgressDebounceDuration)
232232

233233
return serverOptions
234234
}

0 commit comments

Comments
 (0)