|
| 1 | +// |
| 2 | +// BaseCommand.swift |
| 3 | +// ChatGPTForXcodeEditorExtension |
| 4 | +// |
| 5 | +// Created by TAISHIN MIYAMOTO on 2023/03/20 |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import XcodeKit |
| 11 | + |
| 12 | +class BaseCommand: NSObject, XCSourceEditorCommand { |
| 13 | + func perform( |
| 14 | + with invocation: XCSourceEditorCommandInvocation, |
| 15 | + completionHandler: @escaping (Error?) -> Void |
| 16 | + ) { |
| 17 | + Task { |
| 18 | + let buffer = invocation.buffer |
| 19 | + guard let selections = buffer.selections as? [XCSourceTextRange], |
| 20 | + let selection = selections.first |
| 21 | + else { |
| 22 | + completionHandler(nil) |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + let lines = (selection.start.line ... selection.end.line) |
| 27 | + .filter { $0 < buffer.lines.count } |
| 28 | + .compactMap { buffer.lines[$0] as? String } |
| 29 | + |
| 30 | + let code = lines.enumerated() |
| 31 | + .map { lineIndex, line -> String in |
| 32 | + if lineIndex == selection.start.line { |
| 33 | + return String(line.dropFirst(selection.start.column)) |
| 34 | + } else if lineIndex == selection.end.line { |
| 35 | + return String(line.prefix(selection.end.column)) |
| 36 | + } else { |
| 37 | + return line |
| 38 | + } |
| 39 | + } |
| 40 | + .joined() |
| 41 | + |
| 42 | + let indentCount = lines |
| 43 | + .first { !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } |
| 44 | + .map { $0.prefix(while: { $0 == " " }).count } ?? 0 |
| 45 | + |
| 46 | + let apiKeyRepository = APIKeyRepository() |
| 47 | + |
| 48 | + let languageRepository = LanguageRepository() |
| 49 | + |
| 50 | + let authToken = apiKeyRepository.getAPIKey() |
| 51 | + |
| 52 | + let language = languageRepository.getSelectedLanguage() |
| 53 | + |
| 54 | + let content = prompt(code, language: language) |
| 55 | + |
| 56 | + do { |
| 57 | + let messageResult = try await ChatGPTClient.send( |
| 58 | + authToken: authToken, |
| 59 | + chatMessages: [ |
| 60 | + .init(role: .system, content: "The assistant should act as the Tech Lead Engineer for iOS."), |
| 61 | + .init(role: .user, content: content) |
| 62 | + ] |
| 63 | + ) |
| 64 | + let indentSpace = String(repeating: " ", count: indentCount) |
| 65 | + let markerComment = "\(indentSpace)// MARK: \(commandType.rawValue)" |
| 66 | + var reviewComment = messageResult.choices.first?.message.content ?? "" |
| 67 | + reviewComment = reviewComment |
| 68 | + .split(separator: "\n") |
| 69 | + .map { "\(indentSpace)/// \($0)" } |
| 70 | + .joined(separator: "\n") |
| 71 | + let comments = [markerComment, reviewComment] |
| 72 | + let result = comments.joined(separator: "\n") |
| 73 | + buffer.lines.insert(result, at: selection.start.line) |
| 74 | + completionHandler(nil) |
| 75 | + } catch { |
| 76 | + completionHandler(error) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + var commandType: Command { |
| 82 | + fatalError("`commandType` must be implemented in subclasses") |
| 83 | + } |
| 84 | + |
| 85 | + func prompt(_ code: String, language: Language) -> String { |
| 86 | + fatalError("`prompt` must be implemented in subclasses") |
| 87 | + } |
| 88 | +} |
0 commit comments