Merge branch 'worktree-agent-a329eae90a7510448'
Browse files at this commit8 changed files +349 -53
- crates/app/src/keymap.rs +155 -40
- crates/app/src/main.rs +11 -0
- crates/engine/src/edith.rs +4 -1
- crates/engine/src/lib.rs +14 -0
- crates/engine/src/playback.rs +1 -2
- crates/engine/src/project.rs +107 -10
- crates/engine/tests/audio_files.rs +45 -0
- crates/engine/tests/saveload.rs +12 -0
crates/app/src/keymap.rsmodified+155 -40
| @@ -14,10 +14,12 @@ | |||
| 14 | //! compares against that string and nothing translates in between. What a | 14 | //! compares against that string and nothing translates in between. What a |
| 15 | //! *person* reads is [`Keymap::display`]'s business, and only there. | 15 | //! *person* reads is [`Keymap::display`]'s business, and only there. |
| 16 | //! | 16 | //! |
| 17 | -//! The parser is strict and every refusal names its 1-based line, like the | 17 | +//! The parser is strict and everything it cannot use names its 1-based line, |
| 18 | -//! project file: this file is generated by the keybindings editor, so a line it | 18 | +//! like the project file. Only the first line refuses the whole file: a binding |
| 19 | -//! did not generate is a corrupt file rather than a dialect. A missing file is | 19 | +//! line the parser cannot use is *dropped*, named in the notice, and every other |
| 20 | -//! not corrupt -- it means nobody has changed a key yet, and the defaults stand. | 20 | +//! line still stands -- one unusable line is not a reason to throw away the |
| 21 | +//! rebinds around it. A missing file is not corrupt either -- it means nobody | ||
| 22 | +//! has changed a key yet, and the defaults stand. | ||
| 21 | //! | 23 | //! |
| 22 | //! Writing mirrors `engine::edith::save`: `.part`, fsync, rename, directory | 24 | //! Writing mirrors `engine::edith::save`: `.part`, fsync, rename, directory |
| 23 | //! fsync, so an interrupted save cannot cost the bindings that were already | 25 | //! fsync, so an interrupted save cannot cost the bindings that were already |
| @@ -338,6 +340,16 @@ impl Chord { | |||
| 338 | } | 340 | } |
| 339 | } | 341 | } |
| 340 | 342 | ||
| 343 | + /// Whether the file can carry this stroke and read it back as itself. gpui | ||
| 344 | + /// reports strokes this format cannot spell -- shift+= arrives as the key | ||
| 345 | + /// `"+"`, which is the chord grammar's own separator -- and binding one | ||
| 346 | + /// would write a line the next load has to drop. Asked before a capture is | ||
| 347 | + /// taken, so a stroke that cannot be saved is refused while the user is | ||
| 348 | + /// still looking at the row. | ||
| 349 | + pub fn bindable(&self) -> bool { | ||
| 350 | + Chord::parse(&self.text(), 0).is_ok_and(|round_trip| round_trip == *self) | ||
| 351 | + } | ||
| 352 | + | ||
| 341 | /// Strict: exactly one optional `ctrl+` and then a key gpui could have | 353 | /// Strict: exactly one optional `ctrl+` and then a key gpui could have |
| 342 | /// reported. A spelling this cannot emit back unchanged is refused rather | 354 | /// reported. A spelling this cannot emit back unchanged is refused rather |
| 343 | /// than normalised, because the round-trip is what makes the file editable | 355 | /// than normalised, because the round-trip is what makes the file editable |
| @@ -527,7 +539,17 @@ impl Keymap { | |||
| 527 | } | 539 | } |
| 528 | }; | 540 | }; |
| 529 | match parse(&text) { | 541 | match parse(&text) { |
| 530 | - Ok(keymap) => (keymap, None), | 542 | + Ok((keymap, dropped)) if dropped.is_empty() => (keymap, None), |
| 543 | + // The lines it could use are in force; the ones it could not are | ||
| 544 | + // named, and only those are lost. | ||
| 545 | + Ok((keymap, dropped)) => ( | ||
| 546 | + keymap, | ||
| 547 | + Some(format!( | ||
| 548 | + "KEYBINDINGS PART-READ — {}, {} — those strokes are unbound", | ||
| 549 | + path.display(), | ||
| 550 | + dropped.join("; ") | ||
| 551 | + )), | ||
| 552 | + ), | ||
| 531 | Err(e) => ( | 553 | Err(e) => ( |
| 532 | Self::defaults(), | 554 | Self::defaults(), |
| 533 | Some(format!( | 555 | Some(format!( |
| @@ -613,7 +635,13 @@ fn emit(keymap: &Keymap) -> String { | |||
| 613 | out | 635 | out |
| 614 | } | 636 | } |
| 615 | 637 | ||
| 616 | -fn parse(text: &str) -> Result<Keymap, String> { | 638 | +/// The magic line is the whole of what makes this a keybindings file, so it is |
| 639 | +/// the only thing refused outright. A *binding* line this parser cannot use is | ||
| 640 | +/// dropped instead, and named in the returned warnings: one unusable line used | ||
| 641 | +/// to cost every other rebind in the file, which is the user's data. What | ||
| 642 | +/// survives is still exactly what emits back, so the by-hand round-trip holds | ||
| 643 | +/// for every line that is kept. | ||
| 644 | +fn parse(text: &str) -> Result<(Keymap, Vec<String>), String> { | ||
| 617 | // One trailing newline terminates the last line rather than starting an | 645 | // One trailing newline terminates the last line rather than starting an |
| 618 | // empty one; a second is an empty line and is refused below. | 646 | // empty one; a second is an empty line and is refused below. |
| 619 | let body = text.strip_suffix('\n').unwrap_or(text); | 647 | let body = text.strip_suffix('\n').unwrap_or(text); |
| @@ -627,26 +655,38 @@ fn parse(text: &str) -> Result<Keymap, String> { | |||
| 627 | } | 655 | } |
| 628 | 656 | ||
| 629 | let mut bindings: Vec<Binding> = Vec::new(); | 657 | let mut bindings: Vec<Binding> = Vec::new(); |
| 658 | + let mut dropped: Vec<String> = Vec::new(); | ||
| 630 | for (i, line) in lines { | 659 | for (i, line) in lines { |
| 631 | let n = i + 1; | 660 | let n = i + 1; |
| 632 | let Some((name, chord)) = line.split_once(' ') else { | 661 | let Some((name, chord)) = line.split_once(' ') else { |
| 633 | - return Err(format!("line {n}: {line:?} is not `<action> <chord>`")); | 662 | + dropped.push(format!("line {n}: {line:?} is not `<action> <chord>`")); |
| 663 | + continue; | ||
| 664 | + }; | ||
| 665 | + let Some(action) = ActionId::from_name(name) else { | ||
| 666 | + dropped.push(format!("line {n}: unknown action {name:?}")); | ||
| 667 | + continue; | ||
| 668 | + }; | ||
| 669 | + let chord = match Chord::parse(chord, n) { | ||
| 670 | + Ok(chord) => chord, | ||
| 671 | + Err(e) => { | ||
| 672 | + dropped.push(e); | ||
| 673 | + continue; | ||
| 674 | + } | ||
| 634 | }; | 675 | }; |
| 635 | - let action = ActionId::from_name(name) | ||
| 636 | - .ok_or_else(|| format!("line {n}: unknown action {name:?}"))?; | ||
| 637 | - let chord = Chord::parse(chord, n)?; | ||
| 638 | // One stroke, one meaning: the second line to claim it would be the | 676 | // One stroke, one meaning: the second line to claim it would be the |
| 639 | - // dead one, and which of them died would depend on line order. | 677 | + // dead one, and which of them died would depend on line order. The |
| 678 | + // first line keeps the stroke, which is the order the file reads in. | ||
| 640 | if let Some(held) = bindings.iter().find(|b| b.chord == chord) { | 679 | if let Some(held) = bindings.iter().find(|b| b.chord == chord) { |
| 641 | - return Err(format!( | 680 | + dropped.push(format!( |
| 642 | "line {n}: {} is already {}'s", | 681 | "line {n}: {} is already {}'s", |
| 643 | chord.text(), | 682 | chord.text(), |
| 644 | held.action.name() | 683 | held.action.name() |
| 645 | )); | 684 | )); |
| 685 | + continue; | ||
| 646 | } | 686 | } |
| 647 | bindings.push(Binding { action, chord }); | 687 | bindings.push(Binding { action, chord }); |
| 648 | } | 688 | } |
| 649 | - Ok(Keymap { bindings }) | 689 | + Ok((Keymap { bindings }, dropped)) |
| 650 | } | 690 | } |
| 651 | 691 | ||
| 652 | #[cfg(test)] | 692 | #[cfg(test)] |
| @@ -660,6 +700,14 @@ mod tests { | |||
| 660 | } | 700 | } |
| 661 | } | 701 | } |
| 662 | 702 | ||
| 703 | + /// A file every line of which the parser could use -- what most assertions | ||
| 704 | + /// are about. A dropped line here is a failure, not a warning. | ||
| 705 | + fn whole(text: &str) -> Keymap { | ||
| 706 | + let (keymap, dropped) = parse(text).expect("a keybindings file"); | ||
| 707 | + assert!(dropped.is_empty(), "unexpected dropped lines: {dropped:?}"); | ||
| 708 | + keymap | ||
| 709 | + } | ||
| 710 | + | ||
| 663 | #[test] | 711 | #[test] |
| 664 | fn every_default_stroke_reaches_its_action() { | 712 | fn every_default_stroke_reaches_its_action() { |
| 665 | let k = Keymap::defaults(); | 713 | let k = Keymap::defaults(); |
| @@ -716,55 +764,116 @@ mod tests { | |||
| 716 | assert!(text.contains("cancel-export escape\n")); | 764 | assert!(text.contains("cancel-export escape\n")); |
| 717 | // Both directions: the file this writes is the file it reads back, and | 765 | // Both directions: the file this writes is the file it reads back, and |
| 718 | // a file read in comes out byte for byte as it went. | 766 | // a file read in comes out byte for byte as it went. |
| 719 | - assert_eq!(parse(&text), Ok(Keymap::defaults())); | 767 | + assert_eq!(whole(&text), Keymap::defaults()); |
| 720 | - assert_eq!(emit(&parse(&text).unwrap()), text); | 768 | + assert_eq!(emit(&whole(&text)), text); |
| 721 | let handwritten = "edith-keys 1\nundo ctrl+z\nplay space\n"; | 769 | let handwritten = "edith-keys 1\nundo ctrl+z\nplay space\n"; |
| 722 | - assert_eq!(emit(&parse(handwritten).unwrap()), handwritten); | 770 | + assert_eq!(emit(&whole(handwritten)), handwritten); |
| 723 | // A keymap that binds nothing is legal -- and unusable, which is the | 771 | // A keymap that binds nothing is legal -- and unusable, which is the |
| 724 | // user's business, not the parser's. | 772 | // user's business, not the parser's. |
| 725 | - assert_eq!(parse("edith-keys 1\n").unwrap().entries(), &[]); | 773 | + assert_eq!(whole("edith-keys 1\n").entries(), &[]); |
| 726 | } | 774 | } |
| 727 | 775 | ||
| 728 | #[test] | 776 | #[test] |
| 729 | fn every_refusal_names_its_line() { | 777 | fn every_refusal_names_its_line() { |
| 778 | + // Only the first line refuses the whole file. | ||
| 730 | let err = |text: &str| parse(text).unwrap_err(); | 779 | let err = |text: &str| parse(text).unwrap_err(); |
| 731 | assert_eq!(err(""), "line 1: not a keybindings file"); | 780 | assert_eq!(err(""), "line 1: not a keybindings file"); |
| 732 | assert_eq!(err("nonsense\n"), "line 1: not a keybindings file"); | 781 | assert_eq!(err("nonsense\n"), "line 1: not a keybindings file"); |
| 733 | assert_eq!(err("edith-keys 2\n"), "line 1: unsupported version 2"); | 782 | assert_eq!(err("edith-keys 2\n"), "line 1: unsupported version 2"); |
| 783 | + | ||
| 784 | + // Every other kind of bad line costs that line and nothing else. | ||
| 785 | + let dropped = |text: &str| parse(text).unwrap().1; | ||
| 734 | assert_eq!( | 786 | assert_eq!( |
| 735 | - err("edith-keys 1\nfly space\n"), | 787 | + dropped("edith-keys 1\nfly space\n"), |
| 736 | - "line 2: unknown action \"fly\"" | 788 | + ["line 2: unknown action \"fly\""] |
| 737 | ); | 789 | ); |
| 738 | assert_eq!( | 790 | assert_eq!( |
| 739 | - err("edith-keys 1\nplay space\nplayspace\n"), | 791 | + dropped("edith-keys 1\nplay space\nplayspace\n"), |
| 740 | - "line 3: \"playspace\" is not `<action> <chord>`" | 792 | + ["line 3: \"playspace\" is not `<action> <chord>`"] |
| 741 | ); | 793 | ); |
| 742 | // An empty line is not optional whitespace, it is a line. | 794 | // An empty line is not optional whitespace, it is a line. |
| 743 | assert_eq!( | 795 | assert_eq!( |
| 744 | - err("edith-keys 1\n\n"), | 796 | + dropped("edith-keys 1\n\n"), |
| 745 | - "line 2: \"\" is not `<action> <chord>`" | 797 | + ["line 2: \"\" is not `<action> <chord>`"] |
| 746 | ); | 798 | ); |
| 747 | assert_eq!( | 799 | assert_eq!( |
| 748 | - err("edith-keys 1\nplay ctrl+alt+x\n"), | 800 | + dropped("edith-keys 1\nplay ctrl+alt+x\n"), |
| 749 | - "line 2: \"ctrl+alt+x\" is not a chord" | 801 | + ["line 2: \"ctrl+alt+x\" is not a chord"] |
| 750 | ); | 802 | ); |
| 751 | assert_eq!( | 803 | assert_eq!( |
| 752 | - err("edith-keys 1\nplay ctrl+\n"), | 804 | + dropped("edith-keys 1\nplay ctrl+\n"), |
| 753 | - "line 2: \"ctrl+\" is not a chord" | 805 | + ["line 2: \"ctrl+\" is not a chord"] |
| 754 | ); | 806 | ); |
| 755 | // gpui reports lowercase key names, so an uppercase one would simply | 807 | // gpui reports lowercase key names, so an uppercase one would simply |
| 756 | - // never match -- refused rather than quietly dead. | 808 | + // never match -- dropped rather than quietly dead. |
| 757 | assert_eq!( | 809 | assert_eq!( |
| 758 | - err("edith-keys 1\nplay X\n"), | 810 | + dropped("edith-keys 1\nplay X\n"), |
| 759 | - "line 2: \"X\" is not a chord" | 811 | + ["line 2: \"X\" is not a chord"] |
| 760 | ); | 812 | ); |
| 761 | - // The same stroke twice: whichever line lost would depend on order. | 813 | + // The same stroke twice: whichever line lost would depend on order, so |
| 814 | + // the second one is the one that goes. | ||
| 762 | assert_eq!( | 815 | assert_eq!( |
| 763 | - err("edith-keys 1\nplay space\ncut space\n"), | 816 | + dropped("edith-keys 1\nplay space\ncut space\n"), |
| 764 | - "line 3: space is already play's" | 817 | + ["line 3: space is already play's"] |
| 765 | ); | 818 | ); |
| 766 | } | 819 | } |
| 767 | 820 | ||
| 821 | + /// The rebind data-loss path: one line the parser cannot use must not cost | ||
| 822 | + /// the rebinds around it (a `capture` of shift+= wrote `play +`, and the | ||
| 823 | + /// next load threw the whole file away and went back to the defaults). | ||
| 824 | + #[test] | ||
| 825 | + fn one_bad_line_costs_only_that_line() { | ||
| 826 | + let text = "edith-keys 1\nplay +\nexport ctrl+w\ncut j\n"; | ||
| 827 | + let (keymap, dropped) = parse(text).expect("the magic line is still good"); | ||
| 828 | + assert_eq!(dropped, ["line 2: \"+\" is not a chord"]); | ||
| 829 | + // The lines around it are in force, which is the whole point. | ||
| 830 | + assert_eq!(keymap.lookup("w", true), Some(ActionId::Export)); | ||
| 831 | + assert_eq!(keymap.lookup("j", false), Some(ActionId::Cut)); | ||
| 832 | + // ...and the one that went is unbound, not silently something else. | ||
| 833 | + assert_eq!(keymap.display(ActionId::Play), "unbound"); | ||
| 834 | + // What survives still round-trips: the kept lines emit back as read. | ||
| 835 | + assert_eq!(emit(&keymap), "edith-keys 1\nexport ctrl+w\ncut j\n"); | ||
| 836 | + | ||
| 837 | + // The notice names the line and the file, and the defaults do *not* | ||
| 838 | + // take over -- that was the data loss. | ||
| 839 | + let dir = std::env::temp_dir().join(format!("edith-keymap-drop-{}", std::process::id())); | ||
| 840 | + let path = dir.join("edith").join("keybindings"); | ||
| 841 | + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); | ||
| 842 | + std::fs::write(&path, text).unwrap(); | ||
| 843 | + let (read, notice) = Keymap::load_from(&path); | ||
| 844 | + assert_eq!(read, keymap); | ||
| 845 | + let notice = notice.expect("a part-read file must say so"); | ||
| 846 | + assert!(notice.contains("line 2:"), "{notice}"); | ||
| 847 | + assert!(notice.contains("keybindings"), "{notice}"); | ||
| 848 | + std::fs::remove_dir_all(&dir).ok(); | ||
| 849 | + } | ||
| 850 | + | ||
| 851 | + /// The other half of the same bug: a stroke the file cannot spell is refused | ||
| 852 | + /// at the capture, before it can ever be written. | ||
| 853 | + #[test] | ||
| 854 | + fn only_a_stroke_the_file_can_spell_is_bindable() { | ||
| 855 | + for (key, ctrl) in [ | ||
| 856 | + ("space", false), | ||
| 857 | + ("=", false), | ||
| 858 | + ("s", true), | ||
| 859 | + ("escape", false), | ||
| 860 | + ] { | ||
| 861 | + assert!(chord(key, ctrl).bindable(), "{key}"); | ||
| 862 | + } | ||
| 863 | + // gpui's name for shift+=, and the grammar's own separator. | ||
| 864 | + assert!(!chord("+", false).bindable()); | ||
| 865 | + assert!(!chord("+", true).bindable()); | ||
| 866 | + // The rest of what `Chord::parse` will not take back. | ||
| 867 | + assert!(!chord("", false).bindable()); | ||
| 868 | + assert!(!chord("X", false).bindable()); | ||
| 869 | + assert!(!chord("page up", false).bindable()); | ||
| 870 | + // A bindable chord is one `rebind_action` may keep: what it writes is | ||
| 871 | + // what the next load reads, for every stroke that passes here. | ||
| 872 | + let mut k = Keymap::defaults(); | ||
| 873 | + k.rebind_action(ActionId::Play, chord("=", true)).unwrap(); | ||
| 874 | + assert_eq!(whole(&emit(&k)), k); | ||
| 875 | + } | ||
| 876 | + | ||
| 768 | #[test] | 877 | #[test] |
| 769 | fn rebinding_refuses_to_steal_another_actions_stroke() { | 878 | fn rebinding_refuses_to_steal_another_actions_stroke() { |
| 770 | let mut k = Keymap::defaults(); | 879 | let mut k = Keymap::defaults(); |
| @@ -801,7 +910,7 @@ mod tests { | |||
| 801 | // reading in that order too. | 910 | // reading in that order too. |
| 802 | let order: Vec<_> = k.entries().iter().map(|b| b.action).collect(); | 911 | let order: Vec<_> = k.entries().iter().map(|b| b.action).collect(); |
| 803 | assert_eq!(order, ActionId::ALL.to_vec()); | 912 | assert_eq!(order, ActionId::ALL.to_vec()); |
| 804 | - assert_eq!(emit(&k), emit(&parse(&emit(&k)).unwrap())); | 913 | + assert_eq!(emit(&k), emit(&whole(&emit(&k)))); |
| 805 | // Every action is now single-bound, so the file is one line each. | 914 | // Every action is now single-bound, so the file is one line each. |
| 806 | assert_eq!(k.entries().len(), ActionId::ALL.len()); | 915 | assert_eq!(k.entries().len(), ActionId::ALL.len()); |
| 807 | } | 916 | } |
| @@ -815,10 +924,7 @@ mod tests { | |||
| 815 | assert_eq!(k.display(ActionId::Undo), "z or ctrl+z"); | 924 | assert_eq!(k.display(ActionId::Undo), "z or ctrl+z"); |
| 816 | // The one key whose common name is not gpui's. | 925 | // The one key whose common name is not gpui's. |
| 817 | assert_eq!(k.display(ActionId::CancelExport), "esc"); | 926 | assert_eq!(k.display(ActionId::CancelExport), "esc"); |
| 818 | - assert_eq!( | 927 | + assert_eq!(whole("edith-keys 1\n").display(ActionId::Cut), "unbound"); |
| 819 | - parse("edith-keys 1\n").unwrap().display(ActionId::Cut), | ||
| 820 | - "unbound" | ||
| 821 | - ); | ||
| 822 | // The label is the editor's column, and never the file's word for it. | 928 | // The label is the editor's column, and never the file's word for it. |
| 823 | assert_eq!(ActionId::CancelExport.label(), "Cancel export"); | 929 | assert_eq!(ActionId::CancelExport.label(), "Cancel export"); |
| 824 | assert_eq!(ActionId::ALL.len(), 23); | 930 | assert_eq!(ActionId::ALL.len(), 23); |
| @@ -846,11 +952,20 @@ mod tests { | |||
| 846 | let (fresh, notice) = Keymap::load_from(&dir.join("nothing-here")); | 952 | let (fresh, notice) = Keymap::load_from(&dir.join("nothing-here")); |
| 847 | assert_eq!(fresh, Keymap::defaults()); | 953 | assert_eq!(fresh, Keymap::defaults()); |
| 848 | assert_eq!(notice, None); | 954 | assert_eq!(notice, None); |
| 849 | - // A corrupt one names its line and still leaves a usable keymap. | 955 | + // A file that is not one at all falls back whole, and names its line. |
| 850 | - std::fs::write(&path, "edith-keys 1\nplay ctrl+alt+x\n").unwrap(); | 956 | + std::fs::write(&path, "edith-keys 9\nplay space\n").unwrap(); |
| 851 | let (fallback, notice) = Keymap::load_from(&path); | 957 | let (fallback, notice) = Keymap::load_from(&path); |
| 852 | assert_eq!(fallback, Keymap::defaults()); | 958 | assert_eq!(fallback, Keymap::defaults()); |
| 853 | let notice = notice.expect("a refused file must say so"); | 959 | let notice = notice.expect("a refused file must say so"); |
| 960 | + assert!(notice.contains("line 1:"), "{notice}"); | ||
| 961 | + assert!(notice.contains("keybindings"), "{notice}"); | ||
| 962 | + // One unusable line inside a good file costs that line only -- the | ||
| 963 | + // defaults do not come back over the rebinds that read fine. | ||
| 964 | + std::fs::write(&path, "edith-keys 1\nplay ctrl+alt+x\nexport ctrl+w\n").unwrap(); | ||
| 965 | + let (kept, notice) = Keymap::load_from(&path); | ||
| 966 | + assert_eq!(kept.lookup("w", true), Some(ActionId::Export)); | ||
| 967 | + assert_eq!(kept.display(ActionId::Play), "unbound"); | ||
| 968 | + let notice = notice.expect("a part-read file must say so"); | ||
| 854 | assert!(notice.contains("line 2:"), "{notice}"); | 969 | assert!(notice.contains("line 2:"), "{notice}"); |
| 855 | assert!(notice.contains("keybindings"), "{notice}"); | 970 | assert!(notice.contains("keybindings"), "{notice}"); |
| 856 | // Saving over it is a whole replacement, not an edit. | 971 | // Saving over it is a whole replacement, not an edit. |
crates/app/src/main.rsmodified+11 -0
| @@ -1299,6 +1299,17 @@ impl Player { | |||
| 1299 | key: key.to_string(), | 1299 | key: key.to_string(), |
| 1300 | ctrl, | 1300 | ctrl, |
| 1301 | }; | 1301 | }; |
| 1302 | + // Only a stroke the file can spell and read back as itself: gpui reports | ||
| 1303 | + // "+" for shift+=, which is the chord grammar's separator, so binding it | ||
| 1304 | + // would write a line the next load would have to drop. Refused here, in | ||
| 1305 | + // front of the user, rather than silently costing that binding later. | ||
| 1306 | + // The row keeps waiting, as it does for a stroke already taken. | ||
| 1307 | + if !chord.bindable() { | ||
| 1308 | + let text = format!("THAT KEY CANNOT BE BOUND — {}", chord.pretty()); | ||
| 1309 | + eprintln!("{text}"); | ||
| 1310 | + self.notice = Some(text.into()); | ||
| 1311 | + return; | ||
| 1312 | + } | ||
| 1302 | let text = match self.keymap.rebind_action(action, chord.clone()) { | 1313 | let text = match self.keymap.rebind_action(action, chord.clone()) { |
| 1303 | Ok(()) => { | 1314 | Ok(()) => { |
| 1304 | self.rebinding = None; | 1315 | self.rebinding = None; |
crates/engine/src/edith.rsmodified+4 -1
| @@ -341,7 +341,10 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> { | |||
| 341 | } | 341 | } |
| 342 | let f = fields(rest, 2, "resolution", n)?; | 342 | let f = fields(rest, 2, "resolution", n)?; |
| 343 | let (width, height) = (number(f[0], n)?, number(f[1], n)?); | 343 | let (width, height) = (number(f[0], n)?, number(f[1], n)?); |
| 344 | - if width == 0 || height == 0 { | 344 | + // The same bound the keystroke has (`crate::is_resolution`): |
| 345 | + // unbounded here, this line reached `open_black` and panicked | ||
| 346 | + // the open with a capacity overflow. | ||
| 347 | + if !crate::is_resolution(width, height) { | ||
| 345 | return Err(format!("line {n}: {width}x{height} is not a picture").into()); | 348 | return Err(format!("line {n}: {width}x{height} is not a picture").into()); |
| 346 | } | 349 | } |
| 347 | doc.resolution = Some((width, height)); | 350 | doc.resolution = Some((width, height)); |
crates/engine/src/lib.rsmodified+14 -0
| @@ -46,6 +46,20 @@ pub fn is_audio(path: &std::path::Path) -> bool { | |||
| 46 | }) | 46 | }) |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | +/// The largest picture this engine composes onto, either way: 8K, which is where | ||
| 50 | +/// a per-frame buffer stops being a sane thing to allocate. | ||
| 51 | +pub const MAX_RESOLUTION: u32 = 7680; | ||
| 52 | + | ||
| 53 | +/// Whether a project resolution is a picture at all: not zero either way, and | ||
| 54 | +/// not past [`MAX_RESOLUTION`]. Both doors ask -- the keystroke | ||
| 55 | +/// ([`PlaybackSession::set_resolution`](playback::PlaybackSession::set_resolution)) | ||
| 56 | +/// and the file ([`edith::load`]) -- so a hand-written `resolution` line cannot | ||
| 57 | +/// reach an allocation a keypress could not, which is how `4294967295` used to | ||
| 58 | +/// panic the open with a capacity overflow. | ||
| 59 | +pub fn is_resolution(width: u32, height: u32) -> bool { | ||
| 60 | + (1..=MAX_RESOLUTION).contains(&width) && (1..=MAX_RESOLUTION).contains(&height) | ||
| 61 | +} | ||
| 62 | + | ||
| 49 | /// Boxed error; the engine has few failure modes and every one of them is fatal | 63 | /// Boxed error; the engine has few failure modes and every one of them is fatal |
| 50 | /// to the session, so a message is all a caller can act on. | 64 | /// to the session, so a message is all a caller can act on. |
| 51 | pub type Error = Box<dyn std::error::Error + Send + Sync>; | 65 | pub type Error = Box<dyn std::error::Error + Send + Sync>; |
crates/engine/src/playback.rsmodified+1 -2
| @@ -638,8 +638,7 @@ impl PlaybackSession { | |||
| 638 | /// keypress rather than a `z`. Upgrade path is snapshotting it beside the | 638 | /// keypress rather than a `z`. Upgrade path is snapshotting it beside the |
| 639 | /// lanes, which every existing snapshot site would then have to carry. | 639 | /// lanes, which every existing snapshot site would then have to carry. |
| 640 | pub fn set_resolution(&mut self, width: u32, height: u32) -> bool { | 640 | pub fn set_resolution(&mut self, width: u32, height: u32) -> bool { |
| 641 | - const MAX: u32 = 7680; | 641 | + if !crate::is_resolution(width, height) { |
| 642 | - if width == 0 || height == 0 || width > MAX || height > MAX { | ||
| 643 | return false; | 642 | return false; |
| 644 | } | 643 | } |
| 645 | if (width, height) == (self.meta.width, self.meta.height) { | 644 | if (width, height) == (self.meta.width, self.meta.height) { |
crates/engine/src/project.rsmodified+107 -10
| @@ -1002,28 +1002,60 @@ impl Project { | |||
| 1002 | /// | 1002 | /// |
| 1003 | /// `V1` and `A1` and no other lane, because a take is one picture and one | 1003 | /// `V1` and `A1` and no other lane, because a take is one picture and one |
| 1004 | /// sound: copying it onto every lane there is would play the same audio | 1004 | /// sound: copying it onto every lane there is would play the same audio |
| 1005 | - /// twice over (and leave an mp4 export with two tracks to copy). The room is | 1005 | + /// twice over (and leave an mp4 export with two tracks to copy). A clip of a |
| 1006 | - /// still opened everywhere, or the lanes it was not inserted into would slide | 1006 | + /// source with no picture ([`crate::is_audio`]) reaches `A1` only -- on a |
| 1007 | - /// out of step with the ones it was. | 1007 | + /// video lane it is a clip that decodes to nothing, and a save carrying one |
| 1008 | + /// does not open again. The room is still opened everywhere, or the lanes it | ||
| 1009 | + /// was not inserted into would slide out of step with the ones it was. | ||
| 1008 | /// | 1010 | /// |
| 1009 | /// Exactly one history snapshot, so one [`Project::undo`] takes it back. | 1011 | /// Exactly one history snapshot, so one [`Project::undo`] takes it back. |
| 1010 | - /// Changes the timeline->source mapping: the caller must reseek. Refused | 1012 | + /// Changes the timeline->source mapping: the caller must reseek. Refused for |
| 1011 | - /// only for an empty `clip`, which the never-empty invariant forbids but the | 1013 | + /// an empty `clip` and for one naming a source that is not there (both of |
| 1012 | - /// public fields still allow a caller to build. | 1014 | + /// which the public fields let a caller build), and for a paste whose ripple |
| 1015 | + /// would push a clip past the last frame there is -- nothing is changed by | ||
| 1016 | + /// any of them. | ||
| 1013 | pub fn paste(&mut self, timeline_frame: u32, clip: Clip) -> bool { | 1017 | pub fn paste(&mut self, timeline_frame: u32, clip: Clip) -> bool { |
| 1018 | + let Some(source) = self.sources.get(clip.source) else { | ||
| 1019 | + return false; | ||
| 1020 | + }; | ||
| 1014 | if clip.out_frame <= clip.in_frame { | 1021 | if clip.out_frame <= clip.in_frame { |
| 1015 | return false; | 1022 | return false; |
| 1016 | } | 1023 | } |
| 1017 | - self.snapshot(); | 1024 | + let audio_only = crate::is_audio(&source.path); |
| 1018 | let at = timeline_frame.min(self.timeline_frames()); | 1025 | let at = timeline_frame.min(self.timeline_frames()); |
| 1026 | + let len = clip.len(); | ||
| 1027 | + // Room for it: `open_room` adds `len` to every start from `at` on, and a | ||
| 1028 | + // hand-written file may hold a clip that ends at the very last frame | ||
| 1029 | + // (`edith::check` permits `start + len == u32::MAX`). Refused here | ||
| 1030 | + // rather than wrapped there -- a wrapped start lands *behind* the clip | ||
| 1031 | + // it belongs after, which is an overlap no save will read back. | ||
| 1032 | + if at.checked_add(len).is_none() | ||
| 1033 | + || self | ||
| 1034 | + .lanes | ||
| 1035 | + .iter() | ||
| 1036 | + .flat_map(|l| &l.clips) | ||
| 1037 | + .any(|c| c.end() > at && c.end().checked_add(len).is_none()) | ||
| 1038 | + { | ||
| 1039 | + return false; | ||
| 1040 | + } | ||
| 1041 | + self.snapshot(); | ||
| 1019 | let clip = Clip { | 1042 | let clip = Clip { |
| 1020 | start: at, | 1043 | start: at, |
| 1021 | link: Some(self.new_link()), | 1044 | link: Some(self.new_link()), |
| 1022 | ..clip | 1045 | ..clip |
| 1023 | }; | 1046 | }; |
| 1024 | - let takes: Vec<usize> = [LaneKind::Video, LaneKind::Audio] | 1047 | + // Only lanes that can play the source: a file with no picture belongs on |
| 1025 | - .into_iter() | 1048 | + // an audio lane and nowhere else, exactly as `place_stream_at` decides |
| 1026 | - .filter_map(|kind| self.index(Lane::new(kind, 0))) | 1049 | + // it for an import. On a video lane it would decode to nothing, and the |
| 1050 | + // save it wrote would not open again. | ||
| 1051 | + let kinds: &[LaneKind] = if audio_only { | ||
| 1052 | + &[LaneKind::Audio] | ||
| 1053 | + } else { | ||
| 1054 | + &[LaneKind::Video, LaneKind::Audio] | ||
| 1055 | + }; | ||
| 1056 | + let takes: Vec<usize> = kinds | ||
| 1057 | + .iter() | ||
| 1058 | + .filter_map(|&kind| self.index(Lane::new(kind, 0))) | ||
| 1027 | .collect(); | 1059 | .collect(); |
| 1028 | for (i, data) in self.lanes.iter_mut().enumerate() { | 1060 | for (i, data) in self.lanes.iter_mut().enumerate() { |
| 1029 | open_room(&mut data.clips, at, clip.len()); | 1061 | open_room(&mut data.clips, at, clip.len()); |
| @@ -1748,6 +1780,71 @@ mod tests { | |||
| 1748 | assert_eq!(p.clips().len(), 3); | 1780 | assert_eq!(p.clips().len(), 3); |
| 1749 | } | 1781 | } |
| 1750 | 1782 | ||
| 1783 | + /// The data-loss paste: a clip of a source with no picture used to land on | ||
| 1784 | + /// `V1` as well as `A1`, where it decodes to nothing -- and the save that | ||
| 1785 | + /// carried it was refused on the way back in ("a box with a larger size"). | ||
| 1786 | + #[test] | ||
| 1787 | + fn a_paste_of_a_source_with_no_picture_stays_off_the_video_lane() { | ||
| 1788 | + let mut p = three(); | ||
| 1789 | + let wav = p.import("/nonexistent/song.wav", 0); | ||
| 1790 | + let copied = Clip { | ||
| 1791 | + source: wav, | ||
| 1792 | + ..PASTED | ||
| 1793 | + }; | ||
| 1794 | + assert!(p.paste(3, copied)); | ||
| 1795 | + // The room is opened on every lane, or the two would slide apart... | ||
| 1796 | + assert_eq!(p.timeline_frames(), 11); | ||
| 1797 | + assert_eq!( | ||
| 1798 | + shape(&p)[0], | ||
| 1799 | + vec![clip(0, 0, 3, 0), clip(5, 3, 5, 0), clip(7, 5, 9, 0)], | ||
| 1800 | + "the video lane got room and nothing else" | ||
| 1801 | + ); | ||
| 1802 | + // ...but the clip itself only reaches the lane that can play it. | ||
| 1803 | + assert_eq!( | ||
| 1804 | + shape(&p)[1], | ||
| 1805 | + vec![ | ||
| 1806 | + clip(0, 0, 3, 0), | ||
| 1807 | + clip(3, 100, 102, wav), | ||
| 1808 | + clip(5, 3, 5, 0), | ||
| 1809 | + clip(7, 5, 9, 0) | ||
| 1810 | + ] | ||
| 1811 | + ); | ||
| 1812 | + // A source with a picture is unchanged: still the grouped pair. | ||
| 1813 | + let mut p = three(); | ||
| 1814 | + assert!(p.paste(3, PASTED)); | ||
| 1815 | + assert_eq!(shape(&p)[0], shape(&p)[1]); | ||
| 1816 | + } | ||
| 1817 | + | ||
| 1818 | + /// A hand-written file may hold a clip that ends at the very last frame | ||
| 1819 | + /// (`edith::check` permits `start + len == u32::MAX`), and the ripple used | ||
| 1820 | + /// to wrap its start round to the front of the lane: an overlap in debug, a | ||
| 1821 | + /// project no reload would accept in release. | ||
| 1822 | + #[test] | ||
| 1823 | + fn a_paste_that_would_run_past_the_last_frame_is_refused() { | ||
| 1824 | + let far = clip(u32::MAX - 90, 0, 90, 0); | ||
| 1825 | + let parts = |c: Clip| { | ||
| 1826 | + Project::from_parts( | ||
| 1827 | + vec![Source::new(FILE, 0)], | ||
| 1828 | + two(vec![c], vec![c]), | ||
| 1829 | + Vec::new(), | ||
| 1830 | + Vec::new(), | ||
| 1831 | + ) | ||
| 1832 | + .expect("valid parts") | ||
| 1833 | + }; | ||
| 1834 | + let mut p = parts(far); | ||
| 1835 | + assert!(!p.paste(0, PASTED), "no room for the two frames it adds"); | ||
| 1836 | + assert_eq!(shape(&p)[0], vec![far], "a refusal changes nothing"); | ||
| 1837 | + assert!(!p.undo(), "...and pushes no history either"); | ||
| 1838 | + assert!( | ||
| 1839 | + !p.paste(u32::MAX, PASTED), | ||
| 1840 | + "appending has nowhere to go either: the timeline ends at the last frame" | ||
| 1841 | + ); | ||
| 1842 | + // With room for exactly those frames the same paste is taken. | ||
| 1843 | + let mut p = parts(clip(u32::MAX - 92, 0, 90, 0)); | ||
| 1844 | + assert!(p.paste(0, PASTED)); | ||
| 1845 | + assert_eq!(p.lane(Lane::V1)[1].end(), u32::MAX); | ||
| 1846 | + } | ||
| 1847 | + | ||
| 1751 | #[test] | 1848 | #[test] |
| 1752 | fn paste_undoes_in_one_step() { | 1849 | fn paste_undoes_in_one_step() { |
| 1753 | let mut p = three(); | 1850 | let mut p = three(); |
crates/engine/tests/audio_files.rsmodified+45 -0
| @@ -245,3 +245,48 @@ fn a_song_survives_a_save_and_a_reload() { | |||
| 245 | ); | 245 | ); |
| 246 | let _ = std::fs::remove_dir_all(&dir); | 246 | let _ = std::fs::remove_dir_all(&dir); |
| 247 | } | 247 | } |
| 248 | + | ||
| 249 | +/// ...and the door the editor itself had onto that same broken project: a copy | ||
| 250 | +/// of the song pasted from the clipboard used to land on `V1` as well as `A1`, | ||
| 251 | +/// where it decodes to nothing -- and the save it wrote was refused on the way | ||
| 252 | +/// back in. The paste is the plain UI path, so this is data loss with no | ||
| 253 | +/// hand-editing anywhere in it. | ||
| 254 | +#[test] | ||
| 255 | +fn a_pasted_song_never_lands_on_the_video_lane() { | ||
| 256 | + let dir = std::env::temp_dir().join(format!("ve_audio_paste_{}", std::process::id())); | ||
| 257 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 258 | + std::fs::create_dir_all(&dir).expect("scratch dir"); | ||
| 259 | + let copy = |name: &str| { | ||
| 260 | + let to = dir.join(name); | ||
| 261 | + std::fs::copy(asset(name), &to).expect("copy the fixture"); | ||
| 262 | + to | ||
| 263 | + }; | ||
| 264 | + | ||
| 265 | + let mut session = open(copy("test_av.mp4")); | ||
| 266 | + session.import(©("test_tone.flac")).expect("import"); | ||
| 267 | + let song = session.lane_clips(Lane::A1)[1]; | ||
| 268 | + let video_before = session.lane_clips(Lane::V1).to_vec(); | ||
| 269 | + assert!(session.paste_at(0.0, song), "paste the copied song"); | ||
| 270 | + | ||
| 271 | + // The video lane got room and nothing else: the same clips, pushed along. | ||
| 272 | + let video_after = session.lane_clips(Lane::V1).to_vec(); | ||
| 273 | + assert_eq!(video_after.len(), video_before.len(), "no clip on V1"); | ||
| 274 | + for (after, before) in video_after.iter().zip(&video_before) { | ||
| 275 | + assert_eq!(after.source, before.source); | ||
| 276 | + assert_eq!(after.start, before.start + song.len()); | ||
| 277 | + } | ||
| 278 | + // The audio lane is the one that gained it, at the playhead. | ||
| 279 | + assert_eq!(session.lane_clips(Lane::A1).len(), 3); | ||
| 280 | + assert_eq!(session.lane_clips(Lane::A1)[0].source, song.source); | ||
| 281 | + | ||
| 282 | + // And the save that follows reopens -- which is what the video-lane clip | ||
| 283 | + // used to cost ("file contains a box with a larger size than it"). | ||
| 284 | + let project = dir.join("pasted.edith"); | ||
| 285 | + session.save_project(&project).expect("save"); | ||
| 286 | + drop(session); | ||
| 287 | + let reloaded = PlaybackSession::open_project(&project).expect("the save reopens"); | ||
| 288 | + reloaded.set_gain(0.0); | ||
| 289 | + assert_eq!(reloaded.lane_clips(Lane::A1).len(), 3); | ||
| 290 | + assert_eq!(reloaded.lane_clips(Lane::V1).len(), video_after.len()); | ||
| 291 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 292 | +} | ||
crates/engine/tests/saveload.rsmodified+12 -0
| @@ -381,6 +381,18 @@ fn a_version_7_project_carries_a_resolution_and_fit_policies() { | |||
| 381 | video 1 0 0 30 0 - - - fit\n", | 381 | video 1 0 0 30 0 - - - fit\n", |
| 382 | "resolution belongs once", | 382 | "resolution belongs once", |
| 383 | ), | 383 | ), |
| 384 | + // Past 8K is refused by the same bound the keystroke has: unbounded, | ||
| 385 | + // this line reached `open_black` and panicked on a capacity overflow. | ||
| 386 | + ( | ||
| 387 | + "edith 7\nresolution 4294967295 4294967295\nsource 0 test_av.mp4\n\ | ||
| 388 | + video 1 0 0 30 0 - - - fit\n", | ||
| 389 | + "4294967295x4294967295 is not a picture", | ||
| 390 | + ), | ||
| 391 | + ( | ||
| 392 | + "edith 7\nresolution 7681 1080\nsource 0 test_av.mp4\n\ | ||
| 393 | + video 1 0 0 30 0 - - - fit\n", | ||
| 394 | + "7681x1080 is not a picture", | ||
| 395 | + ), | ||
| 384 | ( | 396 | ( |
| 385 | "edith 7\nresolution 960 720\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - squish\n", | 397 | "edith 7\nresolution 960 720\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - squish\n", |
| 386 | "not a fit policy", | 398 | "not a fit policy", |