@@ -36,19 +36,78 @@ public protocol CommandHandler {
3636}
3737
3838public struct CommandHandlerDependencyKey : DependencyKey {
39- public static var liveValue : CommandHandler = NoopCommandHandler ( )
39+ public static var liveValue : CommandHandler = UniversalCommandHandler . shared
40+ public static var testValue : CommandHandler = NoopCommandHandler ( )
4041}
4142
4243public extension DependencyValues {
44+ /// In production, you need to override the command handler globally by setting
45+ /// ``UniversalCommandHandler.shared.commandHandler``.
46+ ///
47+ /// In tests, you can use ``withDependency`` to mock it.
4348 var commandHandler : CommandHandler {
4449 get { self [ CommandHandlerDependencyKey . self] }
4550 set { self [ CommandHandlerDependencyKey . self] = newValue }
4651 }
4752}
4853
49- struct NoopCommandHandler : CommandHandler {
50- static let shared : CommandHandler = NoopCommandHandler ( )
54+ public final class UniversalCommandHandler : CommandHandler {
55+ public static let shared : UniversalCommandHandler = UniversalCommandHandler ( )
56+
57+ public var commandHandler : CommandHandler = NoopCommandHandler ( )
58+
59+ private init ( ) { }
60+
61+ public func presentSuggestions( _ suggestions: [ SuggestionBasic . CodeSuggestion ] ) async {
62+ await commandHandler. presentSuggestions ( suggestions)
63+ }
64+
65+ public func presentPreviousSuggestion( ) async {
66+ await commandHandler. presentPreviousSuggestion ( )
67+ }
68+
69+ public func presentNextSuggestion( ) async {
70+ await commandHandler. presentNextSuggestion ( )
71+ }
5172
73+ public func rejectSuggestions( ) async {
74+ await commandHandler. rejectSuggestions ( )
75+ }
76+
77+ public func acceptSuggestion( ) async {
78+ await commandHandler. acceptSuggestion ( )
79+ }
80+
81+ public func dismissSuggestion( ) async {
82+ await commandHandler. dismissSuggestion ( )
83+ }
84+
85+ public func generateRealtimeSuggestions( sourceEditor: SourceEditor ? ) async {
86+ await commandHandler. generateRealtimeSuggestions ( sourceEditor: sourceEditor)
87+ }
88+
89+ public func openChat( forceDetach: Bool ) {
90+ commandHandler. openChat ( forceDetach: forceDetach)
91+ }
92+
93+ public func sendChatMessage( _ message: String ) async {
94+ await commandHandler. sendChatMessage ( message)
95+ }
96+
97+ public func acceptPromptToCode( ) async {
98+ await commandHandler. acceptPromptToCode ( )
99+ }
100+
101+ public func handleCustomCommand( _ command: CustomCommand ) async {
102+ await commandHandler. handleCustomCommand ( command)
103+ }
104+
105+ public func toast( _ string: String , as type: ToastType ) {
106+ commandHandler. toast ( string, as: type)
107+ }
108+ }
109+
110+ struct NoopCommandHandler : CommandHandler {
52111 func presentSuggestions( _: [ CodeSuggestion ] ) async { }
53112 func presentPreviousSuggestion( ) async { }
54113 func presentNextSuggestion( ) async { }
0 commit comments