Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: show last message per line, simplify logic #162617

Merged
merged 1 commit into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 31 additions & 22 deletions src/vs/workbench/contrib/testing/browser/testingDecorations.ts
Expand Up @@ -10,6 +10,8 @@ import { equals } from 'vs/base/common/arrays';
import { RunOnceScheduler } from 'vs/base/common/async';
import { Emitter, Event } from 'vs/base/common/event';
import { IMarkdownString, MarkdownString } from 'vs/base/common/htmlContent';
import { stripIcons } from 'vs/base/common/iconLabels';
import { Iterable } from 'vs/base/common/iterator';
import { Disposable, DisposableStore, IReference, MutableDisposable } from 'vs/base/common/lifecycle';
import { ResourceMap } from 'vs/base/common/map';
import { Constants } from 'vs/base/common/uint';
Expand Down Expand Up @@ -39,7 +41,6 @@ import { testingRunAllIcon, testingRunIcon, testingStatesToIcons } from 'vs/work
import { testMessageSeverityColors } from 'vs/workbench/contrib/testing/browser/theme';
import { DefaultGutterClickAction, getTestingConfiguration, TestingConfigKeys } from 'vs/workbench/contrib/testing/common/configuration';
import { labelForTestInState, Testing } from 'vs/workbench/contrib/testing/common/constants';
import { IncrementalTestCollectionItem, InternalTestItem, IRichLocation, ITestMessage, ITestRunProfile, TestDiffOpType, TestMessageType, TestResultItem, TestResultState, TestRunProfileBitset } from 'vs/workbench/contrib/testing/common/testTypes';
import { ITestDecoration as IPublicTestDecoration, ITestingDecorationsService, TestDecorations } from 'vs/workbench/contrib/testing/common/testingDecorations';
import { ITestingPeekOpener } from 'vs/workbench/contrib/testing/common/testingPeekOpener';
import { isFailedState, maxPriority } from 'vs/workbench/contrib/testing/common/testingStates';
Expand All @@ -48,7 +49,7 @@ import { ITestProfileService } from 'vs/workbench/contrib/testing/common/testPro
import { LiveTestResult } from 'vs/workbench/contrib/testing/common/testResult';
import { ITestResultService } from 'vs/workbench/contrib/testing/common/testResultService';
import { getContextForTestItem, ITestService, testsInFile } from 'vs/workbench/contrib/testing/common/testService';
import { stripIcons } from 'vs/base/common/iconLabels';
import { IncrementalTestCollectionItem, InternalTestItem, IRichLocation, ITestMessage, ITestRunProfile, TestDiffOpType, TestMessageType, TestResultItem, TestResultState, TestRunProfileBitset } from 'vs/workbench/contrib/testing/common/testTypes';

const MAX_INLINE_MESSAGE_LENGTH = 128;

Expand Down Expand Up @@ -79,7 +80,8 @@ export class TestingDecorationService extends Disposable implements ITestingDeco
rangeUpdateVersionId?: number;
/** Counter for the results rendered in the document */
generation: number;
value: TestDecorations<ITestDecoration>;
value: readonly ITestDecoration[];
map: ReadonlyMap<string, ITestDecoration>;
}>();

