yamled 0.0.12

Format-preserving YAML edits: change what you meant, keep every other byte
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
use std::fmt;
use std::ops::Range;

use crate::index::{Index, Location, Style};
use crate::text::{
    LF, NEWLINE, column, comment_start, dash_before, dedent, has_blank_line, indent, join,
    line_end, line_start, spaced, starts_line, terminated,
};
use crate::{Error, Path};

mod arrange;
#[cfg(feature = "serde")]
mod values;

#[cfg(feature = "serde")]
pub use values::Position;

/// Whether a new sequence item is set off from its neighbours by a blank line,
/// for a sequence whose own items do not settle it. A sequence whose
/// neighbours are all separated, or all adjacent, keeps its spacing whatever
/// this says; one with fewer than two items, or with both kinds of gap, gets
/// this spacing. Set it with [`Document::with_spacing`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Spacing {
    /// Items follow each other directly.
    #[default]
    Tight,
    /// One blank line between items.
    Blank,
}

/// A sequence item taken out of a document by [`Document::take`], with the
/// comments it owns, ready for [`Document::put`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Fragment {
    text: String,
}

impl Fragment {
    /// The item as text, its dash at column 0.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.text
    }
}

/// A splice that removes a sequence item, and the item's own text.
struct Cut {
    from: usize,
    to: usize,
    with: String,
    text: String,
}

/// A read-only view of one node.
#[derive(Clone, Copy, Debug)]
pub struct Node<'a> {
    document: &'a Document,
    id: usize,
}

impl Node<'_> {
    /// Where the value is written.
    #[must_use]
    pub fn value(&self) -> Location {
        let node = &self.document.index.nodes[self.id];
        Location::of(&self.document.source, node.value.clone())
    }

    /// Where the key is written, for a mapping value.
    #[must_use]
    pub fn key(&self) -> Option<Location> {
        let node = &self.document.index.nodes[self.id];
        let key = node.key.as_ref()?;
        Some(Location::of(&self.document.source, key.range.clone()))
    }

    /// The lines this node owns: its comments above, its entry, and its
    /// value, with the blank lines its last block scalar keeps (`|+`).
    #[must_use]
    pub fn owned(&self) -> Location {
        let source = &self.document.source;
        let index = &self.document.index;
        let start = index.owned_start(source, self.id);
        let end = line_end(source, self.document.item_end(self.id));
        Location::of(source, start..end)
    }

    /// How the value is written.
    #[must_use]
    pub fn style(&self) -> Style {
        self.document.index.nodes[self.id].style
    }

    /// How many items or entries a collection holds; 0 for a scalar.
    #[must_use]
    pub fn len(&self) -> usize {
        self.document.index.nodes[self.id].children.len()
    }

    /// Whether a collection is empty, or the node is a scalar.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// The value as written in the source.
    #[must_use]
    pub fn text(&self) -> &str {
        let node = &self.document.index.nodes[self.id];
        &self.document.source[node.value.clone()]
    }
}

/// A YAML document that edits its own text in place.
///
/// Each edit splices the source and parses it again. When the result does not
/// parse, the edit returns an error and the document keeps its old text.
#[derive(Clone, Debug)]
pub struct Document {
    source: String,
    index: Index,
    compact: bool,
    spacing: Spacing,
    #[cfg(feature = "serde")]
    quotes: crate::Quotes,
    /// What a [`Document::batch`] has recorded; `None` outside a batch.
    pending: Option<Pending>,
}

/// The edits a batch has recorded against the text it began with.
#[derive(Clone, Debug, Default)]
struct Pending {
    /// The splices, in call order.
    splices: Vec<Splice>,
    /// The keys inserted so far, by mapping node, which the starting index
    /// cannot show to the next insert.
    #[cfg(feature = "serde")]
    keys: Vec<(usize, String)>,
}

/// One recorded edit: the bytes `from..to` of the text the batch began with,
/// and what replaces them.
#[derive(Clone, Debug)]
struct Splice {
    from: usize,
    to: usize,
    with: String,
}

