Implement window.onDidCloseTerminal extension API

Fixes #10925
This commit is contained in:
Daniel Imms 2016-09-15 12:03:40 -07:00
parent 8f6caf1f9f
commit e3831ee39c
8 changed files with 82 additions and 11 deletions

5
src/vs/vscode.d.ts vendored
View file

@ -3328,6 +3328,11 @@ declare namespace vscode {
*/
export const onDidChangeTextEditorViewColumn: Event<TextEditorViewColumnChangeEvent>;
/**
* An [event](#Event) which fires when a terminal closes.
*/
export const onDidCloseTerminal: Event<Terminal>;
/**
* Show the given document in a text editor. A [column](#ViewColumn) can be provided
* to control where the editor is being shown. Might change the [active editor](#window.activeTextEditor).

View file

@ -109,6 +109,7 @@ export class ExtHostAPIImplementation {
const languageFeatures = col.define(ExtHostContext.ExtHostLanguageFeatures).set<ExtHostLanguageFeatures>(new ExtHostLanguageFeatures(threadService, extHostDocuments, extHostCommands, extHostHeapMonitor, extHostDiagnostics));
const extHostFileSystemEvent = col.define(ExtHostContext.ExtHostFileSystemEventService).set<ExtHostFileSystemEventService>(new ExtHostFileSystemEventService());
const extHostQuickOpen = col.define(ExtHostContext.ExtHostQuickOpen).set<ExtHostQuickOpen>(new ExtHostQuickOpen(threadService));
const extHostTerminalService = col.define(ExtHostContext.ExtHostTerminalService).set<ExtHostTerminalService>(new ExtHostTerminalService(threadService));
col.define(ExtHostContext.ExtHostExtensionService).set(extensionService);
col.finish(false, threadService);
@ -122,7 +123,6 @@ export class ExtHostAPIImplementation {
const extHostMessageService = new ExtHostMessageService(threadService);
const extHostStatusBar = new ExtHostStatusBar(threadService);
const extHostOutputService = new ExtHostOutputService(threadService);
const extHostTerminalService = new ExtHostTerminalService(threadService);
const workspacePath = contextService.getWorkspace() ? contextService.getWorkspace().resource.fsPath : undefined;
const extHostWorkspace = new ExtHostWorkspace(threadService, workspacePath);
const languages = new ExtHostLanguages(threadService);
@ -235,6 +235,7 @@ export class ExtHostAPIImplementation {
onDidChangeTextEditorViewColumn(listener, thisArg?, disposables?) {
return extHostEditors.onDidChangeTextEditorViewColumn(listener, thisArg, disposables);
},
onDidCloseTerminal: extHostTerminalService.onDidCloseTerminal.bind(extHostTerminalService),
showInformationMessage: (message, ...items) => {
return extHostMessageService.showMessage(Severity.Info, message, items);
},

View file

@ -310,6 +310,10 @@ export abstract class ExtHostQuickOpenShape {
$validateInput(input: string): TPromise<string> { throw ni(); }
}
export abstract class ExtHostTerminalServiceShape {
$acceptTerminalClosed(id: number): void { throw ni(); }
}
// --- proxy identifiers
export const MainContext = {
@ -343,4 +347,5 @@ export const ExtHostContext = {
ExtHostLanguageFeatures: createExtId<ExtHostLanguageFeaturesShape>('ExtHostLanguageFeatures', ExtHostLanguageFeaturesShape),
ExtHostQuickOpen: createExtId<ExtHostQuickOpenShape>('ExtHostQuickOpen', ExtHostQuickOpenShape),
ExtHostExtensionService: createExtId<ExtHostExtensionServiceShape>('ExtHostExtensionService', ExtHostExtensionServiceShape),
ExtHostTerminalService: createExtId<ExtHostTerminalServiceShape>('ExtHostTerminalService', ExtHostTerminalServiceShape)
};

View file

@ -4,9 +4,10 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
import Event, {Emitter} from 'vs/base/common/event';
import vscode = require('vscode');
import {MainContext, MainThreadTerminalServiceShape} from './extHost.protocol';
import {ExtHostTerminalServiceShape, MainContext, MainThreadTerminalServiceShape} from './extHost.protocol';
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
export class ExtHostTerminal implements vscode.Terminal {
@ -14,10 +15,11 @@ export class ExtHostTerminal implements vscode.Terminal {
private _id: number;
private _proxy: MainThreadTerminalServiceShape;
private _disposed: boolean;
private _queuedRequests: ApiRequest[] = [];
private _queuedRequests: ApiRequest[];
constructor(proxy: MainThreadTerminalServiceShape, name?: string, shellPath?: string, shellArgs?: string[]) {
this._name = name;
this._queuedRequests = [];
this._proxy = proxy;
this._proxy.$createTerminal(name, shellPath, shellArgs).then((id) => {
this._id = id;
@ -70,16 +72,48 @@ export class ExtHostTerminal implements vscode.Terminal {
}
}
export class ExtHostTerminalService {
export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
private _onDidCloseTerminal: Emitter<vscode.Terminal>;
private _proxy: MainThreadTerminalServiceShape;
private _terminals: ExtHostTerminal[];
constructor(threadService: IThreadService) {
this._onDidCloseTerminal = new Emitter<vscode.Terminal>();
this._proxy = threadService.get(MainContext.MainThreadTerminalService);
this._terminals = [];
}
public createTerminal(name?: string, shellPath?: string, shellArgs?: string[]): vscode.Terminal {
return new ExtHostTerminal(this._proxy, name, shellPath, shellArgs);
let terminal = new ExtHostTerminal(this._proxy, name, shellPath, shellArgs);
this._terminals.push(terminal);
return terminal;
}
public get onDidCloseTerminal(): Event<vscode.Terminal> {
return this._onDidCloseTerminal && this._onDidCloseTerminal.event;
}
public $acceptTerminalClosed(id: number): void {
let index = this._getTerminalIndexById(id);
if (index === null) {
// The terminal was not created by the terminal API, ignore it
return;
}
let terminal = this._terminals.splice(index, 1)[0];
this._onDidCloseTerminal.fire(terminal);
}
private _getTerminalIndexById(id: number): number {
let index: number = null;
this._terminals.some((terminal, i) => {
let thisId = (<any>terminal)._id;
if (thisId === id) {
index = i;
return true;
}
});
return index;
}
}

View file

@ -4,20 +4,33 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import {ITerminalService} from 'vs/workbench/parts/terminal/electron-browser/terminal';
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
import {IPanelService} from 'vs/workbench/services/panel/common/panelService';
import {IPartService} from 'vs/workbench/services/part/common/partService';
import {MainThreadTerminalServiceShape} from './extHost.protocol';
import {ITerminalService, ITerminalInstance} from 'vs/workbench/parts/terminal/electron-browser/terminal';
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
import {TPromise} from 'vs/base/common/winjs.base';
import {ExtHostContext, ExtHostTerminalServiceShape, MainThreadTerminalServiceShape} from './extHost.protocol';
export class MainThreadTerminalService extends MainThreadTerminalServiceShape {
private _proxy: ExtHostTerminalServiceShape;
private _toDispose: IDisposable[];
constructor(
@IPanelService private panelService: IPanelService,
@IPartService private partService: IPartService,
@IThreadService private threadService: IThreadService,
@ITerminalService private terminalService: ITerminalService
) {
super();
this._proxy = threadService.get(ExtHostContext.ExtHostTerminalService);
this._toDispose = [];
this._toDispose.push(terminalService.onInstanceClosed((terminalInstance) => this._onTerminalClosed(terminalInstance)));
}
public dispose(): void {
this._toDispose = dispose(this._toDispose);
}
public $createTerminal(name?: string, shellPath?: string, shellArgs?: string[]): TPromise<number> {
@ -51,4 +64,8 @@ export class MainThreadTerminalService extends MainThreadTerminalServiceShape {
terminalInstance.sendText(text, addNewLine);
}
}
private _onTerminalClosed(terminalInstance: ITerminalInstance): void {
this._proxy.$acceptTerminalClosed(terminalInstance.id);
}
}

View file

@ -58,6 +58,7 @@ export interface ITerminalService {
activeTerminalInstanceIndex: number;
configHelper: TerminalConfigHelper;
onActiveInstanceChanged: Event<string>;
onInstanceClosed: Event<ITerminalInstance>;
onInstancesChanged: Event<string>;
onInstanceTitleChanged: Event<string>;
terminalInstances: ITerminalInstance[];

View file

@ -29,12 +29,13 @@ export class TerminalInstance implements ITerminalInstance {
private static ID_COUNTER = 1;
private static EOL_REGEX = /\r?\n/g;
private _id: number;
private _title: string;
private _onClosed: Emitter<TerminalInstance>;
private _onTitleChanged: Emitter<string>;
public get id(): number { return this._id; }
public get title(): string { return this._title; }
public get onClosed(): Event<TerminalInstance> { return this._onClosed.event; }
public get onTitleChanged(): Event<string> { return this._onTitleChanged.event; }
private isExiting: boolean = false;
@ -59,6 +60,7 @@ export class TerminalInstance implements ITerminalInstance {
) {
this._id = TerminalInstance.ID_COUNTER++;
this._onTitleChanged = new Emitter<string>();
this._onClosed = new Emitter<TerminalInstance>();
this.createProcess(workspace, name, shell);
if (container) {
@ -168,7 +170,9 @@ export class TerminalInstance implements ITerminalInstance {
}
this.process = null;
}
this._onClosed.fire(this);
this.toDispose = lifecycle.dispose(this.toDispose);
// TODO: Move exit callback to listen onClose event
this.onExitCallback(this);
}

View file

@ -23,14 +23,16 @@ export class TerminalService implements ITerminalService {
private _activeTerminalInstanceIndex: number = 0;
private _configHelper: TerminalConfigHelper;
private _onActiveInstanceChanged: Emitter<string>;
private _onInstancesChanged: Emitter<string>;
private _onInstanceClosed: Emitter<ITerminalInstance>;
private _onInstanceTitleChanged: Emitter<string>;
private _onInstancesChanged: Emitter<string>;
private _terminalInstances: ITerminalInstance[] = [];
public get activeTerminalInstanceIndex(): number { return this._activeTerminalInstanceIndex; }
public get configHelper(): TerminalConfigHelper { return this._configHelper; }
public get onActiveInstanceChanged(): Event<string> { return this._onActiveInstanceChanged.event; }
public get onInstancesChanged(): Event<string> { return this._onInstancesChanged.event; }
public get onInstanceClosed(): Event<ITerminalInstance> { return this._onInstanceClosed.event; }
public get onInstanceTitleChanged(): Event<string> { return this._onInstanceTitleChanged.event; }
public get onInstancesChanged(): Event<string> { return this._onInstancesChanged.event; }
public get terminalInstances(): ITerminalInstance[] { return this._terminalInstances; }
private terminalContainer: HTMLElement;
@ -45,6 +47,7 @@ export class TerminalService implements ITerminalService {
@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService
) {
this._onActiveInstanceChanged = new Emitter<string>();
this._onInstanceClosed = new Emitter<ITerminalInstance>();
this._onInstancesChanged = new Emitter<string>();
this._onInstanceTitleChanged = new Emitter<string>();
this.terminalFocusContextKey = KEYBINDING_CONTEXT_TERMINAL_FOCUS.bindTo(this.contextKeyService);
@ -65,6 +68,7 @@ export class TerminalService implements ITerminalService {
name,
shell);
terminalInstance.addDisposable(terminalInstance.onTitleChanged(this._onInstanceTitleChanged.fire, this._onInstanceTitleChanged));
terminalInstance.addDisposable(terminalInstance.onClosed(this._onInstanceClosed.fire, this._onInstanceClosed));
this.terminalInstances.push(terminalInstance);
if (this.terminalInstances.length === 1) {
// It's the first instance so it should be made active automatically