forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateCustomCopilotFileView.swift
More file actions
195 lines (173 loc) · 6.86 KB
/
CreateCustomCopilotFileView.swift
File metadata and controls
195 lines (173 loc) · 6.86 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
194
195
import SwiftUI
import ConversationServiceProvider
import AppKitExtension
public struct CreateCustomCopilotFileView: View {
public let promptType: PromptType
public let editorPluginVersion: String
public let getCurrentProjectURL: () async -> URL?
public let onSuccess: (String) -> Void
public let onError: (String) -> Void
@State private var fileName = ""
@State private var projectURL: URL?
@State private var fileAlreadyExists = false
@Environment(\.dismiss) private var dismiss
public init(
promptType: PromptType,
editorPluginVersion: String,
getCurrentProjectURL: @escaping () async -> URL?,
onSuccess: @escaping (String) -> Void,
onError: @escaping (String) -> Void
) {
self.promptType = promptType
self.editorPluginVersion = editorPluginVersion
self.getCurrentProjectURL = getCurrentProjectURL
self.onSuccess = onSuccess
self.onError = onError
}
public var body: some View {
Form {
VStack(alignment: .center, spacing: 20) {
HStack(alignment: .center) {
Spacer()
Text("Create \(promptType.displayName)").font(.headline)
Spacer()
AdaptiveHelpLink(action: openHelpLink)
}
// Content
VStack(alignment: .leading, spacing: 4) {
TextFieldsContainer {
TextField("File name", text: Binding(
get: { fileName },
set: { newValue in
fileName = newValue
updateFileExistence()
}
))
.disableAutocorrection(true)
.textContentType(.none)
.onSubmit {
Task { await createPromptFile() }
}
}
validationMessageView
}
HStack(spacing: 8) {
Spacer()
Button("Cancel", role: .cancel) { dismiss() }
Button("Create") { Task { await createPromptFile() } }
.buttonStyle(.borderedProminent)
.disabled(disableCreateButton)
.keyboardShortcut(.defaultAction)
}
}
.textFieldStyle(.plain)
.multilineTextAlignment(.trailing)
.padding(20)
}
.frame(width: 350, height: 190)
.onAppear {
fileName = ""
Task { await resolveProjectURL() }
}
}
// MARK: - Derived values
private var trimmedFileName: String {
fileName.trimmingCharacters(in: .whitespacesAndNewlines)
}
private var disableCreateButton: Bool {
trimmedFileName.isEmpty || fileAlreadyExists
}
@ViewBuilder
private var validationMessageView: some View {
HStack(alignment: .center, spacing: 6) {
if fileAlreadyExists && !trimmedFileName.isEmpty {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.red)
Text("'.github/\(promptType.directoryName)/\(trimmedFileName)\(promptType.fileExtension)' already exists")
.font(.caption)
.foregroundColor(.red)
.lineLimit(2)
.multilineTextAlignment(.leading)
.truncationMode(.middle)
.fixedSize(horizontal: false, vertical: true)
} else if trimmedFileName.isEmpty {
Image(systemName: "info.circle")
.foregroundColor(.secondary)
Text("Enter the name of \(promptType.rawValue) file")
.font(.caption)
.foregroundColor(.secondary)
} else {
Text("Location:")
.foregroundColor(.primary)
.padding(.leading, 10)
.layoutPriority(1)
Text(".github/\(promptType.directoryName)/\(trimmedFileName)\(promptType.fileExtension)")
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(2)
.multilineTextAlignment(.leading)
.truncationMode(.middle)
.fixedSize(horizontal: false, vertical: true)
}
}
.padding(.horizontal, 2)
}
// MARK: - Actions / Helpers
private func openHelpLink() {
if let url = URL(string: promptType.helpLink(editorPluginVersion: editorPluginVersion)) {
NSWorkspace.shared.open(url)
}
}
/// Resolves the active project URL (if any) and updates state.
private func resolveProjectURL() async {
let projectURL = await getCurrentProjectURL()
await MainActor.run {
self.projectURL = projectURL
updateFileExistence()
}
}
private func updateFileExistence() {
let name = trimmedFileName
guard !name.isEmpty, let projectURL else {
fileAlreadyExists = false
return
}
let filePath = promptType.getFilePath(fileName: name, projectURL: projectURL)
fileAlreadyExists = FileManager.default.fileExists(atPath: filePath.path)
}
/// Creates the prompt file if it doesn't already exist.
private func createPromptFile() async {
guard let projectURL else {
await MainActor.run {
onError("No active workspace found")
}
return
}
let directoryPath = promptType.getDirectoryPath(projectURL: projectURL)
let filePath = promptType.getFilePath(fileName: trimmedFileName, projectURL: projectURL)
// Re-check existence to avoid race with external creation.
if FileManager.default.fileExists(atPath: filePath.path) {
await MainActor.run {
self.fileAlreadyExists = true
onError("\(promptType.displayName) '\(trimmedFileName)\(promptType.fileExtension)' already exists")
}
return
}
do {
try FileManager.default.createDirectory(
at: directoryPath,
withIntermediateDirectories: true
)
try promptType.defaultTemplate.write(to: filePath, atomically: true, encoding: .utf8)
await MainActor.run {
onSuccess("Created \(promptType.rawValue) file '\(trimmedFileName)\(promptType.fileExtension)'")
NSWorkspace.openFileInXcode(fileURL: filePath)
dismiss()
}
} catch {
await MainActor.run {
onError("Failed to create \(promptType.rawValue) file: \(error)")
}
}
}
}