提交 68f98da4 编写于 作者: J Johannes Rieken

outline - markers for every outline group, replace splice with inplace-coalesce

上级 fe27b591
......@@ -284,12 +284,27 @@ function topStep<T>(array: T[], compare: (a: T, b: T) => number, result: T[], i:
/**
* @returns a new array with all undefined or null values removed. The original array is not modified at all.
*/
export function coalesce<T>(array: T[]): T[] {
export function coalesce<T>(array: T[]): T[];
export function coalesce<T>(array: T[], inplace: true): void;
export function coalesce<T>(array: T[], inplace?: true): void | T[] {
if (!array) {
return array;
if (!inplace) {
return array;
}
}
if (!inplace) {
return array.filter(e => !!e);
return array.filter(e => !!e);
} else {
let to = 0;
for (let i = 0; i < array.length; i++) {
if (!!array[i]) {
array[to] = array[i];
to += 1;
}
}
array.length = to;
}
}
/**
......@@ -486,4 +501,4 @@ export function pushToEnd<T>(arr: T[], value: T): void {
arr.splice(index, 1);
arr.push(value);
}
}
\ No newline at end of file
}
......@@ -304,5 +304,43 @@ suite('Arrays', () => {
sparse = arrays.coalesce(sparse);
assert.equal(sparse.length, 5);
});
test('coalesce - inplace', function () {
let a = [null, 1, null, 2, 3];
arrays.coalesce(a, true);
assert.equal(a.length, 3);
assert.equal(a[0], 1);
assert.equal(a[1], 2);
assert.equal(a[2], 3);
a = [null, 1, null, void 0, undefined, 2, 3];
arrays.coalesce(a, true);
assert.equal(a.length, 3);
assert.equal(a[0], 1);
assert.equal(a[1], 2);
assert.equal(a[2], 3);
let b = [];
b[10] = 1;
b[20] = 2;
b[30] = 3;
arrays.coalesce(b, true);
assert.equal(b.length, 3);
assert.equal(b[0], 1);
assert.equal(b[1], 2);
assert.equal(b[2], 3);
let sparse = [];
sparse[0] = 1;
sparse[1] = 1;
sparse[17] = 1;
sparse[1000] = 1;
sparse[1001] = 1;
assert.equal(sparse.length, 1002);
arrays.coalesce(sparse, true);
assert.equal(sparse.length, 5);
});
});
......@@ -12,7 +12,7 @@ import { fuzzyScore, FuzzyScore } from 'vs/base/common/filters';
import { IPosition } from 'vs/editor/common/core/position';
import { Range, IRange } from 'vs/editor/common/core/range';
import { first, size } from 'vs/base/common/collections';
import { isFalsyOrEmpty, binarySearch } from 'vs/base/common/arrays';
import { isFalsyOrEmpty, binarySearch, coalesce } from 'vs/base/common/arrays';
import { commonPrefixLength } from 'vs/base/common/strings';
import { IMarker, MarkerSeverity } from 'vs/platform/markers/common/markers';
import { onUnexpectedExternalError } from 'vs/base/common/errors';
......@@ -164,11 +164,12 @@ export class OutlineGroup extends TreeElement {
let myMarkers: IMarker[] = [];
let myTopSev: MarkerSeverity;
while (start < markers.length && Range.areIntersecting(markers[start], item.symbol.range)) {
for (; start < markers.length && Range.areIntersecting(item.symbol.range, markers[start]); start++) {
// remove markers intersecting with this outline element
// and store them in a 'private' array.
let marker = markers.splice(start, 1)[0];
let marker = markers[start];
myMarkers.push(marker);
markers[start] = undefined;
if (!myTopSev || marker.severity > myTopSev) {
myTopSev = marker.severity;
}
......@@ -188,6 +189,8 @@ export class OutlineGroup extends TreeElement {
topSev: myTopSev
};
}
coalesce(markers, true);
}
}
......@@ -311,10 +314,10 @@ export class OutlineModel extends TreeElement {
readonly id = 'root';
readonly parent = undefined;
private _groups: { [id: string]: OutlineGroup; } = Object.create(null);
protected _groups: { [id: string]: OutlineGroup; } = Object.create(null);
children: { [id: string]: OutlineGroup | OutlineElement; } = Object.create(null);
private constructor(readonly textModel: ITextModel) {
protected constructor(readonly textModel: ITextModel) {
super();
}
......@@ -376,7 +379,7 @@ export class OutlineModel extends TreeElement {
marker.sort(Range.compareRangesUsingStarts);
for (const key in this._groups) {
this._groups[key].updateMarker(marker);
this._groups[key].updateMarker(marker.slice(0));
}
}
}
......@@ -149,4 +149,37 @@ suite('OutlineModel', function () {
assert.equal(c1.marker, undefined);
assert.equal(c2.marker.count, 1);
});
test('OutlineElement - updateMarker/multiple groups', function () {
let model = new class extends OutlineModel {
constructor() {
super(null);
}
readyForTesting() {
this._groups = this.children as any;
}
};
model.children['g1'] = new OutlineGroup('g1', model, null, 1);
model.children['g1'].children['c1'] = new OutlineElement('c1', model.children['g1'], fakeSymbolInformation(new Range(1, 1, 11, 1)));
model.children['g2'] = new OutlineGroup('g2', model, null, 1);
model.children['g2'].children['c2'] = new OutlineElement('c2', model.children['g2'], fakeSymbolInformation(new Range(1, 1, 7, 1)));
model.children['g2'].children['c2'].children['c2.1'] = new OutlineElement('c2.1', model.children['g2'].children['c2'], fakeSymbolInformation(new Range(1, 3, 2, 19)));
model.children['g2'].children['c2'].children['c2.2'] = new OutlineElement('c2.2', model.children['g2'].children['c2'], fakeSymbolInformation(new Range(4, 1, 6, 10)));
model.readyForTesting();
const data = [
fakeMarker(new Range(1, 1, 2, 8)),
fakeMarker(new Range(6, 1, 6, 98)),
];
model.updateMarker(data);
assert.equal(model.children['g1'].children['c1'].marker.count, 2);
assert.equal(model.children['g2'].children['c2'].children['c2.1'].marker.count, 1);
assert.equal(model.children['g2'].children['c2'].children['c2.2'].marker.count, 1);
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册