提交 aeb52ceb 编写于 作者: J Johannes Rieken

add DiagnosticsCollection#forEach

上级 fdc48b3f
/*---------------------------------------------------------------------------------------------
* 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
......@@ -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).
......
......@@ -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');
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册