Skip to content

Commit 5475981

Browse files
committed
Support injecting notification handler from everywhere
1 parent d8c5797 commit 5475981

1 file changed

Lines changed: 13 additions & 26 deletions

File tree

Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotService.swift

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ public class GitHubCopilotBaseService {
9696
let projectRootURL: URL
9797
var server: GitHubCopilotLSP
9898
var localProcessServer: CopilotLocalProcessServer?
99-
@GitHubCopilotSuggestionActor
100-
private var serverNotificationHandlers =
101-
[AnyHashable: (ServerNotification) async throws -> Bool]()
99+
let notificationHandler: ServerNotificationHandler
102100

103101
deinit {
104102
localProcessServer?.terminate()
@@ -107,16 +105,19 @@ public class GitHubCopilotBaseService {
107105
init(designatedServer: GitHubCopilotLSP) {
108106
projectRootURL = URL(fileURLWithPath: "/")
109107
server = designatedServer
108+
notificationHandler = .init()
110109
}
111110

112111
init(projectRootURL: URL) throws {
113112
self.projectRootURL = projectRootURL
114-
let (server, localServer) = try {
113+
let notificationHandler = ServerNotificationHandler()
114+
self.notificationHandler = notificationHandler
115+
let (server, localServer) = try { [notificationHandler] in
115116
let urls = try GitHubCopilotBaseService.createFoldersIfNeeded()
116117
let executionParams: Process.ExecutionParameters
117118
let runner = UserDefaults.shared.value(for: \.runNodeWith)
118119

119-
guard let agentJSURL = {
120+
guard let agentJSURL = { () -> URL? in
120121
let languageServerDotJS = urls.executableURL
121122
.appendingPathComponent("copilot/dist/language-server.js")
122123
if FileManager.default.fileExists(atPath: languageServerDotJS.path) {
@@ -199,26 +200,12 @@ public class GitHubCopilotBaseService {
199200
)
200201
}()
201202
}
202-
let localServer = CopilotLocalProcessServer(executionParameters: executionParams)
203+
let localServer = CopilotLocalProcessServer(
204+
executionParameters: executionParams,
205+
serverNotificationHandler: notificationHandler
206+
)
203207

204208
localServer.logMessages = UserDefaults.shared.value(for: \.gitHubCopilotVerboseLog)
205-
localServer.notificationHandler = { [weak self] notification, respond in
206-
Task {
207-
for handler in self?.serverNotificationHandlers.values ?? [] {
208-
do {
209-
let handled = try await handler(notification)
210-
if handled {
211-
respond(nil)
212-
return
213-
}
214-
} catch {
215-
respond(.failure(error))
216-
return
217-
}
218-
}
219-
respond(.handlerUnavailable(notification.method.rawValue))
220-
}
221-
}
222209
let server = InitializingServer(server: localServer)
223210

224211
server.initializeParamsProvider = {
@@ -306,16 +293,16 @@ public class GitHubCopilotBaseService {
306293

307294
func registerNotificationHandler(
308295
id: AnyHashable,
309-
_ block: @escaping (ServerNotification) async throws -> Bool
296+
_ block: @escaping ServerNotificationHandler.Handler
310297
) {
311298
Task { @GitHubCopilotSuggestionActor in
312-
self.serverNotificationHandlers[id] = block
299+
self.notificationHandler.handlers[id] = block
313300
}
314301
}
315302

316303
func unregisterNotificationHandler(id: AnyHashable) {
317304
Task { @GitHubCopilotSuggestionActor in
318-
self.serverNotificationHandlers[id] = nil
305+
self.notificationHandler.handlers[id] = nil
319306
}
320307
}
321308
}

0 commit comments

Comments
 (0)