|
| 1 | +import CopilotForXcodeKit |
| 2 | +import Foundation |
| 3 | +import Workspace |
| 4 | +import Logger |
| 5 | +import BuiltinExtension |
| 6 | + |
| 7 | +public final class CodeiumExtension: BuiltinExtension { |
| 8 | + public var suggestionService: SuggestionServiceType? { _suggestionService } |
| 9 | + public var chatService: ChatServiceType? { nil } |
| 10 | + public var promptToCodeService: PromptToCodeServiceType? { nil } |
| 11 | + let workspacePool: WorkspacePool |
| 12 | + |
| 13 | + let serviceLocator: ServiceLocator |
| 14 | + let _suggestionService: CodeiumSuggestionService |
| 15 | + |
| 16 | + public init(workspacePool: WorkspacePool) { |
| 17 | + self.workspacePool = workspacePool |
| 18 | + serviceLocator = .init(workspacePool: workspacePool) |
| 19 | + _suggestionService = .init(serviceLocator: serviceLocator) |
| 20 | + } |
| 21 | + |
| 22 | + public func workspaceDidOpen(_: WorkspaceInfo) {} |
| 23 | + |
| 24 | + public func workspaceDidClose(_: WorkspaceInfo) {} |
| 25 | + |
| 26 | + public func workspace(_ workspace: WorkspaceInfo, didOpenDocumentAt documentURL: URL) { |
| 27 | + // check if file size is larger than 15MB, if so, return immediately |
| 28 | + if let attrs = try? FileManager.default |
| 29 | + .attributesOfItem(atPath: documentURL.path), |
| 30 | + let fileSize = attrs[FileAttributeKey.size] as? UInt64, |
| 31 | + fileSize > 15 * 1024 * 1024 |
| 32 | + { return } |
| 33 | + |
| 34 | + Task { |
| 35 | + do { |
| 36 | + let content = try String(contentsOf: documentURL, encoding: .utf8) |
| 37 | + guard let service = await serviceLocator.getService(from: workspace) else { return } |
| 38 | + try await service.notifyOpenTextDocument(fileURL: documentURL, content: content) |
| 39 | + } catch { |
| 40 | + Logger.gitHubCopilot.error(error.localizedDescription) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public func workspace(_ workspace: WorkspaceInfo, didSaveDocumentAt documentURL: URL) { |
| 46 | + // unimplemented |
| 47 | + } |
| 48 | + |
| 49 | + public func workspace(_ workspace: WorkspaceInfo, didCloseDocumentAt documentURL: URL) { |
| 50 | + Task { |
| 51 | + do { |
| 52 | + guard let service = await serviceLocator.getService(from: workspace) else { return } |
| 53 | + try await service.notifyCloseTextDocument(fileURL: documentURL) |
| 54 | + } catch { |
| 55 | + Logger.gitHubCopilot.error(error.localizedDescription) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public func workspace( |
| 61 | + _ workspace: WorkspaceInfo, |
| 62 | + didUpdateDocumentAt documentURL: URL, |
| 63 | + content: String |
| 64 | + ) { |
| 65 | + // check if file size is larger than 15MB, if so, return immediately |
| 66 | + if let attrs = try? FileManager.default |
| 67 | + .attributesOfItem(atPath: documentURL.path), |
| 68 | + let fileSize = attrs[FileAttributeKey.size] as? UInt64, |
| 69 | + fileSize > 15 * 1024 * 1024 |
| 70 | + { return } |
| 71 | + |
| 72 | + Task { |
| 73 | + do { |
| 74 | + let content = try String(contentsOf: documentURL, encoding: .utf8) |
| 75 | + guard let service = await serviceLocator.getService(from: workspace) else { return } |
| 76 | + try await service.notifyOpenTextDocument(fileURL: documentURL, content: content) |
| 77 | + } catch { |
| 78 | + Logger.gitHubCopilot.error(error.localizedDescription) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public func appConfigurationDidChange(_ configuration: AppConfiguration) { |
| 84 | + if !configuration.chatServiceInUse && !configuration.suggestionServiceInUse { |
| 85 | + for workspace in workspacePool.workspaces.values { |
| 86 | + guard let plugin = workspace.plugin(for: CodeiumWorkspacePlugin.self) |
| 87 | + else { continue } |
| 88 | + plugin.terminate() |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public func terminate() { |
| 94 | + for workspace in workspacePool.workspaces.values { |
| 95 | + guard let plugin = workspace.plugin(for: CodeiumWorkspacePlugin.self) |
| 96 | + else { continue } |
| 97 | + plugin.terminate() |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +final class ServiceLocator { |
| 103 | + let workspacePool: WorkspacePool |
| 104 | + |
| 105 | + init(workspacePool: WorkspacePool) { |
| 106 | + self.workspacePool = workspacePool |
| 107 | + } |
| 108 | + |
| 109 | + func getService(from workspace: WorkspaceInfo) async -> CodeiumService? { |
| 110 | + guard let workspace = workspacePool.workspaces[workspace.workspaceURL], |
| 111 | + let plugin = workspace.plugin(for: CodeiumWorkspacePlugin.self) |
| 112 | + else { return nil } |
| 113 | + return plugin.codeiumService |
| 114 | + } |
| 115 | +} |
| 116 | + |
0 commit comments