-
Notifications
You must be signed in to change notification settings - Fork 39.4k
Expand file tree
/
Copy pathextHostTerminalShellIntegration.ts
More file actions
484 lines (425 loc) · 19.7 KB
/
extHostTerminalShellIntegration.ts
File metadata and controls
484 lines (425 loc) · 19.7 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type * as vscode from 'vscode';
import { TerminalShellExecutionCommandLineConfidence } from './extHostTypes.js';
import { Disposable, DisposableStore, toDisposable } from '../../../base/common/lifecycle.js';
import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';
import { MainContext, type ExtHostTerminalShellIntegrationShape, type MainThreadTerminalShellIntegrationShape } from './extHost.protocol.js';
import { IExtHostRpcService } from './extHostRpcService.js';
import { IExtHostTerminalService } from './extHostTerminalService.js';
import { Emitter, type Event } from '../../../base/common/event.js';
import { URI } from '../../../base/common/uri.js';
import { AsyncIterableProducer, Barrier, type AsyncIterableEmitter } from '../../../base/common/async.js';
export interface IExtHostTerminalShellIntegration extends ExtHostTerminalShellIntegrationShape {\n\treadonly _serviceBrand: undefined;
readonly onDidChangeTerminalShellIntegration: Event<vscode.TerminalShellIntegrationChangeEvent>;
readonly onDidStartTerminalShellExecution: Event<vscode.TerminalShellExecutionStartEvent>;
readonly onDidEndTerminalShellExecution: Event<vscode.TerminalShellExecutionEndEvent>;
}
export const IExtHostTerminalShellIntegration = createDecorator<IExtHostTerminalShellIntegration>('IExtHostTerminalShellIntegration');
export class ExtHostTerminalShellIntegration extends Disposable implements IExtHostTerminalShellIntegration {
readonly _serviceBrand: undefined;
protected _proxy: MainThreadTerminalShellIntegrationShape;
private _activeShellIntegrations: Map</*instanceId*/number, InternalTerminalShellIntegration> = new Map();
protected readonly _onDidChangeTerminalShellIntegration = this._register(new Emitter<vscode.TerminalShellIntegrationChangeEvent>());
readonly onDidChangeTerminalShellIntegration = this._onDidChangeTerminalShellIntegration.event;
protected readonly _onDidStartTerminalShellExecution = this._register(new Emitter<vscode.TerminalShellExecutionStartEvent>());
readonly onDidStartTerminalShellExecution = this._onDidStartTerminalShellExecution.event;
protected readonly _onDidEndTerminalShellExecution = this._register(new Emitter<vscode.TerminalShellExecutionEndEvent>());
readonly onDidEndTerminalShellExecution = this._onDidEndTerminalShellExecution.event;
constructor(
@IExtHostRpcService extHostRpc: IExtHostRpcService,
@IExtHostTerminalService private readonly _extHostTerminalService: IExtHostTerminalService,
) {
super();
this._proxy = extHostRpc.getProxy(MainContext.MainThreadTerminalShellIntegration);
// Clean up listeners
this._register(toDisposable(() => {
for (const [_, integration] of this._activeShellIntegrations) {
integration.dispose();
}
this._activeShellIntegrations.clear();
}));
// Convenient test code:
// this.onDidChangeTerminalShellIntegration(e => {
// console.log('*** onDidChangeTerminalShellIntegration', e);
// });
// this.onDidStartTerminalShellExecution(async e => {
// console.log('*** onDidStartTerminalShellExecution', e);
// // new Promise<void>(r => {
// // (async () => {
// // for await (const d of e.execution.read()) {
// // console.log('data2', d);
// // }
// // })();
// // });
// for await (const d of e.execution.read()) {
// console.log('data', d);
// }
// });
// this.onDidEndTerminalShellExecution(e => {
// console.log('*** onDidEndTerminalShellExecution', e);
// });
// setTimeout(() => {
// console.log('before executeCommand(\"echo hello\")');
// Array.from(this._activeShellIntegrations.values())[0].value.executeCommand('echo hello');
// console.log('after executeCommand(\"echo hello\")');
// }, 4000);
}
public $shellIntegrationChange(instanceId: number, supportsExecuteCommandApi: boolean): void {
const terminal = this._extHostTerminalService.getTerminalById(instanceId);
if (!terminal) {
return;
}
const apiTerminal = terminal.value;
let shellIntegration = this._activeShellIntegrations.get(instanceId);
if (!shellIntegration) {
shellIntegration = new InternalTerminalShellIntegration(terminal.value, supportsExecuteCommandApi, this._onDidStartTerminalShellExecution);
this._activeShellIntegrations.set(instanceId, shellIntegration);
shellIntegration.store.add(terminal.onWillDispose(() => this._activeShellIntegrations.get(instanceId)?.dispose()));
shellIntegration.store.add(shellIntegration.onDidRequestShellExecution(commandLine => this._proxy.$executeCommand(instanceId, commandLine)));
shellIntegration.store.add(shellIntegration.onDidRequestEndExecution(e => this._onDidEndTerminalShellExecution.fire(e)));
shellIntegration.store.add(shellIntegration.onDidRequestChangeShellIntegration(e => this._onDidChangeTerminalShellIntegration.fire(e)));
terminal.shellIntegration = shellIntegration.value;
}
this._onDidChangeTerminalShellIntegration.fire({
terminal: apiTerminal,
shellIntegration: shellIntegration.value
});
}
public $shellExecutionStart(instanceId: number, supportsExecuteCommandApi: boolean, commandLineValue: string, commandLineConfidence: TerminalShellExecutionCommandLineConfidence, isTrusted: boolean, cwd: string | undefined): void {
// Force shellIntegration creation if it hasn't been created yet, this could when events\n\t\t// don't come through on startup
if (!this._activeShellIntegrations.has(instanceId)) {
this.$shellIntegrationChange(instanceId, supportsExecuteCommandApi);
}
const commandLine: vscode.TerminalShellExecutionCommandLine = {
value: commandLineValue,
confidence: commandLineConfidence,
isTrusted
};
this._activeShellIntegrations.get(instanceId)?.startShellExecution(commandLine, this._convertCwdToUri(cwd));
}
public $shellExecutionEnd(instanceId: number, commandLineValue: string, commandLineConfidence: TerminalShellExecutionCommandLineConfidence, isTrusted: boolean, exitCode: number | undefined): void {
const commandLine: vscode.TerminalShellExecutionCommandLine = {
value: commandLineValue,
confidence: commandLineConfidence,
isTrusted
};
this._activeShellIntegrations.get(instanceId)?.endShellExecution(commandLine, exitCode);
}
public $shellExecutionData(instanceId: number, data: string): void {
this._activeShellIntegrations.get(instanceId)?.emitData(data);
}
public $shellEnvChange(instanceId: number, shellEnvKeys: string[], shellEnvValues: string[], isTrusted: boolean): void {
this._activeShellIntegrations.get(instanceId)?.setEnv(shellEnvKeys, shellEnvValues, isTrusted);
}
public $cwdChange(instanceId: number, cwd: string | undefined): void {
this._activeShellIntegrations.get(instanceId)?.setCwd(this._convertCwdToUri(cwd));
}
public $closeTerminal(instanceId: number): void {
this._activeShellIntegrations.get(instanceId)?.dispose();
this._activeShellIntegrations.delete(instanceId);
}
private _convertCwdToUri(cwd: string | undefined): URI | undefined {
// IMPORTANT: cwd is provided to the exthost as a string from the renderer and only\n\t\t// converted to a URI on the machine in which the pty is hosted on. The string version of\n\t\t// the cwd is used from the renderer such that it's access is synchronous and its event\n\t\t// comes through in order relative to other shell integration events.
return cwd ? URI.file(cwd) : undefined;
}
}
interface IExecutionProperties {
isMultiLine: boolean;
unresolvedCommandLines: string[] | undefined;
}
export class InternalTerminalShellIntegration extends Disposable {
private _pendingExecutions: InternalTerminalShellExecution[] = [];
private _pendingEndingExecution: InternalTerminalShellExecution | undefined;
private _currentExecutionProperties: IExecutionProperties | undefined;
private _currentExecution: InternalTerminalShellExecution | undefined;
get currentExecution(): InternalTerminalShellExecution | undefined { return this._currentExecution; }
private _env: vscode.TerminalShellIntegrationEnvironment | undefined;
private _cwd: URI | undefined;
readonly store: DisposableStore = this._register(new DisposableStore());
readonly value: vscode.TerminalShellIntegration;
protected readonly _onDidRequestChangeShellIntegration = this._register(new Emitter<vscode.TerminalShellIntegrationChangeEvent>());
readonly onDidRequestChangeShellIntegration = this._onDidRequestChangeShellIntegration.event;
protected readonly _onDidRequestShellExecution = this._register(new Emitter<string>());
readonly onDidRequestShellExecution = this._onDidRequestShellExecution.event;
protected readonly _onDidRequestEndExecution = this._register(new Emitter<vscode.TerminalShellExecutionEndEvent>());
readonly onDidRequestEndExecution = this._onDidRequestEndExecution.event;
protected readonly _onDidRequestNewExecution = this._register(new Emitter<string>());
readonly onDidRequestNewExecution = this._onDidRequestNewExecution.event;
constructor(
private readonly _terminal: vscode.Terminal,
supportsExecuteCommandApi: boolean,
private readonly _onDidStartTerminalShellExecution: Emitter<vscode.TerminalShellExecutionStartEvent>
) {
super();
const that = this;
this.value = {
get cwd(): URI | undefined {
return that._cwd;
},
get env(): vscode.TerminalShellIntegrationEnvironment | undefined {
if (!that._env) {
return undefined;
}
return Object.freeze({
isTrusted: that._env.isTrusted,
value: Object.freeze({ ...that._env.value })
});
},
// executeCommand(commandLine: string): vscode.TerminalShellExecution;
// executeCommand(executable: string, args: string[]): vscode.TerminalShellExecution;
executeCommand(commandLineOrExecutable: string, args?: string[]): vscode.TerminalShellExecution {
if (!supportsExecuteCommandApi) {
throw new Error('This terminal does not support the executeCommand API.');
}
let commandLineValue = commandLineOrExecutable;
if (args) {
for (const arg of args) {
const wrapInQuotes = !arg.match(/[\"'`]/) && arg.match(/\\s/);
if (wrapInQuotes) {
commandLineValue += ` \"${arg}\"`;
} else {
commandLineValue += ` ${arg}`;
}
}
}
that._onDidRequestShellExecution.fire(commandLineValue);
// Fire the event in a microtask to allow the extension to use the execution before\n\t\t\t\t// the start event fires
const commandLine: vscode.TerminalShellExecutionCommandLine = {
value: commandLineValue,
confidence: TerminalShellExecutionCommandLineConfidence.High,
isTrusted: true
};
const execution = that.requestNewShellExecution(commandLine, that._cwd).value;
return execution;
}
};
}
requestNewShellExecution(commandLine: vscode.TerminalShellExecutionCommandLine, cwd: URI | undefined) {
const execution = new InternalTerminalShellExecution(commandLine, cwd ?? this._cwd);
const unresolvedCommandLines = splitAndSanitizeCommandLine(commandLine.value);
if (unresolvedCommandLines.length > 1) {
this._currentExecutionProperties = {
isMultiLine: true,
unresolvedCommandLines: splitAndSanitizeCommandLine(commandLine.value),
};
}
this._pendingExecutions.push(execution);
this._onDidRequestNewExecution.fire(commandLine.value);
return execution;
}
startShellExecution(commandLine: vscode.TerminalShellExecutionCommandLine, cwd: URI | undefined): undefined {
// Since an execution is starting, fire the end event for any execution that is awaiting to\n\t\t// end. When this happens it means that the data stream may not be flushed and therefore may\n\t\t// fire events after the end event.
if (this._pendingEndingExecution) {
this._onDidRequestEndExecution.fire({ terminal: this._terminal, shellIntegration: this.value, execution: this._pendingEndingExecution.value, exitCode: undefined });
this._pendingEndingExecution = undefined;
}
if (this._currentExecution) {
// If the current execution is multi-line, check if this command line is part of it.
if (this._currentExecutionProperties?.isMultiLine && this._currentExecutionProperties.unresolvedCommandLines) {
const subExecutionResult = isSubExecution(this._currentExecutionProperties.unresolvedCommandLines, commandLine);
if (subExecutionResult) {
this._currentExecutionProperties.unresolvedCommandLines = subExecutionResult.unresolvedCommandLines;
return;
}
}
this._currentExecution.endExecution(undefined);
this._currentExecution.flush();
this._onDidRequestEndExecution.fire({ terminal: this._terminal, shellIntegration: this.value, execution: this._currentExecution.value, exitCode: undefined });
}
// Get the matching pending execution, how strict this is depends on the confidence of the\n // command line
let currentExecution: InternalTerminalShellExecution | undefined;
if (commandLine.confidence === TerminalShellExecutionCommandLineConfidence.High) {
for (const [i, execution] of this._pendingExecutions.entries()) {
if (execution.value.commandLine.value === commandLine.value) {
currentExecution = execution;
this._currentExecutionProperties = {
isMultiLine: false,
unresolvedCommandLines: undefined,
};
currentExecution = execution;
this._pendingExecutions.splice(i, 1);
break;
} else {
const subExecutionResult = isSubExecution(splitAndSanitizeCommandLine(execution.value.commandLine.value), commandLine);
if (subExecutionResult) {
this._currentExecutionProperties = {
isMultiLine: true,
unresolvedCommandLines: subExecutionResult.unresolvedCommandLines,
};
currentExecution = execution;
this._pendingExecutions.splice(i, 1);
break;
}
}
}
} else {
currentExecution = this._pendingExecutions.shift();
}
// If there is no execution, create a new one
if (!currentExecution) {
// Fallback to the shell integration's cwd as the cwd may not have been restored after a reload
currentExecution = new InternalTerminalShellExecution(commandLine, cwd ?? this._cwd);
}
this._currentExecution = currentExecution;
this._onDidStartTerminalShellExecution.fire({ terminal: this._terminal, shellIntegration: this.value, execution: this._currentExecution.value });
}
emitData(data: string): void {
this.currentExecution?.emitData(data);
}
endShellExecution(commandLine: vscode.TerminalShellExecutionCommandLine | undefined, exitCode: number | undefined): void {
// If the current execution is multi-line, don't end it until the next command line is\n\t\t// confirmed to not be a part of it.
if (this._currentExecutionProperties?.isMultiLine) {
if (this._currentExecutionProperties.unresolvedCommandLines && this._currentExecutionProperties.unresolvedCommandLines.length > 0) {
return;
}
}
if (this._currentExecution) {
const commandLineForEvent = this._currentExecutionProperties?.isMultiLine ? this._currentExecution.value.commandLine : commandLine;
this._currentExecution.endExecution(commandLineForEvent);
const currentExecution = this._currentExecution;
this._pendingEndingExecution = currentExecution;
this._currentExecution = undefined;
// IMPORTANT: Ensure the current execution's data events are flushed in order to\n\t\t\t// prevent data events firing after the end event fires.
currentExecution.flush().then(() => {
// Only fire if it's still the same execution, if it's changed it would have already\n\t\t\t\t// been fired.
if (this._pendingEndingExecution === currentExecution) {
this._onDidRequestEndExecution.fire({ terminal: this._terminal, shellIntegration: this.value, execution: currentExecution.value, exitCode });
this._pendingEndingExecution = undefined;
}
});
}
}
setEnv(keys: string[], values: string[], isTrusted: boolean): void {
const env: { [key: string]: string | undefined } = {};
for (let i = 0; i < keys.length; i++) {
env[keys[i]] = values[i];
}
this._env = { value: env, isTrusted };
this._fireChangeEvent();
}
setCwd(cwd: URI | undefined): void {
let wasChanged = false;
if (URI.isUri(this._cwd)) {
wasChanged = !URI.isUri(cwd) || this._cwd.toString() !== cwd.toString();
} else if (this._cwd !== cwd) {
wasChanged = true;
}
if (wasChanged) {
this._cwd = cwd;
this._fireChangeEvent();
}
}
private _fireChangeEvent() {
this._onDidRequestChangeShellIntegration.fire({ terminal: this._terminal, shellIntegration: this.value });
}
}
class InternalTerminalShellExecution {
readonly value: vscode.TerminalShellExecution;
private _dataStream: ShellExecutionDataStream | undefined;
private _isEnded: boolean = false;
constructor(
private _commandLine: vscode.TerminalShellExecutionCommandLine,
readonly cwd: URI | undefined,
) {
const that = this;
this.value = {
get commandLine(): vscode.TerminalShellExecutionCommandLine {
return that._commandLine;
},
get cwd(): URI | undefined {
return that.cwd;
},
read(): AsyncIterable<string> {
return that._createDataStream();
}
};
}
private _createDataStream(): AsyncIterable<string> {
if (!this._dataStream) {
if (this._isEnded) {
return AsyncIterableProducer.EMPTY;
}
this._dataStream = new ShellExecutionDataStream();
}
return this._dataStream.createIterable();
}
emitData(data: string): void {
if (!this._isEnded) {
this._dataStream?.emitData(data);
}
}
endExecution(commandLine: vscode.TerminalShellExecutionCommandLine | undefined): void {
if (commandLine) {
this._commandLine = commandLine;
}
this._dataStream?.endExecution();
this._isEnded = true;
}
async flush(): Promise<void> {
if (this._dataStream) {
await this._dataStream.flush();
this._dataStream.dispose();
this._dataStream = undefined;
}
}
}
class ShellExecutionDataStream extends Disposable {
private _barrier: Barrier | undefined;
private _iterables: AsyncIterableProducer<string>[] = [];
private _emitters: AsyncIterableEmitter<string>[] = [];
createIterable(): AsyncIterable<string> {
if (!this._barrier) {
this._barrier = new Barrier();
}
const barrier = this._barrier;
const iterable = new AsyncIterableProducer<string>(async emitter => {
this._emitters.push(emitter);
await barrier.wait();
});
this._iterables.push(iterable);
return iterable;
}
emitData(data: string): void {
for (const emitter of this._emitters) {
emitter.emitOne(data);
}
}
endExecution(): void {
this._barrier?.open();
}
async flush(): Promise<void> {
await this._barrier?.wait();
}
}
function splitAndSanitizeCommandLine(commandLine: string): string[] {
return commandLine
.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0);
}
/**
* When executing something that the shell considers multiple commands, such as\n * a comment followed by a command, this needs to all be tracked under a single\n * execution.\n */
function isSubExecution(unresolvedCommandLines: string[], commandLine: vscode.TerminalShellExecutionCommandLine): { unresolvedCommandLines: string[] } | false {
if (unresolvedCommandLines.length === 0) {
return false;
}
const newUnresolvedCommandLines = [...unresolvedCommandLines];
const subExecutionLines = splitAndSanitizeCommandLine(commandLine.value);
if (newUnresolvedCommandLines && newUnresolvedCommandLines.length > 0) {
// If all sub-execution lines are in the command line, this is part of the\n\t\t// multi-line execution.
while (newUnresolvedCommandLines.length > 0) {
if (newUnresolvedCommandLines[0] !== subExecutionLines[0]) {
break;
}
newUnresolvedCommandLines.shift();
subExecutionLines.shift();
}
if (subExecutionLines.length === 0) {
return { unresolvedCommandLines: newUnresolvedCommandLines };
}
}
return false;
}