Skip to content

Commit b7b11b2

Browse files
committed
fix: Remove blank line with 'On completion Delete', toggling via Tasks' command
Partial fix for #3342
1 parent f46e019 commit b7b11b2

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

resources/sample_vaults/Tasks-Demo/Manual Testing/Task Toggling Scenarios/On Completion Delete.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
- [ ] #task Complete in **Source Mode** with `Tasks: Toggle task done` command 🏁 delete
1414
- Line after
1515

16-
==❌ This shows \#3342 - blank line remains, as of Tasks `7.18.4`.==
16+
✅ Whole line is removed.
1717

1818
## Live Preview
1919

@@ -23,7 +23,7 @@
2323
- [ ] #task Complete in **Live Preview** with `Tasks: Toggle task done` command 🏁 delete
2424
- Line after
2525

26-
==❌ This shows \#3342 - blank line remains, as of Tasks `7.18.4`.==
26+
✅ Whole line is removed.
2727

2828
### Complete in Live Preview clicking checkbox
2929

src/Commands/ToggleDone.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ export const toggleDone = (checking: boolean, editor: Editor, view: MarkdownView
3636
const line = editor.getLine(lineNumber);
3737

3838
const insertion = toggleLine(line, path);
39-
editor.setLine(lineNumber, insertion.text);
39+
40+
if (insertion.text.length > 0) {
41+
editor.setLine(lineNumber, insertion.text);
42+
} else {
43+
// https://github.com/obsidian-tasks-group/obsidian-tasks/issues/3342
44+
// If insertion.text is empty, delete the line instead, so we don't leave a trailing end-of-line character around.
45+
// This behaves gracefully even if there is no end-of-line character on the last line in the file.
46+
const from = { line: lineNumber, ch: 0 };
47+
const to = { line: lineNumber + 1, ch: 0 };
48+
// console.log(`Deleting line+EOL "${editor.getRange(from, to)}", because insertion.text is empty.`);
49+
editor.replaceRange('', from, to);
50+
}
4051

4152
/* Cursor positions are 0-based for both "line" and "ch" offsets.
4253
* If "ch" offset bigger than the line length, will just continue to next line(s).

0 commit comments

Comments
 (0)