Reveal opened file in queries panel
This commit is contained in:
@@ -17,5 +17,31 @@ export class QueriesPanel extends DisposableObject {
|
||||
treeDataProvider: dataProvider,
|
||||
});
|
||||
this.push(treeView);
|
||||
|
||||
window.onDidChangeActiveTextEditor((textEditor) => {
|
||||
if (!textEditor) {
|
||||
return;
|
||||
}
|
||||
|
||||
const filePath = textEditor.document.uri.fsPath;
|
||||
|
||||
const item = dataProvider.getTreeItemByPath(filePath);
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
treeView.selection.length === 1 &&
|
||||
treeView.selection[0].path === item.path
|
||||
) {
|
||||
// The item is already selected
|
||||
return;
|
||||
}
|
||||
|
||||
void treeView.reveal(item, {
|
||||
select: true,
|
||||
focus: false,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
import { DisposableObject } from "../common/disposable-object";
|
||||
import { FileTreeNode } from "../common/file-tree-nodes";
|
||||
import { App } from "../common/app";
|
||||
import { containsPath } from "../common/files";
|
||||
|
||||
export interface QueryDiscoverer {
|
||||
readonly buildQueryTree: () => Array<FileTreeNode<string>> | undefined;
|
||||
@@ -41,6 +42,54 @@ export class QueryTreeDataProvider
|
||||
return this.onDidChangeTreeDataEmitter.event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a specific tree view item by its path. If it's not found, returns undefined.
|
||||
*
|
||||
* @param path The path to retrieve the item for.
|
||||
*/
|
||||
public getTreeItemByPath(path: string): QueryTreeViewItem | undefined {
|
||||
const itemPath = this.findItemPath(path, this.queryTreeItems);
|
||||
if (!itemPath) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return itemPath[itemPath.length - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a specific tree view item by path.
|
||||
*
|
||||
* @param path The path to find the item for.
|
||||
* @param items The items to search.
|
||||
* @param currentPath The current path to the item.
|
||||
* @return The path to the tree view item, or undefined if it could not be found. The last item in the
|
||||
* array is the item itself.
|
||||
*/
|
||||
private findItemPath(
|
||||
path: string,
|
||||
items: QueryTreeViewItem[],
|
||||
currentPath: QueryTreeViewItem[] = [],
|
||||
): QueryTreeViewItem[] | undefined {
|
||||
const relevantItems = items.filter((item) => containsPath(item.path, path));
|
||||
|
||||
const matchingItem = relevantItems.find((item) => item.path === path);
|
||||
if (matchingItem) {
|
||||
return [...currentPath, matchingItem];
|
||||
}
|
||||
|
||||
for (const item of relevantItems) {
|
||||
const childItem = this.findItemPath(path, item.children, [
|
||||
...currentPath,
|
||||
item,
|
||||
]);
|
||||
if (childItem) {
|
||||
return childItem;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private createTree(): QueryTreeViewItem[] {
|
||||
const queryTree = this.queryDiscoverer.buildQueryTree();
|
||||
if (queryTree === undefined) {
|
||||
@@ -95,4 +144,14 @@ export class QueryTreeDataProvider
|
||||
return item.children;
|
||||
}
|
||||
}
|
||||
|
||||
public getParent(item: QueryTreeViewItem): QueryTreeViewItem | undefined {
|
||||
const itemPath = this.findItemPath(item.path, this.queryTreeItems);
|
||||
if (!itemPath) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// The item itself is last in the last, so the parent is the second last item.
|
||||
return itemPath[itemPath.length - 2];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as vscode from "vscode";
|
||||
export class QueryTreeViewItem extends vscode.TreeItem {
|
||||
constructor(
|
||||
name: string,
|
||||
public readonly path: string | undefined,
|
||||
public readonly path: string,
|
||||
public readonly children: QueryTreeViewItem[],
|
||||
) {
|
||||
super(name);
|
||||
|
||||
Reference in New Issue
Block a user