Skip to content

Commit

Permalink
proposed event for when diagnostics change, #30075
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Mar 13, 2018
1 parent 6b0b81c commit 3aea86d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ declare module 'vscode' {
}

export namespace languages {

/**
*
*/
export const onDidChangeDiagnostics: Event<Uri[]>;

export const diagnostics: DiagnosticInformation;
}

Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/api/node/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ export function createApiFactory(
createDiagnosticCollection(name?: string): vscode.DiagnosticCollection {
return extHostDiagnostics.createDiagnosticCollection(name);
},
get onDidChangeDiagnostics() {
checkProposedApiEnabled(extension);
return extHostDiagnostics.onDidChangeDiagnostics;
},
diagnostics: {
has: proposedApiFunction(extension, uri => extHostDiagnostics.hasDiagnostics(uri)),
get: proposedApiFunction(extension, uri => extHostDiagnostics.getDiagnostics(uri)),
Expand Down
43 changes: 40 additions & 3 deletions src/vs/workbench/api/node/extHostDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,29 @@ import * as vscode from 'vscode';
import { MainContext, MainThreadDiagnosticsShape, ExtHostDiagnosticsShape, IMainContext } from './extHost.protocol';
import { DiagnosticSeverity } from './extHostTypes';
import { mergeSort } from 'vs/base/common/arrays';
import Event, { Emitter, debounceEvent, mapEvent } from 'vs/base/common/event';
import { keys } from 'vs/base/common/map';

export class DiagnosticCollection implements vscode.DiagnosticCollection {

private static readonly _maxDiagnosticsPerFile: number = 250;

private readonly _name: string;
private readonly _onDidChangeDiagnostics: Emitter<(vscode.Uri | string)[]>;

private _proxy: MainThreadDiagnosticsShape;
private _isDisposed = false;
private _data = new Map<string, vscode.Diagnostic[]>();

constructor(name: string, proxy: MainThreadDiagnosticsShape) {
constructor(name: string, proxy: MainThreadDiagnosticsShape, onDidChangeDiagnostics: Emitter<(vscode.Uri | string)[]>) {
this._name = name;
this._proxy = proxy;
this._onDidChangeDiagnostics = onDidChangeDiagnostics;
}

dispose(): void {
if (!this._isDisposed) {
this._onDidChangeDiagnostics.fire(keys(this._data));
this._proxy.$clear(this.name);
this._proxy = undefined;
this._data = undefined;
Expand Down Expand Up @@ -136,6 +141,7 @@ export class DiagnosticCollection implements vscode.DiagnosticCollection {
entries.push([uri, marker]);
}

this._onDidChangeDiagnostics.fire(toSync);
this._proxy.$changeMany(this.name, entries);
}

Expand All @@ -147,6 +153,7 @@ export class DiagnosticCollection implements vscode.DiagnosticCollection {

clear(): void {
this._checkDisposed();
this._onDidChangeDiagnostics.fire(keys(this._data));
this._data.clear();
this._proxy.$clear(this.name);
}
Expand Down Expand Up @@ -222,6 +229,36 @@ export class ExtHostDiagnostics implements ExtHostDiagnosticsShape {

private readonly _proxy: MainThreadDiagnosticsShape;
private readonly _collections: DiagnosticCollection[] = [];
private readonly _onDidChangeDiagnostics = new Emitter<(vscode.Uri | string)[]>();

static _debouncer(last: (vscode.Uri | string)[], current: (vscode.Uri | string)[]): (vscode.Uri | string)[] {
if (!last) {
return current;
} else {
return last.concat(current);
}
}

static _mapper(last: (vscode.Uri | string)[]): vscode.Uri[] {
let res: vscode.Uri[] = [];
let map = new Set<string>();
for (const uri of last) {
if (typeof uri === 'string') {
if (!map.has(uri)) {
map.add(uri);
res.push(URI.parse(uri));
}
} else {
if (!map.has(uri.toString())) {
map.add(uri.toString());
res.push(uri);
}
}
}
return res;
}

readonly onDidChangeDiagnostics: Event<vscode.Uri[]> = mapEvent(debounceEvent(this._onDidChangeDiagnostics.event, ExtHostDiagnostics._debouncer, 50), ExtHostDiagnostics._mapper);

constructor(mainContext: IMainContext) {
this._proxy = mainContext.getProxy(MainContext.MainThreadDiagnostics);
Expand All @@ -232,10 +269,10 @@ export class ExtHostDiagnostics implements ExtHostDiagnosticsShape {
name = '_generated_diagnostic_collection_name_#' + ExtHostDiagnostics._idPool++;
}

const { _collections, _proxy } = this;
const { _collections, _proxy, _onDidChangeDiagnostics } = this;
const result = new class extends DiagnosticCollection {
constructor() {
super(name, _proxy);
super(name, _proxy, _onDidChangeDiagnostics);
_collections.push(this);
}
dispose() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Diagnostic, DiagnosticSeverity, Range } from 'vs/workbench/api/node/ext
import { MainThreadDiagnosticsShape } from 'vs/workbench/api/node/extHost.protocol';
import { IMarkerData } from 'vs/platform/markers/common/markers';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { Emitter } from 'vs/base/common/event';

suite('ExtHostDiagnostics', () => {

Expand All @@ -27,7 +28,7 @@ suite('ExtHostDiagnostics', () => {

test('disposeCheck', function () {

const collection = new DiagnosticCollection('test', new DiagnosticsShape());
const collection = new DiagnosticCollection('test', new DiagnosticsShape(), new Emitter());

collection.dispose();
collection.dispose(); // that's OK
Expand All @@ -44,13 +45,13 @@ suite('ExtHostDiagnostics', () => {


test('diagnostic collection, forEach, clear, has', function () {
let collection = new DiagnosticCollection('test', new DiagnosticsShape());
let collection = new DiagnosticCollection('test', new DiagnosticsShape(), new Emitter());
assert.equal(collection.name, 'test');
collection.dispose();
assert.throws(() => collection.name);

let c = 0;
collection = new DiagnosticCollection('test', new DiagnosticsShape());
collection = new DiagnosticCollection('test', new DiagnosticsShape(), new Emitter());
collection.forEach(() => c++);
assert.equal(c, 0);

Expand Down Expand Up @@ -87,7 +88,7 @@ suite('ExtHostDiagnostics', () => {
});

test('diagnostic collection, immutable read', function () {
let collection = new DiagnosticCollection('test', new DiagnosticsShape());
let collection = new DiagnosticCollection('test', new DiagnosticsShape(), new Emitter());
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')
Expand All @@ -112,7 +113,7 @@ suite('ExtHostDiagnostics', () => {


test('diagnostics collection, set with dupliclated tuples', function () {
let collection = new DiagnosticCollection('test', new DiagnosticsShape());
let collection = new DiagnosticCollection('test', new DiagnosticsShape(), new Emitter());
let uri = URI.parse('sc:hightower');
collection.set([
[uri, [new Diagnostic(new Range(0, 0, 0, 1), 'message-1')]],
Expand Down Expand Up @@ -168,7 +169,7 @@ suite('ExtHostDiagnostics', () => {
lastEntries = entries;
return super.$changeMany(owner, entries);
}
});
}, new Emitter());
let uri = URI.parse('sc:hightower');

collection.set([[uri, [new Diagnostic(new Range(0, 0, 1, 1), 'error')]]]);
Expand All @@ -192,7 +193,7 @@ suite('ExtHostDiagnostics', () => {

test('diagnostics collection, tuples and undefined (small array), #15585', function () {

const collection = new DiagnosticCollection('test', new DiagnosticsShape());
const collection = new DiagnosticCollection('test', new DiagnosticsShape(), new Emitter());
let uri = URI.parse('sc:hightower');
let uri2 = URI.parse('sc:nomad');
let diag = new Diagnostic(new Range(0, 0, 0, 1), 'ffff');
Expand All @@ -213,7 +214,7 @@ suite('ExtHostDiagnostics', () => {

test('diagnostics collection, tuples and undefined (large array), #15585', function () {

const collection = new DiagnosticCollection('test', new DiagnosticsShape());
const collection = new DiagnosticCollection('test', new DiagnosticsShape(), new Emitter());
const tuples: [URI, Diagnostic[]][] = [];

for (let i = 0; i < 500; i++) {
Expand Down Expand Up @@ -242,7 +243,7 @@ suite('ExtHostDiagnostics', () => {
lastEntries = entries;
return super.$changeMany(owner, entries);
}
});
}, new Emitter());
let uri = URI.parse('aa:bb');

let diagnostics: Diagnostic[] = [];
Expand Down

0 comments on commit 3aea86d

Please sign in to comment.