make vscode.d.ts use a declare module, #13990

* a declared module allows to have a sibling module containing experimental API
* this change also cleans up some bogous type pollution that happened because vscode used to be both, a declare module and a global namespace
This commit is contained in:
Johannes Rieken 2016-10-19 11:43:40 +02:00
parent 27a24ea7d1
commit dc7402b978
14 changed files with 124 additions and 124 deletions

View file

@ -8,7 +8,7 @@
// Declaring the following because the code gets compiled with es5, which lack definitions for console and timers.
declare var console: {
assert(value: any, message?: string, ...optionalParams: any[]): void;
dir(obj: any, options?: {showHidden?: boolean, depth?: number, colors?: boolean}): void;
dir(obj: any, options?: { showHidden?: boolean, depth?: number, colors?: boolean }): void;
error(message?: any, ...optionalParams: any[]): void;
info(message?: any, ...optionalParams: any[]): void;
log(message?: any, ...optionalParams: any[]): void;
@ -16,4 +16,92 @@ declare var console: {
timeEnd(label: string): void;
trace(message?: any, ...optionalParams: any[]): void;
warn(message?: any, ...optionalParams: any[]): void;
};
};
// ---- ES6 promise ------------------------------------------------------
/**
* Represents the completion of an asynchronous operation.
*/
interface Promise<T> extends Thenable<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult>(onfulfilled?: (value: T) => TResult | Thenable<TResult>, onrejected?: (reason: any) => TResult | Thenable<TResult>): Promise<TResult>;
then<TResult>(onfulfilled?: (value: T) => TResult | Thenable<TResult>, onrejected?: (reason: any) => void): Promise<TResult>;
/**
* Attaches a callback for only the rejection of the Promise.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch(onrejected?: (reason: any) => T | Thenable<T>): Promise<T>;
// [Symbol.toStringTag]: string;
}
interface PromiseConstructor {
// /**
// * A reference to the prototype.
// */
// prototype: Promise<any>;
/**
* Creates a new Promise.
* @param executor A callback used to initialize the promise. This callback is passed two arguments:
* a resolve callback used to resolve the promise with a value or the result of another promise,
* and a reject callback used to reject the promise with a provided reason or error.
*/
new <T>(executor: (resolve: (value?: T | Thenable<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T>(values: Array<T | Thenable<T>>): Promise<T[]>;
/**
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
* or rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
race<T>(values: Array<T | Thenable<T>>): Promise<T>;
/**
* Creates a new rejected promise for the provided reason.
* @param reason The reason the promise was rejected.
* @returns A new rejected Promise.
*/
reject(reason: any): Promise<void>;
/**
* Creates a new rejected promise for the provided reason.
* @param reason The reason the promise was rejected.
* @returns A new rejected Promise.
*/
reject<T>(reason: any): Promise<T>;
/**
* Creates a new resolved promise for the provided value.
* @param value A promise.
* @returns A promise whose internal state matches the provided promise.
*/
resolve<T>(value: T | Thenable<T>): Promise<T>;
/**
* Creates a new resolved promise.
* @returns A resolved promise.
*/
resolve(): Promise<void>;
// [Symbol.species]: Function;
}
declare var Promise: PromiseConstructor;

View file

@ -6,14 +6,14 @@
'use strict';
import * as assert from 'assert';
import { workspace, window, Position, Range, commands } from 'vscode';
import { workspace, window, Position, Range, commands, TextEditor, TextDocument } from 'vscode';
import { createRandomFile, deleteFile, cleanUp } from './utils';
suite('editor tests', () => {
teardown(cleanUp);
function withRandomFileEditor(initialContents: string, run: (editor: vscode.TextEditor, doc: vscode.TextDocument) => Thenable<void>): Thenable<boolean> {
function withRandomFileEditor(initialContents: string, run: (editor: TextEditor, doc: TextDocument) => Thenable<void>): Thenable<boolean> {
return createRandomFile(initialContents).then(file => {
return workspace.openTextDocument(file).then(doc => {
return window.showTextDocument(doc).then((editor) => {
@ -57,7 +57,7 @@ suite('editor tests', () => {
});
});
function executeReplace(editor: vscode.TextEditor, range: Range, text: string, undoStopBefore: boolean, undoStopAfter: boolean): Thenable<boolean> {
function executeReplace(editor: TextEditor, range: Range, text: string, undoStopBefore: boolean, undoStopAfter: boolean): Thenable<boolean> {
return editor.edit((builder) => {
builder.replace(range, text);
}, { undoStopBefore: undoStopBefore, undoStopAfter: undoStopAfter });

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

@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
declare namespace vscode {
declare module 'vscode' {
/**
* The version of the editor.
@ -4197,15 +4197,6 @@ declare namespace vscode {
}
}
// TS 1.6 & node_module
// export = vscode;
// when used for JS*
// !!! DO NOT MODIFY ABOVE COMMENT ("when used for JS*") IT IS BEING USED TO DETECT JS* ONLY CHANGES !!!
declare module 'vscode' {
export = vscode;
}
/**
* Thenable is a common denominator between ES6 promises, Q, jquery.Deferred, WinJS.Promise,
* and others. This API makes no assumption about what promise libary is being used which
@ -4222,90 +4213,3 @@ interface Thenable<R> {
then<TResult>(onfulfilled?: (value: R) => TResult | Thenable<TResult>, onrejected?: (reason: any) => TResult | Thenable<TResult>): Thenable<TResult>;
then<TResult>(onfulfilled?: (value: R) => TResult | Thenable<TResult>, onrejected?: (reason: any) => void): Thenable<TResult>;
}
// ---- ES6 promise ------------------------------------------------------
/**
* Represents the completion of an asynchronous operation.
*/
interface Promise<T> extends Thenable<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult>(onfulfilled?: (value: T) => TResult | Thenable<TResult>, onrejected?: (reason: any) => TResult | Thenable<TResult>): Promise<TResult>;
then<TResult>(onfulfilled?: (value: T) => TResult | Thenable<TResult>, onrejected?: (reason: any) => void): Promise<TResult>;
/**
* Attaches a callback for only the rejection of the Promise.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch(onrejected?: (reason: any) => T | Thenable<T>): Promise<T>;
// [Symbol.toStringTag]: string;
}
interface PromiseConstructor {
// /**
// * A reference to the prototype.
// */
// prototype: Promise<any>;
/**
* Creates a new Promise.
* @param executor A callback used to initialize the promise. This callback is passed two arguments:
* a resolve callback used to resolve the promise with a value or the result of another promise,
* and a reject callback used to reject the promise with a provided reason or error.
*/
new <T>(executor: (resolve: (value?: T | Thenable<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T>(values: Array<T | Thenable<T>>): Promise<T[]>;
/**
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
* or rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
race<T>(values: Array<T | Thenable<T>>): Promise<T>;
/**
* Creates a new rejected promise for the provided reason.
* @param reason The reason the promise was rejected.
* @returns A new rejected Promise.
*/
reject(reason: any): Promise<void>;
/**
* Creates a new rejected promise for the provided reason.
* @param reason The reason the promise was rejected.
* @returns A new rejected Promise.
*/
reject<T>(reason: any): Promise<T>;
/**
* Creates a new resolved promise for the provided value.
* @param value A promise.
* @returns A promise whose internal state matches the provided promise.
*/
resolve<T>(value: T | Thenable<T>): Promise<T>;
/**
* Creates a new resolved promise.
* @returns A resolved promise.
*/
resolve(): Promise<void>;
// [Symbol.species]: Function;
}
declare var Promise: PromiseConstructor;

View file

@ -88,7 +88,7 @@ export class ExtHostCommands extends ExtHostCommandsShape {
$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
let command = this._commands[id];
if (!command) {
return Promise.reject<T>(`Contributed command '${id}' does not exist.`);
return TPromise.wrapError<T>(`Contributed command '${id}' does not exist.`);
}
let {callback, thisArg, description} = command;
@ -98,14 +98,14 @@ export class ExtHostCommands extends ExtHostCommandsShape {
try {
validateConstraint(args[i], description.args[i].constraint);
} catch (err) {
return Promise.reject<T>(`Running the contributed command:'${id}' failed. Illegal argument '${description.args[i].name}' - ${description.args[i].description}`);
return TPromise.wrapError<T>(`Running the contributed command:'${id}' failed. Illegal argument '${description.args[i].name}' - ${description.args[i].description}`);
}
}
}
try {
let result = callback.apply(thisArg, args);
return Promise.resolve(result);
return TPromise.as(result);
} catch (err) {
// console.log(err);
// try {
@ -113,7 +113,7 @@ export class ExtHostCommands extends ExtHostCommandsShape {
// } catch (err) {
// //
// }
return Promise.reject<T>(`Running the contributed command:'${id}' failed.`);
return TPromise.wrapError<T>(`Running the contributed command:'${id}' failed.`);
}
}

View file

@ -16,6 +16,7 @@ import { fromRange, TextDocumentSaveReason } from 'vs/workbench/api/node/extHost
import { IResourceEdit } from 'vs/editor/common/services/bulkEdit';
import { ExtHostDocuments } from 'vs/workbench/api/node/extHostDocuments';
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
import * as vscode from 'vscode';
declare class WeakMap<K, V> {
// delete(key: K): boolean;

View file

@ -16,19 +16,19 @@ import { Selection, Range, Position, EditorOptions, EndOfLine, TextEditorRevealT
import { ISingleEditOperation } from 'vs/editor/common/editorCommon';
import { IResolvedTextEditorConfiguration, ISelectionChangeEvent } from 'vs/workbench/api/node/mainThreadEditorsTracker';
import * as TypeConverters from './extHostTypeConverters';
import { TextDocument, TextEditorSelectionChangeEvent, TextEditorOptionsChangeEvent, TextEditorOptions, TextEditorViewColumnChangeEvent, ViewColumn } from 'vscode';
import { MainContext, MainThreadEditorsShape, ExtHostEditorsShape, ITextEditorAddData, ITextEditorPositionData } from './extHost.protocol';
import * as vscode from 'vscode';
export class ExtHostEditors extends ExtHostEditorsShape {
public onDidChangeTextEditorSelection: Event<TextEditorSelectionChangeEvent>;
private _onDidChangeTextEditorSelection: Emitter<TextEditorSelectionChangeEvent>;
public onDidChangeTextEditorSelection: Event<vscode.TextEditorSelectionChangeEvent>;
private _onDidChangeTextEditorSelection: Emitter<vscode.TextEditorSelectionChangeEvent>;
public onDidChangeTextEditorOptions: Event<TextEditorOptionsChangeEvent>;
private _onDidChangeTextEditorOptions: Emitter<TextEditorOptionsChangeEvent>;
public onDidChangeTextEditorOptions: Event<vscode.TextEditorOptionsChangeEvent>;
private _onDidChangeTextEditorOptions: Emitter<vscode.TextEditorOptionsChangeEvent>;
public onDidChangeTextEditorViewColumn: Event<TextEditorViewColumnChangeEvent>;
private _onDidChangeTextEditorViewColumn: Emitter<TextEditorViewColumnChangeEvent>;
public onDidChangeTextEditorViewColumn: Event<vscode.TextEditorViewColumnChangeEvent>;
private _onDidChangeTextEditorViewColumn: Emitter<vscode.TextEditorViewColumnChangeEvent>;
private _editors: { [id: string]: ExtHostTextEditor };
private _proxy: MainThreadEditorsShape;
@ -43,13 +43,13 @@ export class ExtHostEditors extends ExtHostEditorsShape {
extHostDocuments: ExtHostDocuments
) {
super();
this._onDidChangeTextEditorSelection = new Emitter<TextEditorSelectionChangeEvent>();
this._onDidChangeTextEditorSelection = new Emitter<vscode.TextEditorSelectionChangeEvent>();
this.onDidChangeTextEditorSelection = this._onDidChangeTextEditorSelection.event;
this._onDidChangeTextEditorOptions = new Emitter<TextEditorOptionsChangeEvent>();
this._onDidChangeTextEditorOptions = new Emitter<vscode.TextEditorOptionsChangeEvent>();
this.onDidChangeTextEditorOptions = this._onDidChangeTextEditorOptions.event;
this._onDidChangeTextEditorViewColumn = new Emitter<TextEditorViewColumnChangeEvent>();
this._onDidChangeTextEditorViewColumn = new Emitter<vscode.TextEditorViewColumnChangeEvent>();
this.onDidChangeTextEditorViewColumn = this._onDidChangeTextEditorViewColumn.event;
this._extHostDocuments = extHostDocuments;
@ -77,7 +77,7 @@ export class ExtHostEditors extends ExtHostEditorsShape {
return this._onDidChangeVisibleTextEditors && this._onDidChangeVisibleTextEditors.event;
}
showTextDocument(document: TextDocument, column: ViewColumn, preserveFocus: boolean): TPromise<vscode.TextEditor> {
showTextDocument(document: vscode.TextDocument, column: vscode.ViewColumn, preserveFocus: boolean): TPromise<vscode.TextEditor> {
return this._proxy.$tryShowTextDocument(<URI>document.uri, TypeConverters.fromViewColumn(column), preserveFocus).then(id => {
let editor = this._editors[id];
if (editor) {
@ -300,7 +300,7 @@ class ExtHostTextEditor implements vscode.TextEditor {
private _documentData: ExtHostDocumentData;
private _selections: Selection[];
private _options: TextEditorOptions;
private _options: vscode.TextEditorOptions;
private _viewColumn: vscode.ViewColumn;
constructor(proxy: MainThreadEditorsShape, id: string, document: ExtHostDocumentData, selections: Selection[], options: EditorOptions, viewColumn: vscode.ViewColumn) {
@ -336,11 +336,11 @@ class ExtHostTextEditor implements vscode.TextEditor {
// ---- options
get options(): TextEditorOptions {
get options(): vscode.TextEditorOptions {
return this._options;
}
set options(value: TextEditorOptions) {
set options(value: vscode.TextEditorOptions) {
this._options = value;
this._runOnProxy(() => {
return this._proxy.$trySetOptions(this._id, this._options);

View file

@ -6,6 +6,7 @@
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
import { MainContext, MainThreadOutputServiceShape } from './extHost.protocol';
import * as vscode from 'vscode';
export class ExtHostOutputChannel implements vscode.OutputChannel {

View file

@ -4,8 +4,9 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import Event, { Emitter } from 'vs/base/common/event';
import vscode = require('vscode');
import { TPromise } from 'vs/base/common/winjs.base';
import Event, { Emitter } from 'vs/base/common/event';
import { ExtHostTerminalServiceShape, MainContext, MainThreadTerminalServiceShape } from './extHost.protocol';
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
@ -38,9 +39,9 @@ export class ExtHostTerminal implements vscode.Terminal {
public get processId(): Thenable<number> {
this._checkDisposed();
if (this._processId) {
return Promise.resolve<number>(this._processId);
return TPromise.as<number>(this._processId);
}
return new Promise<number>((resolve) => {
return new TPromise<number>((resolve) => {
setTimeout(() => {
this.processId.then(resolve);
}, 200);

View file

@ -6,6 +6,7 @@
import URI from 'vs/base/common/uri';
import { illegalArgument } from 'vs/base/common/errors';
import * as vscode from 'vscode';
export class Disposable {

View file

@ -12,6 +12,7 @@ import { TPromise } from 'vs/base/common/winjs.base';
import { fromRange } from 'vs/workbench/api/node/extHostTypeConverters';
import { Uri, CancellationToken } from 'vscode';
import { MainContext, MainThreadWorkspaceShape } from './extHost.protocol';
import * as vscode from 'vscode';
export class ExtHostWorkspace {

View file

@ -52,7 +52,7 @@ function connectToRenderer(): TPromise<IRendererConnection> {
// Print a console message when rejection isn't handled within N seconds. For details:
// see https://nodejs.org/api/process.html#process_event_unhandledrejection
// and https://nodejs.org/api/process.html#process_event_rejectionhandled
const unhandledPromises: Promise<any>[] = [];
const unhandledPromises: TPromise<any>[] = [];
process.on('unhandledRejection', (reason, promise) => {
unhandledPromises.push(promise);
setTimeout(() => {

View file

@ -29,6 +29,7 @@ import { ExtHostDocuments } from 'vs/workbench/api/node/extHostDocuments';
import * as ExtHostTypeConverters from 'vs/workbench/api/node/extHostTypeConverters';
import { MainContext, ExtHostContext } from 'vs/workbench/api/node/extHost.protocol';
import { ExtHostDiagnostics } from 'vs/workbench/api/node/extHostDiagnostics';
import * as vscode from 'vscode';
const defaultSelector = { scheme: 'far' };
const model: EditorCommon.IModel = EditorModel.createFromString(

View file

@ -15,6 +15,7 @@ import { OneGetThreadService } from './testThreadService';
import * as EditorCommon from 'vs/editor/common/editorCommon';
import { IResourceEdit } from 'vs/editor/common/services/bulkEdit';
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
import * as vscode from 'vscode';
suite('ExtHostDocumentSaveParticipant', () => {

View file

@ -40,6 +40,7 @@ import { asWinJsPromise } from 'vs/base/common/async';
import { MainContext, ExtHostContext } from 'vs/workbench/api/node/extHost.protocol';
import { ExtHostDiagnostics } from 'vs/workbench/api/node/extHostDiagnostics';
import { ExtHostHeapService } from 'vs/workbench/api/node/extHostHeapService';
import * as vscode from 'vscode';
const defaultSelector = { scheme: 'far' };
const model: EditorCommon.IModel = EditorModel.createFromString(