m2-syn 0.2.0

Typed syntax trees, traversal, and parser adapters for Macaulay2
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
use std::hash::{Hash, Hasher};
use std::ops::Index;
use std::sync::Arc;

/// A boundary in a source buffer, as a byte offset.
///
/// Boundaries sit between bytes rather than on them, so a span's `end` is the
/// offset past its last byte and `end - start` is its length. One offset is
/// enough because a span's only job is to index a buffer: line and column are
/// facts about a particular buffer's contents, recovered from it when something
/// needs to *display* a location, and so they belong with the buffer rather
/// than with the span. This is what `rustc` does too — its `Span` stores `lo`
/// and `hi` byte positions and nothing else, and `SourceMap` binary-searches a
/// line table on demand.
pub type Offset = usize;

/// One text change: the text a buffer gains at a point, or the stretch it
/// loses.
///
/// An edit acts on *both* halves of the indexing relation — on source text
/// through [`Self::apply`] and on a [`Span`] through [`Span::shifted`] — and
/// those two actions are designed to agree.
///
/// An insertion carries the text it inserts rather than a pair of boundaries,
/// because the boundaries are recoverable (`at` and `at + text.len()`) while the
/// text is not, and without the text an edit cannot act on a buffer at all.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TextEdit {
    /// Inserts `text` at one byte boundary.
    Insertion {
        /// The insertion boundary.
        at: Offset,
        /// The inserted text.
        text: String,
    },
    /// Deletes one half-open byte range.
    Deletion {
        /// The first deleted byte.
        start: Offset,
        /// The boundary after the last deleted byte.
        end: Offset,
    },
}

impl TextEdit {
    /// Creates an insertion of `text` landing at `at`.
    pub fn insertion(at: Offset, text: impl Into<String>) -> Self {
        Self::Insertion {
            at,
            text: text.into(),
        }
    }

    /// Creates a deletion of the text occupying `start..end`.
    ///
    /// # Panics
    ///
    /// Panics when `start` is greater than `end`.
    pub fn deletion(start: Offset, end: Offset) -> Self {
        assert!(start <= end, "deletion start must not exceed its end");
        Self::Deletion { start, end }
    }

    /// Returns `offset` in the buffer this edit produces.
    ///
    /// For an insertion of width `d` at `s` this is `offset + (offset >= s) * d`
    /// — everything at or after the insertion point moves by the width, and
    /// nothing else moves at all. A deletion is the mirror image, with the text
    /// it covered collapsing onto its start.
    ///
    /// The rule is monotone in `offset`, so applying it to both ends of a range
    /// can never invert them, and only the edit's *width* enters the arithmetic.
    /// Reaching the same result by summing two absolute offsets would
    /// manufacture an overflow the buffer never causes.
    fn shift(&self, offset: Offset) -> Offset {
        match self {
            Self::Insertion { at, text } => {
                if offset >= *at {
                    offset + text.len()
                } else {
                    offset
                }
            }
            Self::Deletion { start, end } => {
                if offset <= *start {
                    offset
                } else if offset <= *end {
                    // Everything the deletion covered now sits at its start.
                    *start
                } else {
                    offset - (end - start)
                }
            }
        }
    }

    /// Returns `source` with this edit applied, or `None` when the edit falls
    /// outside the buffer or across a character boundary.
    pub fn apply(&self, source: &str) -> Option<String> {
        let (cut, resumes) = match self {
            Self::Insertion { at, .. } => (*at, *at),
            Self::Deletion { start, end } => (*start, *end),
        };
        let mut edited = String::from(source.get(..cut)?);
        if let Self::Insertion { text, .. } = self {
            edited.push_str(text);
        }
        edited.push_str(source.get(resumes..)?);
        Some(edited)
    }
}

/// Where syntax originated: a stretch of a buffer, or nowhere at all.
///
/// A zero-width `Range` is `start == end`, which is what the virtual marker
/// nodes use — end of input, the `Adj` between adjacent expressions,
/// an omitted component in `(x,,y)`. Because boundaries sit between bytes,
/// `x..x` already names a position exactly, so such a marker needs no
/// representation of its own. An inverted pair can never be constructed:
/// [`Self::new`] collapses it to `Detached` rather than fabricating a range.
///
#[derive(Debug, Clone)]
struct SourceSlice {
    text: Arc<str>,
    origin: Offset,
}

