patchloom 0.37.5

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations, multi-file batching, markdown operations, and MCP server
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
//! AST-aware symbol movement between files.

use super::Language;
use super::symbols::{
    check_no_overlapping_spans, extract_symbol_text, extract_symbols_or_timeout, find_symbol,
    full_symbol_span,
};

/// Position to insert symbols in the target file.
#[derive(Debug, Clone)]
pub enum MovePosition {
    End,
    Start,
    After(String),
    Before(String),
}

/// Result of a move operation.
#[derive(Debug)]
pub struct MoveResult {
    pub source_content: String,
    pub target_content: String,
    pub symbols_moved: usize,
}

/// Parse a position string into a MovePosition.
///
/// Unknown tokens fail closed so agents inventing `top` / `first` do not
/// silently append at end.
pub fn parse_position(s: Option<&str>) -> anyhow::Result<MovePosition> {
    match s {
        None | Some("") | Some("end") => Ok(MovePosition::End),
        Some("start") => Ok(MovePosition::Start),
        Some(raw) if let Some(name) = raw.strip_prefix("after:") => {
            if name.is_empty() {
                return Err(crate::exit::InvalidInputError {
                    msg: "ast.move position after: requires a symbol name".into(),
                }
                .into());
            }
            Ok(MovePosition::After(name.to_string()))
        }
        Some(raw) if let Some(name) = raw.strip_prefix("before:") => {
            if name.is_empty() {
                return Err(crate::exit::InvalidInputError {
                    msg: "ast.move position before: requires a symbol name".into(),
                }
                .into());
            }
            Ok(MovePosition::Before(name.to_string()))
        }
        Some(other) => Err(crate::exit::InvalidInputError {
            msg: format!(
                "unknown ast.move position {other:?}; use start, end, after:NAME, or before:NAME"
            ),
        }
        .into()),
    }
}

/// Move named symbols from source to target file.
pub fn move_symbols(
    source: &str,
    target: &str,
    symbols: &[String],
    position: MovePosition,
    lang: Language,
) -> anyhow::Result<MoveResult> {
    if symbols.is_empty() {
        return Err(anyhow::Error::new(crate::exit::InvalidInputError {
            msg: "ast move symbols must not be empty".into(),
        }));
    }

    let src_symbols = extract_symbols_or_timeout(source, lang)?;
    move_symbols_from_symbols(source, target, &src_symbols, symbols, position, lang)
}

/// Like [`move_symbols`] using a pre-extracted source symbol list (tx tree cache).
pub(crate) fn move_symbols_from_symbols(
    source: &str,
    target: &str,
    src_symbols: &[super::symbols::SymbolDef],
    symbols: &[String],
    position: MovePosition,
    lang: Language,
) -> anyhow::Result<MoveResult> {
    if symbols.is_empty() {
        return Err(anyhow::Error::new(crate::exit::InvalidInputError {
            msg: "ast move symbols must not be empty".into(),
        }));
    }

    let eol = crate::write::detect_eol(source);
    let lines: Vec<&str> = crate::ops::file::text_lines(source).collect();

    // Collect symbols to move
    let mut to_move: Vec<(usize, usize, String)> = Vec::new(); // (start_0, end_0, text)
    let mut missing: Vec<&str> = Vec::new();

    for name in symbols {
        if let Some(sym) = find_symbol(src_symbols, name) {
            let (full_start, full_end) = full_symbol_span(source, sym, lang);
            let text = extract_symbol_text(source, sym, lang).to_string();
            to_move.push((
                full_start.saturating_sub(1),
                full_end.min(lines.len()),
                text,
            ));
        } else {
            missing.push(name);
        }
    }

    if !missing.is_empty() {
        return Err(anyhow::Error::new(crate::exit::NoMatchError {
            msg: format!("symbol(s) not found in source: {}", missing.join(", ")),
        }));
    }

    // symbols non-empty and no missing names implies to_move is non-empty.
    if to_move.is_empty() {
        return Ok(MoveResult {
            source_content: source.to_string(),
            target_content: target.to_string(),
            symbols_moved: 0,
        });
    }

    // Detect overlapping spans that would corrupt content during removal
    let spans: Vec<(usize, usize)> = to_move.iter().map(|(s, e, _)| (*s, *e)).collect();
    let span_names: Vec<&str> = to_move
        .iter()
        .map(|(s, _, _)| {
            src_symbols
                .iter()
                .find(|sym| {
                    let (fs, _) = full_symbol_span(source, sym, lang);
                    fs.saturating_sub(1) == *s
                })
                .map(|sym| sym.name.as_str())
                .unwrap_or("?")
        })
        .collect();
    check_no_overlapping_spans(&spans, &span_names)?;

    let symbols_moved = to_move.len();

    // Sort by start position (ascending) for ordered insertion into target
    to_move.sort_by_key(|(start, _, _)| *start);
    let ordered_texts: Vec<String> = to_move.iter().map(|(_, _, t)| t.clone()).collect();

    // Remove from source in reverse order
    let mut src_lines: Vec<String> = lines.iter().map(|l| l.to_string()).collect();
    let mut sorted_desc = to_move.clone();
    sorted_desc.sort_by_key(|b| std::cmp::Reverse(b.0));
    for (start_0, end_0, _) in &sorted_desc {
        let mut remove_end = *end_0;
        // Also remove trailing blank line if present
        if remove_end < src_lines.len() && src_lines[remove_end].trim().is_empty() {
            remove_end += 1;
        }
        let drain_end = remove_end.min(src_lines.len());
        src_lines.drain(*start_0..drain_end);
    }

    let mut source_result = src_lines.join(eol);
    if source.ends_with('\n') && !source_result.ends_with('\n') {
        source_result.push_str(eol);
    }

    // Build text to insert into target
    let mut insert_text = String::new();
    for (i, text) in ordered_texts.iter().enumerate() {
        if i > 0 {
            insert_text.push_str(eol);
        }
        insert_text.push_str(text.trim_end_matches(['\r', '\n']));
        insert_text.push_str(eol);
    }

    // Insert into target (use target's EOL style, not source's)
    let target_eol = crate::write::detect_eol(target);
    let target_result = insert_into_target(target, &insert_text, &position, lang, target_eol)?;

    Ok(MoveResult {
        source_content: source_result,
        target_content: target_result,
        symbols_moved,
    })
}

/// Count leading preamble lines that `position=start` must stay after (#2530).
fn preamble_line_count(source: &str, lang: Language) -> usize {
    let lines: Vec<&str> = crate::ops::file::text_lines(source).collect();
    let mut i = 0usize;
    if lines
        .first()
        .is_some_and(|l| l.starts_with("#!") && !l.starts_with("#!["))
    {
        i += 1;
    }
    match lang {
        Language::Python => {
            if i < lines.len() && is_python_encoding_comment(lines[i]) {
                i += 1;
            }
            i = skip_blank_lines(&lines, i);
            if i < lines.len()
                && let Some(after_doc) = skip_python_string_literal(&lines, i)
            {
                i = skip_blank_lines(&lines, after_doc);
            }
            while i < lines.len() && is_python_future_import(lines[i]) {
                i += 1;
                if i < lines.len() && lines[i].trim().is_empty() {
                    let next = i + 1;
                    if next < lines.len() && is_python_future_import(lines[next]) {
                        i = next;
                    }
                }
            }
            i
        }
        Language::Rust => {
            while i < lines.len() {
                let t = lines[i].trim();
                if t.is_empty() {
                    if i + 1 < lines.len() && is_rust_inner_preamble(lines[i + 1]) {
                        i += 1;
                        continue;
                    }
                    break;
                }
                if t.starts_with("//!") {
                    i += 1;
                    continue;
                }
                if t.starts_with("#![") {
                    i = skip_rust_crate_attr(&lines, i);
                    continue;
                }
                break;
            }
            i
        }
        _ => i,
    }
}

fn skip_blank_lines(lines: &[&str], mut i: usize) -> usize {
    while i < lines.len() && lines[i].trim().is_empty() {
        i += 1;
    }
    i
}

fn is_python_encoding_comment(line: &str) -> bool {
    let t = line.trim();
    t.starts_with('#') && t.to_ascii_lowercase().contains("coding")
}

fn is_python_future_import(line: &str) -> bool {
    let t = line.trim();
    t.starts_with("from __future__ import")
}

fn skip_python_string_literal(lines: &[&str], start: usize) -> Option<usize> {
    let t = lines[start].trim_start();
    let quote = if t.starts_with("\"\"\"") {
        "\"\"\""
    } else if t.starts_with("'''") {
        "'''"
    } else if t.starts_with('"') {
        "\""
    } else if t.starts_with('\'') {
        "'"
    } else {
        return None;
    };
    if quote.len() == 1 {
        // Single-line module docstring only.
        if t[quote.len()..].contains(quote) {
            return Some(start + 1);
        }
        return None;
    }
    if t[quote.len()..].contains(quote) {
        return Some(start + 1);
    }
    let mut i = start + 1;
    while i < lines.len() {
        if lines[i].contains(quote) {
            return Some(i + 1);
        }
        i += 1;
    }
    None
}