/**
Expand Down Expand Up @@ -150,15 +152,15 @@ export class TestingDecorationService extends Disposable implements ITestingDeco
}

/** @inheritdoc */
public syncDecorations(resource: URI): TestDecorations {
public syncDecorations(resource: URI): ReadonlyMap<string, ITestDecoration> {
const model = this.modelService.getModel(resource);
if (!model) {
return new TestDecorations();
return new Map();
}

const cached = this.decorationCache.get(resource);
if (cached && cached.generation === this.generation && (cached.rangeUpdateVersionId === undefined || cached.rangeUpdateVersionId !== model.getVersionId())) {
return cached.value;
return cached.map;
}

return this.applyDecorations(model);
Expand All @@ -171,7 +173,7 @@ export class TestingDecorationService extends Disposable implements ITestingDeco
return undefined;
}

const decoration = this.syncDecorations(resource).value.find(v => v instanceof RunTestDecoration && v.isForTest(testId));
const decoration = Iterable.find(this.syncDecorations(resource).values(), v => v instanceof RunTestDecoration && v.isForTest(testId));
if (!decoration) {
return undefined;
}
Expand All @@ -192,10 +194,10 @@ export class TestingDecorationService extends Disposable implements ITestingDeco
const uriStr = model.uri.toString();
const cached = this.decorationCache.get(model.uri);
const testRangesUpdated = cached?.rangeUpdateVersionId === model.getVersionId();
const lastDecorations = cached?.value ?? new TestDecorations<ITestDecoration>();
const newDecorations = new TestDecorations<ITestDecoration>();
const lastDecorations = cached?.value ?? [];

model.changeDecorations(accessor => {
const map = model.changeDecorations(accessor => {
const newDecorations: ITestDecoration[] = [];
const runDecorations = new TestDecorations<{ line: number; id: ''; test: IncrementalTestCollectionItem; resultItem: TestResultItem | undefined }>();
for (const test of this.testService.collection.all) {
if (!test.item.range || test.item.uri?.toString() !== uriStr) {
Expand All @@ -209,7 +211,7 @@ export class TestingDecorationService extends Disposable implements ITestingDeco

for (const [line, tests] of runDecorations.lines()) {
const multi = tests.length > 1;
let existing = lastDecorations.value.find(d => d instanceof RunTestDecoration && d.exactlyContainsTests(tests)) as RunTestDecoration | undefined;
let existing = lastDecorations.find(d => d instanceof RunTestDecoration && d.exactlyContainsTests(tests)) as RunTestDecoration | undefined;

// see comment in the constructor for what's going on here
if (existing && testRangesUpdated && model.getDecorationRange(existing.id)?.startLineNumber !== line) {
Expand All @@ -233,14 +235,14 @@ export class TestingDecorationService extends Disposable implements ITestingDeco
for (const task of lastResult.tasks) {
for (const m of task.otherMessages) {
if (!this.invalidatedMessages.has(m) && m.location?.uri.toString() === uriStr) {
const decoration = lastDecorations.findOnLine(m.location.range.startLineNumber, l => l instanceof TestMessageDecoration && l.testMessage === m)
const decoration = lastDecorations.find(l => l instanceof TestMessageDecoration && l.testMessage === m)
|| this.instantiationService.createInstance(TestMessageDecoration, m, undefined, model);
newDecorations.push(decoration);
}
}
}

const messageLines = new Set<number>();
const messageLines = new Map</* line number */ number, /* index in newDecorations */ number>();
for (const test of lastResult.tests) {
for (let taskId = 0; taskId < test.tasks.length; taskId++) {
const state = test.tasks[taskId];
Expand All @@ -253,14 +255,17 @@ export class TestingDecorationService extends Disposable implements ITestingDeco
// Only add one message per line number. Overlapping messages
// don't appear well, and the peek will show all of them (#134129)
const line = m.location.range.startLineNumber;
let index: number;
if (messageLines.has(line)) {
continue;
index = messageLines.get(line)!;
} else {
index = newDecorations.length;
messageLines.set(line, index);
}
messageLines.add(line);

const previous = lastDecorations.findOnLine(line, l => l instanceof TestMessageDecoration && l.testMessage === m);
const previous = lastDecorations.find(l => l instanceof TestMessageDecoration && l.testMessage === m);
if (previous) {
newDecorations.push(previous);
newDecorations[index] = previous;
continue;
}

Expand All @@ -279,28 +284,32 @@ export class TestingDecorationService extends Disposable implements ITestingDeco
}

const saveFromRemoval = new Set<string>();
for (const decoration of newDecorations.value) {
for (const decoration of newDecorations) {
if (decoration.id === '') {
decoration.id = accessor.addDecoration(decoration.editorDecoration.range, decoration.editorDecoration.options);
} else {
saveFromRemoval.add(decoration.id);
}
}

for (const decoration of lastDecorations.value) {
for (const decoration of lastDecorations) {
if (!saveFromRemoval.has(decoration.id)) {
accessor.removeDecoration(decoration.id);
}
}

const map = new Map(newDecorations.map(d => [d.id, d]));
this.decorationCache.set(model.uri, {
generation: this.generation,
rangeUpdateVersionId: cached?.rangeUpdateVersionId,
value: newDecorations,
map,
});

return map;
});

return newDecorations;
return map || new Map();
}
}

Expand Down Expand Up @@ -338,8 +347,8 @@ export class TestingDecorations extends Disposable implements IEditorContributio
if (e.target.position && this.currentUri) {
const modelDecorations = editor.getModel()?.getDecorationsInRange(Range.fromPositions(e.target.position)) ?? [];
for (const { id } of modelDecorations) {
const cache = decorations.syncDecorations(this.currentUri) as TestDecorations<ITestDecoration>;
if (cache.get(id)?.click(e)) {
const cache = decorations.syncDecorations(this.currentUri);
if ((cache.get(id) as ITestDecoration | undefined)?.click(e)) {
e.event.stopPropagation();
return;
}
Expand Down
39 changes: 1 addition & 38 deletions src/vs/workbench/contrib/testing/common/testingDecorations.ts
Expand Up @@ -30,7 +30,7 @@ export interface ITestingDecorationsService {
* Ensures decorations in the given document URI are up to date,
* and returns them.
*/
syncDecorations(resource: URI): TestDecorations;
syncDecorations(resource: URI): ReadonlyMap<string, ITestDecoration>;

/**
* Gets the range where a test ID is displayed, in the given URI.
Expand Down Expand Up @@ -59,49 +59,12 @@ export interface ITestDecoration {

export class TestDecorations<T extends { id: string; line: number } = ITestDecoration> {
public value: T[] = [];

private _idMap?: Map<string, T>;

/**
* Looks up a decoration by ID.
*/
public get(decorationId: string) {
if (this._idMap) {
return this._idMap.get(decorationId);
} else if (this.value.length > 16) {
this._idMap = new Map();
for (const value of this.value) { this._idMap.set(value.id, value); }
return this._idMap.get(decorationId);
} else {
return this.value.find(v => v.id === decorationId);
}
}

/**
* Adds a new value to the decorations.
*/
public push(value: T) {
const searchIndex = binarySearch(this.value, value, (a, b) => a.line - b.line);
this.value.splice(searchIndex < 0 ? ~searchIndex : searchIndex, 0, value);
this._idMap = undefined;
}

/**
* Finds the value that exists on the given line, if any.
*/
public findOnLine(line: number, predicate: (value: T) => boolean): T | undefined {
const lineStart = binarySearch<{ line: number }>(this.value, { line }, (a, b) => a.line - b.line);
if (lineStart < 0) {
return undefined;
}

for (let i = lineStart; i < this.value.length && this.value[i].line === line; i++) {
if (predicate(this.value[i])) {
return this.value[i];
}
}

return undefined;
}

/**
Expand Down