Skip to content

Commit 368f9c3

Browse files
committed
Only change selection when tree view is visible
1 parent 1e58e5a commit 368f9c3

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed
Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,82 @@
11
import { DisposableObject } from "../common/disposable-object";
22
import { QueryTreeDataProvider } from "./query-tree-data-provider";
33
import { QueryDiscovery } from "./query-discovery";
4-
import { window } from "vscode";
4+
import { TextEditor, TreeView, window } from "vscode";
55
import { App } from "../common/app";
6+
import { QueryTreeViewItem } from "./query-tree-view-item";
67

78
export class QueriesPanel extends DisposableObject {
9+
private readonly dataProvider: QueryTreeDataProvider;
10+
private readonly treeView: TreeView<QueryTreeViewItem>;
11+
812
public constructor(
913
queryDiscovery: QueryDiscovery,
1014
readonly app: App,
1115
) {
1216
super();
1317

14-
const dataProvider = new QueryTreeDataProvider(queryDiscovery, app);
18+
this.dataProvider = new QueryTreeDataProvider(queryDiscovery, app);
1519

16-
const treeView = window.createTreeView("codeQLQueries", {
17-
treeDataProvider: dataProvider,
20+
this.treeView = window.createTreeView("codeQLQueries", {
21+
treeDataProvider: this.dataProvider,
1822
});
19-
this.push(treeView);
23+
this.push(this.treeView);
24+
25+
// Keep track of whether the user has changed their text editor while
26+
// the tree view was not visible. If so, we will focus the text editor
27+
// in the tree view when it becomes visible.
28+
let changedTextEditor: TextEditor | undefined = undefined;
2029

2130
window.onDidChangeActiveTextEditor((textEditor) => {
31+
if (!this.treeView.visible) {
32+
changedTextEditor = textEditor;
33+
34+
return;
35+
}
36+
37+
// Reset the changedTextEditor variable so we don't try to show it when
38+
// the tree view becomes next visible.
39+
changedTextEditor = undefined;
40+
2241
if (!textEditor) {
2342
return;
2443
}
2544

26-
const filePath = textEditor.document.uri.fsPath;
45+
void this.revealTextEditor(textEditor);
46+
});
2747

28-
const item = dataProvider.getTreeItemByPath(filePath);
29-
if (!item) {
48+
this.treeView.onDidChangeVisibility((e) => {
49+
if (!e.visible) {
3050
return;
3151
}
3252

33-
if (
34-
treeView.selection.length === 1 &&
35-
treeView.selection[0].path === item.path
36-
) {
37-
// The item is already selected
53+
if (!changedTextEditor) {
3854
return;
3955
}
4056

41-
void treeView.reveal(item, {
42-
select: true,
43-
focus: false,
44-
});
57+
void this.revealTextEditor(changedTextEditor);
58+
});
59+
}
60+
61+
private revealTextEditor(textEditor: TextEditor): void {
62+
const filePath = textEditor.document.uri.fsPath;
63+
64+
const item = this.dataProvider.getTreeItemByPath(filePath);
65+
if (!item) {
66+
return;
67+
}
68+
69+
if (
70+
this.treeView.selection.length === 1 &&
71+
this.treeView.selection[0].path === item.path
72+
) {
73+
// The item is already selected
74+
return;
75+
}
76+
77+
void this.treeView.reveal(item, {
78+
select: true,
79+
focus: false,
4580
});
4681
}
4782
}

0 commit comments

Comments
 (0)