|
| 1 | +import CopilotForXcodeKit |
| 2 | +import Foundation |
| 3 | +import Preferences |
| 4 | +import SuggestionModel |
| 5 | +import SuggestionProvider |
| 6 | + |
| 7 | +public final class BuiltinExtensionSuggestionServiceProvider< |
| 8 | + T: BuiltinExtension |
| 9 | +>: SuggestionServiceProvider { |
| 10 | + public var configuration: SuggestionServiceConfiguration { |
| 11 | + guard let service else { |
| 12 | + return .init( |
| 13 | + acceptsRelevantCodeSnippets: true, |
| 14 | + mixRelevantCodeSnippetsInSource: true, |
| 15 | + acceptsRelevantSnippetsFromOpenedFiles: true |
| 16 | + ) |
| 17 | + } |
| 18 | + |
| 19 | + return service.configuration |
| 20 | + } |
| 21 | + |
| 22 | + let extensionManager: BuiltinExtensionManager |
| 23 | + |
| 24 | + public init( |
| 25 | + extension: T.Type, |
| 26 | + extensionManager: BuiltinExtensionManager = .shared |
| 27 | + ) { |
| 28 | + self.extensionManager = extensionManager |
| 29 | + } |
| 30 | + |
| 31 | + var service: CopilotForXcodeKit.SuggestionServiceType? { |
| 32 | + extensionManager.extensions.first { $0 is T }?.suggestionService |
| 33 | + } |
| 34 | + |
| 35 | + public func getSuggestions( |
| 36 | + _ request: SuggestionProvider.SuggestionRequest, |
| 37 | + workspaceInfo: CopilotForXcodeKit.WorkspaceInfo |
| 38 | + ) async throws -> [SuggestionModel.CodeSuggestion] { |
| 39 | + guard let service else { return [] } |
| 40 | + return try await service.getSuggestions( |
| 41 | + .init( |
| 42 | + fileURL: request.fileURL, |
| 43 | + relativePath: request.relativePath, |
| 44 | + language: .init( |
| 45 | + rawValue: languageIdentifierFromFileURL(request.fileURL).rawValue |
| 46 | + ) ?? .plaintext, |
| 47 | + content: request.content, |
| 48 | + cursorPosition: .init( |
| 49 | + line: request.cursorPosition.line, |
| 50 | + character: request.cursorPosition.character |
| 51 | + ), |
| 52 | + tabSize: request.tabSize, |
| 53 | + indentSize: request.indentSize, |
| 54 | + usesTabsForIndentation: request.usesTabsForIndentation, |
| 55 | + relevantCodeSnippets: request.relevantCodeSnippets.map { $0.converted } |
| 56 | + ), |
| 57 | + workspace: workspaceInfo |
| 58 | + ).map { $0.converted } |
| 59 | + } |
| 60 | + |
| 61 | + public func cancelRequest( |
| 62 | + workspaceInfo: CopilotForXcodeKit.WorkspaceInfo |
| 63 | + ) async { |
| 64 | + guard let service else { return } |
| 65 | + await service.cancelRequest(workspace: workspaceInfo) |
| 66 | + } |
| 67 | + |
| 68 | + public func notifyAccepted( |
| 69 | + _ suggestion: SuggestionModel.CodeSuggestion, |
| 70 | + workspaceInfo: CopilotForXcodeKit.WorkspaceInfo |
| 71 | + ) async { |
| 72 | + guard let service else { return } |
| 73 | + await service.notifyAccepted(suggestion.converted, workspace: workspaceInfo) |
| 74 | + } |
| 75 | + |
| 76 | + public func notifyRejected( |
| 77 | + _ suggestions: [SuggestionModel.CodeSuggestion], |
| 78 | + workspaceInfo: CopilotForXcodeKit.WorkspaceInfo |
| 79 | + ) async { |
| 80 | + guard let service else { return } |
| 81 | + await service.notifyRejected(suggestions.map(\.converted), workspace: workspaceInfo) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +extension SuggestionProvider.SuggestionRequest { |
| 86 | + var converted: CopilotForXcodeKit.SuggestionRequest { |
| 87 | + .init( |
| 88 | + fileURL: fileURL, |
| 89 | + relativePath: relativePath, |
| 90 | + language: .init(rawValue: languageIdentifierFromFileURL(fileURL).rawValue) |
| 91 | + ?? .plaintext, |
| 92 | + content: content, |
| 93 | + cursorPosition: .init( |
| 94 | + line: cursorPosition.line, |
| 95 | + character: cursorPosition.character |
| 96 | + ), |
| 97 | + tabSize: tabSize, |
| 98 | + indentSize: indentSize, |
| 99 | + usesTabsForIndentation: usesTabsForIndentation, |
| 100 | + relevantCodeSnippets: relevantCodeSnippets.map(\.converted) |
| 101 | + ) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +extension SuggestionModel.CodeSuggestion { |
| 106 | + var converted: CopilotForXcodeKit.CodeSuggestion { |
| 107 | + .init( |
| 108 | + id: id, |
| 109 | + text: text, |
| 110 | + position: .init( |
| 111 | + line: position.line, |
| 112 | + character: position.character |
| 113 | + ), |
| 114 | + range: .init( |
| 115 | + start: .init( |
| 116 | + line: range.start.line, |
| 117 | + character: range.start.character |
| 118 | + ), |
| 119 | + end: .init( |
| 120 | + line: range.end.line, |
| 121 | + character: range.end.character |
| 122 | + ) |
| 123 | + ) |
| 124 | + ) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +extension CopilotForXcodeKit.CodeSuggestion { |
| 129 | + var converted: SuggestionModel.CodeSuggestion { |
| 130 | + .init( |
| 131 | + id: id, |
| 132 | + text: text, |
| 133 | + position: .init( |
| 134 | + line: position.line, |
| 135 | + character: position.character |
| 136 | + ), |
| 137 | + range: .init( |
| 138 | + start: .init( |
| 139 | + line: range.start.line, |
| 140 | + character: range.start.character |
| 141 | + ), |
| 142 | + end: .init( |
| 143 | + line: range.end.line, |
| 144 | + character: range.end.character |
| 145 | + ) |
| 146 | + ) |
| 147 | + ) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +extension SuggestionProvider.RelevantCodeSnippet { |
| 152 | + var converted: CopilotForXcodeKit.RelevantCodeSnippet { |
| 153 | + .init(content: content, priority: priority, filePath: filePath) |
| 154 | + } |
| 155 | +} |
| 156 | + |
0 commit comments