Skip to content

Commit 24bf8d2

Browse files
committed
feat: All tasks are now accessible in scripting, as query.allTasks
1 parent 29fced0 commit 24bf8d2

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/Query/SearchInfo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Task } from '../Task/Task';
2-
import { type QueryContext, makeQueryContext } from '../Scripting/QueryContext';
2+
import { type QueryContext, makeQueryContextWithTasks } from '../Scripting/QueryContext';
33

44
/**
55
* SearchInfo contains selected data passed in from the {@link Query} being executed.
@@ -30,6 +30,6 @@ export class SearchInfo {
3030
* @return A QueryContext, or undefined if the path to the query file is unknown.
3131
*/
3232
public queryContext(): QueryContext | undefined {
33-
return this.queryPath ? makeQueryContext(this.queryPath) : undefined;
33+
return this.queryPath ? makeQueryContextWithTasks(this.queryPath, this.allTasks) : undefined;
3434
}
3535
}

src/Scripting/QueryContext.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Task } from '../Task/Task';
12
import { TasksFile } from './TasksFile';
23

34
/**
@@ -18,6 +19,7 @@ import { TasksFile } from './TasksFile';
1819
export interface QueryContext {
1920
query: {
2021
file: TasksFile;
22+
allTasks: Readonly<Task[]>;
2123
};
2224
}
2325

@@ -32,6 +34,17 @@ export function makeQueryContext(path: string): QueryContext {
3234
return {
3335
query: {
3436
file: tasksFile,
37+
allTasks: [],
38+
},
39+
};
40+
}
41+
42+
export function makeQueryContextWithTasks(path: string, allTasks: Readonly<Task[]>): QueryContext {
43+
const tasksFile = new TasksFile(path);
44+
return {
45+
query: {
46+
file: tasksFile,
47+
allTasks: allTasks,
3548
},
3649
};
3750
}

tests/Scripting/QueryContext.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { FolderField } from '../../src/Query/Filter/FolderField';
44
import { PathField } from '../../src/Query/Filter/PathField';
55
import { RootField } from '../../src/Query/Filter/RootField';
66
import { makeQueryContext } from '../../src/Scripting/QueryContext';
7+
import { FunctionField } from '../../src/Query/Filter/FunctionField';
8+
import { SearchInfo } from '../../src/Query/SearchInfo';
79

810
describe('QueryContext', () => {
911
describe('values should all match their corresponding filters', () => {
@@ -34,5 +36,20 @@ describe('QueryContext', () => {
3436
const filter = new FilenameField().createFilterOrErrorMessage(instruction);
3537
expect(filter).toMatchTask(task);
3638
});
39+
40+
it('allTasks', () => {
41+
// Arrange
42+
// An artificial example, just to demonstrate that query.allTasks is accessible via scripting
43+
const instruction = 'group by function query.allTasks.length';
44+
const grouper = new FunctionField().createGrouperFromLine(instruction);
45+
expect(grouper).not.toBeNull();
46+
const searchInfo = new SearchInfo(path, [task]);
47+
48+
// Act
49+
const group: string[] = grouper!.grouper(task, searchInfo);
50+
51+
// Assert
52+
expect(group).toEqual(['1']);
53+
});
3754
});
3855
});

0 commit comments

Comments
 (0)