impl Document {
    /// Index one YAML document.
    ///
    /// # Errors
    ///
    /// [`Error::Parse`] for text that is not YAML, and [`Error::Unsupported`]
    /// for `\r\n` line breaks or a stream of several documents.
    pub fn parse(source: impl Into<String>) -> Result<Self, Error> {
        let source = source.into();
        if source.contains('\r') {
            return Err(Error::Unsupported {
                path: Path::root(),
                what: "edit text with carriage returns",
            });
        }
        let index = Index::build(&source)?;
        let compact = compact_lists(&source, &index);
        Ok(Self {
            source,
            index,
            compact,
            spacing: Spacing::default(),
            #[cfg(feature = "serde")]
            quotes: crate::Quotes::default(),
            pending: None,
        })
    }

    /// Use `spacing` for new items where a sequence does not settle it.
    #[must_use]
    pub fn with_spacing(mut self, spacing: Spacing) -> Self {
        self.spacing = spacing;
        self
    }

    /// Quote every string this document writes as `quotes` says.
    #[cfg(feature = "serde")]
    #[must_use]
    pub fn with_quotes(mut self, quotes: crate::Quotes) -> Self {
        self.quotes = quotes;
        self
    }

    /// Run several edits and parse the result once, at the end.
    ///
    /// Inside `edits`, every path names the document as it was when the batch
    /// began, and reads see that text: an edit does not move the nodes the
    /// next one names. The edits must touch separate bytes; two inserts at
    /// one place land in call order. [`Document::reindent`] proves its result
    /// with a parse, so it is refused inside a batch. When `edits` fails, or
    /// the result does not parse, the document keeps its old text. A batch
    /// inside a batch joins it.
    ///
    /// `edits` may return the caller's own error type, so a caller that wraps
    /// a document can run its own steps inside one batch.
    ///
    /// # Errors
    ///
    /// Whatever `edits` returns, [`Error::Unsupported`] for edits that
    /// overlap, and [`Error::Invalid`] when the result does not parse.
    pub fn batch<R, E: From<Error>>(
        &mut self,
        edits: impl FnOnce(&mut Self) -> Result<R, E>,
    ) -> Result<R, E> {
        if self.pending.is_some() {
            return edits(self);
        }
        self.pending = Some(Pending::default());
        let result = edits(self);
        let pending = self.pending.take().unwrap_or_default();
        let value = result?;
        self.apply(pending.splices)?;
        Ok(value)
    }

