diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index b4a2aabc165f7..c519b350ef60a 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -769,4 +769,23 @@ declare module 'vscode' { } //#endregion + + //#region Terminal + + export namespace window { + /** + * The currently active terminals or an empty array. + * + * @readonly + */ + export let terminals: Terminal[]; + + /** + * An [event](#Event) which fires when a terminal has been created, either through the + * [createTerminal](#window.createTerminal) API or commands. + */ + export const onDidOpenTerminal: Event; + } + + //#endregion } diff --git a/src/vs/workbench/api/electron-browser/mainThreadTerminalService.ts b/src/vs/workbench/api/electron-browser/mainThreadTerminalService.ts index ee934a107b53b..e5ad89600954e 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadTerminalService.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadTerminalService.ts @@ -22,8 +22,20 @@ export class MainThreadTerminalService implements MainThreadTerminalServiceShape ) { this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostTerminalService); this._toDispose = []; + this._toDispose.push(terminalService.onInstanceCreated((terminalInstance) => { + // Delay this message so the TerminalInstance constructor has a chance to finish and + // return the ID normally to the extension host. The ID that is passed here will be used + // to register non-extension API terminals in the extension host. + setTimeout(() => this._onTerminalOpened(terminalInstance), 100); + })); this._toDispose.push(terminalService.onInstanceDisposed((terminalInstance) => this._onTerminalDisposed(terminalInstance))); this._toDispose.push(terminalService.onInstanceProcessIdReady((terminalInstance) => this._onTerminalProcessIdReady(terminalInstance))); + + // Set initial ext host state + this.terminalService.terminalInstances.forEach(t => { + this._onTerminalOpened(t); + t.processReady.then(() => this._onTerminalProcessIdReady(t)); + }); } public dispose(): void { @@ -78,6 +90,10 @@ export class MainThreadTerminalService implements MainThreadTerminalServiceShape this._proxy.$acceptTerminalClosed(terminalInstance.id); } + private _onTerminalOpened(terminalInstance: ITerminalInstance): void { + this._proxy.$acceptTerminalOpened(terminalInstance.id, terminalInstance.title); + } + private _onTerminalProcessIdReady(terminalInstance: ITerminalInstance): void { this._proxy.$acceptTerminalProcessId(terminalInstance.id, terminalInstance.processId); } diff --git a/src/vs/workbench/api/node/extHost.api.impl.ts b/src/vs/workbench/api/node/extHost.api.impl.ts index da7a17f7d2559..b016a227a7199 100644 --- a/src/vs/workbench/api/node/extHost.api.impl.ts +++ b/src/vs/workbench/api/node/extHost.api.impl.ts @@ -316,6 +316,9 @@ export function createApiFactory( get visibleTextEditors() { return extHostEditors.getVisibleTextEditors(); }, + get terminals() { + return extHostTerminalService.terminals; + }, showTextDocument(documentOrUri: vscode.TextDocument | vscode.Uri, columnOrOptions?: vscode.ViewColumn | vscode.TextDocumentShowOptions, preserveFocus?: boolean): TPromise { let documentPromise: TPromise; if (URI.isUri(documentOrUri)) { @@ -351,6 +354,9 @@ export function createApiFactory( onDidCloseTerminal(listener, thisArg?, disposables?) { return extHostTerminalService.onDidCloseTerminal(listener, thisArg, disposables); }, + onDidOpenTerminal(listener, thisArg?, disposables?) { + return extHostTerminalService.onDidOpenTerminal(listener, thisArg, disposables); + }, get state() { return extHostWindow.state; }, diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index 44e0ba2ce5b84..c2902a8e2043a 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -728,6 +728,7 @@ export interface ExtHostQuickOpenShape { export interface ExtHostTerminalServiceShape { $acceptTerminalClosed(id: number): void; + $acceptTerminalOpened(id: number, name: string): void; $acceptTerminalProcessId(id: number, processId: number): void; } diff --git a/src/vs/workbench/api/node/extHostTerminalService.ts b/src/vs/workbench/api/node/extHostTerminalService.ts index 2a0f3081e2b8d..4e993934fd295 100644 --- a/src/vs/workbench/api/node/extHostTerminalService.ts +++ b/src/vs/workbench/api/node/extHostTerminalService.ts @@ -20,21 +20,28 @@ export class ExtHostTerminal implements vscode.Terminal { constructor( proxy: MainThreadTerminalServiceShape, - name?: string, - shellPath?: string, - shellArgs?: string[], - cwd?: string, - env?: { [key: string]: string }, - waitOnExit?: boolean + name: string = '', + id?: number ) { + this._proxy = proxy; this._name = name; + if (id) { + this._id = id; + } this._queuedRequests = []; - this._proxy = proxy; this._pidPromise = new Promise(c => { this._pidPromiseComplete = c; }); + } - this._proxy.$createTerminal(name, shellPath, shellArgs, cwd, env, waitOnExit).then((id) => { + public create( + shellPath?: string, + shellArgs?: string[], + cwd?: string, + env?: { [key: string]: string }, + waitOnExit?: boolean + ): void { + this._proxy.$createTerminal(this._name, shellPath, shellArgs, cwd, env, waitOnExit).then((id) => { this._id = id; this._queuedRequests.forEach((r) => { r.run(this._proxy, this._id); @@ -44,12 +51,10 @@ export class ExtHostTerminal implements vscode.Terminal { } public get name(): string { - this._checkDisposed(); return this._name; } public get processId(): Thenable { - this._checkDisposed(); return this._pidPromise; } @@ -99,23 +104,29 @@ export class ExtHostTerminal implements vscode.Terminal { export class ExtHostTerminalService implements ExtHostTerminalServiceShape { private readonly _onDidCloseTerminal: Emitter; + private readonly _onDidOpenTerminal: Emitter; private _proxy: MainThreadTerminalServiceShape; private _terminals: ExtHostTerminal[]; + public get terminals(): ExtHostTerminal[] { return this._terminals; } + constructor(mainContext: IMainContext) { this._onDidCloseTerminal = new Emitter(); + this._onDidOpenTerminal = new Emitter(); this._proxy = mainContext.getProxy(MainContext.MainThreadTerminalService); this._terminals = []; } public createTerminal(name?: string, shellPath?: string, shellArgs?: string[]): vscode.Terminal { - let terminal = new ExtHostTerminal(this._proxy, name, shellPath, shellArgs); + let terminal = new ExtHostTerminal(this._proxy, name); + terminal.create(shellPath, shellArgs); this._terminals.push(terminal); return terminal; } public createTerminalFromOptions(options: vscode.TerminalOptions): vscode.Terminal { - let terminal = new ExtHostTerminal(this._proxy, options.name, options.shellPath, options.shellArgs, options.cwd, options.env /*, options.waitOnExit*/); + let terminal = new ExtHostTerminal(this._proxy, options.name); + terminal.create(options.shellPath, options.shellArgs, options.cwd, options.env /*, options.waitOnExit*/); this._terminals.push(terminal); return terminal; } @@ -124,6 +135,10 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape { return this._onDidCloseTerminal && this._onDidCloseTerminal.event; } + public get onDidOpenTerminal(): Event { + return this._onDidOpenTerminal && this._onDidOpenTerminal.event; + } + public $acceptTerminalClosed(id: number): void { let index = this._getTerminalIndexById(id); if (index === null) { @@ -134,6 +149,18 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape { this._onDidCloseTerminal.fire(terminal); } + public $acceptTerminalOpened(id: number, name: string): void { + let index = this._getTerminalIndexById(id); + if (index !== null) { + // The terminal has already been created (via createTerminal*), only fire the event + this._onDidOpenTerminal.fire(this.terminals[index]); + return; + } + let terminal = new ExtHostTerminal(this._proxy, name, id); + this._terminals.push(terminal); + this._onDidOpenTerminal.fire(terminal); + } + public $acceptTerminalProcessId(id: number, processId: number): void { let terminal = this._getTerminalById(id); if (terminal) { diff --git a/src/vs/workbench/parts/terminal/common/terminal.ts b/src/vs/workbench/parts/terminal/common/terminal.ts index ed019f21ef791..079e4ac86d7a3 100644 --- a/src/vs/workbench/parts/terminal/common/terminal.ts +++ b/src/vs/workbench/parts/terminal/common/terminal.ts @@ -149,6 +149,7 @@ export interface ITerminalService { configHelper: ITerminalConfigHelper; onActiveTabChanged: Event; onTabDisposed: Event; + onInstanceCreated: Event; onInstanceDisposed: Event; onInstanceProcessIdReady: Event; onInstancesChanged: Event; @@ -234,6 +235,8 @@ export interface ITerminalInstance { onProcessIdReady: Event; + processReady: TPromise; + /** * The title of the terminal. This is either title or the process currently running or an * explicit name given to the terminal instance through the extension API. diff --git a/src/vs/workbench/parts/terminal/common/terminalService.ts b/src/vs/workbench/parts/terminal/common/terminalService.ts index 104182733c671..a3534b05ff0cb 100644 --- a/src/vs/workbench/parts/terminal/common/terminalService.ts +++ b/src/vs/workbench/parts/terminal/common/terminalService.ts @@ -24,6 +24,7 @@ export abstract class TerminalService implements ITerminalService { protected _terminalContainer: HTMLElement; protected _onInstancesChanged: Emitter; protected _onTabDisposed: Emitter; + protected _onInstanceCreated: Emitter; protected _onInstanceDisposed: Emitter; protected _onInstanceProcessIdReady: Emitter; protected _onInstanceTitleChanged: Emitter; @@ -36,6 +37,7 @@ export abstract class TerminalService implements ITerminalService { public get activeTabIndex(): number { return this._activeTabIndex; } public get onActiveTabChanged(): Event { return this._onActiveTabChanged.event; } public get onTabDisposed(): Event { return this._onTabDisposed.event; } + public get onInstanceCreated(): Event { return this._onInstanceCreated.event; } public get onInstanceDisposed(): Event { return this._onInstanceDisposed.event; } public get onInstanceProcessIdReady(): Event { return this._onInstanceProcessIdReady.event; } public get onInstanceTitleChanged(): Event { return this._onInstanceTitleChanged.event; } @@ -57,6 +59,7 @@ export abstract class TerminalService implements ITerminalService { this._onActiveTabChanged = new Emitter(); this._onTabDisposed = new Emitter(); + this._onInstanceCreated = new Emitter(); this._onInstanceDisposed = new Emitter(); this._onInstanceProcessIdReady = new Emitter(); this._onInstanceTitleChanged = new Emitter(); diff --git a/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts b/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts index a7f25db296004..2333fd8b76ec0 100644 --- a/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts +++ b/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts @@ -107,7 +107,9 @@ export class TerminalInstance implements ITerminalInstance { public disableLayout: boolean; public get id(): number { return this._id; } + // TODO: Ideally processId would be merged into processReady public get processId(): number { return this._processId; } + public get processReady(): TPromise { return this._processReady; } public get onDisposed(): Event { return this._onDisposed.event; } public get onFocused(): Event { return this._onFocused.event; } public get onProcessIdReady(): Event { return this._onProcessIdReady.event; } diff --git a/src/vs/workbench/parts/terminal/electron-browser/terminalService.ts b/src/vs/workbench/parts/terminal/electron-browser/terminalService.ts index a09efd1925d12..715cf6d36093d 100644 --- a/src/vs/workbench/parts/terminal/electron-browser/terminalService.ts +++ b/src/vs/workbench/parts/terminal/electron-browser/terminalService.ts @@ -82,6 +82,7 @@ export class TerminalService extends AbstractTerminalService implements ITermina // It's the first instance so it should be made active automatically this.setActiveInstanceByIndex(0); } + this._onInstanceCreated.fire(instance); this._onInstancesChanged.fire(); this._suggestShellChange(wasNewTerminalAction); return instance;