/
hierarchalByLocation.ts
283 lines (241 loc) · 9.74 KB
/
hierarchalByLocation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AbstractTreeViewState } from 'vs/base/browser/ui/tree/abstractTree';
import { ObjectTree } from 'vs/base/browser/ui/tree/objectTree';
import { Emitter } from 'vs/base/common/event';
import { FuzzyScore } from 'vs/base/common/filters';
import { Iterable } from 'vs/base/common/iterator';
import { Disposable } from 'vs/base/common/lifecycle';
import { isDefined } from 'vs/base/common/types';
import { ByLocationTestItemElement } from 'vs/workbench/contrib/testing/browser/explorerProjections/hierarchalNodes';
import { IActionableTestTreeElement, ITestTreeProjection, TestExplorerTreeElement, TestItemTreeElement, TestTreeErrorMessage } from 'vs/workbench/contrib/testing/browser/explorerProjections/index';
import { NodeChangeList, NodeRenderDirective, NodeRenderFn, peersHaveChildren } from 'vs/workbench/contrib/testing/browser/explorerProjections/nodeHelper';
import { IComputedStateAndDurationAccessor, refreshComputedState } from 'vs/workbench/contrib/testing/common/getComputedState';
import { InternalTestItem, TestDiffOpType, TestItemExpandState, TestResultState, TestsDiff } from 'vs/workbench/contrib/testing/common/testTypes';
import { TestResultItemChangeReason } from 'vs/workbench/contrib/testing/common/testResult';
import { ITestResultService } from 'vs/workbench/contrib/testing/common/testResultService';
import { ITestService } from 'vs/workbench/contrib/testing/common/testService';
import { TestId } from 'vs/workbench/contrib/testing/common/testId';
const computedStateAccessor: IComputedStateAndDurationAccessor<IActionableTestTreeElement> = {
getOwnState: i => i instanceof TestItemTreeElement ? i.ownState : TestResultState.Unset,
getCurrentComputedState: i => i.state,
setComputedState: (i, s) => i.state = s,
getCurrentComputedDuration: i => i.duration,
getOwnDuration: i => i instanceof TestItemTreeElement ? i.ownDuration : undefined,
setComputedDuration: (i, d) => i.duration = d,
getChildren: i => Iterable.filter(
i.children.values(),
(t): t is TestItemTreeElement => t instanceof TestItemTreeElement,
),
*getParents(i) {
for (let parent = i.parent; parent; parent = parent.parent) {
yield parent;
}
},
};
/**
* Projection that lists tests in their traditional tree view.
*/
export class HierarchicalByLocationProjection extends Disposable implements ITestTreeProjection {
private readonly updateEmitter = new Emitter<void>();
protected readonly changes = new NodeChangeList<ByLocationTestItemElement>();
protected readonly items = new Map<string, ByLocationTestItemElement>();
/**
* Gets root elements of the tree.
*/
protected get roots(): Iterable<ByLocationTestItemElement> {
const rootsIt = Iterable.map(this.testService.collection.rootItems, r => this.items.get(r.item.extId));
return Iterable.filter(rootsIt, isDefined);
}
/**
* @inheritdoc
*/
public readonly onUpdate = this.updateEmitter.event;
constructor(
private readonly lastState: AbstractTreeViewState,
@ITestService private readonly testService: ITestService,
@ITestResultService private readonly results: ITestResultService,
) {
super();
this._register(testService.onDidProcessDiff((diff) => this.applyDiff(diff)));
// when test results are cleared, recalculate all state
this._register(results.onResultsChanged((evt) => {
if (!('removed' in evt)) {
return;
}
for (const inTree of [...this.items.values()].sort((a, b) => b.depth - a.depth)) {
const lookup = this.results.getStateById(inTree.test.item.extId)?.[1];
inTree.ownDuration = lookup?.ownDuration;
refreshComputedState(computedStateAccessor, inTree, lookup?.ownComputedState ?? TestResultState.Unset).forEach(this.addUpdated);
}
this.updateEmitter.fire();
}));
// when test states change, reflect in the tree
this._register(results.onTestChanged(ev => {
let result = ev.item;
if (result.ownComputedState === TestResultState.Unset) {
const fallback = results.getStateById(result.item.extId);
if (fallback) {
result = fallback[1];
}
}
const item = this.items.get(result.item.extId);
if (!item) {
return;
}
// Skip refreshing the duration if we can trivially tell it didn't change.
const refreshDuration = ev.reason === TestResultItemChangeReason.OwnStateChange && ev.previousOwnDuration !== result.ownDuration;
// For items without children, always use the computed state. They are
// either leaves (for which it's fine) or nodes where we haven't expanded
// children and should trust whatever the result service gives us.
const explicitComputed = item.children.size ? undefined : result.computedState;
item.retired = !!result.retired;
item.ownState = result.ownComputedState;
item.ownDuration = result.ownDuration;
refreshComputedState(computedStateAccessor, item, explicitComputed, refreshDuration).forEach(this.addUpdated);
this.addUpdated(item);
this.updateEmitter.fire();
}));
for (const test of testService.collection.all) {
this.storeItem(this.createItem(test));
}
}
/**
* Gets the depth of children to expanded automatically for the node,
*/
protected getRevealDepth(element: ByLocationTestItemElement): number | undefined {
return element.depth === 0 ? 0 : undefined;
}
/**
* @inheritdoc
*/
public getElementByTestId(testId: string): TestItemTreeElement | undefined {
return this.items.get(testId);
}
/**
* @inheritdoc
*/
private applyDiff(diff: TestsDiff) {
for (const op of diff) {
switch (op.op) {
case TestDiffOpType.Add: {
const item = this.createItem(op.item);
this.storeItem(item);
break;
}
case TestDiffOpType.Update: {
const patch = op.item;
const existing = this.items.get(patch.extId);
if (!existing) {
break;
}
// parent needs to be re-rendered on an expand update, so that its
// children are rewritten.
const needsParentUpdate = existing.test.expand === TestItemExpandState.NotExpandable && patch.expand;
existing.update(patch);
if (needsParentUpdate) {
this.changes.addedOrRemoved(existing);
} else {
this.changes.updated(existing);
}
break;
}
case TestDiffOpType.Remove: {
const toRemove = this.items.get(op.itemId);
if (!toRemove) {
break;
}
this.changes.addedOrRemoved(toRemove);
const queue: Iterable<TestExplorerTreeElement>[] = [[toRemove]];
while (queue.length) {
for (const item of queue.pop()!) {
if (item instanceof ByLocationTestItemElement) {
queue.push(this.unstoreItem(this.items, item));
}
}
}
}
}
}
if (diff.length !== 0) {
this.updateEmitter.fire();
}
}
/**
* @inheritdoc
*/
public applyTo(tree: ObjectTree<TestExplorerTreeElement, FuzzyScore>) {
this.changes.applyTo(tree, this.renderNode, () => this.roots);
}
/**
* @inheritdoc
*/
public expandElement(element: TestItemTreeElement, depth: number): void {
if (!(element instanceof ByLocationTestItemElement)) {
return;
}
if (element.test.expand === TestItemExpandState.NotExpandable) {
return;
}
this.testService.collection.expand(element.test.item.extId, depth);
}
protected createItem(item: InternalTestItem): ByLocationTestItemElement {
const parentId = TestId.parentId(item.item.extId);
const parent = parentId ? this.items.get(parentId)! : null;
return new ByLocationTestItemElement(item, parent, n => this.changes.addedOrRemoved(n));
}
protected readonly addUpdated = (item: IActionableTestTreeElement) => {
const cast = item as ByLocationTestItemElement;
this.changes.updated(cast);
};
protected renderNode: NodeRenderFn = (node, recurse) => {
if (node instanceof TestTreeErrorMessage) {
return { element: node };
}
if (node.depth === 0) {
// Omit the test controller root if there are no siblings
if (!peersHaveChildren(node, () => this.roots)) {
return NodeRenderDirective.Concat;
}
// Omit roots that have no child tests
if (node.children.size === 0) {
return NodeRenderDirective.Omit;
}
}
return {
element: node,
collapsible: node.test.expand !== TestItemExpandState.NotExpandable,
collapsed: this.lastState.expanded[node.test.item.extId] !== undefined
? !this.lastState.expanded[node.test.item.extId]
: node.depth > 0,
children: recurse(node.children),
};
};
protected unstoreItem(items: Map<string, TestItemTreeElement>, treeElement: ByLocationTestItemElement) {
const parent = treeElement.parent;
parent?.children.delete(treeElement);
items.delete(treeElement.test.item.extId);
if (parent instanceof ByLocationTestItemElement) {
refreshComputedState(computedStateAccessor, parent, undefined, !!treeElement.duration).forEach(this.addUpdated);
}
return treeElement.children;
}
protected storeItem(treeElement: ByLocationTestItemElement) {
treeElement.parent?.children.add(treeElement);
this.items.set(treeElement.test.item.extId, treeElement);
this.changes.addedOrRemoved(treeElement);
const reveal = this.getRevealDepth(treeElement);
if (reveal !== undefined || this.lastState.expanded[treeElement.test.item.extId]) {
this.expandElement(treeElement, reveal || 0);
}
const prevState = this.results.getStateById(treeElement.test.item.extId)?.[1];
if (prevState) {
treeElement.retired = !!prevState.retired;
treeElement.ownState = prevState.computedState;
treeElement.ownDuration = prevState.ownDuration;
refreshComputedState(computedStateAccessor, treeElement, undefined, !!treeElement.ownDuration).forEach(this.addUpdated);
}
}
}