Skip to content

Commit c6a7e1f

Browse files
Rename from path to pathData where appropriate
1 parent d626cea commit c6a7e1f

4 files changed

Lines changed: 58 additions & 49 deletions

File tree

extensions/ql-vscode/src/common/vscode/file-path-discovery.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ interface PathData {
2121
}
2222

2323
/**
24-
* Discovers all files matching a given filter contained in the workspace.
24+
* Discovers and watches for changes to all files matching a given filter
25+
* contained in the workspace. Also allows computing extra data about each
26+
* file path, and only recomputing the data when the file changes.
2527
*
2628
* Scans the whole workspace on startup, and then watches for changes to files
2729
* to do the minimum work to keep up with changes.
@@ -30,11 +32,11 @@ interface PathData {
3032
* relevant, and what extra data to compute for each file.
3133
*/
3234
export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
33-
/** The set of known paths we are tracking */
34-
protected paths: T[] = [];
35+
/** The set of known paths and associated data that we are tracking */
36+
protected pathData: T[] = [];
3537

36-
/** Event that fires whenever the set of known paths changes */
37-
protected readonly onDidChangePathsEmitter: AppEventEmitter<void>;
38+
/** Event that fires whenever the contents of `pathData` changes */
39+
protected readonly onDidChangePathDataEmitter: AppEventEmitter<void>;
3840

3941
/**
4042
* 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 {
6062
constructor(name: string, private readonly fileWatchPattern: string) {
6163
super(name, extLogger);
6264

63-
this.onDidChangePathsEmitter = this.push(new EventEmitter<void>());
65+
this.onDidChangePathDataEmitter = this.push(new EventEmitter<void>());
6466
this.push(
6567
workspace.onDidChangeWorkspaceFolders(
6668
this.workspaceFoldersChanged.bind(this),
@@ -138,7 +140,7 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
138140
}
139141

140142
if (pathsUpdated) {
141-
this.onDidChangePathsEmitter.fire();
143+
this.onDidChangePathDataEmitter.fire();
142144
}
143145
}
144146

@@ -170,9 +172,11 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
170172
}
171173

172174
private handleRemovedPath(path: string): boolean {
173-
const oldLength = this.paths.length;
174-
this.paths = this.paths.filter((q) => !containsPath(path, q.path));
175-
return this.paths.length !== oldLength;
175+
const oldLength = this.pathData.length;
176+
this.pathData = this.pathData.filter(
177+
(existingPathData) => !containsPath(path, existingPathData.path),
178+
);
179+
return this.pathData.length !== oldLength;
176180
}
177181

178182
private async handleChangedDirectory(path: string): Promise<boolean> {
@@ -199,18 +203,23 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
199203

200204
private async addOrUpdatePath(path: string): Promise<boolean> {
201205
const data = await this.getDataForPath(path);
202-
const existingDataIndex = this.paths.findIndex((x) => x.path === path);
203-
if (existingDataIndex !== -1) {
206+
const existingPathDataIndex = this.pathData.findIndex(
207+
(existingPathData) => existingPathData.path === path,
208+
);
209+
if (existingPathDataIndex !== -1) {
204210
if (
205-
this.shouldOverwriteExistingData(data, this.paths[existingDataIndex])
211+
this.shouldOverwriteExistingData(
212+
data,
213+
this.pathData[existingPathDataIndex],
214+
)
206215
) {
207-
this.paths.splice(existingDataIndex, 1, data);
216+
this.pathData.splice(existingPathDataIndex, 1, data);
208217
return true;
209218
} else {
210219
return false;
211220
}
212221
} else {
213-
this.paths.push(data);
222+
this.pathData.push(data);
214223
return true;
215224
}
216225
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class QueryDiscovery
4848
* Event that fires when the set of queries in the workspace changes.
4949
*/
5050
public get onDidChangeQueries(): Event<void> {
51-
return this.onDidChangePathsEmitter.event;
51+
return this.onDidChangePathDataEmitter.event;
5252
}
5353

