-
Notifications
You must be signed in to change notification settings - Fork 739
Expand file tree
/
Copy pathtreeUtils.ts
More file actions
129 lines (120 loc) · 5.04 KB
/
treeUtils.ts
File metadata and controls
129 lines (120 loc) · 5.04 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { DirectoryTreeNode } from './directoryTreeNode';
import { FileChangeNode } from './fileChangeNode';
import { TreeNode } from './treeNode';
export namespace TreeUtils {
export function processCheckboxUpdates(checkboxUpdates: vscode.TreeCheckboxChangeEvent<TreeNode>, selection: readonly TreeNode[]) {
const selectionContainsUpdates = selection.some(node => checkboxUpdates.items.some(update => update[0] === node));
const checkedNodes: FileChangeNode[] = [];
const uncheckedNodes: FileChangeNode[] = [];
for (const [node, newState] of checkboxUpdates.items) {
if (node instanceof FileChangeNode) {
if (newState === vscode.TreeItemCheckboxState.Checked) {
checkedNodes.push(node);
} else {
uncheckedNodes.push(node);
}
node.updateFromCheckboxChanged(newState);
} else if (node instanceof DirectoryTreeNode) {
collectAllDescendants(node, newState, checkedNodes, uncheckedNodes);
}
}
if (selectionContainsUpdates) {
for (const selected of selection) {
if (!(selected instanceof FileChangeNode)) {
continue;
}
if (!checkedNodes.includes(selected) && !uncheckedNodes.includes(selected)) {
// Only process files that have checkboxes (files without checkboxState, like those under commits, are skipped)
if (selected.checkboxState?.state === vscode.TreeItemCheckboxState.Unchecked) {
selected.updateFromCheckboxChanged(vscode.TreeItemCheckboxState.Checked);
checkedNodes.push(selected);
} else if (selected.checkboxState?.state === vscode.TreeItemCheckboxState.Checked) {
selected.updateFromCheckboxChanged(vscode.TreeItemCheckboxState.Unchecked);
uncheckedNodes.push(selected);
}
}
}
}
// Eagerly update ancestor directory checkbox states by walking from each
// affected file up through its parent directories. With manageCheckboxStateManually,
// we must set directory states before refresh rather than relying solely on
// getTreeItem(), since VS Code may not apply checkboxState changes during a refresh.
const allAffected = [...checkedNodes, ...uncheckedNodes];
for (const node of allAffected) {
let parent = node.getParent();
while (parent instanceof DirectoryTreeNode) {
parent.updateCheckboxFromChildren();
parent = parent.getParent();
}
}
// Refresh the tree so checkbox visual state updates.
// Refreshing the topmost affected directory will cascade to all descendants.
const refreshedDirs = new Set<DirectoryTreeNode>();
for (const node of allAffected) {
let topDir: DirectoryTreeNode | undefined;
let parent = node.getParent();
while (parent instanceof DirectoryTreeNode) {
topDir = parent;
parent = parent.getParent();
}
if (topDir && !refreshedDirs.has(topDir)) {
refreshedDirs.add(topDir);
topDir.refresh(topDir);
}
}
// If a directory was clicked directly, also refresh it
for (const [node] of checkboxUpdates.items) {
if (node instanceof DirectoryTreeNode && !refreshedDirs.has(node)) {
refreshedDirs.add(node);
node.refresh(node);
}
}
// For flat layout (files have no directory parent), refresh file nodes directly
for (const node of allAffected) {
const parent = node.getParent();
if (!(parent instanceof DirectoryTreeNode)) {
node.refresh(node);
}
}
// Send API requests without firing state change events (UI is already updated optimistically).
// This prevents race conditions where overlapping markFiles calls cause checkboxes to flicker.
if (checkedNodes.length > 0) {
const prModel = checkedNodes[0].pullRequest;
const filenames = checkedNodes.map(n => n.fileName);
prModel.markFiles(filenames, false, 'viewed').then(() => {
checkedNodes[0].refreshFileViewedContext();
});
}
if (uncheckedNodes.length > 0) {
const prModel = uncheckedNodes[0].pullRequest;
const filenames = uncheckedNodes.map(n => n.fileName);
prModel.markFiles(filenames, false, 'unviewed').then(() => {
uncheckedNodes[0].refreshFileViewedContext();
});
}
}
function collectAllDescendants(
dirNode: DirectoryTreeNode,
newState: vscode.TreeItemCheckboxState,
checkedNodes: FileChangeNode[],
uncheckedNodes: FileChangeNode[]
): void {
for (const child of dirNode._children) {
if (child instanceof FileChangeNode) {
if (newState === vscode.TreeItemCheckboxState.Checked) {
checkedNodes.push(child);
} else {
uncheckedNodes.push(child);
}
child.updateFromCheckboxChanged(newState);
} else if (child instanceof DirectoryTreeNode) {
collectAllDescendants(child, newState, checkedNodes, uncheckedNodes);
}
}
}
}