debt - use Map instead of object

This commit is contained in:
Johannes Rieken
2016-12-30 15:37:12 +01:00
parent 8385b8c8bc
commit 118b3e0d5a
9 changed files with 110 additions and 117 deletions

View File

@@ -15,13 +15,13 @@ import { DiagnosticSeverity } from './extHostTypes';
export class DiagnosticCollection implements vscode.DiagnosticCollection {
private static _maxDiagnosticsPerFile: number = 250;
private static readonly _maxDiagnosticsPerFile: number = 250;
private readonly _name: string;
private _name: string;
private _proxy: MainThreadDiagnosticsShape;
private _isDisposed = false;
private _data: { [uri: string]: vscode.Diagnostic[] } = Object.create(null);
private _data = new Map<string, vscode.Diagnostic[]>();
constructor(name: string, proxy: MainThreadDiagnosticsShape) {
this._name = name;
@@ -66,7 +66,7 @@ export class DiagnosticCollection implements vscode.DiagnosticCollection {
}
// update single row
this._data[first.toString()] = diagnostics;
this._data.set(first.toString(), diagnostics);
toSync = [first];
} else if (Array.isArray(first)) {
@@ -83,19 +83,19 @@ export class DiagnosticCollection implements vscode.DiagnosticCollection {
for (const {tuple} of sortedTuples) {
const [uri, diagnostics] = tuple;
if (!lastUri || uri.toString() !== lastUri.toString()) {
if (lastUri && this._data[lastUri.toString()].length === 0) {
delete this._data[lastUri.toString()];
if (lastUri && this._data.get(lastUri.toString()).length === 0) {
this._data.delete(lastUri.toString());
}
lastUri = uri;
toSync.push(uri);
this._data[uri.toString()] = [];
this._data.set(uri.toString(), []);
}
if (!diagnostics) {
// [Uri, undefined] means clear this
this._data[uri.toString()].length = 0;
this._data.get(uri.toString()).length = 0;
} else {
this._data[uri.toString()].push(...diagnostics);
this._data.get(uri.toString()).push(...diagnostics);
}
}
}
@@ -104,7 +104,7 @@ export class DiagnosticCollection implements vscode.DiagnosticCollection {
const entries: [URI, IMarkerData[]][] = [];
for (let uri of toSync) {
let marker: IMarkerData[];
let diagnostics = this._data[uri.toString()];
let diagnostics = this._data.get(uri.toString());
if (diagnostics) {
// no more than 250 diagnostics per file
@@ -144,27 +144,27 @@ export class DiagnosticCollection implements vscode.DiagnosticCollection {
delete(uri: vscode.Uri): void {
this._checkDisposed();
delete this._data[uri.toString()];
this._data.delete(uri.toString());
this._proxy.$changeMany(this.name, [[<URI>uri, undefined]]);
}
clear(): void {
this._checkDisposed();
this._data = Object.create(null);
this._data.clear();
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) {
this._data.forEach((value, key) => {
let uri = URI.parse(key);
callback.apply(thisArg, [uri, this.get(uri), this]);
}
});
}
get(uri: URI): vscode.Diagnostic[] {
this._checkDisposed();
let result = this._data[uri.toString()];
let result = this._data.get(uri.toString());
if (Array.isArray(result)) {
return <vscode.Diagnostic[]>Object.freeze(result.slice(0));
}
@@ -172,7 +172,7 @@ export class DiagnosticCollection implements vscode.DiagnosticCollection {
has(uri: URI): boolean {
this._checkDisposed();
return Array.isArray(this._data[uri.toString()]);
return Array.isArray(this._data.get(uri.toString()));
}
private _checkDisposed() {