-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathShowMessageRequestHandler.swift
More file actions
89 lines (73 loc) · 3 KB
/
ShowMessageRequestHandler.swift
File metadata and controls
89 lines (73 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import JSONRPC
import Foundation
import Combine
import Logger
import AppKit
import LanguageServerProtocol
import NotificationCenterCoordinator
import UserNotifications
public protocol ShowMessageRequestHandler {
func handleShowMessageRequest(
_ request: ShowMessageRequest,
callback: @escaping @Sendable (Result<MessageActionItem?, JSONRPCResponseError<JSONValue>>) async -> Void
)
}
public final class ShowMessageRequestHandlerImpl: ShowMessageRequestHandler {
public static let shared = ShowMessageRequestHandlerImpl()
private init() {}
public func handleShowMessageRequest(
_ request: ShowMessageRequest,
callback: @escaping @Sendable (Result<MessageActionItem?, JSONRPCResponseError<JSONValue>>) async -> Void
) {
guard let params = request.params else { return }
Logger.gitHubCopilot.debug("Received Show Message Request: \(params)")
Task { @MainActor in
await NotificationCenterCoordinator.shared.setupIfNeeded()
let actionCount = params.actions?.count ?? 0
// Use notification for messages with no action, alert for messages with actions
if actionCount == 0 {
await showMessageRequestNotification(params)
await callback(.success(nil))
} else {
let selectedAction = showMessageRequestAlert(params)
await callback(.success(selectedAction))
}
}
}
@MainActor
func showMessageRequestNotification(_ params: ShowMessageRequestParams) async {
let content = UNMutableNotificationContent()
content.title = "GitHub Copilot for Xcode"
content.body = params.message
content.sound = .default
let request = UNNotificationRequest(
identifier: UUID().uuidString,
content: content,
trigger: nil
)
do {
try await UNUserNotificationCenter.current().add(request)
} catch {
Logger.gitHubCopilot.error("Failed to show notification: \(error)")
}
}
@MainActor
func showMessageRequestAlert(_ params: ShowMessageRequestParams) -> MessageActionItem? {
let alert = NSAlert()
alert.messageText = "GitHub Copilot"
alert.informativeText = params.message
alert.alertStyle = params.type == .info ? .informational : .warning
let actions = params.actions ?? []
for item in actions {
alert.addButton(withTitle: item.title)
}
let response = alert.runModal()
// Map the button response to the corresponding action
// .alertFirstButtonReturn = 1000, .alertSecondButtonReturn = 1001, etc.
let buttonIndex = response.rawValue - NSApplication.ModalResponse.alertFirstButtonReturn.rawValue
guard buttonIndex >= 0 && buttonIndex < actions.count else {
return nil
}
return actions[buttonIndex]
}
}