Skip to content

Commit e3bf946

Browse files
committed
Fix that command handler is not correctly setup
1 parent 1428dcb commit e3bf946

File tree

3 files changed

+80
-30
lines changed

3 files changed

+80
-30
lines changed

Core/Sources/Service/Service.swift

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,27 @@ public final class Service {
4949
private init() {
5050
@Dependency(\.workspacePool) var workspacePool
5151
let commandHandler = PseudoCommandHandler()
52+
UniversalCommandHandler.shared.commandHandler = commandHandler
5253
self.commandHandler = commandHandler
5354

54-
func setup<R>(_ operation: () -> R) -> R {
55-
withDependencies { values in
56-
values.commandHandler = commandHandler
57-
} operation: {
58-
operation() //
59-
}
60-
}
61-
62-
realtimeSuggestionController = setup { .init() }
63-
scheduledCleaner = setup { .init() }
64-
let guiController = setup { GraphicalUserInterfaceController() }
55+
realtimeSuggestionController = .init()
56+
scheduledCleaner = .init()
57+
let guiController = GraphicalUserInterfaceController()
6558
self.guiController = guiController
66-
globalShortcutManager = setup { .init(guiController: guiController) }
67-
68-
keyBindingManager = setup {
69-
.init(
70-
workspacePool: workspacePool,
71-
acceptSuggestion: {
72-
Task { await PseudoCommandHandler().acceptSuggestion() }
73-
},
74-
dismissSuggestion: {
75-
Task { await PseudoCommandHandler().dismissSuggestion() }
76-
}
77-
)
78-
}
59+
globalShortcutManager = .init(guiController: guiController)
60+
61+
keyBindingManager = .init(
62+
workspacePool: workspacePool,
63+
acceptSuggestion: {
64+
Task { await PseudoCommandHandler().acceptSuggestion() }
65+
},
66+
dismissSuggestion: {
67+
Task { await PseudoCommandHandler().dismissSuggestion() }
68+
}
69+
)
7970

8071
#if canImport(ProService)
81-
proService = setup { ProService() }
72+
proService = ProService()
8273
#endif
8374

8475
BuiltinExtensionManager.shared.setupExtensions([
@@ -149,7 +140,7 @@ public extension Service {
149140
) {
150141
do {
151142
#if canImport(ProService)
152-
try Service.shared.proService.handleXPCServiceRequests(
143+
try proService.handleXPCServiceRequests(
153144
endpoint: endpoint,
154145
requestBody: requestBody,
155146
reply: reply

Tool/Sources/CommandHandler/CommandHandler.swift

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,78 @@ public protocol CommandHandler {
3636
}
3737

3838
public 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

4243
public 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 {}

Tool/Sources/SharedUIComponents/AsyncCodeBlock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44
import Perception
55
import SwiftUI
66

7-
public struct AsyncCodeBlock: View {
7+
public struct AsyncCodeBlock: View { // chat: hid
88
@State var storage = Storage()
99
@Environment(\.colorScheme) var colorScheme
1010

0 commit comments

Comments
 (0)