Skip to content

Commit f1ca085

Browse files
authored
Add base support for request cancelation (#46)
* Add base support for request cancelation * Lint
1 parent f18094c commit f1ca085

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2025 Spotify AB.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
import BuildServerProtocol
21+
import Foundation
22+
import LanguageServerProtocol
23+
24+
private let logger = makeFileLevelBSPLogger()
25+
26+
/// Handles the cancel request notification.
27+
///
28+
/// Does nothing but relay the cancel request to its observers.
29+
final class CancelRequestHandler {
30+
private var observers: [any CancelRequestObserver]
31+
32+
init(observers: [any CancelRequestObserver] = []) {
33+
self.observers = observers
34+
}
35+
36+
func onCancelRequest(_ notification: CancelRequestNotification) throws {
37+
for observer in observers {
38+
try observer.cancel(request: notification.id)
39+
}
40+
}
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2025 Spotify AB.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
import BuildServerProtocol
21+
import LanguageServerProtocol
22+
23+
/// Protocol for objects that need to be notified of cancellation requests.
24+
protocol CancelRequestObserver: AnyObject {
25+
func cancel(request: RequestID) throws
26+
}

Sources/SourceKitBazelBSP/RequestHandlers/PrepareHandler.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,11 @@ extension PrepareHandler: InvalidatedTargetObserver {
9999
buildCache.removeAll()
100100
}
101101
}
102+
103+
// When the user changes targets in the IDE in the middle of a background index request,
104+
// the LSP asks us to cancel the background one to be able to prioritize the IDE one.
105+
extension PrepareHandler: CancelRequestObserver {
106+
func cancel(request: RequestID) throws {
107+
// no-op, to be implemented
108+
}
109+
}

Sources/SourceKitBazelBSP/Server/SourceKitBazelBSPServer.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ package final class SourceKitBazelBSPServer {
5757
registry.register(notificationHandler: { (_: OnBuildInitializedNotification) in
5858
// no-op
5959
})
60-
registry.register(notificationHandler: { (_: CancelRequestNotification) in
61-
// no-op, no request canceling since the code today is not async
62-
})
6360
registry.register(requestHandler: { (_: WorkspaceWaitForBuildSystemUpdatesRequest, _: RequestID) in
6461
// FIXME: no-op, no special handling since the code today is not async, but I might be wrong here.
6562
VoidResponse()
@@ -98,6 +95,12 @@ package final class SourceKitBazelBSPServer {
9895
connection: connection
9996
)
10097
registry.register(notificationHandler: watchedFileChangeHandler.onWatchedFilesDidChange)
98+
99+
// CancelRequestNotification
100+
let cancelRequestHandler = CancelRequestHandler(
101+
observers: [prepareHandler] // `prepare` is the only case of cancelation I'm aware of.
102+
)
103+
registry.register(notificationHandler: cancelRequestHandler.onCancelRequest)
101104
}
102105

103106
package convenience init(

0 commit comments

Comments
 (0)