Skip to content

Commit fc940d9

Browse files
committed
Add id to CustomCommand
1 parent fffb445 commit fc940d9

15 files changed

+76
-68
lines changed

Copilot for Xcode/CustomCommandView.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ struct CustomCommandView: View {
1616
"Real-time Suggestions",
1717
"Prefetch Suggestions",
1818
"Chat with Selection",
19-
"Prompt to Code",
20-
"# Custom Commands:",
19+
"Prompt to Code"
2120
]
2221

2322
return existed + builtin
@@ -65,6 +64,7 @@ struct CustomCommandView: View {
6564
Spacer()
6665
Button(action: {
6766
editingCommand = .init(isNew: true, command: CustomCommand(
67+
commandId: UUID().uuidString,
6868
name: "New Command",
6969
feature: .chatWithSelection(
7070
extraSystemPrompt: nil,
@@ -236,6 +236,7 @@ struct EditCustomCommandView: View {
236236
}
237237

238238
lazy var newCommand = CustomCommand(
239+
commandId: editingCommand?.command.id ?? UUID().uuidString,
239240
name: name,
240241
feature: {
241242
switch commandType {
@@ -283,7 +284,7 @@ struct EditCustomCommandView: View {
283284
}
284285

285286
if let index = settings.customCommands.firstIndex(where: {
286-
$0.name == originalName
287+
$0.id == newCommand.id
287288
}) {
288289
settings.customCommands[index] = newCommand
289290
} else {
@@ -351,10 +352,12 @@ struct CustomCommandView_Preview: PreviewProvider {
351352
isOpen: .constant(true),
352353
settings: .init(customCommands: .init(wrappedValue: [
353354
.init(
355+
commandId: "1",
354356
name: "Explain Code",
355357
feature: .chatWithSelection(extraSystemPrompt: nil, prompt: "Hello")
356358
),
357359
.init(
360+
commandId: "2",
358361
name: "Refactor Code",
359362
feature: .promptToCode(
360363
extraSystemPrompt: nil,
@@ -363,6 +366,7 @@ struct CustomCommandView_Preview: PreviewProvider {
363366
)
364367
),
365368
.init(
369+
commandId: "3",
366370
name: "Tell Me A Joke",
367371
feature: .customChat(systemPrompt: "Joke", prompt: "")
368372
),
@@ -378,6 +382,7 @@ struct EditCustomCommandView_Preview: PreviewProvider {
378382
editingCommand: .constant(CustomCommandView.EditingCommand(
379383
isNew: false,
380384
command: .init(
385+
commandId: "4",
381386
name: "Explain Code",
382387
feature: .promptToCode(
383388
extraSystemPrompt: nil,

Core/Sources/Client/AsyncXPCService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ public struct AsyncXPCService {
198198
}
199199

200200
public func customCommand(
201-
name: String,
201+
id: String,
202202
editorContent: EditorContent
203203
) async throws -> UpdatedContent? {
204204
try await suggestionRequest(
205205
connection,
206206
editorContent,
207-
{ service in { service.customCommand(name: name, editorContent: $0, withReply: $1) } }
207+
{ service in { service.customCommand(id: id, editorContent: $0, withReply: $1) } }
208208
)
209209
}
210210
}

Core/Sources/Preferences/CustomCommand.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import CryptoKit
23

34
public struct CustomCommand: Codable {
45
/// The custom command feature.
@@ -10,11 +11,33 @@ public struct CustomCommand: Codable {
1011
case customChat(systemPrompt: String?, prompt: String?)
1112
}
1213

14+
public var id: String { commandId ?? legacyId }
15+
public var commandId: String?
1316
public var name: String
1417
public var feature: Feature
1518

16-
public init(name: String, feature: Feature) {
19+
public init(commandId: String, name: String, feature: Feature) {
20+
self.commandId = commandId
1721
self.name = name
1822
self.feature = feature
1923
}
24+
25+
var legacyId: String {
26+
name.sha1HexString
27+
}
28+
}
29+
30+
private extension Digest {
31+
var bytes: [UInt8] { Array(makeIterator()) }
32+
var data: Data { Data(bytes) }
33+
34+
var hexStr: String {
35+
bytes.map { String(format: "%02X", $0) }.joined()
36+
}
37+
}
38+
39+
private extension String {
40+
var sha1HexString: String {
41+
Insecure.SHA1.hash(data: data(using: .utf8) ?? Data()).hexStr
42+
}
2043
}

Core/Sources/Preferences/Keys.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,15 @@ public extension UserDefaultPreferenceKeys {
227227
struct CustomCommandsKey: UserDefaultPreferenceKey {
228228
public let defaultValue: [CustomCommand] = [
229229
.init(
230+
commandId: "BuiltInCustomCommandExplainSelection",
230231
name: "Explain Selection",
231232
feature: .chatWithSelection(
232233
extraSystemPrompt: nil,
233234
prompt: "Explain the code concisely, do not interpret or translate it."
234235
)
235236
),
236237
.init(
238+
commandId: "BuiltInCustomCommandAddDocumentationToSelection",
237239
name: "Add Documentation to Selection",
238240
feature: .promptToCode(
239241
extraSystemPrompt: nil,

Core/Sources/Service/GUI/GraphicalUserInterfaceController.swift.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public final class GraphicalUserInterfaceController {
2525
))
2626
}
2727
}
28-
suggestionWidget.onCustomCommandClicked = { name in
28+
suggestionWidget.onCustomCommandClicked = { command in
2929
Task {
3030
let commandHandler = PseudoCommandHandler()
31-
await commandHandler.handleCustomDomain(name: name)
31+
await commandHandler.handleCustomCommand(command)
3232
}
3333
}
3434
}

Core/Sources/Service/SuggestionCommandHandler/CommentBaseCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ struct CommentBaseCommandHandler: SuggestionCommandHandler {
183183
throw NotSupportedInCommentMode()
184184
}
185185

186-
func customCommand(name: String, editor: EditorContent) async throws -> UpdatedContent? {
186+
func customCommand(id: String, editor: EditorContent) async throws -> UpdatedContent? {
187187
throw NotSupportedInCommentMode()
188188
}
189189
}

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ struct PseudoCommandHandler {
6868
))
6969
}
7070

71-
func handleCustomDomain(name: String) async {
71+
func handleCustomCommand(_ command: CustomCommand) async {
7272
guard let editor = await getEditorContent(sourceEditor: nil)
7373
else {
7474
do {
75-
try await Environment.triggerAction(name)
75+
try await Environment.triggerAction(command.name)
7676
} catch {
7777
let presenter = PresentInWindowSuggestionPresenter()
7878
presenter.presentError(error)
@@ -82,7 +82,7 @@ struct PseudoCommandHandler {
8282

8383
let handler = WindowBaseCommandHandler()
8484
do {
85-
try await handler.handleCustomCommand(name: name, editor: editor)
85+
try await handler.handleCustomCommand(id: command.id, editor: editor)
8686
} catch {
8787
let presenter = PresentInWindowSuggestionPresenter()
8888
presenter.presentError(error)

Core/Sources/Service/SuggestionCommandHandler/SuggestionCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ protocol SuggestionCommandHandler {
2121
@ServiceActor
2222
func promptToCode(editor: EditorContent) async throws -> UpdatedContent?
2323
@ServiceActor
24-
func customCommand(name: String, editor: EditorContent) async throws -> UpdatedContent?
24+
func customCommand(id: String, editor: EditorContent) async throws -> UpdatedContent?
2525
}

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,10 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
263263
return nil
264264
}
265265

266-
func customCommand(name: String, editor: EditorContent) async throws -> UpdatedContent? {
266+
func customCommand(id: String, editor: EditorContent) async throws -> UpdatedContent? {
267267
Task {
268268
do {
269-
try await handleCustomCommand(name: name, editor: editor)
269+
try await handleCustomCommand(id: id, editor: editor)
270270
} catch {
271271
presenter.presentError(error)
272272
}
@@ -276,13 +276,13 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
276276
}
277277

278278
extension WindowBaseCommandHandler {
279-
func handleCustomCommand(name: String, editor: EditorContent) async throws {
279+
func handleCustomCommand(id: String, editor: EditorContent) async throws {
280280
struct CommandNotFoundError: Error, LocalizedError {
281281
var errorDescription: String? { "Command not found" }
282282
}
283283

284284
let availableCommands = UserDefaults.shared.value(for: \.customCommands)
285-
guard let command = availableCommands.first(where: { $0.name == name })
285+
guard let command = availableCommands.first(where: { $0.id == id })
286286
else { throw CommandNotFoundError() }
287287

288288
switch command.feature {

Core/Sources/Service/XPCService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ public class XPCService: NSObject, XPCServiceProtocol {
240240
}
241241

242242
public func customCommand(
243-
name: String,
243+
id: String,
244244
editorContent: Data,
245245
withReply reply: @escaping (Data?, Error?) -> Void
246246
) {
247247
replyWithUpdatedContent(editorContent: editorContent, withReply: reply) { handler, editor in
248-
try await handler.customCommand(name: name, editor: editor)
248+
try await handler.customCommand(id: id, editor: editor)
249249
}
250250
}
251251

0 commit comments

Comments
 (0)