impl PartialEq for SourceSlice {
    fn eq(&self, other: &Self) -> bool {
        self.origin == other.origin && Arc::ptr_eq(&self.text, &other.text)
    }
}

impl Eq for SourceSlice {}

impl Hash for SourceSlice {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.origin.hash(state);
        std::ptr::hash(Arc::as_ptr(&self.text), state);
    }
}

impl SourceSlice {
    fn covers(&self, start: Offset, end: Offset) -> bool {
        self.origin <= start && end <= self.origin.saturating_add(self.text.len())
    }
}

fn common_source(
    left: Option<&SourceSlice>,
    right: Option<&SourceSlice>,
    start: Offset,
    end: Offset,
) -> Option<SourceSlice> {
    match (left, right) {
        (Some(left), Some(right)) if left == right => Some(left.clone()),
        (Some(source), None) | (None, Some(source)) if source.covers(start, end) => {
            Some(source.clone())
        }
        _ => None,
    }
}

/// Where syntax originated and, when available, the source buffer it indexes.
///
/// Source-derived tokens retain only this value. Their spelling is recovered
/// by slicing the shared source buffer; it is never copied into token or syntax
/// node storage. Spans constructed only from offsets remain useful for
/// diagnostics and synthetic trees, but naturally cannot yield source text.
#[derive(Debug, Clone, Default)]
pub struct Span {
    bounds: Option<(Offset, Offset)>,
    source: Option<SourceSlice>,
}

impl PartialEq for Span {
    fn eq(&self, other: &Self) -> bool {
        self.bounds == other.bounds && self.source == other.source
    }
}

impl Eq for Span {}

impl Hash for Span {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.bounds.hash(state);
        self.source.hash(state);
    }
}

impl Span {
    /// A detached span without source text.
    #[allow(non_upper_case_globals)]
    pub const Detached: Self = Self {
        bounds: None,
        source: None,
    };

    /// Creates a span with no source location.
    pub fn detached() -> Self {
        Self::Detached
    }

    /// Builds a span covering `start..end`, where `end` is the offset past the
    /// last byte.
    ///
    /// An inverted pair collapses to `Detached` rather than fabricating a range.
    pub fn new(start: Offset, end: Offset) -> Self {
        if start <= end {
            Self {
                bounds: Some((start, end)),
                source: None,
            }
        } else {
            Self::Detached
        }
    }

    /// A zero-width span at one boundary, for syntax that occupies no bytes.
    pub fn from_offset(offset: Offset) -> Self {
        Self {
            bounds: Some((offset, offset)),
            source: None,
        }
    }

    /// Attaches the complete source fragment covered by this span.
    ///
    /// Existing source backing is retained. For an offset-only span, `text`
    /// must have the same byte width as the span; its first byte is associated
    /// with the span's opening offset. Detached syntax may also carry a source
    /// fragment, which is useful for programmatically constructed syntax.
    ///
    /// # Panics
    ///
    /// Panics when an offset-only span and `text` have different byte widths.
    pub fn with_text(self, text: impl Into<Arc<str>>) -> Self {
        if self.source.is_some() {
            return self;
        }
        let text = text.into();
        let origin = match self.bounds {
            Some((start, end)) => {
                assert_eq!(
                    end - start,
                    text.len(),
                    "source fragment width must equal its span width"
                );
                start
            }
            None => 0,
        };
        Self {
            source: Some(SourceSlice { text, origin }),
            ..self
        }
    }

    /// Replaces this span's source fragment while retaining its opening
    /// location and updating its width.
    pub fn replaced_text(&self, text: impl Into<Arc<str>>) -> Self {
        let text = text.into();
        let bounds = self
            .start()
            .map(|start| (start, start.saturating_add(text.len())));
        let origin = bounds.map_or(0, |(start, _)| start);
        Self {
            bounds,
            source: Some(SourceSlice { text, origin }),
        }
    }

    /// Creates a source-backed span covering an entire source buffer.
    pub fn source(text: impl Into<Arc<str>>) -> Self {
        let text = text.into();
        let end = text.len();
        Self {
            bounds: Some((0, end)),
            source: Some(SourceSlice { text, origin: 0 }),
        }
    }