5454
/**
@@ -59,7 +59,7 @@ export class QueryDiscovery
5959
public buildQueryTree(): Array<FileTreeNode<string>> {
6060
const roots = [];
6161
for (const workspaceFolder of getOnDiskWorkspaceFoldersObjects()) {
62-
const queriesInRoot = this.paths.filter((query) =>
62+
const queriesInRoot = this.pathData.filter((query) =>
6363
containsPath(workspaceFolder.uri.fsPath, query.path),
6464
);
6565
if (queriesInRoot.length === 0) {
@@ -106,10 +106,10 @@ export class QueryDiscovery
106106
private recomputeAllQueryLanguages() {
107107
// All we know is that something has changed in the set of known query packs.
108108
// We have no choice but to recompute the language for all queries.
109-
for (const query of this.paths) {
109+
for (const query of this.pathData) {
110110
query.language = this.determineQueryLanguage(query.path);
111111
}
112-
this.onDidChangePathsEmitter.fire();
112+
this.onDidChangePathDataEmitter.fire();
113113
}
114114

115115
private determineQueryLanguage(path: string): QueryLanguage | undefined {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class QueryPackDiscovery extends FilePathDiscovery<QueryPack> {
2727
* Event that fires when the set of query packs in the workspace changes.
2828
*/
2929
public get onDidChangeQueryPacks(): Event<void> {
30-
return this.onDidChangePathsEmitter.event;
30+
return this.onDidChangePathDataEmitter.event;
3131
}
3232

