Skip to content

Commit 53a5d3e

Browse files
committed
Add presentModification and presentFile to command handler
1 parent c03b403 commit 53a5d3e

File tree

3 files changed

+129
-21
lines changed

3 files changed

+129
-21
lines changed

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import AppKit
33
import BuiltinExtension
44
import CodeiumService
55
import CommandHandler
6+
import ComposableArchitecture
67
import enum CopilotForXcodeKit.SuggestionServiceError
78
import Dependencies
89
import Logger
10+
import ModificationBasic
911
import PlusFeatureFlag
1012
import Preferences
1113
import SuggestionBasic
1214
import SuggestionInjector
15+
import Terminal
1316
import Toast
1417
import Workspace
1518
import WorkspaceSuggestionService
@@ -268,6 +271,38 @@ struct PseudoCommandHandler: CommandHandler {
268271
}
269272
}
270273

274+
func presentModification(fileURL: URL, snippets: [ModificationSnippet]) async {
275+
do {
276+
@Dependency(\.workspacePool) var workspacePool
277+
let (workspace, filespace) = try await workspacePool
278+
.fetchOrCreateWorkspaceAndFilespace(fileURL: fileURL)
279+
let store = await Service.shared.guiController.store
280+
let content = try String(contentsOf: fileURL)
281+
let lines = content.breakLines(appendLineBreakToLastLine: false)
282+
await store.send(.promptToCodeGroup(.activateOrCreatePromptToCode(.init(
283+
promptToCodeState: Shared(.init(
284+
source: .init(
285+
language: filespace.language,
286+
documentURL: filespace.fileURL,
287+
projectRootURL: workspace.projectRootURL,
288+
content: content,
289+
lines: lines
290+
),
291+
snippets: IdentifiedArray(uniqueElements: snippets),
292+
instruction: "",
293+
extraSystemPrompt: "",
294+
isAttachedToTarget: true
295+
)),
296+
indentSize: filespace.codeMetadata.indentSize ?? 4,
297+
usesTabsForIndentation: filespace.codeMetadata.usesTabsForIndentation ?? false,
298+
commandName: nil,
299+
isContinuous: false
300+
))))
301+
} catch {
302+
toast.toast(content: error.localizedDescription, type: .error)
303+
}
304+
}
305+
271306
func acceptSuggestion() async {
272307
do {
273308
if UserDefaults.shared.value(for: \.alwaysAcceptSuggestionWithAccessibilityAPI) {
@@ -481,6 +516,22 @@ struct PseudoCommandHandler: CommandHandler {
481516
store.send(.suggestionWidget(.toastPanel(.toast(.toast(message, type, nil)))))
482517
}
483518
}
519+
520+
func presentFile(at fileURL: URL, line: Int = 0) async {
521+
let terminal = Terminal()
522+
do {
523+
_ = try await terminal.runCommand(
524+
"/bin/bash",
525+
arguments: [
526+
"-c",
527+
"xed -l \(line) \"\(fileURL.path)\"",
528+
],
529+
environment: [:]
530+
)
531+
} catch {
532+
print(error)
533+
}
534+
}
484535
}
485536

486537
extension PseudoCommandHandler {

Tool/Sources/CommandHandler/CommandHandler.swift

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Dependencies
22
import Foundation
3+
import ModificationBasic
34
import Preferences
45
import SuggestionBasic
56
import Toast
@@ -22,9 +23,10 @@ public protocol CommandHandler {
2223
func openChat(forceDetach: Bool, activateThisApp: Bool)
2324
func sendChatMessage(_ message: String) async
2425

25-
// MARK: Prompt to Code
26+
// MARK: Modification
2627

2728
func acceptPromptToCode() async
29+
func presentModification(fileURL: URL, snippets: [ModificationSnippet]) async
2830

2931
// MARK: Custom Command
3032

@@ -33,11 +35,15 @@ public protocol CommandHandler {
3335
// MARK: Toast
3436

3537
func toast(_ string: String, as type: ToastType)
38+
39+
// MARK: Others
40+
41+
func presentFile(at fileURL: URL, line: Int) async
3642
}
3743

3844
public struct CommandHandlerDependencyKey: DependencyKey {
3945
public static var liveValue: CommandHandler = UniversalCommandHandler.shared
40-
public static var testValue: CommandHandler = NoopCommandHandler()
46+
public static var testValue: CommandHandler = NOOPCommandHandler()
4147
}
4248

4349
public extension DependencyValues {
@@ -52,10 +58,10 @@ public extension DependencyValues {
5258
}
5359

5460
public final class UniversalCommandHandler: CommandHandler {
55-
public static let shared: UniversalCommandHandler = UniversalCommandHandler()
56-
57-
public var commandHandler: CommandHandler = NoopCommandHandler()
58-
61+
public static let shared: UniversalCommandHandler = .init()
62+
63+
public var commandHandler: CommandHandler = NOOPCommandHandler()
64+
5965
private init() {}
6066

6167
public func presentSuggestions(_ suggestions: [SuggestionBasic.CodeSuggestion]) async {
@@ -98,27 +104,78 @@ public final class UniversalCommandHandler: CommandHandler {
98104
await commandHandler.acceptPromptToCode()
99105
}
100106

107+
public func presentModification(fileURL: URL, snippets: [ModificationSnippet]) async {
108+
await commandHandler.presentModification(fileURL: fileURL, snippets: snippets)
109+
}
110+
101111
public func handleCustomCommand(_ command: CustomCommand) async {
102112
await commandHandler.handleCustomCommand(command)
103113
}
104114

105115
public func toast(_ string: String, as type: ToastType) {
106116
commandHandler.toast(string, as: type)
107117
}
118+
119+
public func presentFile(at fileURL: URL, line: Int) async {
120+
await commandHandler.presentFile(at: fileURL, line: line)
121+
}
108122
}
109123

110-
struct NoopCommandHandler: CommandHandler {
111-
func presentSuggestions(_: [CodeSuggestion]) async {}
112-
func presentPreviousSuggestion() async {}
113-
func presentNextSuggestion() async {}
114-
func rejectSuggestions() async {}
115-
func acceptSuggestion() async {}
116-
func dismissSuggestion() async {}
117-
func generateRealtimeSuggestions(sourceEditor: SourceEditor?) async {}
118-
func openChat(forceDetach: Bool, activateThisApp: Bool) {}
119-
func sendChatMessage(_: String) async {}
120-
func acceptPromptToCode() async {}
121-
func handleCustomCommand(_: CustomCommand) async {}
122-
func toast(_: String, as: ToastType) {}
124+
struct NOOPCommandHandler: CommandHandler {
125+
func presentSuggestions(_ suggestions: [CodeSuggestion]) async {
126+
print("present \(suggestions.count) suggestions")
127+
}
128+
129+
func presentPreviousSuggestion() async {
130+
print("previous suggestion")
131+
}
132+
133+
func presentNextSuggestion() async {
134+
print("next suggestion")
135+
}
136+
137+
func rejectSuggestions() async {
138+
print("reject suggestions")
139+
}
140+
141+
func acceptSuggestion() async {
142+
print("accept suggestion")
143+
}
144+
145+
func dismissSuggestion() async {
146+
print("dismiss suggestion")
147+
}
148+
149+
func generateRealtimeSuggestions(sourceEditor: SourceEditor?) async {
150+
print("generate realtime suggestions")
151+
}
152+
153+
func openChat(forceDetach: Bool, activateThisApp: Bool) {
154+
print("open chat")
155+
}
156+
157+
func sendChatMessage(_: String) async {
158+
print("send chat message")
159+
}
160+
161+
func acceptPromptToCode() async {
162+
print("accept prompt to code")
163+
}
164+
165+
func presentModification(fileURL: URL, snippets: [ModificationSnippet]) {
166+
print("present modification")
167+
}
168+
169+
func handleCustomCommand(_: CustomCommand) async {
170+
print("handle custom command")
171+
}
172+
173+
func toast(_: String, as: ToastType) {
174+
print("toast")
175+
}
176+
177+
func presentFile(at fileURL: URL, line: Int) async {
178+
print("present file")
179+
}
123180
}
124181

Tool/Sources/ModificationBasic/ModificationAgent.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public enum ModificationAgentResponse {
1010
public struct ModificationAgentRequest {
1111
var code: String
1212
var requirement: String
13-
var source: PromptToCodeSource
13+
var source: ModificationSource
1414
var isDetached: Bool
1515
var extraSystemPrompt: String?
1616
var generateDescriptionRequirement: Bool?
1717

18-
public struct PromptToCodeSource {
18+
public struct ModificationSource {
1919
public var language: CodeLanguage
2020
public var documentURL: URL
2121
public var projectRootURL: URL

0 commit comments

Comments
 (0)