Skip to content

Commit b94ab81

Browse files
committed
Support using {{selected_code}} in custom chat command
1 parent 92ec079 commit b94ab81

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Foundation
2+
import SuggestionModel
3+
import XcodeInspector
4+
5+
struct CustomCommandTemplateProcessor {
6+
func process(_ text: String) -> String {
7+
let info = getEditorInformation()
8+
if let editorContent = info.editorContent {
9+
let updatedText = text.replacingOccurrences(of: "{{selected_code}}", with: """
10+
```\(info.language.rawValue)
11+
\(editorContent.selectedContent.trimmingCharacters(in: ["\n"]))
12+
```
13+
""")
14+
return updatedText
15+
} else {
16+
let updatedText = text.replacingOccurrences(of: "{{selected_code}}", with: "")
17+
return updatedText
18+
}
19+
}
20+
21+
struct EditorInformation {
22+
let editorContent: SourceEditor.Content?
23+
let language: CodeLanguage
24+
}
25+
26+
func getEditorInformation() -> EditorInformation {
27+
let editorContent = XcodeInspector.shared.focusedEditor?.content
28+
let documentURL = XcodeInspector.shared.activeDocumentURL
29+
let language = languageIdentifierFromFileURL(documentURL)
30+
31+
return .init(
32+
editorContent: editorContent,
33+
language: language
34+
)
35+
}
36+
}
37+

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import ChatService
2-
import SuggestionModel
3-
import GitHubCopilotService
42
import Environment
53
import Foundation
4+
import GitHubCopilotService
65
import LanguageServerProtocol
76
import Logger
87
import OpenAIService
98
import SuggestionInjector
9+
import SuggestionModel
1010
import SuggestionWidget
1111
import XPCShared
1212

@@ -53,7 +53,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
5353
forFileAt: fileURL,
5454
editor: editor
5555
)
56-
56+
5757
try Task.checkCancellation()
5858

5959
if filespace.presentingSuggestion != nil {
@@ -388,7 +388,7 @@ extension WindowBaseCommandHandler {
388388

389389
presenter.presentPromptToCode(fileURL: fileURL)
390390
}
391-
391+
392392
private func startChat(
393393
specifiedSystemPrompt: String?,
394394
extraSystemPrompt: String?,
@@ -402,15 +402,16 @@ extension WindowBaseCommandHandler {
402402

403403
let chat = WidgetDataSource.shared.createChatIfNeeded(for: focusedElementURI)
404404

405-
chat.mutateSystemPrompt(specifiedSystemPrompt)
406-
chat.mutateExtraSystemPrompt(extraSystemPrompt ?? "")
405+
let templateProcessor = CustomCommandTemplateProcessor()
406+
chat.mutateSystemPrompt(specifiedSystemPrompt.map(templateProcessor.process))
407+
chat.mutateExtraSystemPrompt(extraSystemPrompt.map(templateProcessor.process) ?? "")
407408

408409
Task {
409410
let customCommandPrefix = {
410411
if let name { return "[\(name)] " }
411412
return ""
412413
}()
413-
414+
414415
if specifiedSystemPrompt != nil || extraSystemPrompt != nil {
415416
await chat.chatGPTService.mutateHistory { history in
416417
history.append(.init(
@@ -422,10 +423,12 @@ extension WindowBaseCommandHandler {
422423
}
423424

424425
if let sendingMessageImmediately, !sendingMessageImmediately.isEmpty {
425-
try await chat.send(content: sendingMessageImmediately)
426+
try await chat
427+
.send(content: templateProcessor.process(sendingMessageImmediately))
426428
}
427429
}
428430

429431
presenter.presentChatRoom(fileURL: focusedElementURI)
430432
}
431433
}
434+

Core/Sources/XcodeInspector/SourceEditor.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ public class SourceEditor {
1616
public var cursorPosition: CursorPosition
1717
/// Line annotations of the source editor.
1818
public var lineAnnotations: [String]
19+
20+
public var selectedContent: String {
21+
if let range = selections.first {
22+
let startIndex = min(
23+
max(0, range.start.line),
24+
lines.endIndex - 1
25+
)
26+
let endIndex = min(
27+
max(startIndex, range.end.line),
28+
lines.endIndex - 1
29+
)
30+
let selectedContent = lines[startIndex...endIndex]
31+
return selectedContent.joined()
32+
}
33+
return ""
34+
}
1935
}
2036

2137
let runningApplication: NSRunningApplication

0 commit comments

Comments
 (0)