Skip to content

Commit 79f0895

Browse files
committed
Update chatWithSelection to run without editor content
1 parent 8150253 commit 79f0895

2 files changed

Lines changed: 6 additions & 70 deletions

File tree

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ struct PseudoCommandHandler {
7474
return it
7575
}
7676
switch command.feature {
77-
case .customChat:
77+
// editor content is not required.
78+
case .customChat, .chatWithSelection:
7879
return .init(
7980
content: "",
8081
lines: [],
@@ -85,7 +86,8 @@ struct PseudoCommandHandler {
8586
indentSize: 0,
8687
usesTabsForIndentation: false
8788
)
88-
case .chatWithSelection, .promptToCode:
89+
// editor content is required.
90+
case .promptToCode:
8991
return nil
9092
}
9193
}() else {

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
234234
func chatWithSelection(editor: EditorContent) async throws -> UpdatedContent? {
235235
Task {
236236
do {
237-
try await startChatWithSelection(
238-
editor: editor,
237+
try await startChat(
239238
specifiedSystemPrompt: nil,
240239
extraSystemPrompt: nil,
241240
sendingMessageImmediately: nil,
@@ -289,8 +288,7 @@ extension WindowBaseCommandHandler {
289288

290289
switch command.feature {
291290
case let .chatWithSelection(extraSystemPrompt, prompt):
292-
try await startChatWithSelection(
293-
editor: editor,
291+
try await startChat(
294292
specifiedSystemPrompt: nil,
295293
extraSystemPrompt: extraSystemPrompt,
296294
sendingMessageImmediately: prompt,
@@ -385,69 +383,6 @@ extension WindowBaseCommandHandler {
385383

386384
presenter.presentPromptToCode(fileURL: fileURL)
387385
}
388-
389-
private func startChatWithSelection(
390-
editor: EditorContent,
391-
specifiedSystemPrompt: String?,
392-
extraSystemPrompt: String?,
393-
sendingMessageImmediately: String?,
394-
name: String?
395-
) async throws {
396-
presenter.markAsProcessing(true)
397-
defer { presenter.markAsProcessing(false) }
398-
399-
let fileURL = try await Environment.fetchCurrentFileURL()
400-
401-
let code = {
402-
guard let selection = editor.selections.last,
403-
selection.start != selection.end else { return "" }
404-
return editor.selectedCode(in: selection)
405-
}()
406-
407-
let chat = WidgetDataSource.shared.createChatIfNeeded(for: fileURL)
408-
409-
chat.mutateSystemPrompt(specifiedSystemPrompt)
410-
chat.mutateExtraSystemPrompt(extraSystemPrompt ?? "")
411-
412-
Task {
413-
let customCommandPrefix = {
414-
if let name { return "[\(name)] " }
415-
return ""
416-
}()
417-
418-
if specifiedSystemPrompt != nil {
419-
await chat.chatGPTService.mutateHistory { history in
420-
history.append(.init(
421-
role: .assistant,
422-
content: "",
423-
summary: "\(customCommandPrefix)System prompt is updated."
424-
))
425-
}
426-
} else if !code.isEmpty, let selection = editor.selections.last {
427-
await chat.chatGPTService.mutateHistory { history in
428-
history.append(.init(
429-
role: .assistant,
430-
content: "",
431-
summary: "\(customCommandPrefix)Chatting about selected code in `\(fileURL.lastPathComponent)` from `\(selection.start.line + 1):\(selection.start.character + 1)` to `\(selection.end.line + 1):\(selection.end.character)`.\nThe code will persist in the conversation."
432-
))
433-
}
434-
} else if !customCommandPrefix.isEmpty {
435-
await chat.chatGPTService.mutateHistory { history in
436-
history.append(.init(
437-
role: .assistant,
438-
content: "",
439-
summary: "\(customCommandPrefix)System prompt is updated."
440-
))
441-
}
442-
}
443-
444-
if let sendingMessageImmediately, !sendingMessageImmediately.isEmpty {
445-
try await chat.send(content: sendingMessageImmediately)
446-
}
447-
}
448-
449-
presenter.presentChatRoom(fileURL: fileURL)
450-
}
451386

452387
private func startChat(
453388
specifiedSystemPrompt: String?,
@@ -459,7 +394,6 @@ extension WindowBaseCommandHandler {
459394
defer { presenter.markAsProcessing(false) }
460395

461396
let focusedElementURI = try await Environment.fetchFocusedElementURI()
462-
let language = UserDefaults.shared.value(for: \.chatGPTLanguage)
463397

464398
let chat = WidgetDataSource.shared.createChatIfNeeded(for: focusedElementURI)
465399

0 commit comments

Comments
 (0)