    /// Returns a subspan at byte offsets relative to this span's opening.
    ///
    /// The returned span shares the same source buffer. Invalid or detached
    /// ranges become detached spans.
    pub fn subspan(&self, start: Offset, end: Offset) -> Self {
        let Some((outer_start, outer_end)) = self.bounds else {
            return Self::Detached;
        };
        let Some(start) = outer_start.checked_add(start) else {
            return Self::Detached;
        };
        let Some(end) = outer_start.checked_add(end) else {
            return Self::Detached;
        };
        if start > end || end > outer_end {
            return Self::Detached;
        }
        Self {
            bounds: Some((start, end)),
            source: self.source.clone(),
        }
    }

    /// Returns the exact source text covered by this span when source backing
    /// is available.
    pub fn text(&self) -> Option<&str> {
        let source = self.source.as_ref()?;
        match self.bounds {
            Some((start, end)) => source
                .text
                .get(start.checked_sub(source.origin)?..end.checked_sub(source.origin)?),
            None => Some(&source.text),
        }
    }

    /// Reports whether both spans slice the same shared source buffer.
    pub fn shares_source(&self, other: &Self) -> bool {
        match (&self.source, &other.source) {
            (Some(left), Some(right)) => Arc::ptr_eq(&left.text, &right.text),
            _ => false,
        }
    }

    /// The boundary this span opens at, or `None` when it is detached.
    pub fn start(&self) -> Option<Offset> {
        self.bounds().map(|(start, _)| start)
    }

    /// The boundary this span closes at, past its last byte.
    pub fn end(&self) -> Option<Offset> {
        self.bounds().map(|(_, end)| end)
    }

    /// The boundaries this span lies between.
    pub fn bounds(&self) -> Option<(Offset, Offset)> {
        self.bounds
    }

    /// How many bytes this span covers, or `None` when it is detached.
    pub fn len(&self) -> Option<usize> {
        self.bounds().map(|(start, end)| end - start)
    }

    /// Reports whether this span covers no bytes, as the virtual marker tokens
    /// do. Detached syntax covers no bytes either.
    pub fn is_empty(&self) -> bool {
        self.len().unwrap_or(0) == 0
    }

    /// Returns whether `offset` lies inside this span, excluding its closing
    /// boundary.
    ///
    /// Boundaries sit between bytes, so the closing one belongs to whatever
    /// follows: in `a+b` the boundary after `a` is contained by `+`, not by `a`.
    /// This is the predicate to use when exactly one span must own a position. A
    /// zero-width span contains nothing, having no interior.
    pub fn contains(&self, offset: Offset) -> bool {
        self.bounds()
            .is_some_and(|(start, end)| start <= offset && offset < end)
    }

    /// Returns whether `offset` lies inside this span or on either boundary.
    ///
    /// Adjacent spans both touch the boundary between them, so this answers
    /// "which syntax is the cursor at" rather than "which syntax owns this
    /// position" — a cursor just past an identifier still touches it, and a
    /// zero-width marker is touched at its own position.
    pub fn touches(&self, offset: Offset) -> bool {
        self.bounds()
            .is_some_and(|(start, end)| start <= offset && offset <= end)
    }

    /// Returns this span's position in the buffer produced by `edit`.
    ///
    /// Both boundaries move under the same rule, so nothing here decides which
    /// side of an insertion a boundary belongs to. Because that rule is monotone
    /// the pair can never invert, and a zero-width span needs no special case:
    /// its two equal boundaries stay equal.
    ///
    /// Text inserted at a span's *closing* boundary therefore extends the span,
    /// which is what an unclosed group wants — it keeps reaching whatever now
    /// follows it. A span wholly removed by a deletion collapses to a zero-width
    /// span at the deletion point rather than becoming [`Self::Detached`],
    /// because it still records where the syntax used to be.
    pub fn shifted(&self, edit: &TextEdit) -> Self {
        match self.bounds() {
            None => Self::Detached,
            Some((start, end)) => Self::new(edit.shift(start), edit.shift(end)),
        }
    }

    /// The smallest span covering both, ignoring detached operands.
    pub fn join(&self, other: Self) -> Self {
        match (self.bounds(), other.bounds()) {
            (None, _) => other,
            (_, None) => self.clone(),
            (Some((start, end)), Some((start2, end2))) => {
                let start = start.min(start2);
                let end = end.max(end2);
                let bounds = Some((start, end));
                let source = common_source(self.source.as_ref(), other.source.as_ref(), start, end);
                Self { bounds, source }
            }
        }
    }

