Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Toggle task done command adds global filter text, if enabled #2015

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
5 changes: 4 additions & 1 deletion src/Commands/ToggleDone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StatusRegistry } from '../StatusRegistry';

import { Task, TaskRegularExpressions } from '../Task';
import { TaskLocation } from '../TaskLocation';
import { GlobalFilter } from '../Config/GlobalFilter';

export const toggleDone = (checking: boolean, editor: Editor, view: MarkdownView | MarkdownFileInfo) => {
if (checking) {
Expand Down Expand Up @@ -91,7 +92,9 @@ export const toggleLine = (line: string, path: string): EditorInsertion => {
return { text: line.replace(TaskRegularExpressions.taskRegex, `$1- [${newStatusString}] $4`) };
} else if (TaskRegularExpressions.listItemRegex.test(line)) {
// Convert the list item to a checklist item.
const text = line.replace(TaskRegularExpressions.listItemRegex, '$1$2 [ ]');
const globalFilter = GlobalFilter.get();
const newTaskText = globalFilter == '' ? '[ ]' : `[ ] ${globalFilter}`;
const text = line.replace(TaskRegularExpressions.listItemRegex, `$1$2 ${newTaskText}`);
return { text, moveTo: { ch: text.length } };
} else {
// Convert the line to a list item.
Expand Down
6 changes: 3 additions & 3 deletions tests/Commands/ToggleDone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ describe('ToggleDone', () => {

GlobalFilter.set('#task');

testToggleLine('|- ', '- [ ] |');
testToggleLine('- |', '- [ ] |');
testToggleLine('- |foobar', '- [ ] foobar|');
testToggleLine('|- ', '- [ ] #task |');
testToggleLine('- |', '- [ ] #task |');
testToggleLine('- |foobar', '- [ ] #task foobar|');
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to see some more thorough testing here.

Such as, what happens when there is already #task present on the line. To ensure that it doesn't add a duplicate tag.

Also, can we test the behaviour with non-tag global filter please?

Also, what happens if the global filter contains characters which are special characters in regular expressions?
(There is some code somewhere to deal with that - like, escaping regex special characters in arbitrary strings).

That's all I can think of for now!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it would be good to have tests for the case where there is no global filter defined, for example to guard against accidental insertion of a space at the beginning of the updated lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I've added more testing in 86b4372. Does that seem sufficient to you? Are there any other tests that are missing?

Some of those tests revealed a smaller bug which is also fixed in that commit. If the task description was empty then toggling the task done would insert an extra space.

- |#task would become - [ ] #task| instead of - [ ] #task|

I also added handling for when the description already includes the global filter.

Also, what happens if the global filter contains characters which are special characters in regular expressions?
I added some tests for this in 6d116e6


it('should complete a task', () => {
Expand Down