generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
Filter.ts
76 lines (67 loc) · 2.83 KB
/
Filter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import type { Task } from '../../Task/Task';
import { Explanation } from '../Explain/Explanation';
import type { SearchInfo } from '../SearchInfo';
import { Statement } from '../Statement';
/**
* A filtering function, that takes a Task object and returns
* whether it matches a particular filtering instruction.
*
* SearchInfo is being introduced as a Parameter Object, in order to later allow
* more data to be passed from the Query down in to the individual filters.
*/
export type FilterFunction = (task: Task, searchInfo: SearchInfo) => boolean;
/**
* A class that represents a parsed filtering instruction from a tasks code block.
*
* It provides access to:
*
* - The original {@link instruction}, after processing of continuation lines and placeholders.
* - An {@link explanation}, showing how the instruction was interpreted.
* - A {@link statement}, which is a {@link Statement} object that gives access to the original text,
* for filters that were created by a {@link Query}.
* - The {@link filterFunction} - a {@link FilterFunction} which tests whether a task matches the filter
*/
export class Filter {
/** _statement may be updated later with {@link setStatement} */
private _statement: Statement;
readonly explanation: Explanation;
public filterFunction: FilterFunction;
public constructor(instruction: string, filterFunction: FilterFunction, explanation: Explanation) {
this._statement = new Statement(instruction, instruction);
this.explanation = explanation;
this.filterFunction = filterFunction;
}
public get statement(): Statement {
return this._statement;
}
/**
* Optionally record more detail about the source statement.
*
* In tests, we only care about the actual instruction being parsed and executed.
* However, in {@link Query}, we want the ability to show user more information.
*/
public setStatement(statement: Statement) {
this._statement = statement;
}
public get instruction(): string {
return this._statement.anyPlaceholdersExpanded;
}
public explainFilterIndented(indent: string) {
const explanation = this.explanation;
const unindentedExplanation = explanation.asString();
const explainedStatement = this._statement.explainStatement(indent);
if (unindentedExplanation === this.instruction) {
return `${explainedStatement}\n`;
} else {
return `${explainedStatement} =>\n${explanation.asString(indent + ' ')}\n`;
}
}
public simulateExplainFilter() {
const needToShowInstruction = this.instruction !== this.explanation.asString();
if (needToShowInstruction) {
return new Explanation(this.instruction + ' =>', [this.explanation]);
} else {
return this.explanation;
}
}
}