Skip to content

Commit af9af99

Browse files
authored
Queries panel: display the query language in the UI (#2472)
1 parent 0d390aa commit af9af99

File tree

5 files changed

+53
-34
lines changed

5 files changed

+53
-34
lines changed

extensions/ql-vscode/src/common/file-tree-nodes.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import { env } from "vscode";
44
/**
55
* A node in the tree of files. This will be either a `FileTreeDirectory` or a `FileTreeLeaf`.
66
*/
7-
export abstract class FileTreeNode {
8-
constructor(private _path: string, private _name: string) {}
7+
export abstract class FileTreeNode<T = undefined> {
8+
constructor(
9+
private _path: string,
10+
private _name: string,
11+
private _data?: T,
12+
) {}
913

1014
public get path(): string {
1115
return this._path;
@@ -15,32 +19,36 @@ export abstract class FileTreeNode {
1519
return this._name;
1620
}
1721

18-
public abstract get children(): readonly FileTreeNode[];
22+
public get data(): T | undefined {
23+
return this._data;
24+
}
25+
26+
public abstract get children(): ReadonlyArray<FileTreeNode<T>>;
1927

2028
public abstract finish(): void;
2129
}
2230

2331
/**
2432
* A directory containing one or more files or other directories.
2533
*/
26-
export class FileTreeDirectory extends FileTreeNode {
34+
export class FileTreeDirectory<T = undefined> extends FileTreeNode<T> {
2735
constructor(
2836
_path: string,
2937
_name: string,
30-
private _children: FileTreeNode[] = [],
38+
private _children: Array<FileTreeNode<T>> = [],
3139
) {
3240
super(_path, _name);
3341
}
3442

35-
public get children(): readonly FileTreeNode[] {
43+
public get children(): ReadonlyArray<FileTreeNode<T>> {
3644
return this._children;
3745
}
3846

39-
public addChild(child: FileTreeNode): void {
47+
public addChild(child: FileTreeNode<T>): void {
4048
this._children.push(child);
4149
}
4250

43-
public createDirectory(relativePath: string): FileTreeDirectory {
51+
public createDirectory(relativePath: string): FileTreeDirectory<T> {
4452
if (relativePath === ".") {
4553
return this;
4654
}
@@ -66,7 +74,7 @@ export class FileTreeDirectory extends FileTreeNode {
6674
child.children[0] instanceof FileTreeDirectory
6775
) {
6876
// collapse children
69-
const replacement = new FileTreeDirectory(
77+
const replacement = new FileTreeDirectory<T>(
7078
child.children[0].path,
7179
`${child.name} / ${child.children[0].name}`,
7280
Array.from(child.children[0].children),
@@ -76,12 +84,12 @@ export class FileTreeDirectory extends FileTreeNode {
7684
});
7785
}
7886

79-
private createChildDirectory(name: string): FileTreeDirectory {
87+
private createChildDirectory(name: string): FileTreeDirectory<T> {
8088
const existingChild = this._children.find((child) => child.name === name);
8189
if (existingChild !== undefined) {
82-
return existingChild as FileTreeDirectory;
90+
return existingChild as FileTreeDirectory<T>;
8391
} else {
84-
const newChild = new FileTreeDirectory(join(this.path, name), name);
92+
const newChild = new FileTreeDirectory<T>(join(this.path, name), name);
8593
this.addChild(newChild);
8694
return newChild;
8795
}
@@ -91,12 +99,12 @@ export class FileTreeDirectory extends FileTreeNode {
9199
/**
92100
* A single file.
93101
*/
94-
export class FileTreeLeaf extends FileTreeNode {
95-
constructor(_path: string, _name: string) {
96-
super(_path, _name);
102+
export class FileTreeLeaf<T = undefined> extends FileTreeNode<T> {
103+
constructor(_path: string, _name: string, _data?: T) {
104+
super(_path, _name, _data);
97105
}
98106

99-
public get children(): readonly FileTreeNode[] {
107+
public get children(): ReadonlyArray<FileTreeNode<T>> {
100108
return [];
101109
}
102110

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface QueryDiscoveryResults {
1818
* A tree of directories and query files.
1919
* May have multiple roots because of multiple workspaces.
2020
*/
21-
queries: FileTreeDirectory[];
21+
queries: Array<FileTreeDirectory<string>>;
2222

2323
/**
2424
* File system paths to watch. If any ql file changes in these directories
@@ -49,7 +49,7 @@ export class QueryDiscovery
4949
this.push(this.watcher.onDidChange(this.refresh.bind(this)));
5050
}
5151

52-
public get queries(): FileTreeDirectory[] | undefined {
52+
public get queries(): Array<FileTreeDirectory<string>> | undefined {
5353
return this.results?.queries;
5454
}
5555

@@ -97,7 +97,7 @@ export class QueryDiscovery
9797
*/
9898
private async discoverQueries(
9999
workspaceFolders: readonly WorkspaceFolder[],
100-
): Promise<FileTreeDirectory[]> {
100+
): Promise<Array<FileTreeDirectory<string>>> {
101101
const rootDirectories = [];
102102
for (const workspaceFolder of workspaceFolders) {
103103
const root = await this.discoverQueriesInWorkspace(workspaceFolder);
@@ -110,7 +110,7 @@ export class QueryDiscovery
110110

111111
private async discoverQueriesInWorkspace(
112112
workspaceFolder: WorkspaceFolder,
113-
): Promise<FileTreeDirectory | undefined> {
113+
): Promise<FileTreeDirectory<string> | undefined> {
114114
const fullPath = workspaceFolder.uri.fsPath;
115115
const name = workspaceFolder.name;
116116

@@ -124,13 +124,13 @@ export class QueryDiscovery
124124
return undefined;
125125
}
126126

127-
const rootDirectory = new FileTreeDirectory(fullPath, name);
127+
const rootDirectory = new FileTreeDirectory<string>(fullPath, name);
128128
for (const queryPath of resolvedQueries) {
129129
const relativePath = normalize(relative(fullPath, queryPath));
130130
const dirName = dirname(relativePath);
131131
const parentDirectory = rootDirectory.createDirectory(dirName);
132132
parentDirectory.addChild(
133-
new FileTreeLeaf(queryPath, basename(queryPath)),
133+
new FileTreeLeaf<string>(queryPath, basename(queryPath), "language"),
134134
);
135135
}
136136

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DisposableObject } from "../pure/disposable-object";
44
import { FileTreeNode } from "../common/file-tree-nodes";
55

66
export interface QueryDiscoverer {
7-
readonly queries: FileTreeNode[] | undefined;
7+
readonly queries: Array<FileTreeNode<string>> | undefined;
88
readonly onDidChangeQueries: Event<void>;
99
}
1010

@@ -40,11 +40,12 @@ export class QueryTreeDataProvider
4040
}
4141

4242
private convertFileTreeNode(
43-
fileTreeDirectory: FileTreeNode,
43+
fileTreeDirectory: FileTreeNode<string>,
4444
): QueryTreeViewItem {
4545
return new QueryTreeViewItem(
4646
fileTreeDirectory.name,
4747
fileTreeDirectory.path,
48+
fileTreeDirectory.data,
4849
fileTreeDirectory.children.map(this.convertFileTreeNode.bind(this)),
4950
);
5051
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ export class QueryTreeViewItem extends vscode.TreeItem {
44
constructor(
55
name: string,
66
path: string,
7+
language: string | undefined,
78
public readonly children: QueryTreeViewItem[],
89
) {
910
super(name);
1011
this.tooltip = path;
12+
this.description = language;
1113
this.collapsibleState = this.children.length
1214
? vscode.TreeItemCollapsibleState.Collapsed
1315
: vscode.TreeItemCollapsibleState.None;

extensions/ql-vscode/test/vscode-tests/minimal-workspace/queries-panel/query-tree-data-provider.test.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@ describe("QueryTreeDataProvider", () => {
3131
it("converts FileTreeNode to QueryTreeViewItem", async () => {
3232
const dataProvider = new QueryTreeDataProvider({
3333
queries: [
34-
new FileTreeDirectory("dir1", "dir1", [
35-
new FileTreeDirectory("dir1/dir2", "dir2", [
36-
new FileTreeLeaf("dir1/dir2/file1", "file1"),
37-
new FileTreeLeaf("dir1/dir2/file1", "file2"),
34+
new FileTreeDirectory<string>("dir1", "dir1", [
35+
new FileTreeDirectory<string>("dir1/dir2", "dir2", [
36+
new FileTreeLeaf<string>(
37+
"dir1/dir2/file1",
38+
"file1",
39+
"javascript",
40+
),
41+
new FileTreeLeaf<string>(
42+
"dir1/dir2/file1",
43+
"file2",
44+
"javascript",
45+
),
3846
]),
3947
]),
40-
new FileTreeDirectory("dir3", "dir3", [
41-
new FileTreeLeaf("dir3/file3", "file3"),
48+
new FileTreeDirectory<string>("dir3", "dir3", [
49+
new FileTreeLeaf<string>("dir3/file3", "file3", "javascript"),
4250
]),
4351
],
4452
onDidChangeQueries: jest.fn(),
@@ -70,8 +78,8 @@ describe("QueryTreeDataProvider", () => {
7078
const onDidChangeQueriesEmitter = new EventEmitter<void>();
7179
const queryDiscoverer: QueryDiscoverer = {
7280
queries: [
73-
new FileTreeDirectory("dir1", "dir1", [
74-
new FileTreeLeaf("dir1/file1", "file1"),
81+
new FileTreeDirectory<string>("dir1", "dir1", [
82+
new FileTreeLeaf<string>("dir1/file1", "file1", "javascript"),
7583
]),
7684
],
7785
onDidChangeQueries: onDidChangeQueriesEmitter.event,
@@ -81,8 +89,8 @@ describe("QueryTreeDataProvider", () => {
8189
expect(dataProvider.getChildren().length).toEqual(1);
8290

8391
queryDiscoverer.queries?.push(
84-
new FileTreeDirectory("dir2", "dir2", [
85-
new FileTreeLeaf("dir2/file2", "file2"),
92+
new FileTreeDirectory<string>("dir2", "dir2", [
93+
new FileTreeLeaf<string>("dir2/file2", "file2", "javascript"),
8694
]),
8795
);
8896
onDidChangeQueriesEmitter.fire();

0 commit comments

Comments
 (0)