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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import Severity from 'vs/base/common/severity';
import { dispose, toDisposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { EditorInputCapabilities, IEditorIdentifier, IUntypedEditorInput } from 'vs/workbench/common/editor';
import { IThemeService, ThemeIcon } from 'vs/platform/theme/common/themeService';
import { EditorInput, IEditorCloseHandler } from 'vs/workbench/common/editor/editorInput';
import { ITerminalInstance, ITerminalInstanceService, terminalEditorId } from 'vs/workbench/contrib/terminal/browser/terminal';
import { getColorClass, getUriClasses } from 'vs/workbench/contrib/terminal/browser/terminalIcon';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IShellLaunchConfig, TerminalExitReason, TerminalLocation, TerminalSettingId } from 'vs/platform/terminal/common/terminal';
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
import { ILifecycleService } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { ConfirmOnKill } from 'vs/workbench/contrib/terminal/common/terminal';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { TerminalContextKeys } from 'vs/workbench/contrib/terminal/common/terminalContextKey';
import { ConfirmResult, IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { Emitter } from 'vs/base/common/event';
export class TerminalEditorInput extends EditorInput implements IEditorCloseHandler {
static readonly ID = 'workbench.editors.terminal';
override readonly closeHandler = this;
private _isDetached = false;
private _isShuttingDown = false;
private _isReverted = false;
private _copyLaunchConfig?: IShellLaunchConfig;
private _terminalEditorFocusContextKey: IContextKey<boolean>;
private _group: IEditorGroup | undefined;
protected readonly _onDidRequestAttach = this._register(new Emitter<ITerminalInstance>());
readonly onDidRequestAttach = this._onDidRequestAttach.event;
setGroup(group: IEditorGroup | undefined) {
this._group = group;
}
get group(): IEditorGroup | undefined {
return this._group;
}
override get typeId(): string {
return TerminalEditorInput.ID;
}
override get editorId(): string | undefined {
return terminalEditorId;
}
override get capabilities(): EditorInputCapabilities {
return EditorInputCapabilities.Readonly | EditorInputCapabilities.Singleton;
}
setTerminalInstance(instance: ITerminalInstance): void {
if (this._terminalInstance) {
throw new Error('cannot set instance that has already been set');
}
this._terminalInstance = instance;
this._setupInstanceListeners();
}
override copy(): EditorInput {
const instance = this._terminalInstanceService.createInstance(this._copyLaunchConfig || {}, TerminalLocation.Editor);
instance.focusWhenReady();
this._copyLaunchConfig = undefined;
return this._instantiationService.createInstance(TerminalEditorInput, instance.resource, instance);
}
/**
* Sets the launch config to use for the next call to EditorInput.copy, which will be used when
* the editor's split command is run.
*/
setCopyLaunchConfig(launchConfig: IShellLaunchConfig) {
this._copyLaunchConfig = launchConfig;
}
/**
* Returns the terminal instance for this input if it has not yet been detached from the input.
*/
get terminalInstance(): ITerminalInstance | undefined {
return this._isDetached ? undefined : this._terminalInstance;
}
showConfirm(): boolean {
if (this._isReverted) {
return false;
}
const confirmOnKill = this._configurationService.getValue<ConfirmOnKill>(TerminalSettingId.ConfirmOnKill);
if (confirmOnKill === 'editor' || confirmOnKill === 'always') {
return this._terminalInstance?.hasChildProcesses || false;
}
return false;
}
async confirm(terminals: ReadonlyArray<IEditorIdentifier>): Promise<ConfirmResult> {
const { choice } = await this._dialogService.show(
Severity.Warning,
localize('confirmDirtyTerminal.message', "Do you want to terminate running processes?"),
[
localize({ key: 'confirmDirtyTerminal.button', comment: ['&& denotes a mnemonic'] }, "&&Terminate"),
localize('cancel', "Cancel")
],
{
cancelId: 1,
detail: terminals.length > 1 ?
terminals.map(terminal => terminal.editor.getName()).join('\n') + '\n\n' + localize('confirmDirtyTerminals.detail', "Closing will terminate the running processes in the terminals.") :
localize('confirmDirtyTerminal.detail', "Closing will terminate the running processes in this terminal.")
}
);
switch (choice) {
case 0: return ConfirmResult.DONT_SAVE;
default: return ConfirmResult.CANCEL;
}
}
override async revert(): Promise<void> {
// On revert just treat the terminal as permanently non-dirty
this._isReverted = true;
}
constructor(
public readonly resource: URI,
private _terminalInstance: ITerminalInstance | undefined,
@IThemeService private readonly _themeService: IThemeService,
@ITerminalInstanceService private readonly _terminalInstanceService: ITerminalInstanceService,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@ILifecycleService private readonly _lifecycleService: ILifecycleService,
@IContextKeyService _contextKeyService: IContextKeyService,
@IDialogService private readonly _dialogService: IDialogService
) {
super();
this._terminalEditorFocusContextKey = TerminalContextKeys.editorFocus.bindTo(_contextKeyService);
if (_terminalInstance) {
this._setupInstanceListeners();
}
}
private _setupInstanceListeners(): void {
const instance = this._terminalInstance;
if (!instance) {
return;
}
this._register(toDisposable(() => {
if (!this._isDetached && !this._isShuttingDown) {
// Will be ignored if triggered by onExit or onDisposed terminal events
// as disposed was already called
instance.dispose(TerminalExitReason.User);
}
}));
const disposeListeners = [
instance.onExit(() => this.dispose()),
instance.onDisposed(() => this.dispose()),
instance.onTitleChanged(() => this._onDidChangeLabel.fire()),
instance.onIconChanged(() => this._onDidChangeLabel.fire()),
instance.onDidFocus(() => this._terminalEditorFocusContextKey.set(true)),
instance.onDidBlur(() => this._terminalEditorFocusContextKey.reset()),
instance.statusList.onDidChangePrimaryStatus(() => this._onDidChangeLabel.fire())
];
// Don't dispose editor when instance is torn down on shutdown to avoid extra work and so
// the editor/tabs don't disappear
this._lifecycleService.onWillShutdown(() => {
this._isShuttingDown = true;
dispose(disposeListeners);
});
}
override getName() {
return this._terminalInstance?.title || this.resource.fragment;
}
override getLabelExtraClasses(): string[] {
if (!this._terminalInstance) {
return [];
}
const extraClasses: string[] = ['terminal-tab'];
const colorClass = getColorClass(this._terminalInstance);
if (colorClass) {
extraClasses.push(colorClass);
}
const uriClasses = getUriClasses(this._terminalInstance, this._themeService.getColorTheme().type);
if (uriClasses) {
extraClasses.push(...uriClasses);
}
if (ThemeIcon.isThemeIcon(this._terminalInstance.icon)) {
extraClasses.push(`codicon-${this._terminalInstance.icon.id}`);
}
return extraClasses;
}
/**
* Detach the instance from the input such that when the input is disposed it will not dispose
* of the terminal instance/process.
*/
detachInstance() {
if (!this._isShuttingDown) {
this._terminalInstance?.detachFromElement();
this._isDetached = true;
}
}
public override getDescription(): string | undefined {
return this._terminalInstance?.description;
}
public override toUntyped(): IUntypedEditorInput {
return {
resource: this.resource,
options: {
override: terminalEditorId,
pinned: true,
forceReload: true
}
};
}
}
|