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

Conversation

axelson
Copy link
Contributor

@axelson axelson commented Jun 3, 2023

New behavior: if the user has a global filter set then the global filter is inserted when the "Tasks: Toggle task done" is run.

Description

The "Tasks: Toggle task done" command will now prepend the globalFilter when creating a new checkbox if the autoInsertGlobalFilter setting is enabled. The autoInsertGlobalFilter setting defaults to false.

Also fix an edge case where an extra empty space was added when the task doesn't have any description besides the global filter. Otherwise some of test cases felt broken because an extra space would be inserted.

Previously '- |#task' would become '- [ ] #task|' (notice the two spaces before #task)
Now it becomes '- [ ] #task|'

TODO:

  • Update this description to reflect all the changes (e.g. the new extractLineItemComponents)

Motivation and Context

Fixes #1320

I like to use the "Tasks: Toggle task done" command to create tasks, but since I have a global filter set I had to manually type the global filter each time. This PR changes the behavior so that if the user has a global filter set then the global filter is inserted when the "Tasks: Toggle task done" is run.

I'm not sure if this requires an update to the documentation. I didn't see anywhere where the behavior of "Tasks: Toggle task done" is explained. However this is a change in behavior that merits a notice in the release notes.

I'm not sure if this should be considered a breaking change.

Proposed release notes text

"Tasks: Toggle task done" now has the ability to prepend the global filter to tasks created with the "Tasks: Toggle task done" command if the "Tasks: Toggle task done" command inserts global task filter setting is enabled.

How has this been tested?

Tested in a test vault and updated the unit tests.

Screenshots (if appropriate)

Screenshot  2023-06-04 09-04-52

Types of changes

Changes visible to users:

  • Bug fix (prefix: fix - non-breaking change which fixes an issue)
  • New feature (prefix: feat - non-breaking change which adds functionality)
  • Breaking change (prefix: feat!! or fix!! - fix or feature that would cause existing functionality to not work as expected)
  • Documentation (prefix: docs - improvements to any documentation content for users)
  • Sample vault (prefix: vault - improvements to the Tasks-Demo sample vault)
  • Contributing Guidelines (prefix: contrib - any improvements to documentation content for contributors - see Contributing to Tasks)

Internal changes:

  • Refactor (prefix: refactor - non-breaking change which only improves the design or structure of existing code, and making no changes to its external behaviour)
  • Tests (prefix: test - additions and improvements to unit tests and the smoke tests)
  • Infrastructure (prefix: chore - examples include GitHub Actions, issue templates)

Checklist

Terms

Fixes 1320

I like to use the "Tasks: Toggle task done" command to create tasks, but
since I have a global filter set I had to manually type the global
filter each time. This PR changes the behavior so that if the user has a
global filter set then the global filter is inserted when the
"Tasks: Toggle task done" is run.
@claremacrae claremacrae added the scope: commands Additions and changes to the commands supplied by Tasks label Jun 3, 2023
@claremacrae
Copy link
Collaborator

Hi, thanks very much indeed for adding this.

I don't personally use a global filter, but I imagine that those who do will find it useful.

Comment on lines 109 to 112
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

@claremacrae
Copy link
Collaborator

I'm not sure if this requires an update to the documentation. I didn't see anywhere where the behavior of "Tasks: Toggle task done" is explained.

Thanks for considering that. I searched the docs for 'command' and only found tangential references, so it's not something to include in the docs right now.

I've just made a note in #767 to ensure that the capability added here will (eventually) be documented...

However this is a change in behavior that merits a notice in the release notes.

Agreed. As a PR, it will automatically get included in the next release notes.

I'm not sure if this should be considered a breaking change.

Having re-read #1320, I am comfortable labelling this a bug fix, just because without this, the command adds lines which Tasks then ignores if the user has a global filter. That seems just wrong.

@claremacrae claremacrae changed the title Toggle task done command adds global filter text fix: Toggle task done command adds global filter text, if any Jun 3, 2023
Also fix the addition of an empty space when the task doesn't have any
description besides the global filter. Otherwise some of test cases felt
broken because an extra space would be inserted.

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

Also fix the case where the task description already includes the global
filter. In that case it is not necessary to add the global filter.
Also use the GlobalFilter.includedIn for the check
@ilandikov
Copy link
Collaborator

I'm a bit confused, in my workflow, I use Tasks to manage tasks with Global Filter and I also have tasks without it. I don't want Tasks to manage those. I use Toggle task cmd to complete both of them and this has been very useful - I have one cmd to complete both types of tasks.

So this kind of breaks it for me, there could be other people with this use-case. Maybe it could be possible to add this feature/bugfix with a setting? (Disabled by default)

@claremacrae
Copy link
Collaborator

I'm a bit confused, in my workflow, I use Tasks to manage tasks with Global Filter and I also have tasks without it. I don't want Tasks to manage those. I use Toggle task cmd to complete both of them and this has been very useful - I have one cmd to complete both types of tasks.

So this kind of breaks it for me, there could be other people with this use-case. Maybe it could be possible to add this feature/bugfix with a setting? (Disabled by default)

Thank you for saying this @ilandikov.

Overnight I had a nagging doubt about whether this might be the case, prompted by a dim recollection of someone else saying that they only used Tasks for a small subset of their checkboxes, and it was in their muscle memory to add the global filter in the few areas where they wanted Tasks to track.

So yes, I do think that having a setting to turn this PR on will be necessary to avoid disrupting people's muscle memory and workflow.

Comment on lines 96 to 98
const addGlobalFilter = globalFilter != '' && !GlobalFilter.includedIn(line);
const newTaskText = addGlobalFilter ? `[ ] ${globalFilter}` : '[ ]';
const text = line.replace(TaskRegularExpressions.listItemRegex, `$1$2 ${newTaskText}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

It just dawned on me that someone recently spent a lot of time moving all code related to the global filter, including manipulating descriptions, in the new GlobalFilter class.

If the above cannot be done with the existing methods in GlobalFilter, I feel that the new code should go in to a new method in that class. The existing names there may give ideas.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah that makes sense. I took a stab at moving as much of the GlobalFilter-related logic to the GlobalFilter class as I could 750df8d. I didn't go all the way because I wanted to keep the regex-related logic in ToggleDone.ts. Let me know if this matches what you're thinking! If not then I'm open to suggestions.

Don't automatically insert the global filter when creating a new task,
instead only insert the global filter if autoInsertGlobalFilter is true.
@axelson axelson changed the title fix: Toggle task done command adds global filter text, if any fix: Toggle task done command adds global filter text, if enabled Jun 4, 2023
@axelson
Copy link
Contributor Author

axelson commented Jun 4, 2023

I'm a bit confused, in my workflow, I use Tasks to manage tasks with Global Filter and I also have tasks without it. I don't want Tasks to manage those. I use Toggle task cmd to complete both of them and this has been very useful - I have one cmd to complete both types of tasks.

So yes, I do think that having a setting to turn this PR on will be necessary to avoid disrupting people's muscle memory and workflow.

Ah I hadn't considered that use case because for me 90% percent of the checkboxes I create I want to be considered as Tasks plugin tasks. In 750df8d I've added a autoInsertGlobalFilter setting that controls this new behavior and defaults to false so there's no change for users unless they opt in to the new behavior.

@axelson axelson requested a review from claremacrae June 4, 2023 19:23
@axelson axelson changed the title fix: Toggle task done command adds global filter text, if enabled feat: Toggle task done command adds global filter text, if enabled Jun 4, 2023
@claremacrae
Copy link
Collaborator

Thank you again for offering this contribution, and apologies for not picking this up yet. It's not forgotten and I will get to it.

@axelson
Copy link
Contributor Author

axelson commented Jun 23, 2023

@claremacrae Thank you! And I'll try to fix the merge conflict over the weekend

@claremacrae
Copy link
Collaborator

@claremacrae Thank you! And I'll try to fix the merge conflict over the weekend

Thank you. That is useful to know - I will try and do the code review before the weekend then, so if there is any feedback you would only need to look at it once.

Create a extractLineItemComponents function

GlobalFilter now does the insertion into the body and operates only on description instead of the entire line

Tweaked listItemRegex to capture the body explicitly
@axelson
Copy link
Contributor Author

axelson commented Jun 25, 2023

Okay I've pushed up some changes based on pairing as well as a few additional changes (such as extractLineItemComponents), I think the main items still remaining is to update the PR description to reflect all the changes and possibly a refactor that will allow us to reduce the repetition in the tests (i.e. a function tied directly to the auto insert behavior, possibly by testing addGlobalFilterToDescriptionDependingOnSettings directly instead of transitively testing it in ToggleDone).

@claremacrae
Copy link
Collaborator

Okay I've pushed up some changes based on pairing as well as a few additional changes (such as extractLineItemComponents),

Thanks!

I think the main items still remaining is to update the PR description to reflect all the changes

Am happy to see the PR description updated...

and possibly a refactor that will allow us to reduce the repetition in the tests (i.e. a function tied directly to the auto insert behavior, possibly by testing addGlobalFilterToDescriptionDependingOnSettings directly instead of transitively testing it in ToggleDone).

I have held off saying this as it feels a bit negative, but I basically feel that all the tests added in the PR are now in the wrong place.

The logic of the new behaviour should now be tested in the GlobalFilter tests.

I would pretty-much revert the changes to ToggleDone.test.ts and then add just these 2 tests:

  • when autoAddGlobalFilter enabled, it only adds global filter at the point of making a list item in to a task
  • when autoAddGlobalFilter disabled, it never adds the global filter

In other words, the tests in ToggleDone are written in terms of user-visible behaviour.

How do you feel about doing more in the testing?

If the description above is too vague, and if you are available on Sunday, we could pair on it together.

If that won't work, I don't mind editing the tests and pushing to your branch, and then merging.

@claremacrae
Copy link
Collaborator

Just to note that @ilandikov has added kindly merged in main and responded to the feedback requests here.

I will have time to review that over the next 2 or 3 weeks, and then figure out a way to first merge this one, and then to merge a PR from ilandikov to add the later stages - so that both contributors get credit...

Thanks to both @axelson and @ilandikov for working on this...

I hope that this message prevents the possible risk of two people working on it at the same time...

@ilandikov
Copy link
Collaborator

@claremacrae
Copy link
Collaborator

Setting to draft status any PRs which are not currently mergable...

@claremacrae claremacrae marked this pull request as draft May 7, 2024 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: commands Additions and changes to the commands supplied by Tasks
Projects
Status: 🔖 Ready
Development

Successfully merging this pull request may close these issues.

Toggle task done command should add the global task filter text
3 participants