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
192 lines (164 loc) · 6.37 KB
/
CreateCustomCopilotFileView.swift
File metadata and controls
192 lines (164 loc) · 6.37 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
import Client
import SwiftUI
import XcodeInspector
struct CreateCustomCopilotFileView: View {
var isOpen: Binding<Bool>
let promptType: PromptType
@State private var fileName = ""
@State private var projectURL: URL?
@State private var fileAlreadyExists = false
@Environment(\.toast) var toast
init(isOpen: Binding<Bool>, promptType: PromptType) {
self.isOpen = isOpen
self.promptType = promptType
}
var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack(alignment: .center) {
Button(action: { self.isOpen.wrappedValue = false }) {
Image(systemName: "xmark.circle.fill")
.foregroundStyle(.secondary)
.padding()
}
.buttonStyle(.plain)
Text("Create \(promptType.displayName)")
.font(.system(size: 13, weight: .bold))
Spacer()
AdaptiveHelpLink(action: openHelpLink)
.padding()
}
.frame(height: 28)
.background(Color(nsColor: .separatorColor))
// Content
VStack(alignment: .leading, spacing: 8) {
Text("Enter the name of \(promptType.rawValue) file:")
.font(.body)
TextField("File name", text: $fileName)
.textFieldStyle(.roundedBorder)
.onSubmit {
Task { await createPromptFile() }
}
.onChange(of: fileName) { _ in
updateFileExistence()
}
validationMessageView
Spacer()
HStack(spacing: 12) {
Spacer()
Button("Cancel") {
self.isOpen.wrappedValue = false
}
.buttonStyle(.bordered)
Button("Create") {
Task { await createPromptFile() }
}
.buttonStyle(.borderedProminent)
.disabled(disableCreateButton)
}
}
.padding(.vertical, 8)
.padding(.horizontal, 20)
}
.frame(width: 350, height: 160)
.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)
.fixedSize(horizontal: false, vertical: true)
.layoutPriority(1)
} else if trimmedFileName.isEmpty {
Image(systemName: "info.circle")
.foregroundColor(.secondary)
Text("Enter a file name")
.font(.caption)
.foregroundColor(.secondary)
} else {
Text(".github/\(promptType.directoryName)/\(trimmedFileName)\(promptType.fileExtension)")
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(2)
.multilineTextAlignment(.leading)
.fixedSize(horizontal: false, vertical: true)
.layoutPriority(1)
}
}
.transition(.opacity)
}
// MARK: - Actions / Helpers
private func openHelpLink() {
if let url = URL(string: promptType.helpLink) {
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 {
toast("No active workspace found", .error)
}
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
toast("\(promptType.displayName) '\(trimmedFileName)\(promptType.fileExtension)' already exists", .warning)
}
return
}
do {
try FileManager.default.createDirectory(
at: directoryPath,
withIntermediateDirectories: true
)
try promptType.defaultTemplate.write(to: filePath, atomically: true, encoding: .utf8)
await MainActor.run {
toast("Created \(promptType.rawValue) file '\(trimmedFileName)\(promptType.fileExtension)'", .info)
NSWorkspace.shared.open(filePath)
self.isOpen.wrappedValue = false
}
} catch {
await MainActor.run {
toast("Failed to create \(promptType.rawValue) file: \(error)", .error)
}
}
}
}