M3.10: autosave and flushDirty

Autosave saves dirty buffers when the window loses focus (DOM blur and
visibilitychange), when the active tab changes and after
editor.autosave.idleSeconds (default 15) without an edit; three settings,
all on by default. flushDirty() in ui/src/stores/autosave.ts is the one
save-all-dirty-and-wait entry point for later deployment, run and
terminal features: it waits for pending buffer sync, then runs
file.saveAll {skipConflicts: true}; one toast per failed file.

Engine: file.saveAll takes skipConflicts and returns skipped; Buffers
tracks buffers in conflict with their file and the hash of the last
read/written content, so our own save reported late is not a conflict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-20 20:59:44 +02:00
parent 228669036c
commit e1dc24dc25
18 changed files with 996 additions and 37 deletions

View file

@ -49,7 +49,10 @@ fn list_describes_the_c2_keys_with_catalog_keys_only() {
"ui.language",
"php.executable",
"phpactor.path",
"phpactor.autoDownload"
"phpactor.autoDownload",
"editor.autosave.onBlur",
"editor.autosave.onTabSwitch",
"editor.autosave.idleSeconds"
]
);
for s in list.as_array().unwrap() {
@ -112,7 +115,10 @@ fn get_set_reset_and_events() {
"ui.language": "en",
"php.executable": "",
"phpactor.path": "",
"phpactor.autoDownload": true
"phpactor.autoDownload": true,
"editor.autosave.onBlur": true,
"editor.autosave.onTabSwitch": true,
"editor.autosave.idleSeconds": 15
})
);
let out = e

View file

@ -265,7 +265,10 @@ fn save_all_saves_every_dirty_buffer() {
);
}
let out = run(&mut engine, "file.saveAll", Value::Null);
assert_eq!(out.value, json!({"saved": [a, b], "failed": []}));
assert_eq!(
out.value,
json!({"saved": [a, b], "failed": [], "skipped": []})
);
assert_eq!(fs::read_to_string(dir.path().join("b.php")).unwrap(), ">b");
}
@ -555,3 +558,104 @@ fn buffer_list_names_open_buffers_without_text() {
);
assert!(out.events.is_empty());
}
#[test]
fn save_all_can_skip_buffers_in_conflict_with_the_disk() {
let (dir, mut engine, _) = open_project(&[("mine.php", b"one"), ("other.php", b"one")]);
let (mine, _) = open_file(&mut engine, "mine.php");
let (other, _) = open_file(&mut engine, "other.php");
for id in [mine, other] {
run(
&mut engine,
"buffer.edit",
json!({"id": id, "baseVersion": 1, "edits": [{"from": 0, "to": 0, "insert": "x"}]}),
);
}
fs::write(dir.path().join("other.php"), "theirs").unwrap();
wait_for(
&mut engine,
|e| matches!(e, Event::FileChangedOnDisk { id, .. } if *id == other),
);
let out = run(&mut engine, "file.saveAll", json!({"skipConflicts": true}));
assert_eq!(
out.value,
json!({"saved": [mine], "failed": [], "skipped": [other]})
);
assert_eq!(
fs::read_to_string(dir.path().join("other.php")).unwrap(),
"theirs"
);
assert_eq!(
fs::read_to_string(dir.path().join("mine.php")).unwrap(),
"xone"
);
run(&mut engine, "file.save", json!({"id": other}));
assert_eq!(
fs::read_to_string(dir.path().join("other.php")).unwrap(),
"xone"
);
run(
&mut engine,
"buffer.edit",
json!({"id": other, "baseVersion": 2, "edits": [{"from": 0, "to": 0, "insert": "y"}]}),
);
let out = run(&mut engine, "file.saveAll", json!({"skipConflicts": true}));
assert_eq!(out.value["saved"], json!([other]));
assert_eq!(out.value["skipped"], json!([]));
let err = engine
.execute("file.saveAll", json!({"skip": true}))
.unwrap_err();
assert_eq!(err.code(), "invalid_params");
}
#[test]
fn a_removed_file_is_skipped_instead_of_recreated() {
let (dir, mut engine, _) = open_project(&[("gone.php", b"one")]);
let (id, _) = open_file(&mut engine, "gone.php");
run(
&mut engine,
"buffer.edit",
json!({"id": id, "baseVersion": 1, "edits": [{"from": 0, "to": 0, "insert": "x"}]}),
);
fs::remove_file(dir.path().join("gone.php")).unwrap();
wait_for(
&mut engine,
|e| matches!(e, Event::FileChangedOnDisk { id: got, removed: true, .. } if *got == id),
);
let out = run(&mut engine, "file.saveAll", json!({"skipConflicts": true}));
assert_eq!(out.value["skipped"], json!([id]));
assert!(!dir.path().join("gone.php").exists());
}
#[test]
fn typing_right_after_a_save_is_not_a_conflict() {
let (_dir, mut engine, _) = open_project(&[("a.php", b"a")]);
let (id, _) = open_file(&mut engine, "a.php");
run(
&mut engine,
"buffer.edit",
json!({"id": id, "baseVersion": 1, "edits": [{"from": 1, "to": 1, "insert": "b"}]}),
);
run(&mut engine, "file.saveAll", json!({"skipConflicts": true}));
run(
&mut engine,
"buffer.edit",
json!({"id": id, "baseVersion": 2, "edits": [{"from": 2, "to": 2, "insert": "c"}]}),
);
let deadline = Instant::now() + Duration::from_millis(600);
while Instant::now() < deadline {
let events = engine.pump_vfs().unwrap();
assert!(
!events
.iter()
.any(|e| matches!(e, Event::FileChangedOnDisk { .. })),
"{events:?}"
);
std::thread::sleep(Duration::from_millis(25));
}
let out = run(&mut engine, "file.saveAll", json!({"skipConflicts": true}));
assert_eq!(out.value["saved"], json!([id]));
}