From aeb52ceb8d24f9aed5128b3b4a86bdc8173bb144 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 8 Apr 2016 16:40:12 +0200 Subject: [PATCH] add DiagnosticsCollection#forEach --- .../vscode-api-tests/src/languages.test.ts | 47 +++++++++++++++++++ src/vs/vscode.d.ts | 8 ++++ .../workbench/api/node/extHostDiagnostics.ts | 16 ++++++- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 extensions/vscode-api-tests/src/languages.test.ts diff --git a/extensions/vscode-api-tests/src/languages.test.ts b/extensions/vscode-api-tests/src/languages.test.ts new file mode 100644 index 00000000000..f4199b0ff50 --- /dev/null +++ b/extensions/vscode-api-tests/src/languages.test.ts @@ -0,0 +1,47 @@ +/*--------------------------------------------------------------------------------------------- + * 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 * as assert from 'assert'; +import {languages, Uri, Diagnostic, Range} from 'vscode'; + +suite('languages namespace tests', () => { + + test('diagnostic collection', function () { + let collection = languages.createDiagnosticCollection('test'); + assert.equal(collection.name, 'test'); + collection.dispose(); + assert.throws(() => collection.name); + + let c = 0; + collection = languages.createDiagnosticCollection('test2'); + collection.forEach(() => c++); + assert.equal(c, 0); + + collection.set(Uri.parse('foo:bar'), [ + new Diagnostic(new Range(0, 0, 1, 1), 'message-1'), + new Diagnostic(new Range(0, 0, 1, 1), 'message-2') + ]); + collection.forEach(() => c++); + assert.equal(c, 1); + + c = 0; + collection.clear(); + collection.forEach(() => c++); + assert.equal(c, 0); + + collection.set(Uri.parse('foo:bar1'), [ + new Diagnostic(new Range(0, 0, 1, 1), 'message-1'), + new Diagnostic(new Range(0, 0, 1, 1), 'message-2') + ]); + collection.set(Uri.parse('foo:bar2'), [ + new Diagnostic(new Range(0, 0, 1, 1), 'message-1'), + new Diagnostic(new Range(0, 0, 1, 1), 'message-2') + ]); + collection.forEach(() => c++); + assert.equal(c, 2); + }); +}); \ No newline at end of file diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index 65e90ef29c4..c5d074b8fcc 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -2501,6 +2501,14 @@ declare namespace vscode { */ clear(): void; + /** + * Iterate over each entry in this collection. + * + * @param callback Function to execute for each entry. + * @param thisArg The `this` context used when invoking the handler function. + */ + forEach(callback: (uri: Uri, diagnostics: Diagnostic[], collection: DiagnosticCollection) => any, thisArg?: any): void; + /** * Dispose and free associated resources. Calls * [clear](#DiagnosticCollection.clear). diff --git a/src/vs/workbench/api/node/extHostDiagnostics.ts b/src/vs/workbench/api/node/extHostDiagnostics.ts index 0722a4a857a..1c6dea5eb6c 100644 --- a/src/vs/workbench/api/node/extHostDiagnostics.ts +++ b/src/vs/workbench/api/node/extHostDiagnostics.ts @@ -28,10 +28,10 @@ class DiagnosticCollection implements vscode.DiagnosticCollection { dispose(): void { if (!this._isDisposed) { - this._isDisposed = true; this._proxy.$clear(this.name); this._proxy = undefined; this._data = undefined; + this._isDisposed = true; } } @@ -56,6 +56,13 @@ class DiagnosticCollection implements vscode.DiagnosticCollection { let toSync: vscode.Uri[]; if (first instanceof URI) { + + if (!diagnostics) { + // remove this entry + this.delete(first); + return; + } + // update single row this._data[first.toString()] = diagnostics; toSync = [first]; @@ -103,6 +110,13 @@ class DiagnosticCollection implements vscode.DiagnosticCollection { this._proxy.$clear(this.name); } + forEach(callback: (uri: URI, diagnostics: vscode.Diagnostic[], collection: DiagnosticCollection) => any, thisArg?: any): void { + this._checkDisposed(); + for (let key in this._data) { + callback.apply(thisArg, [URI.parse(key), this._data[key], this]); + } + } + private _checkDisposed() { if (this._isDisposed) { throw new Error('illegal state - object is disposed'); -- GitLab