-
Notifications
You must be signed in to change notification settings - Fork 28k
/
chatClearActions.ts
124 lines (111 loc) · 4.16 KB
/
chatClearActions.ts
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Codicon } from 'vs/base/common/codicons';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { ServicesAccessor } from 'vs/editor/browser/editorExtensions';
import { localize } from 'vs/nls';
import { AccessibleNotificationEvent, IAccessibleNotificationService } from 'vs/platform/accessibility/common/accessibility';
import { Action2, IAction2Options, MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { ViewAction } from 'vs/workbench/browser/parts/views/viewPane';
import { ActiveEditorContext } from 'vs/workbench/common/contextkeys';
import { CHAT_CATEGORY } from 'vs/workbench/contrib/chat/browser/actions/chatActions';
import { clearChatEditor } from 'vs/workbench/contrib/chat/browser/actions/chatClear';
import { IChatWidgetService } from 'vs/workbench/contrib/chat/browser/chat';
import { ChatEditorInput } from 'vs/workbench/contrib/chat/browser/chatEditorInput';
import { ChatViewPane } from 'vs/workbench/contrib/chat/browser/chatViewPane';
import { CONTEXT_IN_CHAT_SESSION, CONTEXT_PROVIDER_EXISTS } from 'vs/workbench/contrib/chat/common/chatContextKeys';
export const ACTION_ID_CLEAR_CHAT = `workbench.action.chat.clear`;
export function registerClearActions() {
registerAction2(class ClearEditorAction extends Action2 {
constructor() {
super({
id: 'workbench.action.chatEditor.clear',
title: {
value: localize('interactiveSession.clear.label', "Clear"),
original: 'Clear'
},
icon: Codicon.clearAll,
f1: false,
precondition: CONTEXT_PROVIDER_EXISTS,
menu: [{
id: MenuId.EditorTitle,
group: 'navigation',
order: 0,
when: ActiveEditorContext.isEqualTo(ChatEditorInput.EditorID),
}]
});
}
async run(accessor: ServicesAccessor, ...args: any[]) {
announceChatCleared(accessor);
await clearChatEditor(accessor);
}
});
registerAction2(class GlobalClearChatAction extends Action2 {
constructor() {
super({
id: ACTION_ID_CLEAR_CHAT,
title: {
value: localize('interactiveSession.clear.label', "Clear"),
original: 'Clear'
},
category: CHAT_CATEGORY,
icon: Codicon.clearAll,
precondition: CONTEXT_PROVIDER_EXISTS,
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyMod.CtrlCmd | KeyCode.KeyL,
mac: {
primary: KeyMod.WinCtrl | KeyCode.KeyL
},
when: CONTEXT_IN_CHAT_SESSION
}
});
}
run(accessor: ServicesAccessor, ...args: any[]) {
announceChatCleared(accessor);
const widgetService = accessor.get(IChatWidgetService);
const widget = widgetService.lastFocusedWidget;
if (!widget) {
return;
}
widget.clear();
}
});
}
const getClearChatActionDescriptorForViewTitle = (viewId: string, providerId: string): Readonly<IAction2Options> & { viewId: string } => ({
viewId,
id: `workbench.action.chat.${providerId}.clear`,
title: {
value: localize('interactiveSession.clear.label', "Clear"),
original: 'Clear'
},
menu: {
id: MenuId.ViewTitle,
when: ContextKeyExpr.equals('view', viewId),
group: 'navigation',
order: 0
},
precondition: CONTEXT_PROVIDER_EXISTS,
category: CHAT_CATEGORY,
icon: Codicon.clearAll,
f1: false
});
export function getClearAction(viewId: string, providerId: string) {
return class ClearAction extends ViewAction<ChatViewPane> {
constructor() {
super(getClearChatActionDescriptorForViewTitle(viewId, providerId));
}
async runInView(accessor: ServicesAccessor, view: ChatViewPane) {
announceChatCleared(accessor);
await view.clear();
}
};
}
function announceChatCleared(accessor: ServicesAccessor): void {
accessor.get(IAccessibleNotificationService).notify(AccessibleNotificationEvent.Clear);
}