    /// The overlap of both, or `Detached` when they do not overlap.
    pub fn meet(&self, other: Self) -> Self {
        match (self.bounds(), other.bounds()) {
            (Some((start, end)), Some((start2, end2))) => {
                let start = start.max(start2);
                let end = end.min(end2);
                if start < end {
                    let source =
                        common_source(self.source.as_ref(), other.source.as_ref(), start, end);
                    Self {
                        bounds: Some((start, end)),
                        source,
                    }
                } else {
                    Self::Detached
                }
            }
            _ => Self::Detached,
        }
    }

    /// Joins every span in an iterator, ignoring detached spans.
    pub fn join_all(spans: impl IntoIterator<Item = Span>) -> Self {
        spans
            .into_iter()
            .reduce(|left, right| left.join(right))
            .unwrap_or_default()
    }
}

/// Indexes the buffer, panicking when the span does not name text in it.
///
/// This is legal despite `Index` and `str` both being foreign because [`Span`]
/// is local, and it cannot overlap `str`'s own
/// `impl<I: SliceIndex<str>> Index<I> for str` because `Span` does not implement
/// `SliceIndex`.
impl Index<Span> for str {
    type Output = str;

    fn index(&self, span: Span) -> &str {
        span.bounds()
            .and_then(|(start, end)| self.get(start..end))
            .unwrap_or_else(|| panic!("{span:?} does not name text in this buffer"))
    }
}

impl Index<&Span> for str {
    type Output = str;

    fn index(&self, span: &Span) -> &str {
        span.bounds()
            .and_then(|(start, end)| self.get(start..end))
            .unwrap_or_else(|| panic!("{span:?} does not name text in this buffer"))
    }
}

/// `String` needs its own implementation rather than reaching `str`'s through
/// deref: it already has an `Index` impl of its own, so operator resolution stops
/// there instead of falling through.
impl Index<Span> for String {
    type Output = str;

    fn index(&self, span: Span) -> &str {
        self.as_str().index(span)
    }
}

impl Index<&Span> for String {
    type Output = str;

    fn index(&self, span: &Span) -> &str {
        self.as_str().index(span)
    }
}

/// A syntax value that reports its source extent.
pub trait Spanned {
    /// Returns the smallest span covering this value.
    fn span(&self) -> Span;
}

impl Spanned for Span {
    fn span(&self) -> Span {
        self.clone()
    }
}

impl<T: Spanned + ?Sized> Spanned for &T {
    fn span(&self) -> Span {
        (*self).span()
    }
}

impl<T: Spanned + ?Sized> Spanned for Box<T> {
    fn span(&self) -> Span {
        self.as_ref().span()
    }
}

impl<T: Spanned> Spanned for Option<T> {
    fn span(&self) -> Span {
        self.as_ref().map_or_else(Span::detached, Spanned::span)
    }
}

impl<T: Spanned> Spanned for Vec<T> {
    fn span(&self) -> Span {
        Span::join_all(self.iter().map(Spanned::span))
    }
}

impl<A: Spanned, B: Spanned> Spanned for (A, B) {
    fn span(&self) -> Span {
        self.0.span().join(self.1.span())
    }
}

