Work in progress for exposing all terminals via API

This commit is contained in:
Daniel Imms 2018-04-09 07:38:08 -07:00
parent eb0f99cb2e
commit 5d9d2d127f
9 changed files with 53 additions and 9 deletions

View file

@ -769,4 +769,12 @@ declare module 'vscode' {
}
//#endregion
//#region Terminal
export namespace window {
export const onDidOpenTerminal: Event<Terminal>;
}
//#endregion
}

View file

@ -22,6 +22,7 @@ export class MainThreadTerminalService implements MainThreadTerminalServiceShape
) {
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostTerminalService);
this._toDispose = [];
this._toDispose.push(terminalService.onInstanceCreated((terminalInstance) => this._onTerminalOpened(terminalInstance)));
this._toDispose.push(terminalService.onInstanceDisposed((terminalInstance) => this._onTerminalDisposed(terminalInstance)));
this._toDispose.push(terminalService.onInstanceProcessIdReady((terminalInstance) => this._onTerminalProcessIdReady(terminalInstance)));
}
@ -78,6 +79,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);
}

View file

@ -351,6 +351,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;
},

View file

@ -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;
}

View file

@ -20,12 +20,7 @@ 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
) {
this._name = name;
this._queuedRequests = [];
@ -33,8 +28,16 @@ export class ExtHostTerminal implements vscode.Terminal {
this._pidPromise = new Promise<number>(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);
@ -99,23 +102,27 @@ export class ExtHostTerminal implements vscode.Terminal {
export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
private readonly _onDidCloseTerminal: Emitter<vscode.Terminal>;
private readonly _onDidOpenTerminal: Emitter<vscode.Terminal>;
private _proxy: MainThreadTerminalServiceShape;
private _terminals: ExtHostTerminal[];
constructor(mainContext: IMainContext) {
this._onDidCloseTerminal = new Emitter<vscode.Terminal>();
this._onDidOpenTerminal = new Emitter<vscode.Terminal>();
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 +131,10 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
return this._onDidCloseTerminal && this._onDidCloseTerminal.event;
}
public get onDidOpenTerminal(): Event<vscode.Terminal> {
return this._onDidOpenTerminal && this._onDidOpenTerminal.event;
}
public $acceptTerminalClosed(id: number): void {
let index = this._getTerminalIndexById(id);
if (index === null) {
@ -134,6 +145,15 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
this._onDidCloseTerminal.fire(terminal);
}
// TOOD: How do we set PID
// TODO: Make sure both API terminals and non-API terminals are created correctly
public $acceptTerminalOpened(id: number, name: string): void {
// TODO: Only create a terminal if it doesn't already exist for the ID
let terminal = new ExtHostTerminal(this._proxy, name);
this._terminals.push(terminal);
this._onDidOpenTerminal.fire(terminal);
}
public $acceptTerminalProcessId(id: number, processId: number): void {
let terminal = this._getTerminalById(id);
if (terminal) {

View file

@ -149,6 +149,7 @@ export interface ITerminalService {
configHelper: ITerminalConfigHelper;
onActiveTabChanged: Event<void>;
onTabDisposed: Event<ITerminalTab>;
onInstanceCreated: Event<ITerminalInstance>;
onInstanceDisposed: Event<ITerminalInstance>;
onInstanceProcessIdReady: Event<ITerminalInstance>;
onInstancesChanged: Event<void>;

View file

@ -24,6 +24,7 @@ export abstract class TerminalService implements ITerminalService {
protected _terminalContainer: HTMLElement;
protected _onInstancesChanged: Emitter<void>;
protected _onTabDisposed: Emitter<ITerminalTab>;
protected _onInstanceCreated: Emitter<ITerminalInstance>;
protected _onInstanceDisposed: Emitter<ITerminalInstance>;
protected _onInstanceProcessIdReady: Emitter<ITerminalInstance>;
protected _onInstanceTitleChanged: Emitter<string>;
@ -36,6 +37,7 @@ export abstract class TerminalService implements ITerminalService {
public get activeTabIndex(): number { return this._activeTabIndex; }
public get onActiveTabChanged(): Event<void> { return this._onActiveTabChanged.event; }
public get onTabDisposed(): Event<ITerminalTab> { return this._onTabDisposed.event; }
public get onInstanceCreated(): Event<ITerminalInstance> { return this._onInstanceCreated.event; }
public get onInstanceDisposed(): Event<ITerminalInstance> { return this._onInstanceDisposed.event; }
public get onInstanceProcessIdReady(): Event<ITerminalInstance> { return this._onInstanceProcessIdReady.event; }
public get onInstanceTitleChanged(): Event<string> { return this._onInstanceTitleChanged.event; }
@ -57,6 +59,7 @@ export abstract class TerminalService implements ITerminalService {
this._onActiveTabChanged = new Emitter<void>();
this._onTabDisposed = new Emitter<ITerminalTab>();
this._onInstanceCreated = new Emitter<ITerminalInstance>();
this._onInstanceDisposed = new Emitter<ITerminalInstance>();
this._onInstanceProcessIdReady = new Emitter<ITerminalInstance>();
this._onInstanceTitleChanged = new Emitter<string>();

View file

@ -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;

View file

@ -402,6 +402,8 @@ export class TerminalTab extends Disposable implements ITerminalTab {
shellLaunchConfig);
this._terminalInstances.splice(this._activeInstanceIndex + 1, 0, instance);
this._initInstanceListeners(instance);
// TODO: Ensure change event is fired
// TODO: Fire create event on service
this._setActiveInstance(instance);
if (this._splitPaneContainer) {