feat: dedupe open documents and drop session restore

This commit is contained in:
2026-10-28 14:46:40 +08:00
parent 55d78485dd
commit e24733ad96
2 changed files with 108 additions and 135 deletions

View File

@@ -1,7 +1,5 @@
use serde::{Deserialize, Serialize};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DocumentTab {
@@ -19,37 +17,7 @@ pub struct DocumentSession {
next_id: u64,
}
#[derive(Debug, Deserialize, Serialize)]
struct PersistedSession {
version: u8,
active: Option<usize>,
tabs: Vec<PathBuf>,
}
impl DocumentSession {
pub fn load(path: &Path) -> Self {
let Ok(raw) = fs::read(path) else {
return Self::default();
};
let Ok(saved) = serde_json::from_slice::<PersistedSession>(&raw) else {
return Self::default();
};
if saved.version != 1 {
return Self::default();
}
let mut session = Self::default();
for path in saved.tabs {
session.open(path, false);
}
session.active_id = saved
.active
.and_then(|index| session.tabs.get(index))
.map(|tab| tab.id)
.or_else(|| session.tabs.last().map(|tab| tab.id));
session
}
pub fn open(&mut self, path: PathBuf, edit_on_open: bool) -> u64 {
let path = normalize_path(path);
if let Some(tab) = self.tabs.iter_mut().find(|tab| tab.path == path) {
@@ -97,28 +65,6 @@ impl DocumentSession {
true
}
pub fn save(&self, path: &Path) -> io::Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
let active = self
.active_id
.and_then(|id| self.tabs.iter().position(|tab| tab.id == id));
let saved = PersistedSession {
version: 1,
active,
tabs: self.tabs.iter().map(|tab| tab.path.clone()).collect(),
};
let body = serde_json::to_vec_pretty(&saved).map_err(io::Error::other)?;
let temporary = path.with_extension("json.tmp");
fs::write(&temporary, body)?;
#[cfg(target_os = "windows")]
if path.exists() {
fs::remove_file(path)?;
}
fs::rename(temporary, path)
}
pub fn active(&self) -> Option<&DocumentTab> {
let id = self.active_id?;
self.tabs.iter().find(|tab| tab.id == id)
@@ -147,7 +93,7 @@ impl DocumentSession {
}
}
fn normalize_path(path: PathBuf) -> PathBuf {
pub(crate) fn normalize_path(path: PathBuf) -> PathBuf {
let absolute = if path.is_absolute() {
path
} else {
@@ -249,33 +195,4 @@ mod tests {
assert!(session.tabs[0].path.ends_with("note.md"));
let _ = fs::remove_dir_all(dir);
}
#[test]
fn persisted_session_keeps_missing_tabs_and_active_order() {
let dir = temp_dir("roundtrip");
let state_path = dir.join("session.json");
let existing = dir.join("existing.md");
let missing = dir.join("missing.md");
fs::write(&existing, "# Existing").unwrap();
let mut session = DocumentSession::default();
session.open(existing.clone(), false);
session.open(missing.clone(), false);
session.save(&state_path).unwrap();
let restored = DocumentSession::load(&state_path);
assert_eq!(restored.tabs.len(), 2);
assert_eq!(
restored.tabs[0].path,
strip_verbatim(fs::canonicalize(existing).unwrap())
);
assert_eq!(
restored.tabs[1].path,
strip_verbatim(fs::canonicalize(&dir).unwrap()).join(missing.file_name().unwrap())
);
assert!(!restored.tabs[0].missing);
assert!(restored.tabs[1].missing);
assert_eq!(restored.active_id, restored.tabs.get(1).map(|tab| tab.id));
let _ = fs::remove_dir_all(dir);
}
}