forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeiumExtension.swift
More file actions
194 lines (168 loc) · 6.54 KB
/
CodeiumExtension.swift
File metadata and controls
194 lines (168 loc) · 6.54 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import BuiltinExtension
import ChatTab
import CopilotForXcodeKit
import Foundation
import Logger
import Preferences
import Workspace
@globalActor public enum CodeiumActor {
public actor TheActor {}
public static let shared = TheActor()
}
public final class CodeiumExtension: BuiltinExtension {
public var extensionIdentifier: String { "com.codeium" }
public let suggestionService: CodeiumSuggestionService
public var chatTabTypes: [any CustomChatTab] {
[TypedCustomChatTab(of: CodeiumChatTab.self)]
}
private var extensionUsage = ExtensionUsage(
isSuggestionServiceInUse: false,
isChatServiceInUse: false
)
private var isLanguageServerInUse: Bool {
get async {
let lifeKeeperIsAlive = await CodeiumServiceLifeKeeper.shared.isAlive
return extensionUsage.isSuggestionServiceInUse
|| extensionUsage.isChatServiceInUse
|| lifeKeeperIsAlive
}
}
let workspacePool: WorkspacePool
let serviceLocator: ServiceLocator
public init(workspacePool: WorkspacePool) {
self.workspacePool = workspacePool
serviceLocator = .init(workspacePool: workspacePool)
suggestionService = .init(serviceLocator: serviceLocator)
}
public func workspaceDidOpen(_ workspace: WorkspaceInfo) {
Task {
do {
guard await isLanguageServerInUse else { return }
guard let service = await serviceLocator.getService(from: workspace) else { return }
try await service.notifyOpenWorkspace(workspaceURL: workspace.workspaceURL)
} catch {
Logger.codeium.error(error.localizedDescription)
}
}
}
public func workspaceDidClose(_ workspace: WorkspaceInfo) {
Task {
do {
guard await isLanguageServerInUse else { return }
guard let service = await serviceLocator.getService(from: workspace) else { return }
try await service.notifyCloseWorkspace(workspaceURL: workspace.workspaceURL)
} catch {
Logger.codeium.error(error.localizedDescription)
}
}
}
public func workspace(_ workspace: WorkspaceInfo, didOpenDocumentAt documentURL: URL) {
Task {
guard await isLanguageServerInUse else { return }
// check if file size is larger than 15MB, if so, return immediately
if let attrs = try? FileManager.default
.attributesOfItem(atPath: documentURL.path),
let fileSize = attrs[FileAttributeKey.size] as? UInt64,
fileSize > 15 * 1024 * 1024
{ return }
do {
let content = try String(contentsOf: documentURL, encoding: .utf8)
guard let service = await serviceLocator.getService(from: workspace) else { return }
try await service.notifyOpenTextDocument(fileURL: documentURL, content: content)
} catch {
Logger.codeium.error(error.localizedDescription)
}
}
}
public func workspace(_ workspace: WorkspaceInfo, didSaveDocumentAt documentURL: URL) {
// unimplemented
}
public func workspace(_ workspace: WorkspaceInfo, didCloseDocumentAt documentURL: URL) {
Task {
guard await isLanguageServerInUse else { return }
do {
guard let service = await serviceLocator.getService(from: workspace) else { return }
try await service.notifyCloseTextDocument(fileURL: documentURL)
} catch {
Logger.codeium.error(error.localizedDescription)
}
}
}
public func workspace(
_ workspace: WorkspaceInfo,
didUpdateDocumentAt documentURL: URL,
content: String?
) {
Task {
guard await isLanguageServerInUse else { return }
// check if file size is larger than 15MB, if so, return immediately
if let attrs = try? FileManager.default
.attributesOfItem(atPath: documentURL.path),
let fileSize = attrs[FileAttributeKey.size] as? UInt64,
fileSize > 15 * 1024 * 1024
{ return }
do {
guard let content else { return }
guard let service = await serviceLocator.getService(from: workspace) else { return }
try await service.notifyChangeTextDocument(fileURL: documentURL, content: content)
try await service.refreshIDEContext(
fileURL: documentURL,
content: content,
cursorPosition: .zero,
tabSize: 4, indentSize: 4, usesTabsForIndentation: false,
workspaceURL: workspace.workspaceURL
)
} catch {
Logger.codeium.error(error.localizedDescription)
}
}
}
public func extensionUsageDidChange(_ usage: ExtensionUsage) {
extensionUsage = usage
Task {
if !(await isLanguageServerInUse) {
terminate()
}
}
}
public func terminate() {
for workspace in workspacePool.workspaces.values {
guard let plugin = workspace.plugin(for: CodeiumWorkspacePlugin.self)
else { continue }
plugin.terminate()
}
}
}
final class ServiceLocator {
let workspacePool: WorkspacePool
init(workspacePool: WorkspacePool) {
self.workspacePool = workspacePool
}
func getService(from workspace: WorkspaceInfo) async -> CodeiumService? {
guard let workspace = workspacePool.workspaces[workspace.workspaceURL],
let plugin = workspace.plugin(for: CodeiumWorkspacePlugin.self)
else { return nil }
return await plugin.codeiumService
}
}
/// A helper class to keep track of a list of items that may keep the service alive.
/// For example, a ``CodeiumChatTab``.
actor CodeiumServiceLifeKeeper {
static let shared = CodeiumServiceLifeKeeper()
private final class WeakObject {
weak var object: AnyObject?
var isAlive: Bool { object != nil }
init(_ object: AnyObject) {
self.object = object
}
}
private var weakObjects = [WeakObject]()
func add(_ object: AnyObject) {
weakObjects.removeAll { !$0.isAlive }
weakObjects.append(WeakObject(object))
}
var isAlive: Bool {
if weakObjects.isEmpty { return false }
return weakObjects.allSatisfy { $0.isAlive }
}
}