Rename from path to pathData where appropriate

This commit is contained in:
Robert
2023-06-16 16:23:39 +01:00
parent d626cea837
commit c6a7e1fb3c
4 changed files with 58 additions and 49 deletions

View File

@@ -21,7 +21,9 @@ interface PathData {
}
/**
* Discovers all files matching a given filter contained in the workspace.
* Discovers and watches for changes to all files matching a given filter
* contained in the workspace. Also allows computing extra data about each
* file path, and only recomputing the data when the file changes.
*
* Scans the whole workspace on startup, and then watches for changes to files
* to do the minimum work to keep up with changes.
@@ -30,11 +32,11 @@ interface PathData {
* relevant, and what extra data to compute for each file.
*/
export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
/** The set of known paths we are tracking */
protected paths: T[] = [];
/** The set of known paths and associated data that we are tracking */
protected pathData: T[] = [];
/** Event that fires whenever the set of known paths changes */
protected readonly onDidChangePathsEmitter: AppEventEmitter<void>;
/** Event that fires whenever the contents of `pathData` changes */
protected readonly onDidChangePathDataEmitter: AppEventEmitter<void>;
/**
* The set of file paths that may have changed on disk since the last time
@@ -60,7 +62,7 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
constructor(name: string, private readonly fileWatchPattern: string) {
super(name, extLogger);
this.onDidChangePathsEmitter = this.push(new EventEmitter<void>());
this.onDidChangePathDataEmitter = this.push(new EventEmitter<void>());
this.push(
workspace.onDidChangeWorkspaceFolders(
this.workspaceFoldersChanged.bind(this),
@@ -138,7 +140,7 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
}
if (pathsUpdated) {
this.onDidChangePathsEmitter.fire();
this.onDidChangePathDataEmitter.fire();
}
}
@@ -170,9 +172,11 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
}
private handleRemovedPath(path: string): boolean {
const oldLength = this.paths.length;
this.paths = this.paths.filter((q) => !containsPath(path, q.path));
return this.paths.length !== oldLength;
const oldLength = this.pathData.length;
this.pathData = this.pathData.filter(
(existingPathData) => !containsPath(path, existingPathData.path),
);
return this.pathData.length !== oldLength;
}
private async handleChangedDirectory(path: string): Promise<boolean> {
@@ -199,18 +203,23 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
private async addOrUpdatePath(path: string): Promise<boolean> {
const data = await this.getDataForPath(path);
const existingDataIndex = this.paths.findIndex((x) => x.path === path);
if (existingDataIndex !== -1) {
const existingPathDataIndex = this.pathData.findIndex(
(existingPathData) => existingPathData.path === path,
);
if (existingPathDataIndex !== -1) {
if (
this.shouldOverwriteExistingData(data, this.paths[existingDataIndex])
this.shouldOverwriteExistingData(
data,
this.pathData[existingPathDataIndex],
)
) {
this.paths.splice(existingDataIndex, 1, data);
this.pathData.splice(existingPathDataIndex, 1, data);
return true;
} else {
return false;
}
} else {
this.paths.push(data);
this.pathData.push(data);
return true;
}
}

View File

@@ -48,7 +48,7 @@ export class QueryDiscovery
* Event that fires when the set of queries in the workspace changes.
*/
public get onDidChangeQueries(): Event<void> {
return this.onDidChangePathsEmitter.event;
return this.onDidChangePathDataEmitter.event;
}
/**
@@ -59,7 +59,7 @@ export class QueryDiscovery
public buildQueryTree(): Array<FileTreeNode<string>> {
const roots = [];
for (const workspaceFolder of getOnDiskWorkspaceFoldersObjects()) {
const queriesInRoot = this.paths.filter((query) =>
const queriesInRoot = this.pathData.filter((query) =>
containsPath(workspaceFolder.uri.fsPath, query.path),
);
if (queriesInRoot.length === 0) {
@@ -106,10 +106,10 @@ export class QueryDiscovery
private recomputeAllQueryLanguages() {
// All we know is that something has changed in the set of known query packs.
// We have no choice but to recompute the language for all queries.
for (const query of this.paths) {
for (const query of this.pathData) {
query.language = this.determineQueryLanguage(query.path);
}
this.onDidChangePathsEmitter.fire();
this.onDidChangePathDataEmitter.fire();
}
private determineQueryLanguage(path: string): QueryLanguage | undefined {

View File

@@ -27,7 +27,7 @@ export class QueryPackDiscovery extends FilePathDiscovery<QueryPack> {
* Event that fires when the set of query packs in the workspace changes.
*/
public get onDidChangeQueryPacks(): Event<void> {
return this.onDidChangePathsEmitter.event;
return this.onDidChangePathDataEmitter.event;
}
/**
@@ -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.paths.filter((queryPack) =>
const packs = this.pathData.filter((queryPack) =>
containsPath(dirname(queryPack.path), queryPath),
);

View File

@@ -25,11 +25,11 @@ class TestFilePathDiscovery extends FilePathDiscovery<TestData> {
}
public get onDidChangePaths() {
return this.onDidChangePathsEmitter.event;
return this.onDidChangePathDataEmitter.event;
}
public getPaths(): TestData[] {
return this.paths;
public getPathData(): TestData[] {
return this.pathData;
}
protected async getDataForPath(path: string): Promise<TestData> {
@@ -116,7 +116,7 @@ describe("FilePathDiscovery", () => {
describe("initialRefresh", () => {
it("should handle no files being present", async () => {
await discovery.initialRefresh();
expect(discovery.getPaths()).toEqual([]);
expect(discovery.getPathData()).toEqual([]);
});
it("should recursively discover all test files", async () => {
@@ -126,7 +126,7 @@ describe("FilePathDiscovery", () => {
await discovery.initialRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([
{ path: join(workspacePath, "123.test"), contents: "123" },
{ path: join(workspacePath, "456.test"), contents: "456" },
@@ -142,7 +142,7 @@ describe("FilePathDiscovery", () => {
await discovery.initialRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
);
});
@@ -155,14 +155,14 @@ describe("FilePathDiscovery", () => {
const didChangePathsListener = jest.fn();
discovery.onDidChangePaths(didChangePathsListener);
expect(discovery.getPaths()).toEqual([]);
expect(discovery.getPathData()).toEqual([]);
const newFile = join(workspacePath, "1.test");
makeTestFile(newFile);
onDidCreateFile.fire(Uri.file(newFile));
await discovery.waitForCurrentRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
);
expect(didChangePathsListener).toHaveBeenCalled();
@@ -174,12 +174,12 @@ describe("FilePathDiscovery", () => {
const didChangePathsListener = jest.fn();
discovery.onDidChangePaths(didChangePathsListener);
expect(discovery.getPaths()).toEqual([]);
expect(discovery.getPathData()).toEqual([]);
onDidCreateFile.fire(Uri.file(join(workspacePath, "1.test")));
await discovery.waitForCurrentRefresh();
expect(discovery.getPaths()).toEqual([]);
expect(discovery.getPathData()).toEqual([]);
expect(didChangePathsListener).not.toHaveBeenCalled();
});
@@ -189,7 +189,7 @@ describe("FilePathDiscovery", () => {
const didChangePathsListener = jest.fn();
discovery.onDidChangePaths(didChangePathsListener);
expect(discovery.getPaths()).toEqual([]);
expect(discovery.getPathData()).toEqual([]);
const newDir = join(workspacePath, "foo");
makeTestFile(join(newDir, "1.test"));
@@ -198,7 +198,7 @@ describe("FilePathDiscovery", () => {
onDidCreateFile.fire(Uri.file(newDir));
await discovery.waitForCurrentRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([
{ path: join(newDir, "1.test"), contents: "1" },
{ path: join(newDir, "bar", "2.test"), contents: "2" },
@@ -217,14 +217,14 @@ describe("FilePathDiscovery", () => {
const didChangePathsListener = jest.fn();
discovery.onDidChangePaths(didChangePathsListener);
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
);
onDidCreateFile.fire(Uri.file(testFile));
await discovery.waitForCurrentRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
);
expect(didChangePathsListener).not.toHaveBeenCalled();
@@ -241,14 +241,14 @@ describe("FilePathDiscovery", () => {
const didChangePathsListener = jest.fn();
discovery.onDidChangePaths(didChangePathsListener);
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "123.test"), contents: "123" }]),
);
onDidChangeFile.fire(Uri.file(testFile));
await discovery.waitForCurrentRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "123.test"), contents: "123" }]),
);
expect(didChangePathsListener).not.toHaveBeenCalled();
@@ -263,7 +263,7 @@ describe("FilePathDiscovery", () => {
const didChangePathsListener = jest.fn();
discovery.onDidChangePaths(didChangePathsListener);
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "1.test"), contents: "foo" }]),
);
@@ -271,7 +271,7 @@ describe("FilePathDiscovery", () => {
onDidChangeFile.fire(Uri.file(testFile));
await discovery.waitForCurrentRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "1.test"), contents: "bar" }]),
);
expect(didChangePathsListener).toHaveBeenCalled();
@@ -288,7 +288,7 @@ describe("FilePathDiscovery", () => {
const didChangePathsListener = jest.fn();
discovery.onDidChangePaths(didChangePathsListener);
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
);
@@ -296,7 +296,7 @@ describe("FilePathDiscovery", () => {
onDidDeleteFile.fire(Uri.file(testFile));
await discovery.waitForCurrentRefresh();
expect(discovery.getPaths()).toEqual([]);
expect(discovery.getPathData()).toEqual([]);
expect(didChangePathsListener).toHaveBeenCalled();
});
@@ -309,14 +309,14 @@ describe("FilePathDiscovery", () => {
const didChangePathsListener = jest.fn();
discovery.onDidChangePaths(didChangePathsListener);
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
);
onDidDeleteFile.fire(Uri.file(testFile));
await discovery.waitForCurrentRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
);
expect(didChangePathsListener).not.toHaveBeenCalled();
@@ -332,7 +332,7 @@ describe("FilePathDiscovery", () => {
const didChangePathsListener = jest.fn();
discovery.onDidChangePaths(didChangePathsListener);
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([
{ path: join(workspacePath, "123.test"), contents: "123" },
{ path: join(workspacePath, "bar", "456.test"), contents: "456" },
@@ -345,7 +345,7 @@ describe("FilePathDiscovery", () => {
onDidDeleteFile.fire(Uri.file(join(workspacePath, "bar")));
await discovery.waitForCurrentRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "123.test"), contents: "123" }]),
);
expect(didChangePathsListener).toHaveBeenCalled();
@@ -395,7 +395,7 @@ describe("FilePathDiscovery", () => {
await discovery.initialRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([{ path: join(workspacePath, "123.test"), contents: "123" }]),
);
@@ -418,7 +418,7 @@ describe("FilePathDiscovery", () => {
});
await discovery.waitForCurrentRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([
{ path: join(workspacePath, "123.test"), contents: "123" },
{ path: join(tmpDir, "workspace2", "456.test"), contents: "456" },
@@ -446,7 +446,7 @@ describe("FilePathDiscovery", () => {
await discovery.initialRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([
{ path: join(tmpDir, "workspace1", "123.test"), contents: "123" },
{ path: join(tmpDir, "workspace2", "456.test"), contents: "456" },
@@ -460,7 +460,7 @@ describe("FilePathDiscovery", () => {
});
await discovery.waitForCurrentRefresh();
expect(new Set(discovery.getPaths())).toEqual(
expect(new Set(discovery.getPathData())).toEqual(
new Set([
{ path: join(tmpDir, "workspace1", "123.test"), contents: "123" },
]),