forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubCopilotWorkspacePlugin.swift
More file actions
50 lines (43 loc) · 1.46 KB
/
GitHubCopilotWorkspacePlugin.swift
File metadata and controls
50 lines (43 loc) · 1.46 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
import Foundation
import Logger
import Workspace
public final class GitHubCopilotWorkspacePlugin: WorkspacePlugin {
public var gitHubCopilotService: GitHubCopilotService?
public override init(workspace: Workspace) {
super.init(workspace: workspace)
do {
gitHubCopilotService = try createGitHubCopilotService()
} catch {
Logger.gitHubCopilot.error("Failed to create GitHub Copilot service: \(error)")
}
}
deinit {
if let gitHubCopilotService {
Task { await gitHubCopilotService.terminate() }
}
}
func createGitHubCopilotService() throws -> GitHubCopilotService {
let newService = try GitHubCopilotService(projectRootURL: projectRootURL)
Task {
try await Task.sleep(nanoseconds: 1_000_000_000)
finishLaunchingService()
}
return newService
}
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
}
}