diff --git a/src/Commands/ToggleDone.ts b/src/Commands/ToggleDone.ts index f7f5fa1059..6ccc25a28b 100644 --- a/src/Commands/ToggleDone.ts +++ b/src/Commands/ToggleDone.ts @@ -75,7 +75,8 @@ export const toggleLine = (line: string, path: string): EditorInsertion => { }); if (task !== null) { const lines = task.toggleWithRecurrenceInUsersOrder().map((t) => t.toFileLineString()); - return { text: lines.join('\n'), moveTo: { line: lines.length - 1 } }; + const newLineNumber = lines.length > 0 ? lines.length - 1 : 0; + return { text: lines.join('\n'), moveTo: { line: newLineNumber } }; } else { // If the task is null this means that we have one of: // 1. a regular checklist item diff --git a/tests/Commands/ToggleDone.test.ts b/tests/Commands/ToggleDone.test.ts index 3158439acc..1a3f19f0b1 100644 --- a/tests/Commands/ToggleDone.test.ts +++ b/tests/Commands/ToggleDone.test.ts @@ -182,6 +182,40 @@ describe('ToggleDone', () => { ); }); + describe('on completion', () => { + it('should delete a self-deleting task - cursor at start of line', () => { + // Issue #3256 - traceback occurred. + testToggleLine( + // Force linebreak + '|- [ ] #task Delete me 🏁 delete', + '|', + ); + }); + + it('should delete a self-deleting task - cursor at end of line', () => { + // Issue #3256 - traceback occurred. + testToggleLine( + // Force linebreak + '- [ ] #task Delete me 🏁 delete|', + '|', + ); + }); + + it('should discard completed recurring task - cursor at start of line', () => { + testToggleLine( + '|- [ ] #task Delete my completed task 🔁 every day 🏁 delete ⏳ 2024-12-31', + '|- [ ] #task Delete my completed task 🔁 every day 🏁 delete ⏳ 2025-01-01', + ); + }); + + it('should discard completed recurring task - cursor at end of line', () => { + testToggleLine( + '- [ ] #task Delete my completed task 🔁 every day 🏁 delete ⏳ 2024-12-31|', + '- [ ] #task Delete my completed task 🔁 every day 🏁 delete ⏳ 2025-01-01|', + ); + }); + }); + describe('should honour next status character', () => { afterEach(() => { GlobalFilter.getInstance().reset();