Only return something from getPathData once discover has run at least once

This commit is contained in:
Robert
2023-06-19 12:20:05 +01:00
parent 1579859c9d
commit 6fe7b82397
6 changed files with 34 additions and 9 deletions

View File

@@ -34,6 +34,12 @@ interface PathData {
* relevant, and what extra data to compute for each file.
*/
export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
/**
* Has `discover` been called. This allows distinguishing between
* "no paths found" and not having scanned yet.
*/
private discoverHasCompletedOnce = false;
/** The set of known paths and associated data that we are tracking */
private pathData: T[] = [];
@@ -73,7 +79,10 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
this.push(this.watcher.onDidChange(this.fileChanged.bind(this)));
}
protected getPathData(): ReadonlyArray<Readonly<T>> {
protected getPathData(): ReadonlyArray<Readonly<T>> | undefined {
if (!this.discoverHasCompletedOnce) {
return undefined;
}
return this.pathData;
}
@@ -171,6 +180,7 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
}
}
this.discoverHasCompletedOnce = true;
if (pathsUpdated) {
this.onDidChangePathDataEmitter.fire();
}

View File

@@ -56,10 +56,15 @@ export class QueryDiscovery
*
* Trivial directories where there is only one child will be collapsed into a single node.
*/
public buildQueryTree(): Array<FileTreeNode<string>> {
public buildQueryTree(): Array<FileTreeNode<string>> | undefined {
const pathData = this.getPathData();
if (pathData === undefined) {
return undefined;
}
const roots = [];
for (const workspaceFolder of getOnDiskWorkspaceFoldersObjects()) {
const queriesInRoot = this.getPathData().filter((query) =>
const queriesInRoot = pathData.filter((query) =>
containsPath(workspaceFolder.uri.fsPath, query.path),
);
if (queriesInRoot.length === 0) {

View File

@@ -37,7 +37,7 @@ export class QueryPackDiscovery extends FilePathDiscovery<QueryPack> {
*/
public getLanguageForQueryFile(queryPath: string): QueryLanguage | undefined {
// Find all packs in a higher directory than the query
const packs = this.getPathData().filter((queryPack) =>
const packs = (this.getPathData() || []).filter((queryPack) =>
containsPath(dirname(queryPack.path), queryPath),
);

View File

@@ -4,7 +4,7 @@ import { DisposableObject } from "../common/disposable-object";
import { FileTreeNode } from "../common/file-tree-nodes";
export interface QueryDiscoverer {
readonly buildQueryTree: () => Array<FileTreeNode<string>>;
readonly buildQueryTree: () => Array<FileTreeNode<string>> | undefined;
readonly onDidChangeQueries: Event<void>;
}
@@ -34,9 +34,11 @@ export class QueryTreeDataProvider
}
private createTree(): QueryTreeViewItem[] {
return this.queryDiscoverer
.buildQueryTree()
.map(this.convertFileTreeNode.bind(this));
const queryTree = this.queryDiscoverer.buildQueryTree();
if (queryTree === undefined) {
return [];
}
return queryTree.map(this.convertFileTreeNode.bind(this));
}
private convertFileTreeNode(

View File

@@ -34,7 +34,7 @@ class TestFilePathDiscovery extends FilePathDiscovery<TestData> {
return this.onDidChangePathData;
}
public getPathData(): readonly TestData[] {
public getPathData(): readonly TestData[] | undefined {
return super.getPathData();
}
@@ -123,6 +123,10 @@ describe("FilePathDiscovery", () => {
});
describe("initialRefresh", () => {
it("should return undefined until initialRefresh is called", async () => {
expect(discovery.getPathData()).toEqual(undefined);
});
it("should handle no files being present", async () => {
await discovery.initialRefresh();
expect(discovery.getPathData()).toEqual([]);

View File

@@ -54,6 +54,10 @@ describe("Query pack discovery", () => {
});
describe("buildQueryTree", () => {
it("returns undefined before initial refresh has been done", async () => {
expect(discovery.buildQueryTree()).toEqual(undefined);
});
it("returns an empty tree when there are no query files", async () => {
await discovery.initialRefresh();