|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { InstantiationType, registerSingleton } from 'vs/platform/instantiation/common/extensions'; |
| 7 | +import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; |
| 8 | +import { Emitter, Event } from 'vs/base/common/event'; |
| 9 | +import { IProcessDataEvent, IProcessProperty, IProcessPropertyMap, IShellLaunchConfig, ITerminalChildProcess, ITerminalLaunchError, ProcessPropertyType } from 'vs/platform/terminal/common/terminal'; |
| 10 | +import { Disposable } from 'vs/base/common/lifecycle'; |
| 11 | + |
| 12 | +export const IEmbedderTerminalService = createDecorator<IEmbedderTerminalService>('embedderTerminalService'); |
| 13 | + |
| 14 | +/** |
| 15 | + * Manages terminals that the embedder can create before the terminal contrib is available. |
| 16 | + */ |
| 17 | +export interface IEmbedderTerminalService { |
| 18 | + readonly _serviceBrand: undefined; |
| 19 | + |
| 20 | + readonly onDidCreateTerminal: Event<IShellLaunchConfig>; |
| 21 | + |
| 22 | + createTerminal(options: IEmbedderTerminalOptions): void; |
| 23 | +} |
| 24 | + |
| 25 | +export type EmbedderTerminal = IShellLaunchConfig & Required<Pick<IShellLaunchConfig, 'customPtyImplementation'>>; |
| 26 | + |
| 27 | +export interface IEmbedderTerminalOptions { |
| 28 | + name: string; |
| 29 | + pty: IEmbedderTerminalPty; |
| 30 | + |
| 31 | + // Extension APIs that have not been implemented for embedders: |
| 32 | + // iconPath?: URI | { light: URI; dark: URI } | ThemeIcon; |
| 33 | + // color?: ThemeColor; |
| 34 | + // location?: TerminalLocation | TerminalEditorLocationOptions | TerminalSplitLocationOptions; |
| 35 | + // isTransient?: boolean; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * See Pseudoterminal on the vscode API for usage. |
| 40 | + */ |
| 41 | +export interface IEmbedderTerminalPty { |
| 42 | + onDidWrite: Event<string>; |
| 43 | + onDidClose?: Event<void | number>; |
| 44 | + onDidChangeName?: Event<string>; |
| 45 | + |
| 46 | + open(): void; |
| 47 | + close(): void; |
| 48 | + |
| 49 | + // Extension APIs that have not been implemented for embedders: |
| 50 | + // onDidOverrideDimensions?: Event<TerminalDimensions | undefined>; |
| 51 | + // handleInput?(data: string): void; |
| 52 | + // setDimensions?(dimensions: TerminalDimensions): void; |
| 53 | +} |
| 54 | + |
| 55 | +class EmbedderTerminalService implements IEmbedderTerminalService { |
| 56 | + declare _serviceBrand: undefined; |
| 57 | + |
| 58 | + private readonly _onDidCreateTerminal = new Emitter<IShellLaunchConfig>(); |
| 59 | + readonly onDidCreateTerminal = Event.buffer(this._onDidCreateTerminal.event); |
| 60 | + |
| 61 | + createTerminal(options: IEmbedderTerminalOptions): void { |
| 62 | + const slc: EmbedderTerminal = { |
| 63 | + name: options.name, |
| 64 | + isFeatureTerminal: true, |
| 65 | + customPtyImplementation(terminalId, cols, rows) { |
| 66 | + return new EmbedderTerminalProcess(terminalId, options.pty); |
| 67 | + }, |
| 68 | + }; |
| 69 | + this._onDidCreateTerminal.fire(slc); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +class EmbedderTerminalProcess extends Disposable implements ITerminalChildProcess { |
| 75 | + readonly #pty: IEmbedderTerminalPty; |
| 76 | + |
| 77 | + readonly shouldPersist = false; |
| 78 | + |
| 79 | + readonly onProcessData: Event<IProcessDataEvent | string>; |
| 80 | + readonly #onProcessReady = this._register(new Emitter<{ pid: number; cwd: string }>()); |
| 81 | + readonly onProcessReady = this.#onProcessReady.event; |
| 82 | + readonly #onDidChangeProperty = this._register(new Emitter<IProcessProperty<any>>()); |
| 83 | + readonly onDidChangeProperty = this.#onDidChangeProperty.event; |
| 84 | + readonly #onProcessExit = this._register(new Emitter<number | undefined>()); |
| 85 | + readonly onProcessExit = this.#onProcessExit.event; |
| 86 | + |
| 87 | + constructor( |
| 88 | + readonly id: number, |
| 89 | + pty: IEmbedderTerminalPty |
| 90 | + ) { |
| 91 | + super(); |
| 92 | + |
| 93 | + this.#pty = pty; |
| 94 | + this.onProcessData = this.#pty.onDidWrite; |
| 95 | + if (this.#pty.onDidClose) { |
| 96 | + this._register(this.#pty.onDidClose(e => this.#onProcessExit.fire(e || undefined))); |
| 97 | + } |
| 98 | + if (this.#pty.onDidChangeName) { |
| 99 | + this._register(this.#pty.onDidChangeName(e => this.#onDidChangeProperty.fire({ |
| 100 | + type: ProcessPropertyType.Title, |
| 101 | + value: e |
| 102 | + }))); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + async start(): Promise<ITerminalLaunchError | undefined> { |
| 107 | + this.#onProcessReady.fire({ pid: -1, cwd: '' }); |
| 108 | + this.#pty.open(); |
| 109 | + return undefined; |
| 110 | + } |
| 111 | + shutdown(): void { |
| 112 | + this.#pty.close(); |
| 113 | + } |
| 114 | + |
| 115 | + // TODO: A lot of these aren't useful for some implementations of ITerminalChildProcess, should |
| 116 | + // they be optional? Should there be a base class for "external" consumers to implement? |
| 117 | + |
| 118 | + input(): void { |
| 119 | + // not supported |
| 120 | + } |
| 121 | + async processBinary(): Promise<void> { |
| 122 | + // not supported |
| 123 | + } |
| 124 | + resize(): void { |
| 125 | + // no-op |
| 126 | + } |
| 127 | + acknowledgeDataEvent(): void { |
| 128 | + // no-op, flow control not currently implemented |
| 129 | + } |
| 130 | + async setUnicodeVersion(): Promise<void> { |
| 131 | + // no-op |
| 132 | + } |
| 133 | + async getInitialCwd(): Promise<string> { |
| 134 | + return ''; |
| 135 | + } |
| 136 | + async getCwd(): Promise<string> { |
| 137 | + return ''; |
| 138 | + } |
| 139 | + async getLatency(): Promise<number> { |
| 140 | + return 0; |
| 141 | + } |
| 142 | + refreshProperty<T extends ProcessPropertyType>(property: ProcessPropertyType): Promise<IProcessPropertyMap[T]> { |
| 143 | + throw new Error(`refreshProperty is not suppported in EmbedderTerminalProcess. property: ${property}`); |
| 144 | + } |
| 145 | + |
| 146 | + updateProperty(property: ProcessPropertyType, value: any): Promise<void> { |
| 147 | + throw new Error(`updateProperty is not suppported in EmbedderTerminalProcess. property: ${property}, value: ${value}`); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +registerSingleton(IEmbedderTerminalService, EmbedderTerminalService, InstantiationType.Delayed); |
0 commit comments