generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Make TagsField a special MultiTextField; rename Field.filte…
…rRegexp() (#1173) * Refactor: Make TagsField a special MultiTextField * Move the MultiTextField class to a separate file * Add some more JSdoc comments * Fix wording in JSDoc * Make variable and method names more specific * Use older, more compatible import syntax * Use JavaScript spelling of RegExp everywhere
- Loading branch information
Showing
13 changed files
with
116 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { Task } from '../../Task'; | ||
import type { IStringMatcher } from '../Matchers/IStringMatcher'; | ||
import { TextField } from './TextField'; | ||
import type { Filter } from './Filter'; | ||
|
||
/** | ||
* MultiTextField is an abstract base class to help implement | ||
* all the filter instructions that act on multiple string values | ||
* such as the tags. | ||
*/ | ||
export abstract class MultiTextField extends TextField { | ||
/** | ||
* Returns the singular form of the field's name. | ||
*/ | ||
public abstract fieldNameSingular(): string; | ||
|
||
/** | ||
* Returns the plural form of the field's name. | ||
* If not overridden, returns the singular form appended with an "s". | ||
*/ | ||
protected fieldNamePlural(): string { | ||
return this.fieldNameSingular() + 's'; | ||
} | ||
|
||
public fieldName(): string { | ||
return `${this.fieldNameSingular()}/${this.fieldNamePlural()}`; | ||
} | ||
|
||
protected fieldPattern(): string { | ||
return `${this.fieldNameSingular()}|${this.fieldNamePlural()}`; | ||
} | ||
|
||
protected filterOperatorPattern(): string { | ||
return `${super.filterOperatorPattern()}|include|do not include`; | ||
} | ||
|
||
/** | ||
* If not overridden, returns a comma-separated concatenation of all | ||
* the values of this field or an empty string if there are not values | ||
* @param task | ||
* @public | ||
*/ | ||
public value(task: Task): string { | ||
return this.values(task).join(', '); | ||
} | ||
|
||
/** | ||
* Returns the array of values of this field, or an empty array | ||
* if the field has no values | ||
* @param task | ||
* @public | ||
*/ | ||
public abstract values(task: Task): string[]; | ||
|
||
protected getFilter(matcher: IStringMatcher, negate: boolean): Filter { | ||
return (task: Task) => { | ||
const match = matcher!.matchesAnyOf(this.values(task)); | ||
return negate ? !match : match; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,17 @@ | ||
import type { Task } from '../../Task'; | ||
import { SubstringMatcher } from '../Matchers/SubstringMatcher'; | ||
import { RegexMatcher } from '../Matchers/RegexMatcher'; | ||
import type { IStringMatcher } from '../Matchers/IStringMatcher'; | ||
import { Field } from './Field'; | ||
import { FilterOrErrorMessage } from './Filter'; | ||
import { TextField } from './TextField'; | ||
import { MultiTextField } from './MultiTextField'; | ||
|
||
/** | ||
* Support the 'tag' and 'tags' search instructions. | ||
* | ||
* Tags can be searched for with and without the hash tag at the start. | ||
*/ | ||
export class TagsField extends Field { | ||
// Handles both ways of referencing the tags query. | ||
private static readonly tagRegexp = | ||
/^(tag|tags) (includes|does not include|include|do not include|regex matches|regex does not match) (.*)/; | ||
|
||
public createFilterOrErrorMessage(line: string): FilterOrErrorMessage { | ||
const match = Field.getMatch(this.filterRegexp(), line); | ||
if (match === null) { | ||
return FilterOrErrorMessage.fromError(`do not understand query filter (${this.fieldName()})`); | ||
} | ||
const filterMethod = match[2]; | ||
const search = match[3]; | ||
let matcher: IStringMatcher | null = null; | ||
if (filterMethod.includes('include')) { | ||
matcher = new SubstringMatcher(search); | ||
} else if (filterMethod.includes('regex')) { | ||
matcher = RegexMatcher.validateAndConstruct(search); | ||
if (matcher === null) { | ||
return FilterOrErrorMessage.fromError( | ||
`cannot parse regex (${this.fieldName()}); check your leading and trailing slashes for your query`, | ||
); | ||
} | ||
} | ||
|
||
if (matcher === null) { | ||
// It's likely this can now never be reached. | ||
// Retained for safety, for now. | ||
return FilterOrErrorMessage.fromError(`do not understand query filter (${this.fieldName()})`); | ||
} | ||
|
||
return FilterOrErrorMessage.fromFilter((task: Task) => { | ||
return TextField.maybeNegate(matcher!.matchesAnyOf(task.tags), filterMethod); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns both forms of the field name, singular and plural. | ||
* @protected | ||
*/ | ||
public fieldName(): string { | ||
return 'tag/tags'; | ||
export class TagsField extends MultiTextField { | ||
public fieldNameSingular(): string { | ||
return 'tag'; | ||
} | ||
|
||
protected filterRegexp(): RegExp { | ||
return TagsField.tagRegexp; | ||
public values(task: Task): string[] { | ||
return task.tags; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters