quiltlang 0.6.0

Quilt, a polyglot metaprogramming language: source code with arrow-bracket syntax for embedding and manipulating code fragments of other languages at code-generation time.
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
use indoc::indoc;
use pretty_assertions::assert_eq;
use quilt::lang::{one_liner, Language};
use quilt::langs::omni::Omni;
use quilt::langs::python::lang::PythonLanguage;
use quilt::prelude::*;
use quilt::term::STerm;

fn expand(lang: &str, code: &str) -> Result<String> {
    let mut omni = Omni::default();
    let term = omni.parse_lang(lang, code.trim())?;
    let expanded = omni.expand_lang(lang, &term)?;
    Ok(expanded.coparse())
}

/**************************************************************/

fn roundtrip(code: &str) -> Result<()> {
    roundtrip_lang("rs", code)
}

/// Note `coparse_quilt`, not `coparse`: this asserts the term renders back as
/// the `.quilt` *source* it was parsed from, which is the reading in which a
/// glyph in content carries a `\` (#223). `coparse` is the other reading — the
/// code the term generates — and the two differ only on escapes.
fn roundtrip_lang(lang: &str, code: &str) -> Result<()> {
    let code = code.trim();
    let mut omni = Omni::default();
    let term = omni.parse_lang(lang, code)?;
    // dbg!(&term);
    // dbg!(&term[0][5]);
    let code2 = term.coparse_quilt();
    // println!("'{code}'");
    // println!("'{code2}'");
    assert_eq!(code, code2);
    Ok(())
}

#[test]
fn rust_hello() -> Result<()> {
    roundtrip(indoc! {r#"
        fn hello() {
            println!("Hello, world!");
        }
    "#})
}

#[test]
fn python_hello() -> Result<()> {
    roundtrip_lang(
        "py",
        indoc! {r#"
            def hello():
                print("Hello, world!")
        "#},
    )
}

#[test]
fn rs_py() -> Result<()> {
    roundtrip(indoc! {r#"
        fn hello() {
            let code = py↖
                def hello():
                    print("Hello, world!")
            ↗;
            println!(code);
        }
    "#})
}

#[test]
fn rs_py_minimal() -> Result<()> {
    roundtrip(indoc! {r#"
        const X: T = py↖1↗;
    "#})
}

#[test]
fn squash() -> Result<()> {
    let rs = "def f(): pass";
    let mut py = PythonLanguage::default();
    let qterm = py.parse_file(&one_liner(rs))?;

    let block = qterm[4].clone();
    dbg!(&block);
    assert_eq!(block.coparse(), "pass");
    let squashed = block.squash();
    assert_eq!(squashed.coparse(), "pass");
    Ok(())
}

#[test]
fn whitespace_ownership() -> Result<()> {
    let rs = indoc! {r#"
        const X: T = py↖ 1 ↗;
    "#};
    let mut omni = Omni::default();
    let term = omni.parse(rs)?;
    assert_eq!(rs, term.coparse());
    assert_eq!("py↖ 1 ↗", term[5].coparse());
    assert_eq!(" 1 ", term[5][0].coparse());
    Ok(())
}

#[test]
fn rs_py_multiline() -> Result<()> {
    roundtrip(indoc! {r#"
        const X: T = py↖
            123
        ↗;
    "#})
}

#[test]
fn rs_py_multiline_no_indent() -> Result<()> {
    roundtrip(indoc! {r#"
        const X: T = py↖
        123
        ↗;
    "#})
}

#[test]
fn expr() -> Result<()> {
    roundtrip_lang("py", "1 ")
}

#[test]
fn rs_empty() -> Result<()> {
    roundtrip(indoc! {r#"
        const X: T = ↖↗;
    "#})
}

#[test]
fn rs_empty_nl() -> Result<()> {
    roundtrip(indoc! {r#"
        const X: T = ↖
        ↗;
    "#})
}

#[test]
fn rs_empty_nl_2() -> Result<()> {
    roundtrip(indoc! {r#"
        const X: T = ↖
        
        ↗;
    "#})
}

#[test]
fn rs_py_empty() -> Result<()> {
    roundtrip(indoc! {r#"
        const X: T = py↖123
        ↗;
    "#})
}

#[test]
fn rs_stmt() -> Result<()> {
    roundtrip(indoc! {r#"
        let code = ↖println!("Hello, world!");↗;
    "#})
}

#[test]
fn rs_var() -> Result<()> {
    roundtrip(indoc! {r#"
        let code = ↖let ↙foo↘ = "bar";↗;
    "#})
}

#[test]
fn reduce_expands_homogeneous() -> Result<()> {
    // ↓ at ground level in Rust expands to `.reduce()` in the coparse output
    let out = expand(
        "rs",
        indoc! {r#"
        fn main() {
            let result = program.↓;
        }
    "#},
    )?;
    assert_eq!(
        out.trim(),
        "fn main() {\n    let result = program.reduce();\n}"
    );
    Ok(())
}

#[test]
fn hetero_reduce_py_coparse() -> Result<()> {
    // py↓ at ground level in Rust expands to `.reduce_py()` in the coparse output
    let out = expand(
        "rs",
        indoc! {r#"
        fn main() {
            let result = program.py↓;
        }
    "#},
    )?;
    assert_eq!(
        out.trim(),
        "fn main() {\n    let result = program.reduce_py();\n}"
    );
    Ok(())
}

#[test]
fn hetero_reduce_py_expands_to_reduce_py() -> Result<()> {
    let out = expand(
        "rs",
        indoc! {r#"
        fn main() {
            let result = program.py↓;
        }
    "#},
    )?;
    assert!(
        out.contains("reduce_py()"),
        "expected `reduce_py()` in output, got: {out}"
    );
    Ok(())
}

#[test]
fn homo_reduce_expands_to_reduce() -> Result<()> {
    let out = expand(
        "rs",
        indoc! {r#"
        fn main() {
            let result = program.↓;
        }
    "#},
    )?;
    assert!(
        out.contains("reduce()"),
        "expected `reduce()` in output, got: {out}"
    );
    assert!(
        !out.contains("reduce_py()"),
        "should not contain `reduce_py()`, got: {out}"
    );
    Ok(())
}

#[test]
fn py_homo_reduce_expands_to_reduce() -> Result<()> {
    let out = expand("py", "result = program.↓")?;
    assert!(
        out.contains("reduce()"),
        "expected `reduce()` in py output, got: {out}"
    );
    Ok(())
}

#[test]
fn py_hetero_reduce_rs_expands_to_reduce_rs() -> Result<()> {
    let out = expand("py", "result = program.rs↓")?;
    assert!(
        out.contains("reduce_rs()"),
        "expected `reduce_rs()` in py output, got: {out}"
    );
    Ok(())
}

/// A multi-line `/* … */` plain comment reaches the language parser as a single
/// `FlatNode::Str` with raw newlines in it. The line table that `Point`s index
/// used to be built by appending that whole string to the current line, so a
/// comment spanning N lines left the table N-1 rows short of what tree-sitter
/// saw — which made an ordinary file fail to parse, and, when a later node's
/// row ran off the end, panic with an index out of bounds.
///
/// Found by `bin/fuzz` (issue #161); the crashing input is kept as
/// `fuzz/seeds/multiline_block_comment`.
#[test]
fn multiline_plain_block_comment_round_trips() -> Result<()> {
    roundtrip(indoc! {r#"
        /* a
           b */
        fn f() {
            println!("hi");
        }
    "#})
}

/// The same, with the comment indented inside a block: the continuation line
/// carries a prefix, which is the case the row bookkeeping feeds into.
#[test]
fn indented_multiline_plain_block_comment_round_trips() -> Result<()> {
    roundtrip(indoc! {r#"
        fn f() {
            /* one
               two
               three */
            let x = 1;
        }
    "#})
}

/// A multi-line string whose continuation lines sit *left* of the code around
/// it. Those lines are content, not indentation: the prefix the enclosing block
/// carries is not part of them, so writing them back must not add it. They used
/// to be re-indented to the block, which changed the program's string — in the
/// host code of every expanded file, not only inside quotes.
#[test]
fn flush_left_string_continuation_is_not_reindented() -> Result<()> {
    roundtrip_lang(
        "rs",
        indoc! {r#"
            fn f() {
                if true {
                    let s = "one
            two
              three";
                }
            }
        "#},
    )?;
    roundtrip_lang(
        "py",
        indoc! {r#"
            def f():
                s = """one
              two
            three"""
                return s
        "#},
    )?;
    roundtrip_lang(
        "ts",
        indoc! {r#"
            function f() {
              const s = `one
            two
                three`;
              return s;
            }
        "#},
    )
}

/// The same string in the host code of a file that also quotes: expansion
/// rewrites the quote and must leave the string exactly as written.
#[test]
fn expand_keeps_a_host_strings_contents() -> Result<()> {
    let out = expand(
        "rs",
        indoc! {r#"
            fn f() {
                if true {
                    let s = "one
            two";
                    let q = rs↖1↗;
                }
            }
        "#},
    )?;
    assert!(
        out.contains("let s = \"one\ntwo\";"),
        "the host string should be untouched, got:\n{out}"
    );
    Ok(())
}

/* --------------------- escaped glyphs round-trip (#223) ------------------- */

/// Every glyph, escaped, at ground level: a `.quilt` file that spells a glyph
/// `\↑` must come back spelling it `\↑`. `coparse` — the *output* reading —
/// deliberately writes it bare, which is why the round trip needs its own
/// renderer rather than a fix to the one the expander uses.
#[test]
fn every_escaped_glyph_round_trips_at_ground_level() -> Result<()> {
    for g in quilt::glyphs::GLYPHS {
        roundtrip(&format!("let s = \"\\{g}\";"))?;
    }
    Ok(())
}

/// …and inside a quote, where the fragment is a different language's code. The
/// brackets (`↖ ↗ ↙ ↘ ⟨ ⟩`) are unambiguous anywhere: none of them is ever
/// deferred to a later stage, so a bare one in a term can only have come from
/// an escape.
#[test]
fn escaped_brackets_round_trip_inside_a_quote() -> Result<()> {
    for g in ['↖', '↗', '↙', '↘', '⟨', '⟩'] {
        roundtrip(&format!("let q = rs↖let s = \"\\{g}\";↗;"))?;
    }
    Ok(())
}

/// The other direction, and the reason the renderer cannot simply escape every
/// glyph it sees: `↑ ↓ ←` inside an unresolved quote are *deferred operators*,
/// left for the next stage, and must reach the output bare. Escaping one would
/// silently turn a staged program into a literal.
#[test]
fn deferred_operators_are_not_escaped() -> Result<()> {
    roundtrip("let q = rs↖let y = ↑;↗;")?;
    roundtrip("let q = rs↖let y = ↓;↗;")?;
    roundtrip("let q = rs↖let y = py↓;↗;")?;
    roundtrip("let q = rs↖fn f() { ←; }↗;")?;
    roundtrip("let q = lean↖def n := ↑↗;")?;
    // Two stages deep, where the inner glyph is deferred twice over.
    roundtrip("let q = rs↖let z = rs↖let y = ↑;↗;↗;")
}

/// The residual, pinned so it is a known shape rather than a surprise: an
/// *operator* glyph escaped inside a quote, in a position where the target
/// grammar makes it a leaf of its own — here a Rust string's contents. Such a
/// leaf is indistinguishable from the deferred-operator placeholder the
/// expander plugs in (both are a single-glyph leaf with a named tag), and the
/// renderer resolves the ambiguity in favour of the operator, because escaping
/// a real deferred operator would break staging outright while this loses an
/// escape that was already lost before #223.
///
/// Telling the two apart needs the placeholder to be marked in the term, which
/// is a representation change — see the discussion on #223.
#[test]
fn an_escaped_operator_inside_a_quoted_string_is_the_known_residual() -> Result<()> {
    let mut omni = Omni::default();
    let src = "let q = rs↖let s = \"\\↑\";↗;";
    let back = omni.parse(src)?.coparse_quilt();
    assert_eq!(
        back, "let q = rs↖let s = \"↑\";↗;",
        "if this now round-trips, the residual on #223 is closed — replace this \
         test with `roundtrip({src:?})`"
    );
    Ok(())
}

/// A `//` line comment must not swallow the bracket that closes the quote it
/// sits in (issue #226). The comment token runs to end of line, so before the
/// fix the `↗` was comment text and the quote was never closed — a `malformed
/// Quilt syntax` error on source that reads as obviously valid, with the
/// workaround (a newline before the closer) discoverable only by accident.
///
/// The same applies to `↘`, and to Quilt's own `⟨//⟩` comment.
#[test]
fn a_line_comment_does_not_eat_the_closing_bracket() -> Result<()> {
    roundtrip("let b = rs↖let x = 1; // hi↗;")?;
    roundtrip("let u = rs↖let y = ↙f()↘; // hi↗;")?;
    // The closer of an *unquote* is bounded too.
    roundtrip("let n = rs↖let y = ↙f() // why↘;↗;")?;

    // Quilt's own comment is meta-level, so it is stripped rather than passed
    // through — `roundtrip` is the wrong assertion for it. What it shares with
    // `//` is that it must not eat the closer.
    let mut omni = Omni::default();
    let out = omni
        .parse_lang("rs", "let q = rs↖let x = 1; ⟨//⟩ note↗;")?
        .coparse();
    assert_eq!(out, "let q = rs↖let x = 1; ↗;");
    Ok(())
}

/// …while a comment at *ground* level still runs to end of line and carries
/// every glyph, closing arrows included. That is the half the #226 fix must not
/// break, and it is not hypothetical: four files under `examples/` explain
/// Quilt in a `//` comment and name the `↙…↘` brackets while doing it, so
/// bounding the token everywhere turned them all into parse errors.
#[test]
fn a_ground_level_line_comment_still_reaches_end_of_line() -> Result<()> {
    let mut omni = Omni::default();
    roundtrip(indoc! {r"
        // prose about the ↙…↘ hole and the ↖…↗ brackets, with ↑ ↓ ← for good measure
        let x = 1;"})?;

    let out = omni
        .parse_lang("rs", "⟨//⟩ a quilt comment about ↙…↘\nlet x = 1;")?
        .coparse();
    assert!(
        !out.contains("quilt comment"),
        "a `⟨//⟩` comment is stripped, not passed through: {out:?}"
    );
    assert!(
        out.contains("let x = 1;"),
        "the code after the comment survived: {out:?}"
    );
    Ok(())
}

/// A glyph inside a `//` or `/* … */` comment is raw text to Quilt — the
/// comment token consumes it, so a `\` there is a literal backslash and not an
/// escape — and the source renderer has to leave it alone (#237). Getting this
/// wrong was invisible until #229 put a comment full of brackets in a test:
/// `coparse_quilt` escaped them, and re-parsing gave a comment with stray
/// backslashes in it.
#[test]
fn a_c_style_comment_is_raw_and_is_not_escaped() -> Result<()> {
    roundtrip("// prose about ↙…↘ and ↑ ↓ ←\nlet x = 1;")?;
    roundtrip("/* block ↙…↘ with ⟨ ⟩ */\nlet x = 1;")?;
    roundtrip("let q = rs↖let x = 1; // hi ↑↗;")?;
    roundtrip_lang("ts", "// ts comment ↙…↘\nconst x = 1;")
}

/// The other half, and the reason the test is on the comment's *spelling* and
/// not only on its tag: Quilt's grammar has `//` and `/* … */` and nothing
/// else, so a `#` or `--` comment is ordinary content to it. A `↑` written in
/// a Python comment had to be spelled `\↑` in the source, and rendering it
/// back bare would turn it into a lift operator. Suppressing escapes for every
/// node a language calls a comment would fix the C-style languages and break
/// these.
#[test]
fn a_hash_or_dash_comment_is_not_raw_and_keeps_its_escapes() -> Result<()> {
    roundtrip_lang("py", "# python comment with \\↙…\\↘ and \\↑\nx = 1")?;
    roundtrip_lang("nix", "{ }  # nix comment \\↑")?;
    roundtrip_lang("bash", "# shell comment \\↓\necho hi")
}

/* ------------------- a quote body left of its host code ------------------- */

/// A multi-line quote whose body sits *left* of the code around it builds the
/// same term as the same quote written at the host's indentation. The body has
/// none of the enclosing indent to strip, which used to fail the whole
/// expansion with a bare "Failed to strip prefix" and no location.
#[test]
fn a_block_quote_left_of_its_host_expands_as_if_indented() -> Result<()> {
    let flush = expand(
        "rs",
        indoc! {r#"
            fn f() {
                if true {
                    let g = rs↖
            fn g() -> u32 {
                1
            }
            ↗;
                }
            }
        "#},
    )?;
    let indented = expand(
        "rs",
        indoc! {r#"
            fn f() {
                if true {
                    let g = rs↖
                        fn g() -> u32 {
                            1
                        }
                    ↗;
                }
            }
        "#},
    )?;
    assert_eq!(flush, indented);
    Ok(())
}

/// The same with the body only *partly* left of the host: what every line
/// shares is stripped, and the body keeps its own relative indentation.
#[test]
fn a_quote_body_partly_left_of_its_host_keeps_its_shape() -> Result<()> {
    let partly = expand(
        "rs",
        indoc! {r#"
            fn f() {
                if true {
                    let g = rs↖
                  fn g() -> u32 {
                      1
                  }
                    ↗;
                }
            }
        "#},
    )?;
    let indented = expand(
        "rs",
        indoc! {r#"
            fn f() {
                if true {
                    let g = rs↖
                        fn g() -> u32 {
                            1
                        }
                    ↗;
                }
            }
        "#},
    )?;
    assert_eq!(partly, indented);
    Ok(())
}

/// An inline-opened quote (`↖fn g…`) whose continuation lines are flush left:
/// the opener line bounds the common indent, so the body is read as written.
#[test]
fn an_inline_quote_left_of_its_host_expands() -> Result<()> {
    let flush = expand(
        "rs",
        indoc! {r#"
            fn f() {
                if true {
                    let g = rs↖fn g() -> u32 {
                1
            }↗;
                }
            }
        "#},
    )?;
    let indented = expand(
        "rs",
        indoc! {r#"
            fn f() {
                if true {
                    let g = rs↖fn g() -> u32 {
                        1
                    }↗;
                }
            }
        "#},
    )?;
    assert_eq!(flush, indented);
    Ok(())
}

/// A whitespace-only line shorter than the host's indent says nothing about the
/// body's indentation: it neither fails nor holds back the dedent.
#[test]
fn a_short_blank_line_in_a_quote_does_not_hold_back_the_dedent() -> Result<()> {
    let blank = "        fn a() {}\n  \n        fn b() {}";
    let src = format!("fn f() {{\n    let g = rs↖\n{blank}\n    ↗;\n}}");
    let out = expand("rs", &src)?;
    let clean = expand(
        "rs",
        "fn f() {\n    let g = rs↖\n        fn a() {}\n\n        fn b() {}\n    ↗;\n}",
    )?;
    assert_eq!(out, clean);
    Ok(())
}

/* -------------------- a quote's body indented past its host -------------------- */

/// The code the first quote in `src` (a TypeScript host) stands for: the body
/// after dedenting, which is what the quote's term coparses to.
fn first_quote_body(src: &str) -> Result<String> {
    first_quote_body_in("ts", src)
}

fn first_quote_body_in(host: &str, src: &str) -> Result<String> {
    fn find(t: &Arc<QTerm>) -> Option<Arc<QTerm>> {
        match &**t {
            QTerm::Quote { term, .. } => Some(term.clone()),
            QTerm::Unquote { term, .. } => find(term),
            QTerm::Tuple { terms, .. } => terms.iter().find_map(find),
        }
    }
    let mut omni = Omni::default();
    let term = omni.parse_lang(host, src.trim())?;
    Ok(find(&term).expect("a quote").coparse())
}

/// A block quote is dedented by its body's common indent, not by its closer's:
/// the `↗` line is blank, and a blank line says nothing about indentation, so
/// a closer left of the body no longer leaves its difference on every line.
#[test]
fn a_block_quote_closed_left_of_its_body_dedents_by_the_body() -> Result<()> {
    let body = first_quote_body(indoc! {r#"
        {
            prog.push(↖
                F[1] = {
                  a: 1,
                };
              ↗);
        }
    "#})?;
    assert_eq!(body, "F[1] = {\n  a: 1,\n};");
    Ok(())
}

/// An inline quote whose last line closes what its opener line opened is read
/// relative to that closer, whatever the host line's indent: here the body is
/// written one level in from the host, the closer half a level.
#[test]
fn an_inline_quote_closed_by_a_bracket_dedents_to_the_closer() -> Result<()> {
    let body = first_quote_body(indoc! {r#"
        {
            prog.push(↖F[1] = {
                a: 1,
              };↗);
        }
    "#})?;
    assert_eq!(body, "F[1] = {\n  a: 1,\n};");
    Ok(())
}

/// The same with the body aligned under the opener's column, far right of the
/// host line.
#[test]
fn an_inline_quote_aligned_to_its_opener_dedents_to_the_closer() -> Result<()> {
    let body = first_quote_body(indoc! {r#"
        {
            prog.push(↖F[1] = {
                        a: 1,
                      };↗);
        }
    "#})?;
    assert_eq!(body, "F[1] = {\n  a: 1,\n};");
    Ok(())
}

/// A closer indented *less* than a middle line: the middle line keeps what it
/// has beyond the closer.
#[test]
fn an_inline_quote_keeps_lines_deeper_than_its_closer() -> Result<()> {
    let body = first_quote_body(indoc! {r#"
        {
            prog.push(↖F[1] = {
                  b: [
                1,
                  ],
              };↗);
        }
    "#})?;
    assert_eq!(body, "F[1] = {\n    b: [\n  1,\n    ],\n};");
    Ok(())
}

/// The body at the host line's own indent, the shape that always worked,
/// builds the same term.
#[test]
fn an_inline_quote_at_its_host_indent_is_unchanged() -> Result<()> {
    let body = first_quote_body(indoc! {r#"
        {
            prog.push(↖F[1] = {
              a: 1,
            };↗);
        }
    "#})?;
    assert_eq!(body, "F[1] = {\n  a: 1,\n};");
    Ok(())
}

/// An inline quote whose last line does not close a bracket keeps its body's
/// indentation: in `↖def f():\n    return 1↗` the indent is the code.
#[test]
fn an_inline_python_def_keeps_its_body_indent() -> Result<()> {
    let body = first_quote_body_in(
        "rs",
        "fn f() {\n    let q = py↖def f():\n        return 1↗;\n}",
    )?;
    assert_eq!(body, "def f():\n    return 1");
    Ok(())
}