    /// The current text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.source
    }

    /// The current text, owned.
    #[must_use]
    pub fn into_string(self) -> String {
        self.source
    }

    /// The node at a path.
    #[must_use]
    pub fn node(&self, path: &Path) -> Option<Node<'_>> {
        self.index.find(path).map(|id| Node { document: self, id })
    }

    /// Where the value at a path is written.
    #[must_use]
    pub fn locate(&self, path: &Path) -> Option<Location> {
        self.node(path).map(|node| node.value())
    }

    /// Where the value at a path is written, or where its nearest existing
    /// ancestor is, for a path that names something missing. A schema error
    /// about a missing key lands on the mapping that lacks it.
    #[must_use]
    pub fn locate_nearest(&self, path: &Path) -> Option<Location> {
        let id = match self.index.walk(path) {
            Ok(id) => id,
            Err((id, _)) if !self.index.nodes.is_empty() => id,
            Err(_) => return None,
        };
        Some(Node { document: self, id }.value())
    }

    /// Remove the node at a path with its key, the comments it owns, and its
    /// line break. Neighbours keep every byte, including their comments.
    /// An item of a flow sequence written on one line goes with one
    /// separator, and the last one leaves `[]`.
    ///
    /// # Errors
    ///
    /// [`Error::NoNode`], [`Error::Unsupported`] for a node inside a flow
    /// mapping or a flow sequence that spans lines, or the only entry of a
    /// mapping, and [`Error::Invalid`].
    pub fn remove(&mut self, path: &Path) -> Result<(), Error> {
        let id = self.id(path)?;
        let parent = self.parent(path, id)?;
        match self.index.nodes[parent].style {
            Style::BlockSequence => {
                let cut = self.cut(path, id, parent)?;
                self.commit_splice(cut.from, cut.to, &cut.with)
            }
            Style::BlockMapping => self.remove_entry(path, id, parent),
            Style::FlowSequence => self.remove_flow_item(path, id, parent),
            Style::Plain
            | Style::SingleQuoted
            | Style::DoubleQuoted
            | Style::Literal
            | Style::Folded
            | Style::Alias
            | Style::FlowMapping => Err(Error::Unsupported {
                path: path.clone(),
                what: "remove from a flow mapping",
            }),
        }
    }

    /// Take a sequence item out, with the comments it owns and the blank
    /// lines its last block scalar keeps (`|+`).
    ///
    /// # Errors
    ///
    /// [`Error::NoNode`], [`Error::WrongKind`] when the parent is not a block
    /// sequence, [`Error::Unsupported`] for the only item of a sequence with
    /// no key, and [`Error::Invalid`].
    pub fn take(&mut self, path: &Path) -> Result<Fragment, Error> {
        let id = self.id(path)?;
        let parent = self.parent(path, id)?;
        if self.index.nodes[parent].style != Style::BlockSequence {
            return Err(Error::WrongKind {
                path: path.clone(),
                expected: "an item of a block sequence",
            });
        }
        let cut = self.cut(path, id, parent)?;
        self.commit_splice(cut.from, cut.to, &cut.with)?;
        Ok(Fragment { text: cut.text })
    }

    /// Insert a fragment into a sequence at an index, re-indented to it, and
    /// spaced as [`Spacing`] describes.
    ///
    /// # Errors
    ///
    /// [`Error::NoNode`] for a missing sequence or an index past its end,
    /// [`Error::WrongKind`] when the path is not a sequence or an empty value,
    /// and [`Error::Invalid`].
    pub fn put(&mut self, path: &Path, index: usize, fragment: &Fragment) -> Result<(), Error> {
        let id = self.id(path)?;
        let node = &self.index.nodes[id];
        let lines: Vec<String> = fragment.text.lines().map(str::to_owned).collect();
        match node.style {
            Style::BlockSequence => {
                let dash = node.children.first().map_or_else(
                    || column(&self.source, node.value.start),
                    |&first| column(&self.source, self.dash(first)),
                );
                let block = join(&indent(&lines, dash));
                let gap = match self.item_spacing(id) {
                    Spacing::Tight => "",
                    Spacing::Blank => NEWLINE,
                };
                let kept = lines.last().is_some_and(|line| line.trim().is_empty());
                if let Some(&child) = node.children.get(index) {
                    let at = self.index.owned_start(&self.source, child);
                    let gap = if kept { "" } else { gap };
                    let text = format!("{block}{LF}{gap}");
                    self.commit_splice(at, at, &text)
                } else if let Some(&last) = node.children.last()
                    && index == node.children.len()
                {
                    let at = line_end(&self.source, self.item_end(last));
                    let lead = if self.source[..at].ends_with(LF) {
                        ""
                    } else {
                        NEWLINE
                    };
                    let gap = if self.ends_in_kept_lines(last) {
                        ""
                    } else {
                        gap
                    };
                    let to = if kept {
                        blank_run_end(&self.source, at)
                    } else {
                        at
                    };
                    let text = format!("{lead}{gap}{block}{LF}");
                    self.commit_splice(at, to, &text)
                } else {
                    Err(Error::NoNode {
                        path: path.clone().index(index),
                    })
                }
            }
            Style::FlowSequence | Style::Plain
                if node.children.is_empty() && self.is_empty_value(id) =>
            {
                if index != 0 {
                    return Err(Error::NoNode {
                        path: path.clone().index(index),
                    });
                }
                self.fill_empty(path, id, &lines)
            }
            Style::Plain
            | Style::SingleQuoted
            | Style::DoubleQuoted
            | Style::Literal
            | Style::Folded
            | Style::Alias
            | Style::FlowSequence
            | Style::BlockMapping
            | Style::FlowMapping => Err(Error::WrongKind {
                path: path.clone(),
                expected: "a block sequence or an empty value",
            }),
        }
    }

    /// The spacing a sequence's items already share, or the document's.
    fn item_spacing(&self, sequence: usize) -> Spacing {
        let mut gaps = self.index.nodes[sequence].children.windows(2).map(|pair| {
            let after = line_end(&self.source, self.item_end(pair[0]));
            has_blank_line(
                &self.source,
                after,
                self.index.owned_start(&self.source, pair[1]),
            )
        });
        match gaps.next() {
            Some(first) if gaps.all(|gap| gap == first) => {
                if first {
                    Spacing::Blank
                } else {
                    Spacing::Tight
                }
            }
            _ => self.spacing,
        }
    }

    fn id(&self, path: &Path) -> Result<usize, Error> {
        self.index
            .find(path)
            .ok_or_else(|| Error::NoNode { path: path.clone() })
    }

    fn parent(&self, path: &Path, id: usize) -> Result<usize, Error> {
        self.index.nodes[id]
            .parent
            .ok_or_else(|| Error::Unsupported {
                path: path.clone(),
                what: "remove or take the root",
            })
    }

    /// Whether a node is `[]`, `~`, `null`, or nothing at all.
    fn is_empty_value(&self, id: usize) -> bool {
        let node = &self.index.nodes[id];
        match node.style {
            Style::FlowSequence => node.children.is_empty(),
            Style::Plain => matches!(self.source[node.value.clone()].trim(), "" | "~" | "null"),
            Style::SingleQuoted
            | Style::DoubleQuoted
            | Style::Literal
            | Style::Folded
            | Style::Alias
            | Style::BlockSequence
            | Style::BlockMapping
            | Style::FlowMapping => false,
        }
    }

    /// The byte after a mapping value's `:`, and the key's column. The `:`
    /// is looked for between the key and its value, or on the key's line
    /// for an empty value, and never inside a comment.
    fn colon(&self, path: &Path, id: usize) -> Result<(usize, usize), Error> {
        let key = self.index.nodes[id]
            .key
            .as_ref()
            .ok_or_else(|| Error::Unsupported {
                path: path.clone(),
                what: "fill an empty value that has no key",
            })?;
        let value = &self.index.nodes[id].value;
        let limit = if value.start > key.range.end {
            value.start
        } else {
            line_end(&self.source, key.range.end)
        };
        let after = &self.source[key.range.end..limit];
        let mut line_offset = 0;
        let mut found = None;
        for line in after.split_inclusive(LF) {
            let code = comment_start(line).map_or(line, |comment| &line[..comment]);
            if let Some(colon) = code.find(':') {
                found = Some(line_offset + colon);
                break;
            }
            line_offset += line.len();
        }
        let offset = found.ok_or_else(|| Error::Unsupported {
            path: path.clone(),
            what: "write the value of a key with no ':' after it",
        })?;
        Ok((
            key.range.end + offset + 1,
            column(&self.source, key.range.start),
        ))
    }

    /// Write `lines` as the items of an empty value (`[]`, `~`, `null`, or
    /// nothing) under its key. The key line keeps its node properties and
    /// comment; an empty value on a later line is replaced with its line, and
    /// its own properties and comment move up to the key line. A tag other
    /// than `!!seq`, short or verbatim, would contradict the items, so it is
    /// refused.
    fn fill_empty(&mut self, path: &Path, id: usize, lines: &[String]) -> Result<(), Error> {
        let (colon, key_column) = self.colon(path, id)?;
        let value = self.index.nodes[id].value.clone();
        let key_line_end = line_end(&self.source, colon);
        let key_line_content =
            key_line_end - usize::from(self.source[..key_line_end].ends_with(LF));
        let (mut properties, mut comment) = self.key_line_parts(colon, Some(&value));
        let end = if value.is_empty() || value.end <= key_line_content {
            key_line_end
        } else {
            if value.start > key_line_content {
                let value_line = line_start(&self.source, value.start);
                if !self.source[key_line_end..value_line].trim().is_empty() {
                    return Err(Error::Unsupported {
                        path: path.clone(),
                        what: "fill an empty value with lines between it and its key",
                    });
                }
                properties = spaced(&[&properties, self.source[value_line..value.start].trim()]);
            }
            let end = line_end(&self.source, value.end);
            comment = spaced(&[&comment, self.source[value.end..end].trim()]);
            end
        };
        if let Some(tag) = properties.split_whitespace().find(|property| {
            property.starts_with('!') && !matches!(*property, "!!seq" | "!<tag:yaml.org,2002:seq>")
        }) {
            return Err(Error::TagMismatch {
                path: path.clone(),
                tag: tag.to_owned(),
            });
        }
        let end = if lines.last().is_some_and(|line| line.trim().is_empty()) {
            blank_run_end(&self.source, end)
        } else {
            end
        };
        let head = spaced(&[&properties, &comment]);
        let step = if self.compact { 0 } else { 2 };
        let placed: Vec<String> = std::iter::once(if head.is_empty() {
            head
        } else {
            format!(" {head}")
        })
        .chain(indent(lines, key_column + step))
        .collect();
        self.commit_splice(colon, end, &terminated(&join(&placed)))
    }

    /// What follows a key's `:` on its line, apart from the part of `value`
    /// written there: node properties such as `&anchor` or `!!tag`, and a
    /// trailing comment.
    fn key_line_parts(&self, colon: usize, value: Option<&Range<usize>>) -> (String, String) {
        let end = line_end(&self.source, colon);
        let mut rest = self.source[colon..end].trim_end_matches(LF).to_owned();
        let line_content = colon + rest.len();
        if let Some(value) = value.filter(|value| (colon..line_content).contains(&value.start)) {
            rest.replace_range(value.start - colon..value.end.min(line_content) - colon, "");
        }
        let (properties, comment) = rest.split_at(comment_start(&rest).unwrap_or(rest.len()));
        (properties.trim().to_owned(), comment.trim().to_owned())
    }

    fn remove_entry(&mut self, path: &Path, id: usize, parent: usize) -> Result<(), Error> {
        let siblings = &self.index.nodes[parent].children;
        if siblings.len() == 1 {
            return Err(Error::Unsupported {
                path: path.clone(),
                what: "remove the only entry of a mapping",
            });
        }
        let entry = self.index.entry_start(id);
        if starts_line(&self.source, entry) {
            let previous_keeps = siblings
                .iter()
                .position(|&sibling| sibling == id)
                .and_then(|at| at.checked_sub(1))
                .is_some_and(|at| self.ends_in_kept_lines(siblings[at]));
            let from = self.index.owned_start(&self.source, id);
            let to = line_end(&self.source, self.item_end(id));
            let to = if previous_keeps {
                blank_run_end(&self.source, to)
            } else {
                to
            };
            return self.commit_splice(from, to, "");
        }
        let Some(&[_, next]) = siblings.windows(2).find(|pair| pair[0] == id) else {
            return Err(Error::Unsupported {
                path: path.clone(),
                what: "remove the last key of a mapping from its dash line",
            });
        };
        let next_entry = self.index.entry_start(next);
        let comments_start = self.index.owned_start(&self.source, next);
        let comments_end = line_start(&self.source, next_entry);
        let line = line_start(&self.source, entry);
        let dash_column = column(&self.source, dash_before(&self.source, entry));
        let comments: Vec<String> = self.source[comments_start..comments_end]
            .lines()
            .map(|comment| comment.trim_start().to_owned())
            .collect();
        let mut with: String = indent(&comments, dash_column)
            .iter()
            .map(|comment| terminated(comment))
            .collect();
        with.push_str(&self.source[line..entry]);
        self.commit_splice(line, next_entry, &with)
    }

    /// The splice that takes a sequence item out, and the item's text
    /// dedented to its dash. Between blank-separated items the blank line
    /// goes with the item below it, except for the last item, which takes the
    /// one above. Taking the only item leaves `key: []`.
    fn cut(&self, path: &Path, id: usize, parent: usize) -> Result<Cut, Error> {
        let source = &self.source;
        let start = self.index.owned_start(source, id);
        let end = line_end(source, self.item_end(id));
        let text = dedent(&source[start..end], column(source, self.dash(id)));
        let siblings = &self.index.nodes[parent].children;
        if siblings.len() == 1 {
            #[expect(
                clippy::map_err_ignore,
                reason = "a missing key is reported as the edit the document cannot do, which names the same path"
            )]
            let (colon, _) = self.colon(path, parent).map_err(|_| Error::Unsupported {
                path: path.clone(),
                what: "take the only item of a sequence with no key",
            })?;
            let (properties, comment) = self.key_line_parts(colon, None);
            let with = format!(" {}{LF}", spaced(&[&properties, "[]", &comment]));
            return Ok(Cut {
                from: colon,
                to: end,
                with,
                text,
            });
        }
        let (mut from, mut to) = (start, end);
        let previous_keeps = siblings
            .iter()
            .position(|&sibling| sibling == id)
            .and_then(|at| at.checked_sub(1))
            .is_some_and(|at| self.ends_in_kept_lines(siblings[at]));
        if siblings.last() == Some(&id) && !previous_keeps {
            while from > 0 {
                let previous = line_start(source, from - 1);
                if !source[previous..from].trim().is_empty() {
                    break;
                }
                from = previous;
            }
        } else {
            to = blank_run_end(source, to);
        }
        Ok(Cut {
            from,
            to,
            with: String::new(),
            text,
        })
    }

    fn remove_flow_item(&mut self, path: &Path, id: usize, parent: usize) -> Result<(), Error> {
        let sequence = self.index.nodes[parent].value.clone();
        if self.source[sequence.clone()].contains(LF) {
            return Err(Error::Unsupported {
                path: path.clone(),
                what: "remove from a flow sequence that spans lines",
            });
        }
        let children = &self.index.nodes[parent].children;
        let at = children
            .iter()
            .position(|&child| child == id)
            .unwrap_or_default();
        let (from, to) = match (at.checked_sub(1), children.get(at + 1)) {
            (_, Some(_)) => (
                self.flow_item_start(parent, at),
                self.flow_item_start(parent, at + 1),
            ),
            (Some(previous), None) => (
                self.index.nodes[children[previous]].value.end,
                self.index.nodes[id].value.end,
            ),
            (None, None) => (sequence.start + 1, sequence.end - 1),
        };
        self.commit_splice(from, to, "")
    }

    /// Where item `k` of a one-line flow sequence begins: after the `[` or
    /// the comma before it, and the spaces after that.
    fn flow_item_start(&self, sequence: usize, k: usize) -> usize {
        let node = &self.index.nodes[sequence];
        let after = match k.checked_sub(1) {
            None => node.value.start + 1,
            Some(previous) => {
                let end = self.index.nodes[node.children[previous]].value.end;
                self.source[end..]
                    .find(',')
                    .map_or(end, |comma| end + comma + 1)
            }
        };
        let rest = &self.source[after..];
        after + rest.len() - rest.trim_start_matches(' ').len()
    }

    /// Whether a node's text ends in a block scalar that keeps its trailing
    /// line breaks (`|+` or `>+`). The blank lines after such a node are part
    /// of its value, so they cannot stay behind when it moves.
    fn ends_in_kept_lines(&self, id: usize) -> bool {
        let mut last = id;
        while let Some(&child) = self.index.nodes[last].children.last() {
            last = child;
        }
        self.keeps_trailing_lines(last)
    }

    /// Where a node's text ends: the end of its value, or of the blank lines
    /// its last block scalar keeps (`|+`), which belong to it. No other blank
    /// line may follow such a node, since YAML would read it into the value.
    fn item_end(&self, id: usize) -> usize {
        let mut last = id;
        while let Some(&child) = self.index.nodes[last].children.last() {
            last = child;
        }
        self.kept_end(last).max(self.index.nodes[id].value.end)
    }

    /// Whether a node is a block scalar with keep chomping (`|+` or `>+`).
    fn keeps_trailing_lines(&self, id: usize) -> bool {
        let node = &self.index.nodes[id];
        matches!(node.style, Style::Literal | Style::Folded)
            && self.source[node.value.start..]
                .split_whitespace()
                .next()
                .is_some_and(|header| header.contains('+'))
    }

    /// Where a node's value ends, including the blank lines after a block
    /// scalar that keeps them: the index trims those, but they are part of
    /// its value, so a replace must take them.
    fn kept_end(&self, id: usize) -> usize {
        let mut end = self.index.nodes[id].value.end;
        if !self.keeps_trailing_lines(id) {
            return end;
        }
        loop {
            let next = line_end(&self.source, end);
            if next >= self.source.len() {
                break;
            }
            let line = &self.source[next..line_end(&self.source, next)];
            if !line.trim().is_empty() {
                break;
            }
            end = next + line.trim_end_matches(LF).len();
        }
        end
    }

    /// The byte of the `-` that starts a sequence item.
    fn dash(&self, id: usize) -> usize {
        dash_before(&self.source, self.index.nodes[id].value.start)
    }

    /// Replace the bytes `from..to` and parse the result, or record the
    /// splice inside a batch. A splice that writes the bytes already there
    /// changes nothing and costs no parse.
    fn commit_splice(&mut self, from: usize, to: usize, with: &str) -> Result<(), Error> {
        if self.source[from..to] == *with {
            return Ok(());
        }
        if let Some(pending) = &mut self.pending {
            pending.splices.push(Splice {
                from,
                to,
                with: with.to_owned(),
            });
            return Ok(());
        }
        let edited = [&self.source[..from], with, &self.source[to..]].concat();
        let index = Index::build(&edited).map_err(|error| Error::Invalid {
            message: error.to_string(),
        })?;
        self.commit(edited, index);
        Ok(())
    }

    /// Apply a batch's splices to the text it began with, in byte order with
    /// call order kept at one place, and parse once.
    fn apply(&mut self, mut splices: Vec<Splice>) -> Result<(), Error> {
        if splices.is_empty() {
            return Ok(());
        }
        splices.sort_by_key(|splice| (splice.from, splice.to));
        let mut edited = String::with_capacity(self.source.len());
        let mut at = 0;
        for splice in &splices {
            if splice.from < at {
                return Err(Error::Unsupported {
                    path: Path::root(),
                    what: "apply overlapping edits in one batch",
                });
            }
            edited.push_str(&self.source[at..splice.from]);
            edited.push_str(&splice.with);
            at = splice.to;
        }
        edited.push_str(&self.source[at..]);
        let index = Index::build(&edited).map_err(|error| Error::Invalid {
            message: error.to_string(),
        })?;
        self.commit(edited, index);
        Ok(())
    }

    fn commit(&mut self, source: String, index: Index) {
        self.compact = compact_lists(&source, &index);
        self.source = source;
        self.index = index;
    }
}