3333
/**
@@ -37,7 +37,7 @@ export class QueryPackDiscovery extends FilePathDiscovery<QueryPack> {
3737
*/
3838
public getLanguageForQueryFile(queryPath: string): QueryLanguage | undefined {
3939
// Find all packs in a higher directory than the query
40-
const packs = this.paths.filter((queryPack) =>
40+
const packs = this.pathData.filter((queryPack) =>
4141
containsPath(dirname(queryPack.path), queryPath),
4242
);
4343

extensions/ql-vscode/test/vscode-tests/minimal-workspace/common/vscode/file-path-discovery.test.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ class TestFilePathDiscovery extends FilePathDiscovery<TestData> {
2525
}
2626

2727
public get onDidChangePaths() {
28-
return this.onDidChangePathsEmitter.event;
28+
return this.onDidChangePathDataEmitter.event;
2929
}
3030

31-
public getPaths(): TestData[] {
32-
return this.paths;
31+
public getPathData(): TestData[] {
32+
return this.pathData;
3333
}
3434

3535
protected async getDataForPath(path: string): Promise<TestData> {
@@ -116,7 +116,7 @@ describe("FilePathDiscovery", () => {
116116
describe("initialRefresh", () => {
117117
it("should handle no files being present", async () => {
118118
await discovery.initialRefresh();
119-
expect(discovery.getPaths()).toEqual([]);
119+
expect(discovery.getPathData()).toEqual([]);
120120
});
121121

122122
it("should recursively discover all test files", async () => {
@@ -126,7 +126,7 @@ describe("FilePathDiscovery", () => {
126126

127127
await discovery.initialRefresh();
128128

129-
expect(new Set(discovery.getPaths())).toEqual(
129+
expect(new Set(discovery.getPathData())).toEqual(
130130
new Set([
131131
{ path: join(workspacePath, "123.test"), contents: "123" },
132132
{ path: join(workspacePath, "456.test"), contents: "456" },
@@ -142,7 +142,7 @@ describe("FilePathDiscovery", () => {
142142

143143
await discovery.initialRefresh();
144144

145-
expect(new Set(discovery.getPaths())).toEqual(
145+
expect(new Set(discovery.getPathData())).toEqual(
146146
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
147147
);
148148
});
@@ -155,14 +155,14 @@ describe("FilePathDiscovery", () => {
155155
const didChangePathsListener = jest.fn();
156156
discovery.onDidChangePaths(didChangePathsListener);
157157

158-
expect(discovery.getPaths()).toEqual([]);
158+
expect(discovery.getPathData()).toEqual([]);
159159

160160
const newFile = join(workspacePath, "1.test");
161161
makeTestFile(newFile);
162162
onDidCreateFile.fire(Uri.file(newFile));
163163
await discovery.waitForCurrentRefresh();
164164

165-
expect(new Set(discovery.getPaths())).toEqual(
165+
expect(new Set(discovery.getPathData())).toEqual(
166166
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
167167
);
168168
expect(didChangePathsListener).toHaveBeenCalled();
@@ -174,12 +174,12 @@ describe("FilePathDiscovery", () => {
174174
const didChangePathsListener = jest.fn();
175175
discovery.onDidChangePaths(didChangePathsListener);
176176

177-
expect(discovery.getPaths()).toEqual([]);
177+
expect(discovery.getPathData()).toEqual([]);
178178

179179
onDidCreateFile.fire(Uri.file(join(workspacePath, "1.test")));
180180
await discovery.waitForCurrentRefresh();
181181

182-
expect(discovery.getPaths()).toEqual([]);
182+
expect(discovery.getPathData()).toEqual([]);
183183
expect(didChangePathsListener).not.toHaveBeenCalled();
184184
});
185185

@@ -189,7 +189,7 @@ describe("FilePathDiscovery", () => {
189189
const didChangePathsListener = jest.fn();
190190
discovery.onDidChangePaths(didChangePathsListener);
191191

192-
expect(discovery.getPaths()).toEqual([]);
192+
expect(discovery.getPathData()).toEqual([]);
193193

194194
const newDir = join(workspacePath, "foo");
195195
makeTestFile(join(newDir, "1.test"));
@@ -198,7 +198,7 @@ describe("FilePathDiscovery", () => {
198198
onDidCreateFile.fire(Uri.file(newDir));
199199
await discovery.waitForCurrentRefresh();
200200

201-
expect(new Set(discovery.getPaths())).toEqual(
201+
expect(new Set(discovery.getPathData())).toEqual(
202202
new Set([
203203
{ path: join(newDir, "1.test"), contents: "1" },
204204
{ path: join(newDir, "bar", "2.test"), contents: "2" },
@@ -217,14 +217,14 @@ describe("FilePathDiscovery", () => {
217217
const didChangePathsListener = jest.fn();
218218
discovery.onDidChangePaths(didChangePathsListener);
219219

220-
expect(new Set(discovery.getPaths())).toEqual(
220+
expect(new Set(discovery.getPathData())).toEqual(
221221
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
222222
);
223223

224224
onDidCreateFile.fire(Uri.file(testFile));
225225
await discovery.waitForCurrentRefresh();
226226

227-
expect(new Set(discovery.getPaths())).toEqual(
227+
expect(new Set(discovery.getPathData())).toEqual(
228228
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
229229
);
230230
expect(didChangePathsListener).not.toHaveBeenCalled();
@@ -241,14 +241,14 @@ describe("FilePathDiscovery", () => {
241241
const didChangePathsListener = jest.fn();
242242
discovery.onDidChangePaths(didChangePathsListener);
243243

244-
expect(new Set(discovery.getPaths())).toEqual(
244+
expect(new Set(discovery.getPathData())).toEqual(
245245
new Set([{ path: join(workspacePath, "123.test"), contents: "123" }]),
246246
);
247247

248248
onDidChangeFile.fire(Uri.file(testFile));
249249
await discovery.waitForCurrentRefresh();
250250

251-
expect(new Set(discovery.getPaths())).toEqual(
251+
expect(new Set(discovery.getPathData())).toEqual(
252252
new Set([{ path: join(workspacePath, "123.test"), contents: "123" }]),
253253
);
254254
expect(didChangePathsListener).not.toHaveBeenCalled();
@@ -263,15 +263,15 @@ describe("FilePathDiscovery", () => {
263263
const didChangePathsListener = jest.fn();
264264
discovery.onDidChangePaths(didChangePathsListener);
265265

266-
expect(new Set(discovery.getPaths())).toEqual(
266+
expect(new Set(discovery.getPathData())).toEqual(
267267
new Set([{ path: join(workspacePath, "1.test"), contents: "foo" }]),
268268
);
269269

270270
writeFileSync(testFile, "bar");
271271
onDidChangeFile.fire(Uri.file(testFile));
272272
await discovery.waitForCurrentRefresh();
273273

274-
expect(new Set(discovery.getPaths())).toEqual(
274+
expect(new Set(discovery.getPathData())).toEqual(
275275
new Set([{ path: join(workspacePath, "1.test"), contents: "bar" }]),
276276
);
277277
expect(didChangePathsListener).toHaveBeenCalled();
@@ -288,15 +288,15 @@ describe("FilePathDiscovery", () => {
288288
const didChangePathsListener = jest.fn();
289289
discovery.onDidChangePaths(didChangePathsListener);
290290

291-
expect(new Set(discovery.getPaths())).toEqual(
291+
expect(new Set(discovery.getPathData())).toEqual(
292292
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
293293
);
294294

295295
rmSync(testFile);
296296
onDidDeleteFile.fire(Uri.file(testFile));
297297
await discovery.waitForCurrentRefresh();
298298

299-
expect(discovery.getPaths()).toEqual([]);
299+
expect(discovery.getPathData()).toEqual([]);
300300
expect(didChangePathsListener).toHaveBeenCalled();
301301
});
302302

@@ -309,14 +309,14 @@ describe("FilePathDiscovery", () => {
309309
const didChangePathsListener = jest.fn();
310310
discovery.onDidChangePaths(didChangePathsListener);
311311

312-
expect(new Set(discovery.getPaths())).toEqual(
312+
expect(new Set(discovery.getPathData())).toEqual(
313313
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
314314
);
315315

316316
onDidDeleteFile.fire(Uri.file(testFile));
317317
await discovery.waitForCurrentRefresh();
318318

319-
expect(new Set(discovery.getPaths())).toEqual(
319+
expect(new Set(discovery.getPathData())).toEqual(
320320
new Set([{ path: join(workspacePath, "1.test"), contents: "1" }]),
321321
);
322322
expect(didChangePathsListener).not.toHaveBeenCalled();
@@ -332,7 +332,7 @@ describe("FilePathDiscovery", () => {
332332
const didChangePathsListener = jest.fn();
333333
discovery.onDidChangePaths(didChangePathsListener);
334334

335-
expect(new Set(discovery.getPaths())).toEqual(
335+
expect(new Set(discovery.getPathData())).toEqual(
336336
new Set([
337337
{ path: join(workspacePath, "123.test"), contents: "123" },
338338
{ path: join(workspacePath, "bar", "456.test"), contents: "456" },
@@ -345,7 +345,7 @@ describe("FilePathDiscovery", () => {
345345
onDidDeleteFile.fire(Uri.file(join(workspacePath, "bar")));
346346
await discovery.waitForCurrentRefresh();
347347

348-
expect(new Set(discovery.getPaths())).toEqual(
348+
expect(new Set(discovery.getPathData())).toEqual(
349349
new Set([{ path: join(workspacePath, "123.test"), contents: "123" }]),
350350
);
351351
expect(didChangePathsListener).toHaveBeenCalled();
@@ -395,7 +395,7 @@ describe("FilePathDiscovery", () => {
395395

396396
await discovery.initialRefresh();
397397

398-
expect(new Set(discovery.getPaths())).toEqual(
398+
expect(new Set(discovery.getPathData())).toEqual(
399399
new Set([{ path: join(workspacePath, "123.test"), contents: "123" }]),
400400
);
401401

@@ -418,7 +418,7 @@ describe("FilePathDiscovery", () => {
418418
});
419419
await discovery.waitForCurrentRefresh();
420420

421-
expect(new Set(discovery.getPaths())).toEqual(
421+
expect(new Set(discovery.getPathData())).toEqual(
422422
new Set([
423423
{ path: join(workspacePath, "123.test"), contents: "123" },
424424
{ path: join(tmpDir, "workspace2", "456.test"), contents: "456" },
@@ -446,7 +446,7 @@ describe("FilePathDiscovery", () => {
446446

447447
await discovery.initialRefresh();
448448

449-
expect(new Set(discovery.getPaths())).toEqual(
449+
expect(new Set(discovery.getPathData())).toEqual(
450450
new Set([
451451
{ path: join(tmpDir, "workspace1", "123.test"), contents: "123" },
452452
{ path: join(tmpDir, "workspace2", "456.test"), contents: "456" },
@@ -460,7 +460,7 @@ describe("FilePathDiscovery", () => {
460460
});
461461
await discovery.waitForCurrentRefresh();
462462

463-
expect(new Set(discovery.getPaths())).toEqual(
463+
expect(new Set(discovery.getPathData())).toEqual(
464464
new Set([
465465
{ path: join(tmpDir, "workspace1", "123.test"), contents: "123" },
466466
]),

0 commit comments

Comments
 (0)