1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use crate::model::track::{Track, TrackNode};
use crate::parse::task_serializer::serialize_tasks;
/// Serialize a track back to its markdown representation.
/// Literal nodes are emitted verbatim. Task sections use the task serializer
/// (which respects the dirty flag for round-trip preservation).
pub fn serialize_track(track: &Track) -> String {
let mut lines = Vec::new();
let last_node = track.nodes.len().saturating_sub(1);
for (i, node) in track.nodes.iter().enumerate() {
match node {
TrackNode::Literal(literal_lines) => {
lines.extend(literal_lines.iter().cloned());
}
TrackNode::Section {
header_lines,
tasks,
trailing_lines,
..
} => {
lines.extend(header_lines.iter().cloned());
// The mirror of the rule below, on the other side of the tasks.
// A section that ended the file has no blank under its header —
// there was nothing to separate — so the first task moved into
// it came out welded to `## Done`.
//
// The condition is narrower than "the header is followed by
// something", and has to be. A header can also be followed
// immediately by *stranded content*, which the parser hangs on
// the first task's `leading_lines` and preserves verbatim
// (`a_stray_line_above_the_first_task_survives_a_write`).
// Pushing a blank in front of that would edit content, not
// layout. So the question is whether the first line this
// section emits is a task line, asked with [`task_indent`] —
// the parser's own test, and not `starts_with("- [")`, which
// also matches a markdown link bullet.
let task_lines = serialize_tasks(tasks, 0);
if header_lines.last().is_some_and(|l| !l.trim().is_empty())
&& task_lines
.first()
.is_some_and(|l| crate::parse::task_parser::task_indent(l).is_some())
{
lines.push(String::new());
}
lines.extend(task_lines);
lines.extend(trailing_lines.iter().cloned());
// One blank line between a section and whatever follows it —
// no more, no fewer.
//
// This was the `ops` layer's job, added to the section move
// because moving a task into an empty `## Done` welded it to
// the next header. But a section move is one of a dozen things
// that puts a task into a section: undo has its own inserts,
// and every one of them that reached an emptied section
// produced the same welded file. Rather than teach each caller
// to remember, the rule lives where nothing can bypass it.
//
// Both halves matter. *Too few* is the weld. *Too many* is the
// drained section — its header blank and its trailing blank are
// one separator counted twice, so a file grew a line every time
// a section emptied, and undoing the delete did not close the
// gap because the task came back after the doubled blank.
if i != last_node {
while lines.len() >= 2
&& lines[lines.len() - 1].trim().is_empty()
&& lines[lines.len() - 2].trim().is_empty()
{
lines.pop();
}
let welded = lines
.last()
.is_some_and(|l| !l.trim().is_empty() && !tasks.is_empty());
if welded {
lines.push(String::new());
}
}
}
}
}
// A section drained of its tasks — `fr clean` archiving the last Done task,
// say — contributes only its header and the blank that separated it from
// those tasks. At end of file that blank separates nothing, and leaving it
// means every clean re-adds a blank row someone then has to strip again.
if matches!(track.nodes.last(), Some(TrackNode::Section { tasks, .. }) if tasks.is_empty()) {
crate::parse::pop_trailing_blanks(&mut lines);
}
let mut out = lines.join("\n");
out.push('\n');
track.eol.apply(out)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::SectionKind;
use crate::ops::task_ops::move_task_between_sections;
use crate::parse::track_parser::parse_track;
/// A section drained of its last task keeps *one* blank before the next
/// header, not the two it inherits — its own, and the one under its header.
///
/// Without this a file grew a line every time a section emptied, and the
/// growth was invisible: the result round-trips through the parser
/// unchanged, so no settledness check would report it.
#[test]
fn a_drained_section_does_not_double_its_separator() {
let source = "\
# T
## Backlog
- [ ] `T-001` One
## Done
- [x] `T-000` Done
";
let mut track = parse_track(source);
track
.section_tasks_mut(SectionKind::Backlog)
.unwrap()
.clear();
assert_eq!(
serialize_track(&track),
"# T\n\n## Backlog\n\n## Done\n\n- [x] `T-000` Done\n"
);
}
/// And a section that gains a task into that emptied space gets the blank
/// back, rather than welding its last task to the next header.
///
/// This is the half that used to live in `ops::task_ops` and fire only on a
/// section move. Undo's own inserts went straight to the model and came out
/// welded.
#[test]
fn a_refilled_section_separates_itself_from_the_next_header() {
let source = "\
# T
## Backlog
## Done
- [x] `T-000` Done
";
let mut track = parse_track(source);
let task = parse_track("# X\n\n## Backlog\n\n- [ ] `T-001` One\n")
.section_tasks(SectionKind::Backlog)[0]
.clone();
track
.section_tasks_mut(SectionKind::Backlog)
.unwrap()
.push(task);
assert_eq!(
serialize_track(&track),
"# T\n\n## Backlog\n\n- [ ] `T-001` One\n\n## Done\n\n- [x] `T-000` Done\n"
);
}
/// A section whose header has no blank under it — because it ended the
/// file, so there was nothing to separate — gets one when it gains a task,
/// rather than welding `- [x] task` to `## Done`.
#[test]
fn a_task_moved_under_a_bare_header_is_not_welded_to_it() {
let mut track = parse_track("# T\n\n## Backlog\n\n- [ ] `T-001` One\n\n## Done\n");
move_task_between_sections(&mut track, "T-001", SectionKind::Backlog, SectionKind::Done);
assert_eq!(
serialize_track(&track),
"# T\n\n## Backlog\n\n## Done\n\n- [ ] `T-001` One\n",
"the header keeps its blank line"
);
}
/// But a header may also sit directly above *stranded content*, which the
/// parser hangs on the first task's `leading_lines` and preserves verbatim.
/// The separator rule asks whether the first line emitted is a task line,
/// precisely so it does not push a blank in front of content like this.
#[test]
fn a_header_above_stranded_content_is_left_alone() {
let source = "## Backlog\n - added: 2025-05-14\n- [ ] plain task\n";
assert_eq!(serialize_track(&parse_track(source)), source);
}
/// The last section gets neither: a separator before end-of-file separates
/// nothing, and every task marked done used to append one.
#[test]
fn the_last_section_gains_no_trailing_blank() {
let source = "\
# T
## Backlog
## Done
- [x] `T-000` Done
";
let mut track = parse_track(source);
let task = parse_track("# X\n\n## Backlog\n\n- [x] `T-001` Two\n")
.section_tasks(SectionKind::Backlog)[0]
.clone();
track
.section_tasks_mut(SectionKind::Done)
.unwrap()
.push(task);
assert!(
serialize_track(&track).ends_with("- [x] `T-001` Two\n"),
"no blank line at end of file"
);
}
#[test]
fn test_round_trip_simple_track() {
let source = "\
# Effect System
> Design and implement the algebraic effect system for Lace.
## Backlog
- [>] `EFF-014` Implement effect inference for closures #core
- added: 2025-05-10
- dep: EFF-003
- [ ] `EFF-015` Effect handler optimization pass #core
- dep: EFF-014
## Parked
- [~] `EFF-020` Higher-order effect handlers #research
## Done
- [x] `EFF-003` Implement effect handler desugaring #core
- resolved: 2025-05-14
";
let track = parse_track(source);
let output = serialize_track(&track);
assert_eq!(output, source);
}
#[test]
fn test_round_trip_empty_sections() {
let source = "\
# Empty Track
## Backlog
## Parked
## Done
";
let track = parse_track(source);
let output = serialize_track(&track);
assert_eq!(output, source);
}
#[test]
fn test_emptied_last_section_leaves_no_blank_row() {
// The blank line under `## Done` belongs to that section's header_lines,
// so draining the section (what `fr clean` does when it archives) used
// to strand it at end of file.
let source = "\
# Test Track
## Backlog
- [ ] `T-100` Keep me
## Done
- [x] `T-001` Archived away
";
let mut track = parse_track(source);
for node in &mut track.nodes {
if let TrackNode::Section {
kind: crate::model::track::SectionKind::Done,
tasks,
..
} = node
{
tasks.clear();
}
}
let output = serialize_track(&track);
assert!(
output.ends_with("## Done\n"),
"expected no trailing blank row, got {:?}",
output
);
// Idempotent: a second pass must not change it again.
assert_eq!(serialize_track(&parse_track(&output)), output);
}
#[test]
fn test_blanks_after_a_trailing_empty_section_are_dropped() {
let track = parse_track("# T\n\n## Backlog\n\n## Done\n\n\n");
assert_eq!(serialize_track(&track), "# T\n\n## Backlog\n\n## Done\n");
}
#[test]
fn test_blanks_after_a_trailing_task_survive() {
// Only an *empty* trailing section gets trimmed — blanks the user wrote
// after real content are their formatting.
let source = "# T\n\n## Done\n\n- [x] `T-001` Done thing\n\n\n";
assert_eq!(serialize_track(&parse_track(source)), source);
}
#[test]
fn test_round_trip_with_subtasks() {
let source = "\
# Test Track
## Backlog
- [>] `T-001` Parent task
- added: 2025-05-10
- [ ] `T-001.1` First subtask
- [>] `T-001.2` Second subtask #cc
- [ ] `T-001.2.1` Deep subtask
- [ ] `T-001.2.2` Another deep subtask
- [ ] `T-001.3` Third subtask
## Done
";
let track = parse_track(source);
let output = serialize_track(&track);
assert_eq!(output, source);
}
}