Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Symbol.iterator functions for vscode.d.ts collection types #151806

Merged
merged 9 commits into from Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/tsconfig.vscode-dts.json
Expand Up @@ -14,6 +14,7 @@
"types": [],
"lib": [
"es5",
"ES2015.Iterable"
],
},
"include": [
Expand Down
9 changes: 8 additions & 1 deletion src/vs/workbench/api/common/extHostDiagnostics.ts
Expand Up @@ -181,9 +181,16 @@ export class DiagnosticCollection implements vscode.DiagnosticCollection {
}

forEach(callback: (uri: URI, diagnostics: ReadonlyArray<vscode.Diagnostic>, collection: DiagnosticCollection) => any, thisArg?: any): void {
this._checkDisposed();
for (const [uri, values] of this) {
callback.call(thisArg, uri, values, this);
}
}

*[Symbol.iterator](): IterableIterator<[uri: vscode.Uri, diagnostics: readonly vscode.Diagnostic[]]> {
this._checkDisposed();
for (const uri of this.#data.keys()) {
callback.apply(thisArg, [uri, this.get(uri), this]);
yield [uri, this.get(uri)];
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/api/common/extHostTerminalService.ts
Expand Up @@ -880,6 +880,10 @@ export class EnvironmentVariableCollection implements vscode.EnvironmentVariable
this.map.forEach((value, key) => callback.call(thisArg, key, value, this));
}

[Symbol.iterator](): IterableIterator<[variable: string, mutator: vscode.EnvironmentVariableMutator]> {
return this.map.entries();
}

delete(variable: string): void {
this.map.delete(variable);
this._onDidChangeCollection.fire();
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHostTypeConverters.ts
Expand Up @@ -1777,6 +1777,7 @@ export namespace TestItem {
add: () => { },
delete: () => { },
forEach: () => { },
*[Symbol.iterator]() { },
get: () => undefined,
replace: () => { },
size: 0,
Expand Down
10 changes: 9 additions & 1 deletion src/vs/workbench/api/common/extHostTypes.ts
Expand Up @@ -2443,7 +2443,7 @@ export class DataTransferItem {
}

@es5ClassCompat
export class DataTransfer {
export class DataTransfer implements vscode.DataTransfer {
#items = new Map<string, DataTransferItem[]>();

constructor(init?: Iterable<readonly [string, DataTransferItem]>) {
Expand Down Expand Up @@ -2474,6 +2474,14 @@ export class DataTransfer {
}
}
}

*[Symbol.iterator](): IterableIterator<[mimeType: string, item: vscode.DataTransferItem]> {
for (const [mime, items] of this.#items) {
for (const item of items) {
yield [mime, item];
}
}
}
}

@es5ClassCompat
Expand Down
8 changes: 4 additions & 4 deletions src/vs/workbench/api/test/browser/extHostTesting.test.ts
Expand Up @@ -36,8 +36,8 @@ const assertTreesEqual = (a: TestItemImpl | undefined, b: TestItemImpl | undefin

assert.deepStrictEqual(simplify(a), simplify(b));

const aChildren = [...a.children].map(c => c.id).sort();
const bChildren = [...b.children].map(c => c.id).sort();
const aChildren = [...a.children].map(([_, c]) => c.id).sort();
const bChildren = [...b.children].map(([_, c]) => c.id).sort();
assert.strictEqual(aChildren.length, bChildren.length, `expected ${a.label}.children.length == ${b.label}.children.length`);
aChildren.forEach(key => assertTreesEqual(a.children.get(key) as TestItemImpl, b.children.get(key) as TestItemImpl));
};
Expand Down Expand Up @@ -242,7 +242,7 @@ suite('ExtHost Testing', () => {

const oldA = single.root.children.get('id-a') as TestItemImpl;
const newA = new TestItemImpl('ctrlId', 'id-a', 'Hello world', undefined);
newA.children.replace([...oldA.children]);
newA.children.replace([...oldA.children].map(([_, item]) => item));
single.root.children.replace([
newA,
new TestItemImpl('ctrlId', 'id-b', single.root.children.get('id-b')!.label, undefined),
Expand Down Expand Up @@ -334,7 +334,7 @@ suite('ExtHost Testing', () => {
},
]);

assert.deepStrictEqual([...single.root.children], [single.root.children.get('id-a')]);
assert.deepStrictEqual([...single.root.children].map(([_, item]) => item), [single.root.children.get('id-a')]);
assert.deepStrictEqual(b.parent, a);
});
});
Expand Down
24 changes: 12 additions & 12 deletions src/vs/workbench/contrib/testing/common/testItemCollection.ts
Expand Up @@ -134,7 +134,7 @@ const diffTestItems = (a: ITestItem, b: ITestItem) => {
return output as Partial<ITestItem> | undefined;
};

export interface ITestChildrenLike<T> extends Iterable<T> {
export interface ITestChildrenLike<T> extends Iterable<[string, T]> {
get(id: string): T | undefined;
delete(id: string): void;
}
Expand Down Expand Up @@ -356,7 +356,7 @@ export class TestItemCollection<T extends ITestItemLike> extends Disposable {
this.connectItemAndChildren(actual, internal, parent);

// Remove any orphaned children.
for (const child of oldChildren) {
for (const [_, child] of oldChildren) {
if (!this.options.getChildren(actual).get(child.id)) {
this.removeItem(TestId.joinToString(fullId, child.id));
}
Expand Down Expand Up @@ -417,7 +417,7 @@ export class TestItemCollection<T extends ITestItemLike> extends Disposable {
this.connectItem(actual, internal, parent);

// Discover any existing children that might have already been added
for (const child of this.options.getChildren(actual)) {
for (const [_, child] of this.options.getChildren(actual)) {
this.upsertItem(child, internal);
}
}
Expand Down Expand Up @@ -464,7 +464,7 @@ export class TestItemCollection<T extends ITestItemLike> extends Disposable {
}

const expandRequests: Promise<void>[] = [];
for (const child of this.options.getChildren(internal.actual)) {
for (const [_, child] of this.options.getChildren(internal.actual)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could be written as

Suggested change
for (const [_, child] of this.options.getChildren(internal.actual)) {
for (const [, child] of this.options.getChildren(internal.actual)) {

But not a big deal

const promise = this.expand(TestId.joinToString(internal.fullId, child.id), levels);
if (isThenable(promise)) {
expandRequests.push(promise);
Expand Down Expand Up @@ -544,7 +544,7 @@ export class TestItemCollection<T extends ITestItemLike> extends Disposable {
}

this.tree.delete(item.fullId.toString());
for (const child of this.options.getChildren(item.actual)) {
for (const [_, child] of this.options.getChildren(item.actual)) {
queue.push(this.tree.get(TestId.joinToString(item.fullId, child.id)));
}
}
Expand All @@ -561,8 +561,8 @@ export class TestItemCollection<T extends ITestItemLike> extends Disposable {
}
}

/** Implementation os vscode.TestItemCollection */
export interface ITestItemChildren<T extends ITestItemLike> extends Iterable<T> {
/** Implementation of vscode.TestItemCollection */
export interface ITestItemChildren<T extends ITestItemLike> extends Iterable<[string, T]> {
readonly size: number;
replace(items: readonly T[]): void;
forEach(callback: (item: T, collection: this) => unknown, thisArg?: unknown): void;
Expand Down Expand Up @@ -607,6 +607,11 @@ export const createTestItemChildren = <T extends ITestItemLike>(api: ITestItemAp
}
},

/** @inheritdoc */
[Symbol.iterator](): IterableIterator<[string, T]> {
return mapped.entries();
},

/** @inheritdoc */
replace(items: Iterable<T>) {
const newMapped = new Map<string, T>();
Expand Down Expand Up @@ -670,10 +675,5 @@ export const createTestItemChildren = <T extends ITestItemLike>(api: ITestItemAp
toJSON() {
return Array.from(mapped.values());
},

/** @inheritdoc */
[Symbol.iterator]() {
return mapped.values();
},
};
};
13 changes: 9 additions & 4 deletions src/vscode-dts/vscode.d.ts
Expand Up @@ -5980,7 +5980,7 @@ declare module 'vscode' {
* To get an instance of a `DiagnosticCollection` use
* {@link languages.createDiagnosticCollection createDiagnosticCollection}.
*/
export interface DiagnosticCollection {
export interface DiagnosticCollection extends Iterable<[uri: Uri, diagnostics: readonly Diagnostic[]]> {

/**
* The name of this diagnostic collection, for instance `typescript`. Every diagnostic
Expand Down Expand Up @@ -10120,7 +10120,7 @@ declare module 'vscode' {
* data transfer. These additional mime types will only be included in the `handleDrop` when the the drag was initiated from
* an element in the same drag and drop controller.
*/
export class DataTransfer {
export class DataTransfer implements Iterable<[mimeType: string, item: DataTransferItem]> {
/**
* Retrieves the data transfer item for a given mime type.
*
Expand All @@ -10146,6 +10146,11 @@ declare module 'vscode' {
* @param thisArg The `this` context used when invoking the handler function.
*/
forEach(callbackfn: (value: DataTransferItem, key: string, dataTransfer: DataTransfer) => void, thisArg?: any): void;

/**
* Get a new iterator with the `[mime, item]` pairs for each element in this data transfer.
*/
[Symbol.iterator](): IterableIterator<[mimeType: string, item: DataTransferItem]>;
}

/**
Expand Down Expand Up @@ -10812,7 +10817,7 @@ declare module 'vscode' {
/**
* A collection of mutations that an extension can apply to a process environment.
*/
export interface EnvironmentVariableCollection {
export interface EnvironmentVariableCollection extends Iterable<[variable: string, mutator: EnvironmentVariableMutator]> {
/**
* Whether the collection should be cached for the workspace and applied to the terminal
* across window reloads. When true the collection will be active immediately such when the
Expand Down Expand Up @@ -15681,7 +15686,7 @@ declare module 'vscode' {
* Collection of test items, found in {@link TestItem.children} and
* {@link TestController.items}.
*/
export interface TestItemCollection {
export interface TestItemCollection extends Iterable<[id: string, testItem: TestItem]> {
/**
* Gets the number of items in the collection.
*/
Expand Down