Skip to content

Commit 0db351f

Browse files
authored
Merge pull request #23 from Avis-Inc/develop
0.1.0
2 parents 36041fd + b52dc9c commit 0db351f

26 files changed

Lines changed: 1095 additions & 610 deletions

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,6 @@ BuildTools/.build
9797
BuildTools/.swiftpm
9898
BuildTools/Package.resolved
9999

100-
*.swp
100+
*.swp
101+
102+
*.xcodeproj

ChatGPTForXcode.xcodeproj/project.pbxproj

Lines changed: 0 additions & 521 deletions
This file was deleted.

ChatGPTForXcode.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 0 additions & 7 deletions
This file was deleted.

ChatGPTForXcode/ChatGPTForXcode.entitlements

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>com.apple.security.app-sandbox</key>
6-
<true/>
7-
<key>com.apple.security.files.user-selected.read-only</key>
8-
<true/>
5+
<key>com.apple.security.app-sandbox</key>
6+
<true/>
7+
<key>com.apple.security.application-groups</key>
8+
<array>
9+
<string>com.ChatGPTForXcode.UserDefaults</string>
10+
</array>
911
</dict>
1012
</plist>
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//
22
// ChatGPTForXcodeApp.swift
33
// ChatGPTForXcode
4-
//
4+
//
55
// Created by TAISHIN MIYAMOTO on 2023/03/08
6-
//
6+
//
77
//
88

99
import SwiftUI
@@ -12,7 +12,8 @@ import SwiftUI
1212
struct ChatGPTForXcodeApp: App {
1313
var body: some Scene {
1414
WindowGroup {
15-
ContentView()
15+
ConfigurationView()
1616
}
17+
.windowResizability(.contentSize)
1718
}
1819
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// PreferencesView.swift
3+
// ChatGPTForXcode
4+
//
5+
// Created by 安部翔太 on 2023/03/17.
6+
//
7+
8+
import SwiftUI
9+
10+
struct ConfigurationView: View {
11+
private let apiKeyRepository = APIKeyRepository()
12+
13+
private let languageRepository = LanguageRepository()
14+
15+
@State private var apiKey = ""
16+
17+
@State private var selectedLanguage = Language.english
18+
19+
var body: some View {
20+
NavigationStack {
21+
VStack(spacing: 10) {
22+
headline("1. Obtain your API Key from OpenAI.")
23+
24+
link()
25+
26+
headline("2. Input your API Key.")
27+
28+
TextField("sk-...", text: $apiKey)
29+
30+
headline("3. Specify the output language setting.")
31+
32+
languagePicker()
33+
}
34+
.padding(.init(top: 25, leading: 25, bottom: 25, trailing: 28))
35+
.frame(width: 400, height: 230, alignment: .center)
36+
.onAppear {
37+
apiKey = apiKeyRepository.getAPIKey()
38+
selectedLanguage = languageRepository.getSelectedLanguage()
39+
}
40+
.onChange(of: apiKey, perform: apiKeyRepository.saveAPIKey(apiKey:))
41+
.onChange(of: selectedLanguage, perform: languageRepository.saveSelectedLanguage(language:))
42+
.navigationTitle("ChatGPT for Xcode")
43+
.toolbar {
44+
toolbarButton()
45+
}
46+
}
47+
}
48+
}
49+
50+
extension ConfigurationView {
51+
private func headline(_ text: String) -> some View {
52+
Text(text)
53+
.font(.title2)
54+
.fontWeight(.semibold)
55+
.frame(maxWidth: .infinity, alignment: .leading)
56+
}
57+
58+
@ViewBuilder
59+
private func link() -> some View {
60+
let urlString = "https://platform.openai.com/account/api-keys"
61+
if let url = URL(string: urlString) {
62+
Link(urlString, destination: url)
63+
.frame(maxWidth: .infinity, alignment: .leading)
64+
}
65+
}
66+
67+
private func languagePicker() -> some View {
68+
Picker("", selection: $selectedLanguage) {
69+
ForEach(Language.allCases, id: \.self) { language in
70+
Text(language.name)
71+
}
72+
}
73+
.labelsHidden()
74+
}
75+
76+
private func toolbarButton() -> some View {
77+
Button {
78+
orderFrontStandardAboutPanel()
79+
} label: {
80+
Image(systemName: "info.circle")
81+
}
82+
.fontWeight(.bold)
83+
}
84+
85+
private func orderFrontStandardAboutPanel() {
86+
NSApplication.shared.orderFrontStandardAboutPanel(
87+
options: [NSApplication.AboutPanelOptionKey(rawValue: "Copyright"): "© 2023 Avis Inc"]
88+
)
89+
}
90+
}
91+
92+
struct ConfigurationView_Previews: PreviewProvider {
93+
static var previews: some View {
94+
ConfigurationView()
95+
}
96+
}

ChatGPTForXcode/ContentView.swift

Lines changed: 0 additions & 27 deletions
This file was deleted.

ChatGPTForXcode.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist renamed to ChatGPTForXcode/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>IDEDidComputeMac32BitWarning</key>
6-
<true/>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
77
</dict>
88
</plist>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

ChatGPTForXcodeEditorExtension/ChatGPTForXcodeEditorExtension.entitlements

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
<dict>
55
<key>com.apple.security.app-sandbox</key>
66
<true/>
7+
<key>com.apple.security.network.client</key>
8+
<true/>
9+
<key>com.apple.security.application-groups</key>
10+
<array>
11+
<string>com.ChatGPTForXcode.UserDefaults</string>
12+
</array>
713
</dict>
814
</plist>

0 commit comments

Comments
 (0)