Skip to content

Commit 5525314

Browse files
committed
Adds last selected memory for grouped views
1 parent 9d5d591 commit 5525314

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/views/viewBase.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ export abstract class ViewBase<
245245
if (this.container.debugging || configuration.get('debug')) {
246246
function addDebuggingInfo(item: TreeItem, node: ViewNode, parent: ViewNode | undefined) {
247247
item.tooltip ??= new MarkdownString(
248-
item.label != null && typeof item.label !== 'string' ? item.label.label : item.label ?? '',
249-
);
248+
item.label != null && typeof item.label !== 'string' ? item.label.label : item.label ?? '',
249+
);
250250

251251
if (typeof item.tooltip === 'string') {
252252
item.tooltip = `${item.tooltip}\n\n---\ncontext: ${
@@ -515,10 +515,12 @@ export abstract class ViewBase<
515515
}
516516

517517
protected onElementCollapsed(e: TreeViewExpansionEvent<ViewNode>): void {
518+
this._expandedNodes.delete(e.element);
518519
this._onDidChangeNodeCollapsibleState.fire({ ...e, state: TreeItemCollapsibleState.Collapsed });
519520
}
520521

521522
protected onElementExpanded(e: TreeViewExpansionEvent<ViewNode>): void {
523+
this._expandedNodes.add(e.element);
522524
this._onDidChangeNodeCollapsibleState.fire({ ...e, state: TreeItemCollapsibleState.Expanded });
523525
}
524526

@@ -732,6 +734,11 @@ export abstract class ViewBase<
732734
return undefined;
733735
}
734736

737+
private _expandedNodes = new WeakSet<ViewNode>();
738+
isNodeExpanded(node: ViewNode): boolean {
739+
return this._expandedNodes.has(node);
740+
}
741+
735742
@debug()
736743
async refresh(reset: boolean = false): Promise<void> {
737744
// If we are resetting, make sure to clear any saved node state
@@ -823,8 +830,8 @@ export abstract class ViewBase<
823830
await this.tree?.reveal(node, options);
824831
} catch (ex) {
825832
if (!node.id || root == null) {
826-
Logger.error(ex, scope);
827-
debugger;
833+
Logger.error(ex, scope);
834+
debugger;
828835

829836
return;
830837
}

src/views/views.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export class Views implements Disposable {
8585
return this._scmGroupedViews;
8686
}
8787

88+
private _lastSelectedByView = new Map<
89+
GroupableTreeViewTypes,
90+
{ node: ViewNode; parents: ViewNode[]; expanded: boolean }
91+
>();
8892
private _welcomeDismissed = false;
8993

9094
constructor(
@@ -482,7 +486,43 @@ export class Views implements Disposable {
482486

483487
private setScmGroupedView<T extends GroupableTreeViewTypes>(type: T, focus?: boolean) {
484488
if (this._scmGroupedView != null) {
485-
return this._scmGroupedView.setView(type, focus);
489+
// Save current selection before switching views
490+
let { view } = this._scmGroupedView;
491+
if (view) {
492+
const node: ViewNode | undefined = view.selection?.[0];
493+
if (node != null) {
494+
const parents: ViewNode[] = [];
495+
496+
let parent: ViewNode | undefined = node;
497+
while (true) {
498+
parent = parent.getParent();
499+
if (parent == null) break;
500+
501+
parents.unshift(parent);
502+
}
503+
504+
this._lastSelectedByView.set(view.type, {
505+
node: node,
506+
parents: parents,
507+
expanded: view.isNodeExpanded(node),
508+
});
509+
}
510+
}
511+
512+
view = this._scmGroupedView.setView(type, focus);
513+
514+
// Restore the last selection for this view type (if any)
515+
if (view) {
516+
const selection = this._lastSelectedByView.get(type);
517+
if (selection != null) {
518+
setTimeout(async () => {
519+
const { node, parents, expanded } = selection;
520+
await view.revealDeep(node, parents, { expand: expanded, focus: focus ?? false, select: true });
521+
}, 1);
522+
}
523+
}
524+
525+
return view;
486526
}
487527

488528
if (!this.scmGroupedViews?.has(type)) {

0 commit comments

Comments
 (0)