-
-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathGitHubCopilotWorkspacePlugin.swift
More file actions
58 lines (52 loc) · 1.84 KB
/
GitHubCopilotWorkspacePlugin.swift
File metadata and controls
58 lines (52 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Foundation
import Logger
import Workspace
public final class GitHubCopilotWorkspacePlugin: WorkspacePlugin {
private var _gitHubCopilotService: GitHubCopilotService?
@GitHubCopilotSuggestionActor
var gitHubCopilotService: GitHubCopilotService? {
if let service = _gitHubCopilotService { return service }
do {
return try createGitHubCopilotService()
} catch {
Logger.gitHubCopilot.error("Failed to create GitHub Copilot service: \(error)")
return nil
}
}
deinit {
if let _gitHubCopilotService {
Task { await _gitHubCopilotService.terminate() }
}
}
@GitHubCopilotSuggestionActor
func createGitHubCopilotService() throws -> GitHubCopilotService {
let newService = try GitHubCopilotService(projectRootURL: projectRootURL)
_gitHubCopilotService = newService
newService.localProcessServer?.terminationHandler = { [weak self] in
Logger.gitHubCopilot.error("GitHub Copilot language server terminated")
self?.terminate()
}
Task {
try await Task.sleep(nanoseconds: 1_000_000_000)
finishLaunchingService()
}
return newService
}
@GitHubCopilotSuggestionActor
func finishLaunchingService() {
guard let workspace, let _gitHubCopilotService else { return }
Task {
for (_, filespace) in workspace.filespaces {
let documentURL = filespace.fileURL
guard let content = try? String(contentsOf: documentURL) else { continue }
try? await _gitHubCopilotService.notifyOpenTextDocument(
fileURL: documentURL,
content: content
)
}
}
}
func terminate() {
_gitHubCopilotService = nil
}
}