fn is_rust_inner_preamble(line: &str) -> bool {
    let t = line.trim();
    t.starts_with("//!") || t.starts_with("#![")
}

fn skip_rust_crate_attr(lines: &[&str], start: usize) -> usize {
    let t = lines[start].trim();
    if t.ends_with(']') {
        return start + 1;
    }
    let mut i = start + 1;
    while i < lines.len() {
        if lines[i].contains(']') {
            return i + 1;
        }
        i += 1;
    }
    start + 1
}

fn insert_into_target(
    target: &str,
    insert_text: &str,
    position: &MovePosition,
    lang: Language,
    eol: &str,
) -> anyhow::Result<String> {
    if target.is_empty() {
        return Ok(insert_text.to_string());
    }

    let lines: Vec<&str> = crate::ops::file::text_lines(target).collect();

    match position {
        MovePosition::End => {
            let mut result = target.to_string();
            if !result.ends_with('\n') {
                result.push_str(eol);
            }
            result.push_str(eol);
            result.push_str(insert_text);
            Ok(result)
        }
        MovePosition::Start => {
            let skip = preamble_line_count(target, lang);
            if skip == 0 {
                let mut result = insert_text.to_string();
                result.push_str(eol);
                result.push_str(target);
                return Ok(result);
            }
            let mut result = String::new();
            for line in &lines[..skip] {
                result.push_str(line);
                result.push_str(eol);
            }
            if skip > 0 && !lines[skip - 1].trim().is_empty() {
                result.push_str(eol);
            }
            result.push_str(insert_text);
            if !insert_text.ends_with('\n') {
                result.push_str(eol);
            }
            if skip < lines.len() && !lines[skip].trim().is_empty() {
                result.push_str(eol);
            }
            for line in &lines[skip..] {
                result.push_str(line);
                result.push_str(eol);
            }
            if !target.ends_with('\n') && result.ends_with('\n') {
                result.truncate(result.len() - eol.len());
            }
            Ok(result)
        }
        MovePosition::After(sym_name) => {
            let symbols = extract_symbols_or_timeout(target, lang)?;
            let sym = find_symbol(&symbols, sym_name).ok_or_else(|| {
                anyhow::Error::new(crate::exit::NoMatchError {
                    msg: format!("symbol '{sym_name}' not found in target"),
                })
            })?;
            let end_0 = sym.end_line.min(lines.len());
            let mut result = String::new();
            for line in &lines[..end_0] {
                result.push_str(line);
                result.push_str(eol);
            }
            result.push_str(eol);
            result.push_str(insert_text);
            for line in &lines[end_0..] {
                result.push_str(line);
                result.push_str(eol);
            }
            if !target.ends_with('\n') && result.ends_with('\n') {
                result.truncate(result.len() - eol.len());
            }
            Ok(result)
        }
        MovePosition::Before(sym_name) => {
            let symbols = extract_symbols_or_timeout(target, lang)?;
            let sym = find_symbol(&symbols, sym_name).ok_or_else(|| {
                anyhow::Error::new(crate::exit::NoMatchError {
                    msg: format!("symbol '{sym_name}' not found in target"),
                })
            })?;
            let (full_start, _) = full_symbol_span(target, sym, lang);
            let start_0 = full_start.saturating_sub(1);
            let mut result = String::new();
            for line in &lines[..start_0] {
                result.push_str(line);
                result.push_str(eol);
            }
            result.push_str(insert_text);
            result.push_str(eol);
            for line in &lines[start_0..] {
                result.push_str(line);
                result.push_str(eol);
            }
            if !target.ends_with('\n') && result.ends_with('\n') {
                result.truncate(result.len() - eol.len());
            }
            Ok(result)
        }
    }
}

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

    #[test]
    fn move_basic() {
        let source = "fn foo() {}\n\nfn bar() {}\n";
        let target = "fn existing() {}\n";
        let result = move_symbols(
            source,
            target,
            &["foo".into()],
            MovePosition::End,
            Language::Rust,
        )
        .unwrap();
        assert!(!result.source_content.contains("fn foo"));
        assert!(result.source_content.contains("fn bar"));
        assert!(result.target_content.contains("fn foo"));
        assert!(result.target_content.contains("fn existing"));
        assert_eq!(result.symbols_moved, 1);
    }

    #[test]
    fn move_to_empty_file() {
        let source = "fn foo() {\n    42\n}\n\nfn bar() {}\n";
        let result = move_symbols(
            source,
            "",
            &["foo".into()],
            MovePosition::End,
            Language::Rust,
        )
        .unwrap();
        assert!(!result.source_content.contains("fn foo"));
        assert!(result.target_content.contains("fn foo()"));
        assert!(
            result.target_content.contains("    42"),
            "target should contain the function body: {}",
            result.target_content
        );
    }

    #[test]
    fn move_multiple_symbols() {
        let source = "fn alpha() {}\n\nfn beta() {}\n\nfn gamma() {}\n";
        let result = move_symbols(
            source,
            "",
            &["alpha".into(), "gamma".into()],
            MovePosition::End,
            Language::Rust,
        )
        .unwrap();
        assert!(!result.source_content.contains("fn alpha"));
        assert!(result.source_content.contains("fn beta"));
        assert!(!result.source_content.contains("fn gamma"));
        assert!(result.target_content.contains("fn alpha"));
        assert!(result.target_content.contains("fn gamma"));
        assert_eq!(result.symbols_moved, 2);
    }

    #[test]
    fn move_symbol_not_found() {
        let source = "fn foo() {}\n";
        let result = move_symbols(
            source,
            "",
            &["nonexistent".into()],
            MovePosition::End,
            Language::Rust,
        );
        assert!(result.is_err(), "expected error, got Ok: {result:?}");
    }

    #[test]
    fn move_preserves_attributes() {
        let source = "#[test]\n#[cfg(unix)]\nfn foo() {}\n\nfn bar() {}\n";
        let result = move_symbols(
            source,
            "",
            &["foo".into()],
            MovePosition::End,
            Language::Rust,
        )
        .unwrap();
        assert!(result.target_content.contains("#[test]"));
        assert!(result.target_content.contains("#[cfg(unix)]"));
        assert!(result.target_content.contains("fn foo()"));
    }

    /// #2530: start is after `from __future__`, not at byte 0.
    #[test]
    fn move_position_start_after_python_future() {
        let source = "def moved():\n    return 1\n";
        let target = "from __future__ import annotations\n\ndef existing():\n    pass\n";
        let result = move_symbols(
            source,
            target,
            &["moved".into()],
            MovePosition::Start,
            Language::Python,
        )
        .unwrap();
        assert_eq!(
            result.target_content,
            "from __future__ import annotations\n\ndef moved():\n    return 1\n\ndef existing():\n    pass\n"
        );
        let future_pos = result.target_content.find("from __future__").unwrap();
        let moved_pos = result.target_content.find("def moved").unwrap();
        assert!(
            future_pos < moved_pos,
            "moved symbol must follow the future import: {}",
            result.target_content
        );
    }

    /// #2530: start is after `//!` inner docs and `#![...]` crate attrs.
    #[test]
    fn move_position_start_after_rust_inner_docs_and_crate_attr() {
        let source = "fn moved() {}\n";
        let target = "//! crate docs\n#![deny(unsafe_code)]\n\nfn existing() {}\n";
        let result = move_symbols(
            source,
            target,
            &["moved".into()],
            MovePosition::Start,
            Language::Rust,
        )
        .unwrap();
        assert_eq!(
            result.target_content,
            "//! crate docs\n#![deny(unsafe_code)]\n\nfn moved() {}\n\nfn existing() {}\n"
        );
        let doc_pos = result.target_content.find("//! crate docs").unwrap();
        let attr_pos = result.target_content.find("#![deny").unwrap();
        let moved_pos = result.target_content.find("fn moved").unwrap();
        assert!(doc_pos < moved_pos && attr_pos < moved_pos);
    }

    #[test]
    fn move_position_start_does_not_split_multiline_inner_attribute() {
        let source = "fn moved() {}\n";
        let target = "#![cfg_attr(\n    not(test),\n)]\nfn existing() {}\n";
        let result = move_symbols(
            source,
            target,
            &["moved".into()],
            MovePosition::Start,
            Language::Rust,
        )
        .unwrap();
        let attr_open = result.target_content.find("#![cfg_attr(").unwrap();
        let attr_close = result.target_content.find(")]").unwrap();
        let moved_pos = result.target_content.find("fn moved").unwrap();
        assert!(
            attr_open < attr_close && attr_close < moved_pos,
            "moved symbol must follow the whole attribute: {}",
            result.target_content
        );
    }

    #[test]
    fn move_position_start() {
        let source = "fn new_fn() {}\n";
        let target = "fn existing() {}\n";
        let result = move_symbols(
            source,
            target,
            &["new_fn".into()],
            MovePosition::Start,
            Language::Rust,
        )
        .unwrap();
        let new_pos = result.target_content.find("fn new_fn").unwrap();
        let existing_pos = result.target_content.find("fn existing").unwrap();
        assert!(new_pos < existing_pos);
    }

    #[test]
    fn move_position_after() {
        let source = "fn moved() {}\n";
        let target = "fn first() {}\n\nfn last() {}\n";
        let result = move_symbols(
            source,
            target,
            &["moved".into()],
            MovePosition::After("first".into()),
            Language::Rust,
        )
        .unwrap();
        let first_pos = result.target_content.find("fn first").unwrap();
        let moved_pos = result.target_content.find("fn moved").unwrap();
        let last_pos = result.target_content.find("fn last").unwrap();
        assert!(first_pos < moved_pos);
        assert!(moved_pos < last_pos);
    }

    #[test]
    fn move_position_before() {
        let source = "fn moved() {}\n";
        let target = "fn first() {}\n\nfn last() {}\n";
        let result = move_symbols(
            source,
            target,
            &["moved".into()],
            MovePosition::Before("last".into()),
            Language::Rust,
        )
        .unwrap();
        let first_pos = result.target_content.find("fn first").unwrap();
        let moved_pos = result.target_content.find("fn moved").unwrap();
        let last_pos = result.target_content.find("fn last").unwrap();
        assert!(first_pos < moved_pos);
        assert!(moved_pos < last_pos);
    }

    #[test]
    fn move_preserves_crlf_line_endings() {
        let source = "fn foo() {}\r\n\r\nfn bar() {}\r\n";
        let target = "fn existing() {}\r\n";
        let result = move_symbols(
            source,
            target,
            &["foo".into()],
            MovePosition::End,
            Language::Rust,
        )
        .unwrap();
        // Source content should preserve CRLF
        let bytes = result.source_content.as_bytes();
        for (i, &b) in bytes.iter().enumerate() {
            if b == b'\n' {
                assert!(
                    i > 0 && bytes[i - 1] == b'\r',
                    "bare LF in source at byte {i}: {:?}",
                    result.source_content
                );
            }
        }
        // Target content should preserve CRLF
        let target_bytes = result.target_content.as_bytes();
        for (i, &b) in target_bytes.iter().enumerate() {
            if b == b'\n' {
                assert!(
                    i > 0 && target_bytes[i - 1] == b'\r',
                    "bare LF in target at byte {i}: {:?}",
                    result.target_content
                );
            }
        }
    }

    #[test]
    fn move_adjacent_decorated_symbols_succeeds() {
        // Simulate two adjacent decorated functions where doc comments could
        // cause span overlap. In practice the spans from tree-sitter should
        // not overlap for well-formed code, but if full_symbol_span extends
        // backwards into the prior symbol's range, we should error instead
        // of silently corrupting content.
        //
        // Create a scenario with a shared doc-comment block that's ambiguous:
        // The test verifies that non-overlapping adjacent symbols work fine.
        let source = "/// Doc for alpha\nfn alpha() {}\n\n/// Doc for beta\nfn beta() {}\n";
        let result = move_symbols(
            source,
            "",
            &["alpha".into(), "beta".into()],
            MovePosition::End,
            Language::Rust,
        );
        // These two should NOT overlap (they have a blank line separator)
        assert_eq!(result.expect("move should succeed").symbols_moved, 2);
    }

    #[test]
    fn move_empty_symbols_is_invalid_input() {
        let source = "fn foo() {}\n";
        let err = move_symbols(source, "", &[], MovePosition::End, Language::Rust)
            .expect_err("empty symbols vec must be invalid_input");
        assert!(
            crate::exit::is_invalid_input(&err),
            "empty symbols must classify as invalid_input: {err}"
        );
        assert!(
            err.to_string().contains("must not be empty"),
            "message must say must not be empty: {err}"
        );
    }

    #[test]
    fn parse_position_variants() {
        assert!(matches!(parse_position(None).unwrap(), MovePosition::End));
        assert!(matches!(
            parse_position(Some("end")).unwrap(),
            MovePosition::End
        ));
        assert!(matches!(
            parse_position(Some("start")).unwrap(),
            MovePosition::Start
        ));
        assert!(matches!(
            parse_position(Some("after:foo")).unwrap(),
            MovePosition::After(ref s) if s == "foo"
        ));
        assert!(matches!(
            parse_position(Some("before:bar")).unwrap(),
            MovePosition::Before(ref s) if s == "bar"
        ));
        let err = parse_position(Some("top")).unwrap_err();
        assert!(
            err.to_string().contains("unknown"),
            "top must fail closed: {err}"
        );
    }
}