Skip to content

Commit 9bef9bf

Browse files
committed
Rewrite the indent guide highlighting logic
1 parent cb06ff1 commit 9bef9bf

File tree

3 files changed

+38
-43
lines changed

3 files changed

+38
-43
lines changed

src/vscode-tree-item/vscode-tree-item.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ export class VscodeTreeItem extends VscElement {
120120
hasBranchItem: false,
121121
rootElement: null,
122122
activeItem: null,
123-
highlightedItems: [],
124123
};
125124

126125
@consume({context: configContext, subscribe: true})
@@ -335,9 +334,16 @@ export class VscodeTreeItem extends VscElement {
335334
if (isShiftDown && this._configContext.multiSelect) {
336335
this._selectRange();
337336
this._treeContextState.emitSelectEvent?.();
337+
338+
this.updateComplete.then(() => {
339+
this._treeContextState.highlightIndentGuides?.();
340+
});
338341
} else {
339342
this._selectItem(isCtrlDown);
340343
this._treeContextState.emitSelectEvent?.();
344+
this.updateComplete.then(() => {
345+
this._treeContextState.highlightIndentGuides?.();
346+
});
341347

342348
if (this._configContext.expandMode === ExpandMode.singleClick) {
343349
if (this.branch && !(this._configContext.multiSelect && isCtrlDown)) {
@@ -351,10 +357,6 @@ export class VscodeTreeItem extends VscElement {
351357
if (!isShiftDown) {
352358
this._treeContextState.prevFocusedItem = this;
353359
}
354-
355-
this.updateComplete.then(() => {
356-
this._treeContextState.highlightGuides?.();
357-
});
358360
}
359361

360362
private _handleDoubleClick(ev: MouseEvent) {

src/vscode-tree/tree-context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export interface TreeContext {
1515
hasBranchItem: boolean;
1616
rootElement: VscodeTree | null;
1717
activeItem: VscodeTreeItem | null;
18-
highlightedItems: VscodeTreeItem[];
19-
highlightGuides?: () => void;
18+
highlightedItems?: Set<VscodeTreeItem>;
19+
highlightIndentGuides?: () => void;
2020
emitSelectEvent?: () => void;
2121
}
2222

src/vscode-tree/vscode-tree.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ export class VscodeTree extends VscElement {
160160
prevFocusedItem: null,
161161
hasBranchItem: false,
162162
rootElement: this,
163-
highlightedItems: [],
164-
highlightGuides: () => {
165-
this._highlightGuides();
163+
highlightedItems: new Set(),
164+
highlightIndentGuides: () => {
165+
this._highlightIndentGuides();
166166
},
167167
emitSelectEvent: () => {
168168
this._emitSelectEvent();
@@ -258,43 +258,33 @@ export class VscodeTree extends VscElement {
258258
this.dispatchEvent(ev);
259259
}
260260

261-
private _highlightGuides() {
262-
const {activeItem, highlightedItems, selectedItems} =
263-
this._treeContextState;
264-
265-
highlightedItems.forEach((i) => (i.highlightedGuides = false));
266-
267-
if (activeItem) {
268-
this._treeContextState.highlightedItems = [];
269-
270-
if (activeItem.branch && activeItem.open) {
271-
activeItem.highlightedGuides = true;
272-
this._treeContextState.highlightedItems.push(activeItem);
273-
} else {
274-
const parent = findParentItem(activeItem);
261+
private _highlightIndentGuideOfItem(item: VscodeTreeItem) {
262+
if (item.branch && item.open) {
263+
item.highlightedGuides = true;
264+
this._treeContextState.highlightedItems?.add(item);
265+
} else {
266+
const parent = findParentItem(item);
275267

276-
if (parent && parent.branch) {
277-
parent.highlightedGuides = true;
278-
this._treeContextState.highlightedItems.push(parent);
279-
}
268+
if (parent) {
269+
parent.highlightedGuides = true;
270+
this._treeContextState.highlightedItems?.add(parent);
280271
}
281272
}
273+
}
282274

283-
if (selectedItems) {
284-
selectedItems.forEach((item) => {
285-
if (item.branch && item.open) {
286-
item.highlightedGuides = true;
287-
this._treeContextState.highlightedItems.push(item);
288-
} else {
289-
const parent = findParentItem(item);
290-
291-
if (parent && parent.branch) {
292-
parent.highlightedGuides = true;
293-
this._treeContextState.highlightedItems.push(item);
294-
}
295-
}
296-
});
275+
private _highlightIndentGuides() {
276+
this._treeContextState.highlightedItems?.forEach(
277+
(i) => (i.highlightedGuides = false)
278+
);
279+
this._treeContextState.highlightedItems?.clear();
280+
281+
if (this._treeContextState.activeItem) {
282+
this._highlightIndentGuideOfItem(this._treeContextState.activeItem);
297283
}
284+
285+
this._treeContextState.selectedItems.forEach((item) => {
286+
this._highlightIndentGuideOfItem(item);
287+
});
298288
}
299289

300290
private _updateConfigContext(changedProperties: PropertyValues) {
@@ -326,6 +316,7 @@ export class VscodeTree extends VscElement {
326316

327317
item.updateComplete.then(() => {
328318
item.focus();
319+
this._highlightIndentGuides();
329320
});
330321
}
331322

@@ -430,6 +421,8 @@ export class VscodeTree extends VscElement {
430421
this._treeContextState.selectedItems.forEach(
431422
(li) => (li.selected = false)
432423
);
424+
this._treeContextState.selectedItems.clear();
425+
this._highlightIndentGuides();
433426

434427
focusedItem.selected = true;
435428
this._emitSelectEvent();
@@ -497,7 +490,7 @@ export class VscodeTree extends VscElement {
497490
}
498491
}
499492

500-
this._highlightGuides();
493+
// this._highlightGuides();
501494
});
502495
};
503496

0 commit comments

Comments
 (0)