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

@ -1,6 +1,6 @@
//! Open buffers: the engine's copy of every file the user is editing.
use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use lutra_text::{Buffer, LineEnding};
@ -38,6 +38,16 @@ pub struct Buffers {
by_id: BTreeMap<BufferId, OpenBuffer>,
/// The id the next buffer gets.
next_id: BufferId,
/// Buffers whose file changed or disappeared on disk while they had
/// unsaved changes ([`crate::Event::FileChangedOnDisk`]). The user has to
/// decide, so `file.saveAll {skipConflicts}` leaves them alone until an
/// explicit save or a reload settles it.
conflicts: BTreeSet<BufferId>,
/// What each buffer's file held when Lutra last read or wrote it: the
/// hash ([`crate::journal::hex_hash`]) of the text and the byte order
/// mark. A change on disk that matches it (our own save reported late, a
/// touch) is not a conflict.
written: BTreeMap<BufferId, (String, bool)>,
}
impl Buffers {
@ -61,9 +71,47 @@ impl Buffers {
/// Removes the buffer with `id`.
pub fn remove(&mut self, id: BufferId) -> Option<OpenBuffer> {
self.conflicts.remove(&id);
self.written.remove(&id);
self.by_id.remove(&id)
}
/// Marks buffer `id` as in conflict with its file on disk.
pub fn mark_conflict(&mut self, id: BufferId) {
if self.by_id.contains_key(&id) {
self.conflicts.insert(id);
}
}
/// Clears the disk conflict of buffer `id` (saved or reloaded).
pub fn clear_conflict(&mut self, id: BufferId) {
self.conflicts.remove(&id);
}
/// Whether buffer `id` is in conflict with its file on disk.
pub fn has_conflict(&self, id: BufferId) -> bool {
self.conflicts.contains(&id)
}
/// Remembers what buffer `id`'s file holds after Lutra read or wrote it.
///
/// # Parameters
/// - `hash`: [`crate::journal::hex_hash`] of the text.
/// - `bom`: whether the file has a byte order mark.
pub fn note_written(&mut self, id: BufferId, hash: String, bom: bool) {
if self.by_id.contains_key(&id) {
self.written.insert(id, (hash, bom));
}
}
/// Whether the file content `hash`/`bom` is what Lutra last read or
/// wrote for buffer `id`.
pub fn is_own_write(&self, id: BufferId, hash: &str, bom: bool) -> bool {
self.written
.get(&id)
.is_some_and(|(h, b)| h == hash && *b == bom)
}
/// The id of the buffer for the project-relative `path`, if open.
pub fn find(&self, path: &Path) -> Option<BufferId> {
self.by_id
@ -98,6 +146,8 @@ impl Buffers {
/// Removes every buffer.
pub fn clear(&mut self) {
self.conflicts.clear();
self.written.clear();
self.by_id.clear();
}
}