Skip to content

Commit 1e58e5a

Browse files
committed
Reveal opened file in queries panel
1 parent 2ebccd5 commit 1e58e5a

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

extensions/ql-vscode/src/queries-panel/queries-panel.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,31 @@ export class QueriesPanel extends DisposableObject {
1717
treeDataProvider: dataProvider,
1818
});
1919
this.push(treeView);
20+
21+
window.onDidChangeActiveTextEditor((textEditor) => {
22+
if (!textEditor) {
23+
return;
24+
}
25+
26+
const filePath = textEditor.document.uri.fsPath;
27+
28+
const item = dataProvider.getTreeItemByPath(filePath);
29+
if (!item) {
30+
return;
31+
}
32+
33+
if (
34+
treeView.selection.length === 1 &&
35+
treeView.selection[0].path === item.path
36+
) {
37+
// The item is already selected
38+
return;
39+
}
40+
41+
void treeView.reveal(item, {
42+
select: true,
43+
focus: false,
44+
});
45+
});
2046
}
2147
}

extensions/ql-vscode/src/queries-panel/query-tree-data-provider.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { DisposableObject } from "../common/disposable-object";
88
import { FileTreeNode } from "../common/file-tree-nodes";
99
import { App } from "../common/app";
10+
import { containsPath } from "../common/files";
1011

1112
export interface QueryDiscoverer {
1213
readonly buildQueryTree: () => Array<FileTreeNode<string>> | undefined;
@@ -41,6 +42,54 @@ export class QueryTreeDataProvider
4142
return this.onDidChangeTreeDataEmitter.event;
4243
}
4344

45+
/**
46+
* Retrieves a specific tree view item by its path. If it's not found, returns undefined.
47+
*
48+
* @param path The path to retrieve the item for.
49+
*/
50+
public getTreeItemByPath(path: string): QueryTreeViewItem | undefined {
51+
const itemPath = this.findItemPath(path, this.queryTreeItems);
52+
if (!itemPath) {
53+
return undefined;
54+
}
55+
56+
return itemPath[itemPath.length - 1];
57+
}
58+
59+
/**
60+
* Find a specific tree view item by path.
61+
*
62+
* @param path The path to find the item for.
63+
* @param items The items to search.
64+
* @param currentPath The current path to the item.
65+
* @return The path to the tree view item, or undefined if it could not be found. The last item in the
66+
* array is the item itself.
67+
*/
68+
private findItemPath(
69+
path: string,
70+
items: QueryTreeViewItem[],
71+
currentPath: QueryTreeViewItem[] = [],
72+
): QueryTreeViewItem[] | undefined {
73+
const relevantItems = items.filter((item) => containsPath(item.path, path));
74+
75+
const matchingItem = relevantItems.find((item) => item.path === path);
76+
if (matchingItem) {
77+
return [...currentPath, matchingItem];
78+
}
79+
80+
for (const item of relevantItems) {
81+
const childItem = this.findItemPath(path, item.children, [
82+
...currentPath,
83+
item,
84+
]);
85+
if (childItem) {
86+
return childItem;
87+
}
88+
}
89+
90+
return undefined;
91+
}
92+
4493
private createTree(): QueryTreeViewItem[] {
4594
const queryTree = this.queryDiscoverer.buildQueryTree();
4695
if (queryTree === undefined) {
@@ -95,4 +144,14 @@ export class QueryTreeDataProvider
95144
return item.children;
96145
}
97146
}
147+
148+
public getParent(item: QueryTreeViewItem): QueryTreeViewItem | undefined {
149+
const itemPath = this.findItemPath(item.path, this.queryTreeItems);
150+
if (!itemPath) {
151+
return undefined;
152+
}
153+
154+
// The item itself is last in the last, so the parent is the second last item.
155+
return itemPath[itemPath.length - 2];
156+
}
98157
}

extensions/ql-vscode/src/queries-panel/query-tree-view-item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as vscode from "vscode";
33
export class QueryTreeViewItem extends vscode.TreeItem {
44
constructor(
55
name: string,
6-
public readonly path: string | undefined,
6+
public readonly path: string,
77
public readonly children: QueryTreeViewItem[],
88
) {
99
super(name);

0 commit comments

Comments
 (0)