Skip to content

Commit 5a66d58

Browse files
authored
Merge pull request #3424 from obsidian-tasks-group/fix-3342-on-completion-blank-line
fix: Stop onCompletion: delete leaving blank lines for non-recurring tasks
2 parents 8cd7a87 + 018f10e commit 5a66d58

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

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

Lines changed: 24 additions & 6 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,15 +23,15 @@
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

3030
- Line before
3131
- [ ] #task Complete in **Live Preview** clicking checkbox 🏁 delete
3232
- Line after
3333

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

3636
## Reading Mode
3737

@@ -43,6 +43,14 @@
4343

4444
✅ Whole line is removed.
4545

46+
### Complete in Reading Mode right-clicking checkbox
47+
48+
- Line before
49+
- [ ] #task Complete in **Reading Mode** right-clicking checkbox 🏁 delete
50+
- Line after
51+
52+
✅ Whole line is removed.
53+
4654
## Search results
4755

4856
### Complete in Search results clicking checkbox
@@ -51,14 +59,24 @@
5159
- [ ] #task Complete in **Search result**s clicking checkbox 🏁 delete
5260
- Line after
5361

62+
✅ Whole line is removed.
63+
64+
### Complete in Search results right-clicking checkbox
65+
66+
- Line before
67+
- [ ] #task Complete in **Search result**s right-clicking checkbox 🏁 delete
68+
- Line after
69+
70+
✅ Whole line is removed.
71+
72+
### Search
73+
5474
```tasks
5575
path includes {{query.file.path}}
56-
heading includes Complete in Search results clicking checkbox
76+
heading includes Complete in Search results
5777
short mode
5878
```
5979

60-
✅ Whole line is removed.
61-
6280
## Remaining steps
6381

6482
```tasks

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+
const replacementTextIsNonEmpty = insertion.text.length > 0;
41+
const taskIsOnLastLine = lineNumber >= editor.lineCount() - 1;
42+
if (replacementTextIsNonEmpty || taskIsOnLastLine) {
43+
editor.setLine(lineNumber, insertion.text);
44+
} else {
45+
// The replacement text is empty, and our line was followed by a new line character,
46+
// so we delete the line and the new-line, to avoid leaving a blank line in the file.
47+
const from = { line: lineNumber, ch: 0 };
48+
const to = { line: lineNumber + 1, ch: 0 };
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).

src/Obsidian/LivePreviewExtension.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,22 @@ class LivePreviewExtension implements PluginValue {
9191
const toggled = task.toggleWithRecurrenceInUsersOrder();
9292
const toggledString = toggled.map((t) => t.toFileLineString()).join(state.lineBreak);
9393

94+
let to = line.to;
95+
96+
if (toggledString === '') {
97+
// We also need to remove any line break at the end of the line.
98+
const nextLine = line.number < state.doc.lines ? state.doc.line(line.number + 1) : null;
99+
if (nextLine) {
100+
// If not the last line, delete up to the start of the next line, including the line break
101+
to = nextLine.from;
102+
}
103+
}
104+
94105
// Creates a CodeMirror transaction in order to update the document.
95106
const transaction = state.update({
96107
changes: {
97108
from: line.from,
98-
to: line.to,
109+
to,
99110
insert: toggledString,
100111
},
101112
});

0 commit comments

Comments
 (0)