impl<T: Spanned> Spanned for &[T] {
    fn span(&self) -> Span {
        Span::join_all(self.iter().map(Spanned::span))
    }
}

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

    /// The span of `text` starting at `start`, as the lexer would emit it:
    /// boundaries around the text, so `end` is one past its last byte.
    fn span_of(start: Offset, text: &str) -> Span {
        Span::new(start, start + text.len())
    }

    #[test]
    fn boundaries_are_gaps_so_adjacent_spans_never_own_the_same_position() {
        // `a+b`: the gap at byte 1 closes `a` and opens `+`.
        let a = span_of(0, "a");
        let plus = span_of(1, "+");

        assert!(!a.contains(1));
        assert!(plus.contains(1));
        // Both touch it, which is what a cursor query wants.
        assert!(a.touches(1));
        assert!(plus.touches(1));
    }

    #[test]
    fn a_marker_token_contains_nothing_but_touches_its_own_position() {
        let eof = Span::from_offset(3);

        assert!(eof.is_empty());
        assert!(!eof.contains(3));
        assert!(eof.touches(3));
    }

    /// An insertion of width `d` at `s` sends `x` to `x + (x >= s) * d`, and
    /// nothing else about it varies — there is no side to choose.
    #[test]
    fn an_insertion_moves_exactly_the_offsets_at_or_after_it() {
        let insert = TextEdit::insertion(3, "xy");

        assert_eq!(insert.shift(2), 2);
        // At the insertion point, so it travels — which is what a marker token
        // at end of input wants, staying past everything typed at it.
        assert_eq!(insert.shift(3), 5);
        assert_eq!(insert.shift(4), 6);
    }

    /// Shifting is monotone, so the two ends of a span can never cross and a
    /// zero-width span stays zero-width without needing a special case.
    #[test]
    fn shifting_never_inverts_a_span() {
        let edits = [TextEdit::insertion(3, "xy"), TextEdit::deletion(2, 5)];

        for edit in &edits {
            for start in 0..8 {
                for end in start..8 {
                    let shifted = Span::new(start, end).shifted(edit);
                    assert_ne!(shifted, Span::Detached, "{start}..{end} under {edit:?}");
                }
            }
            assert!(Span::from_offset(3).shifted(edit).is_empty());
        }
    }

    #[test]
    fn insertion_at_a_span_boundary_moves_it_or_extends_the_span() {
        let span = span_of(2, "xy");

        // At byte 2, the span's own start: both ends clear the threshold, so the
        // span moves whole and the new text lands before it.
        assert_eq!(
            span.shifted(&TextEdit::insertion(2, "AB")),
            span_of(4, "xy")
        );
        // At byte 4, the span's own closing boundary: only the end clears it, so
        // the span extends to keep reaching what now follows.
        assert_eq!(span.shifted(&TextEdit::insertion(4, "AB")), Span::new(2, 6));
    }

    #[test]
    fn insertion_strictly_inside_a_span_extends_it() {
        let span = span_of(0, "abcd");

        assert_eq!(
            span.shifted(&TextEdit::insertion(2, "efg")),
            span_of(0, "abcdefg")
        );
    }

    #[test]
    fn text_after_a_deletion_moves_back_and_covered_text_collapses() {
        let deleted = TextEdit::deletion(1, 3);

        // Wholly after: shifts back by the deleted width.
        assert_eq!(span_of(4, "z").shifted(&deleted), span_of(2, "z"));
        // Wholly before: untouched.
        assert_eq!(span_of(0, "a").shifted(&deleted), span_of(0, "a"));
        // Wholly covered: collapses to where it used to be, not detached.
        let covered = span_of(1, "xy").shifted(&deleted);
        assert_eq!(covered, Span::from_offset(1));
        assert!(covered.is_empty());
    }

    #[test]
    fn a_detached_span_is_unaffected_by_edits() {
        assert_eq!(
            Span::detached().shifted(&TextEdit::insertion(0, "abc")),
            Span::detached()
        );
    }

    /// Offsets near the top of the range are ordinary buffer positions, so
    /// shifting must stay within the edit's own width rather than summing two
    /// absolute offsets.
    #[test]
    fn shifting_late_offsets_does_not_overflow() {
        let span = Span::new(usize::MAX - 4, usize::MAX - 3);
        // Two bytes inserted far below the span.
        let insert = TextEdit::insertion(1, "ab");

        // The displacement is two bytes. Summing the absolute offsets instead
        // would compute `(usize::MAX - 4) + 3`, overflowing.
        assert_eq!(
            span.shifted(&insert),
            Span::new(usize::MAX - 2, usize::MAX - 1)
        );
    }

    /// `shift` moves its argument whenever the edit has any width, so leaving
    /// earlier text alone rests entirely on its guards.
    #[test]
    fn text_before_an_edit_never_moves() {
        let edits = [TextEdit::insertion(8, "AB"), TextEdit::deletion(8, 10)];
        let earlier = [
            span_of(5, "ab"),
            Span::new(0, 2),
            // Ends just short of the edit. A span ending *exactly* at an
            // insertion extends instead, which
            // `insertion_at_a_span_boundary_moves_it_or_extends_the_span`
            // covers.
            Span::new(6, 7),
            // A zero-width marker before the edit.
            Span::from_offset(1),
        ];

        for edit in &edits {
            for span in &earlier {
                assert_eq!(span.shifted(edit), *span, "{span:?} moved under {edit:?}");
            }
        }
    }

    /// Inserting at a span's own start carries the whole span forward: both
    /// endpoints move by the edit's width, so the span keeps its length and the
    /// new text lands outside it.
    #[test]
    fn inserting_at_a_span_start_moves_both_endpoints() {
        let span = Span::new(2, 6);
        let shifted = span.shifted(&TextEdit::insertion(2, "xyz"));

        assert_eq!(shifted, Span::new(5, 9));
        assert_eq!(shifted.len(), span.len());
    }

    #[test]
    fn an_inverted_pair_collapses_rather_than_becoming_a_range() {
        assert_eq!(Span::new(5, 2), Span::Detached);
    }

    #[test]
    #[should_panic(expected = "deletion start must not exceed its end")]
    fn an_inverted_deletion_is_rejected_at_construction() {
        TextEdit::deletion(3, 1);
    }

    #[test]
    fn adjacent_spans_do_not_have_an_overlap() {
        assert_eq!(Span::new(0, 1).meet(Span::new(1, 2)), Span::Detached);
    }

    #[test]
    fn a_span_indexes_the_buffer_it_came_from() {
        let source = "quotient ring";

        assert_eq!(&source[span_of(9, "ring")], "ring");
        // A marker token names the empty string at its own position.
        assert_eq!(&source[Span::from_offset(9)], "");
        assert_eq!(Span::new(0, source.len()), Span::new(0, 13));

        // Spans that name no text in this buffer are refused rather than guessed.
    }

    #[test]
    #[should_panic(expected = "does not name text in this buffer")]
    fn indexing_with_a_detached_span_is_a_bug_rather_than_an_empty_slice() {
        let _ = &"ring"[Span::detached()];
    }

    /// The law tying the two halves together: an edit acts on the buffer and on
    /// the span, and a span disjoint from the edit still names the same text
    /// afterwards. This is what `shifted` is *for*, so it is checked against
    /// every edit shape rather than a single hand-picked case.
    #[test]
    fn an_edit_acts_on_buffer_and_span_alike() {
        let source = "quotient ring R";
        let ring = span_of(9, "ring");

        let disjoint = [
            // Before the span.
            TextEdit::insertion(0, "the "),
            TextEdit::deletion(0, 9),
            // Exactly at its opening boundary: the span moves whole, so it
            // still names the same text. An insertion at the *closing*
            // boundary extends the span instead and is not disjoint.
            TextEdit::insertion(9, "graded "),
            // After the span.
            TextEdit::insertion(15, " = QQ"),
            TextEdit::deletion(13, 15),
            // A multi-byte character, to catch byte/char confusion.
            TextEdit::insertion(0, "ℤ "),
        ];

        for edit in &disjoint {
            let edited = edit.apply(source).expect("edit lands inside the buffer");
            assert_eq!(
                &edited[ring.shifted(edit)],
                &source[&ring],
                "{edit:?} broke the span"
            );
        }
    }

    /// The general form of the same law: for an edit landing *inside* a span,
    /// the shifted span names the edited text, so the edit acts on the slice too.
    #[test]
    fn an_edit_inside_a_span_acts_on_the_text_the_span_names() {
        let source = "quotient ring";
        let ring = span_of(9, "ring");
        let edit = TextEdit::insertion(11, "AB");

        let edited = edit.apply(source).expect("edit lands inside the buffer");
        // The span still names its own text, with the insertion carried into it.
        assert_eq!(&edited[ring.shifted(&edit)], "riABng");
        // Which is exactly the edit acting on the original slice, rebased to it.
        let inside = TextEdit::insertion(11 - ring.start().unwrap(), "AB");
        assert_eq!(inside.apply(&source[ring]).unwrap(), "riABng");
    }

    /// Text typed at a span's closing boundary is absorbed by it. This is what
    /// an unclosed group wants: its span keeps reaching the end of whatever has
    /// been typed since, without anyone re-deriving it.
    #[test]
    fn a_span_absorbs_text_appended_at_its_closing_boundary() {
        let source = "f(x";
        // An unclosed group, running from its opening delimiter to end of input.
        let group = Span::new(1, source.len());
        assert_eq!(&source[&group], "(x");

        let typed = TextEdit::insertion(3, ", y");
        let edited = typed.apply(source).expect("edit lands inside the buffer");

        assert_eq!(&edited[group.shifted(&typed)], "(x, y");
        // Still opens at the delimiter and still reaches end of input.
        assert_eq!(group.shifted(&typed), Span::new(1, edited.len()));
    }
}