-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
terminalEditor.ts
328 lines (289 loc) · 14.5 KB
/
terminalEditor.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as dom from 'vs/base/browser/dom';
import { IActionViewItem } from 'vs/base/browser/ui/actionbar/actionbar';
import { Action, IAction, Separator, SubmenuAction } from 'vs/base/common/actions';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Codicon } from 'vs/base/common/codicons';
import { FindReplaceState } from 'vs/editor/contrib/find/findState';
import { localize } from 'vs/nls';
import { DropdownWithPrimaryActionViewItem } from 'vs/platform/actions/browser/dropdownWithPrimaryActionViewItem';
import { IMenu, IMenuActionOptions, IMenuService, MenuId, MenuItemAction } from 'vs/platform/actions/common/actions';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IEditorOptions } from 'vs/platform/editor/common/editor';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { ICreateTerminalOptions, ITerminalProfile, TerminalLocation } from 'vs/platform/terminal/common/terminal';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { EditorPane } from 'vs/workbench/browser/parts/editor/editorPane';
import { IEditorOpenContext } from 'vs/workbench/common/editor';
import { ITerminalEditorService, ITerminalService } from 'vs/workbench/contrib/terminal/browser/terminal';
import { TerminalEditorInput } from 'vs/workbench/contrib/terminal/browser/terminalEditorInput';
import { TerminalFindWidget } from 'vs/workbench/contrib/terminal/browser/terminalFindWidget';
import { TerminalTabContextMenuGroup } from 'vs/workbench/contrib/terminal/browser/terminalMenus';
import { ITerminalProfileResolverService, KEYBINDING_CONTEXT_TERMINAL_FIND_VISIBLE, TerminalCommandId } from 'vs/workbench/contrib/terminal/common/terminal';
import { ITerminalContributionService } from 'vs/workbench/contrib/terminal/common/terminalExtensionPoints';
import { terminalStrings } from 'vs/workbench/contrib/terminal/common/terminalStrings';
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
import { isLinux, isMacintosh } from 'vs/base/common/platform';
import { BrowserFeatures } from 'vs/base/browser/canIUse';
import { createAndFillInContextMenuActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
import { INotificationService } from 'vs/platform/notification/common/notification';
const xtermSelector = '.terminal.xterm';
const findWidgetSelector = '.simple-find-part-wrapper';
export class TerminalEditor extends EditorPane {
public static readonly ID = 'terminalEditor';
private _parentElement: HTMLElement | undefined;
private _editorInput?: TerminalEditorInput = undefined;
private _lastDimension?: dom.Dimension;
private readonly _dropdownMenu: IMenu;
private _findWidget: TerminalFindWidget;
private _findWidgetVisible: IContextKey<boolean>;
private _findState: FindReplaceState;
private readonly _instanceMenu: IMenu;
private _cancelContextMenu: boolean = false;
get findState(): FindReplaceState { return this._findState; }
constructor(
@ITelemetryService telemetryService: ITelemetryService,
@IThemeService themeService: IThemeService,
@IStorageService storageService: IStorageService,
@ITerminalEditorService private readonly _terminalEditorService: ITerminalEditorService,
@ITerminalProfileResolverService private readonly _terminalProfileResolverService: ITerminalProfileResolverService,
@ITerminalContributionService private readonly _terminalContributionService: ITerminalContributionService,
@ITerminalService private readonly _terminalService: ITerminalService,
@IInstantiationService instantiationService: IInstantiationService,
@IContextKeyService contextKeyService: IContextKeyService,
@IMenuService menuService: IMenuService,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IContextMenuService private readonly _contextMenuService: IContextMenuService,
@INotificationService private readonly _notificationService: INotificationService
) {
super(TerminalEditor.ID, telemetryService, themeService, storageService);
this._findState = new FindReplaceState();
this._findWidget = instantiationService.createInstance(TerminalFindWidget, this._findState);
this._findWidgetVisible = KEYBINDING_CONTEXT_TERMINAL_FIND_VISIBLE.bindTo(contextKeyService);
this._dropdownMenu = this._register(menuService.createMenu(MenuId.TerminalNewDropdownContext, contextKeyService));
this._instanceMenu = this._register(menuService.createMenu(MenuId.TerminalInstanceContext, contextKeyService));
}
override async setInput(newInput: TerminalEditorInput, options: IEditorOptions | undefined, context: IEditorOpenContext, token: CancellationToken) {
this._editorInput?.terminalInstance?.detachFromElement();
this._editorInput = newInput;
await super.setInput(newInput, options, context, token);
this._editorInput.terminalInstance?.attachToElement(this._parentElement!);
if (this._lastDimension) {
this.layout(this._lastDimension);
}
this._editorInput.terminalInstance?.setVisible(true);
if (this._editorInput.terminalInstance) {
// since the editor does not monitor focus changes, for ex. between the terminal
// panel and the editors, this is needed so that the active instance gets set
// when focus changes between them.
this._register(this._editorInput.terminalInstance.onFocused(() => this._setActiveInstance()));
}
}
private _setActiveInstance(): void {
if (!this._editorInput?.terminalInstance) {
return;
}
this._terminalEditorService.setActiveInstance(this._editorInput.terminalInstance);
}
override focus() {
this._editorInput?.terminalInstance?.focus();
}
// eslint-disable-next-line @typescript-eslint/naming-convention
protected createEditor(parent: HTMLElement): void {
this._parentElement = parent;
this._registerListeners();
}
private _registerListeners(): void {
if (!this._parentElement) {
return;
}
this._register(dom.addDisposableListener(this._parentElement, 'mousedown', async (event: MouseEvent) => {
if (this._terminalEditorService.instances.length === 0) {
return;
}
if (event.which === 2 && isLinux) {
// Drop selection and focus terminal on Linux to enable middle button paste when click
// occurs on the selection itself.
const terminal = this._terminalEditorService.activeInstance;
if (terminal) {
terminal.focus();
}
} else if (event.which === 3) {
const rightClickBehavior = this._terminalService.configHelper.config.rightClickBehavior;
if (rightClickBehavior === 'copyPaste' || rightClickBehavior === 'paste') {
const terminal = this._terminalEditorService.activeInstance;
if (!terminal) {
return;
}
// copyPaste: Shift+right click should open context menu
if (rightClickBehavior === 'copyPaste' && event.shiftKey) {
this._openContextMenu(event);
return;
}
if (rightClickBehavior === 'copyPaste' && terminal.hasSelection()) {
await terminal.copySelection();
terminal.clearSelection();
} else {
if (BrowserFeatures.clipboard.readText) {
terminal.paste();
} else {
this._notificationService.info(`This browser doesn't support the clipboard.readText API needed to trigger a paste, try ${isMacintosh ? '⌘' : 'Ctrl'}+V instead.`);
}
}
// Clear selection after all click event bubbling is finished on Mac to prevent
// right-click selecting a word which is seemed cannot be disabled. There is a
// flicker when pasting but this appears to give the best experience if the
// setting is enabled.
if (isMacintosh) {
setTimeout(() => {
terminal.clearSelection();
}, 0);
}
this._cancelContextMenu = true;
}
}
}));
this._register(dom.addDisposableListener(this._parentElement, 'contextmenu', (event: MouseEvent) => {
const rightClickBehavior = this._terminalService.configHelper.config.rightClickBehavior;
if (!this._cancelContextMenu && rightClickBehavior !== 'copyPaste' && rightClickBehavior !== 'paste') {
if (!this._cancelContextMenu) {
this._openContextMenu(event);
}
event.preventDefault();
event.stopImmediatePropagation();
this._cancelContextMenu = false;
}
}));
}
private _openContextMenu(event: MouseEvent): void {
const standardEvent = new StandardMouseEvent(event);
const anchor: { x: number, y: number } = { x: standardEvent.posx, y: standardEvent.posy };
const actions: IAction[] = [];
const actionsDisposable = createAndFillInContextMenuActions(this._instanceMenu, undefined, actions);
this._contextMenuService.showContextMenu({
getAnchor: () => anchor,
getActions: () => actions,
getActionsContext: () => this._parentElement,
onHide: () => actionsDisposable.dispose()
});
}
layout(dimension: dom.Dimension): void {
this._editorInput?.terminalInstance?.layout(dimension);
this._lastDimension = dimension;
}
override setVisible(visible: boolean, group?: IEditorGroup): void {
super.setVisible(visible, group);
return this._editorInput?.terminalInstance?.setVisible(visible);
}
override getActionViewItem(action: IAction): IActionViewItem | undefined {
switch (action.id) {
case TerminalCommandId.CreateWithProfileButton: {
const actions = this._getTabActionBarArgs(this._terminalService.availableProfiles);
const button = this._instantiationService.createInstance(DropdownWithPrimaryActionViewItem, actions.primaryAction, actions.dropdownAction, actions.dropdownMenuActions, actions.className, this._contextMenuService);
return button;
}
}
return super.getActionViewItem(action);
}
private _getTabActionBarArgs(profiles: ITerminalProfile[]): {
primaryAction: MenuItemAction,
dropdownAction: IAction,
dropdownMenuActions: IAction[],
className: string,
dropdownIcon?: string
} {
const dropdownActions: IAction[] = [];
const submenuActions: IAction[] = [];
const defaultProfileName = this._terminalProfileResolverService.defaultProfileName;
for (const p of profiles) {
const isDefault = p.profileName === defaultProfileName;
const options: IMenuActionOptions = {
arg: {
config: p,
target: TerminalLocation.Editor
} as ICreateTerminalOptions,
shouldForwardArgs: true
};
if (isDefault) {
dropdownActions.unshift(this._instantiationService.createInstance(MenuItemAction, { id: TerminalCommandId.NewWithProfile, title: localize('defaultTerminalProfile', "{0} (Default)", p.profileName), category: TerminalTabContextMenuGroup.Profile }, undefined, options));
submenuActions.unshift(this._instantiationService.createInstance(MenuItemAction, { id: TerminalCommandId.Split, title: localize('defaultTerminalProfile', "{0} (Default)", p.profileName), category: TerminalTabContextMenuGroup.Profile }, undefined, options));
} else {
dropdownActions.push(this._instantiationService.createInstance(MenuItemAction, { id: TerminalCommandId.NewWithProfile, title: p.profileName.replace(/[\n\r\t]/g, ''), category: TerminalTabContextMenuGroup.Profile }, undefined, options));
submenuActions.push(this._instantiationService.createInstance(MenuItemAction, { id: TerminalCommandId.Split, title: p.profileName.replace(/[\n\r\t]/g, ''), category: TerminalTabContextMenuGroup.Profile }, undefined, options));
}
}
// TODO: Support contributed profiles with editor target
for (const contributed of this._terminalContributionService.terminalProfiles) {
dropdownActions.push(new Action(TerminalCommandId.NewWithProfile, contributed.title.replace(/[\n\r\t]/g, ''), undefined, true, () => this._terminalService.createContributedTerminalProfile(contributed.extensionIdentifier, contributed.id, {
isSplitTerminal: false,
target: TerminalLocation.Editor
})));
submenuActions.push(new Action(TerminalCommandId.NewWithProfile, contributed.title.replace(/[\n\r\t]/g, ''), undefined, true, () => this._terminalService.createContributedTerminalProfile(contributed.extensionIdentifier, contributed.id, {
isSplitTerminal: true,
target: TerminalLocation.Editor
})));
}
if (dropdownActions.length > 0) {
dropdownActions.push(new SubmenuAction('split.profile', 'Split...', submenuActions));
dropdownActions.push(new Separator());
}
for (const [, configureActions] of this._dropdownMenu.getActions()) {
for (const action of configureActions) {
// make sure the action is a MenuItemAction
if ('alt' in action) {
dropdownActions.push(action);
}
}
}
const primaryAction = this._instantiationService.createInstance(
MenuItemAction,
{
id: TerminalCommandId.CreateTerminalEditor,
title: localize('terminal.new', "New Terminal"),
icon: Codicon.plus
},
{
id: 'workbench.action.splitEditor',
title: terminalStrings.split.value,
icon: Codicon.splitHorizontal
},
undefined);
const dropdownAction = new Action('refresh profiles', 'Launch Profile...', 'codicon-chevron-down', true);
return { primaryAction, dropdownAction, dropdownMenuActions: dropdownActions, className: 'terminal-tab-actions' };
}
focusFindWidget() {
if (this._parentElement && !this._parentElement?.querySelector(findWidgetSelector)) {
this._parentElement.querySelector(xtermSelector)!.appendChild(this._findWidget.getDomNode());
}
this._findWidgetVisible.set(true);
const activeInstance = this._terminalEditorService.activeInstance;
if (activeInstance && activeInstance.hasSelection() && activeInstance.selection!.indexOf('\n') === -1) {
this._findWidget.reveal(activeInstance.selection);
} else {
this._findWidget.reveal();
}
}
hideFindWidget() {
this._findWidgetVisible.reset();
this.focus();
this._findWidget.hide();
}
showFindWidget() {
const activeInstance = this._terminalEditorService.activeInstance;
if (activeInstance && activeInstance.hasSelection() && activeInstance.selection!.indexOf('\n') === -1) {
this._findWidget.show(activeInstance.selection);
} else {
this._findWidget.show();
}
}
getFindWidget(): TerminalFindWidget {
return this._findWidget;
}
}