withStatusBarProgress API and implementation

This commit is contained in:
Johannes Rieken
2017-01-13 12:09:19 +01:00
parent 39c2de9b58
commit 33492341c9
11 changed files with 285 additions and 1 deletions

View File

@@ -20,6 +20,7 @@ import { ExtHostDiagnostics } from 'vs/workbench/api/node/extHostDiagnostics';
import { ExtHostTreeExplorers } from 'vs/workbench/api/node/extHostTreeExplorers';
import { ExtHostWorkspace } from 'vs/workbench/api/node/extHostWorkspace';
import { ExtHostQuickOpen } from 'vs/workbench/api/node/extHostQuickOpen';
import { ExtHostProgress } from 'vs/workbench/api/node/extHostProgress';
import { ExtHostHeapService } from 'vs/workbench/api/node/extHostHeapService';
import { ExtHostStatusBar } from 'vs/workbench/api/node/extHostStatusBar';
import { ExtHostCommands } from 'vs/workbench/api/node/extHostCommands';
@@ -84,6 +85,7 @@ export function createApiFactory(initData: IInitData, threadService: IThreadServ
// Other instances
const extHostMessageService = new ExtHostMessageService(threadService);
const extHostStatusBar = new ExtHostStatusBar(threadService);
const extHostProgress = new ExtHostProgress(threadService.get(MainContext.MainThreadProgress));
const extHostOutputService = new ExtHostOutputService(threadService);
const workspacePath = contextService.hasWorkspace() ? contextService.getWorkspace().resource.fsPath : undefined;
const extHostWorkspace = new ExtHostWorkspace(threadService, workspacePath);
@@ -274,6 +276,9 @@ export function createApiFactory(initData: IInitData, threadService: IThreadServ
setStatusBarMessage(text: string, timeoutOrThenable?: number | Thenable<any>): vscode.Disposable {
return extHostStatusBar.setStatusBarMessage(text, timeoutOrThenable);
},
withStatusBarProgress: proposedApiFunction(extension, <R>(task: (progress: vscode.Progress<string>, token: vscode.CancellationToken) => Thenable<R>): Thenable<R> => {
return extHostProgress.withStatusBarProgress(task);
}),
createOutputChannel(name: string): vscode.OutputChannel {
return extHostOutputService.createOutputChannel(name);
},

View File

@@ -24,6 +24,7 @@ import { MainThreadLanguageFeatures } from './mainThreadLanguageFeatures';
import { MainThreadLanguages } from './mainThreadLanguages';
import { MainThreadMessageService } from './mainThreadMessageService';
import { MainThreadOutputService } from './mainThreadOutputService';
import { MainThreadProgress } from './mainThreadProgress';
import { MainThreadQuickOpen } from './mainThreadQuickOpen';
import { MainThreadStatusBar } from './mainThreadStatusBar';
import { MainThreadStorage } from './mainThreadStorage';
@@ -74,6 +75,7 @@ export class ExtHostContribution implements IWorkbenchContribution {
col.define(MainContext.MainThreadLanguages).set(create(MainThreadLanguages));
col.define(MainContext.MainThreadMessageService).set(create(MainThreadMessageService));
col.define(MainContext.MainThreadOutputService).set(create(MainThreadOutputService));
col.define(MainContext.MainThreadProgress).set(create(MainThreadProgress));
col.define(MainContext.MainThreadQuickOpen).set(create(MainThreadQuickOpen));
col.define(MainContext.MainThreadStatusBar).set(create(MainThreadStatusBar));
col.define(MainContext.MainThreadStorage).set(create(MainThreadStorage));
@@ -97,4 +99,4 @@ export class ExtHostContribution implements IWorkbenchContribution {
// Register File Tracker
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(
ExtHostContribution
);
);

View File

@@ -183,6 +183,12 @@ export abstract class MainThreadOutputServiceShape {
$close(channelId: string): TPromise<void> { throw ni(); }
}
export abstract class MainThreadProgressShape {
$progressStart(handle: number): void { throw ni(); }
$progressReport(handle: number, message: string): void { throw ni(); }
$progressEnd(handle: number, err?: any): void { throw ni(); }
}
export abstract class MainThreadTerminalServiceShape {
$createTerminal(name?: string, shellPath?: string, shellArgs?: string[], waitOnExit?: boolean): TPromise<number> { throw ni(); }
$dispose(terminalId: number): void { throw ni(); }
@@ -370,6 +376,7 @@ export const MainContext = {
MainThreadLanguages: createMainId<MainThreadLanguagesShape>('MainThreadLanguages', MainThreadLanguagesShape),
MainThreadMessageService: createMainId<MainThreadMessageServiceShape>('MainThreadMessageService', MainThreadMessageServiceShape),
MainThreadOutputService: createMainId<MainThreadOutputServiceShape>('MainThreadOutputService', MainThreadOutputServiceShape),
MainThreadProgress: createMainId<MainThreadProgressShape>('MainThreadProgress', MainThreadProgressShape),
MainThreadQuickOpen: createMainId<MainThreadQuickOpenShape>('MainThreadQuickOpen', MainThreadQuickOpenShape),
MainThreadStatusBar: createMainId<MainThreadStatusBarShape>('MainThreadStatusBar', MainThreadStatusBarShape),
MainThreadStorage: createMainId<MainThreadStorageShape>('MainThreadStorage', MainThreadStorageShape),

View File

@@ -0,0 +1,46 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { Progress, CancellationToken } from 'vscode';
import { MainThreadProgressShape } from './extHost.protocol';
export class ExtHostProgress {
private _proxy: MainThreadProgressShape;
private _handles: number = 0;
constructor(proxy: MainThreadProgressShape) {
this._proxy = proxy;
}
withStatusBarProgress<R>(task: (progress: Progress<string>, token: CancellationToken) => Thenable<R>): Thenable<R> {
const handle = this._handles++;
this._proxy.$progressStart(handle);
const progress = {
report: (message: string) => {
this._proxy.$progressReport(handle, message);
}
};
let p: Thenable<R>;
try {
p = task(progress, null);
} catch (err) {
this._proxy.$progressEnd(handle);
throw err;
}
return p.then(result => {
this._proxy.$progressEnd(handle);
return result;
}, err => {
this._proxy.$progressEnd(handle, err);
throw err;
});
}
}

View File

@@ -0,0 +1,44 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { IProgressService2 } from 'vs/platform/progress/common/progress';
import { TPromise } from 'vs/base/common/winjs.base';
import { MainThreadProgressShape } from './extHost.protocol';
export class MainThreadProgress extends MainThreadProgressShape {
private _progressService: IProgressService2;
private progress = new Map<number, { resolve: Function, reject: Function, progress: { report(m: string) } }>();
constructor(
@IProgressService2 progressService: IProgressService2
) {
super();
this._progressService = progressService;
}
$progressStart(handle: number): void {
this._progressService.withStatusBarProgress(progress => {
return new TPromise<any>((resolve, reject) => {
this.progress.set(handle, { resolve, reject, progress });
});
});
}
$progressReport(handle: number, message: string): void {
this.progress.get(handle).progress.report(message);
}
$progressEnd(handle: number, err: any): void {
if (err) {
this.progress.get(handle).reject(err);
} else {
this.progress.get(handle).resolve();
}
this.progress.delete(handle);
}
}