/// A document prints as its current text.
impl fmt::Display for Document {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.source)
    }
}

/// Whether the file writes list items at their key's column rather than
/// under it, judged from the first block list that is a mapping value. The
/// parser starts such a list's span at its first item's value, so the column
/// is read from that item's dash.
fn compact_lists(source: &str, index: &Index) -> bool {
    index
        .nodes
        .iter()
        .find(|node| node.style == Style::BlockSequence && node.key.is_some())
        .and_then(|node| {
            let key = node.key.as_ref()?;
            let first = index.nodes.get(*node.children.first()?)?;
            let dash = dash_before(source, first.value.start);
            Some(column(source, dash) == column(source, key.range.start))
        })
        .unwrap_or(false)
}

/// The start of the first line at or after `at` (a line start) that is not
/// blank. Blank lines that would land after a block scalar keeping its
/// trailing lines are dropped, because YAML would read them into it.
fn blank_run_end(source: &str, mut at: usize) -> usize {
    while at < source.len() && source[at..line_end(source, at)].trim().is_empty() {
        at = line_end(source, at);
    }
    at
}

#[cfg(test)]
mod tests {
    use indoc::indoc;

    use super::Document;
    use crate::Error;

    #[test]
    fn an_edit_that_would_not_parse_leaves_the_text_unchanged() {
        let source = indoc! {"
            a: 1
            b: 2
        "};
        let mut document = Document::parse(source).unwrap();
        let broken = document.commit_splice(0, 4, "a: [1");
        assert!(matches!(broken, Err(Error::Invalid { .. })));
        assert_eq!(document.as_str(), source);
    }
}