
This adds the ability for Zed to restore unsaved buffers on restart. The user is no longer prompted to save/discard/cancel when trying to close a Zed window with dirty buffers in it. Instead those dirty buffers are stored and restored on restart. It does this by saving the contents of dirty buffers to the internal SQLite database in which Zed stores other data too. On restart, if there are dirty buffers in the database, they are restored. On certain events (buffer changed, file saved, ...) Zed will serialize these buffers, throttled to a 100ms, so that we don't overload the machine by saving on every keystroke. When Zed quits, it waits until all the buffers are serialized. ### Current limitations - It does not persist undo-history (right now we don't persist/restore undo-history regardless of dirty buffers or not) - It does not restore buffers in windows without projects/worktrees. Example: if you open a new window with `cmd-shift-n` and type something in a buffer, this will _not_ be stored and you will be asked whether to save/discard on quit. In the future, we want to fix this by also restoring windows without projects/worktrees. ### Demo https://github.com/user-attachments/assets/45c63237-8848-471f-8575-ac05496bba19 ### Related tickets I'm unsure about closing them, without also fixing the 2nd limitation: restoring of worktree-less windows. So let's wait until that. - https://github.com/zed-industries/zed/issues/4985 - https://github.com/zed-industries/zed/issues/4683 ### Note on performance - Serializing editing buffer (asynchronously on background thread) with 500k lines takes ~200ms on M3 Max. That's an extreme case and that performance seems acceptable. Release Notes: - Added automatic restoring of unsaved buffers. Zed can now be closed even if there are unsaved changes in buffers. One current limitation is that this only works when having projects open, not single files or empty windows with unsaved buffers. The feature can be turned off by setting `{"session": {"restore_unsaved_buffers": false}}`. --------- Co-authored-by: Bennet <bennet@zed.dev> Co-authored-by: Antonio <antonio@zed.dev>
98 lines
3 KiB
Rust
98 lines
3 KiB
Rust
use anyhow::Result;
|
|
use std::path::PathBuf;
|
|
|
|
use db::{define_connection, query, sqlez::statement::Statement, sqlez_macros::sql};
|
|
use workspace::{ItemId, WorkspaceDb, WorkspaceId};
|
|
|
|
define_connection! {
|
|
pub static ref TERMINAL_DB: TerminalDb<WorkspaceDb> =
|
|
&[sql!(
|
|
CREATE TABLE terminals (
|
|
workspace_id INTEGER,
|
|
item_id INTEGER UNIQUE,
|
|
working_directory BLOB,
|
|
PRIMARY KEY(workspace_id, item_id),
|
|
FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
|
|
ON DELETE CASCADE
|
|
) STRICT;
|
|
),
|
|
// Remove the unique constraint on the item_id table
|
|
// SQLite doesn't have a way of doing this automatically, so
|
|
// we have to do this silly copying.
|
|
sql!(
|
|
CREATE TABLE terminals2 (
|
|
workspace_id INTEGER,
|
|
item_id INTEGER,
|
|
working_directory BLOB,
|
|
PRIMARY KEY(workspace_id, item_id),
|
|
FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
|
|
ON DELETE CASCADE
|
|
) STRICT;
|
|
|
|
INSERT INTO terminals2 (workspace_id, item_id, working_directory)
|
|
SELECT workspace_id, item_id, working_directory FROM terminals;
|
|
|
|
DROP TABLE terminals;
|
|
|
|
ALTER TABLE terminals2 RENAME TO terminals;
|
|
)];
|
|
}
|
|
|
|
impl TerminalDb {
|
|
query! {
|
|
pub async fn update_workspace_id(
|
|
new_id: WorkspaceId,
|
|
old_id: WorkspaceId,
|
|
item_id: ItemId
|
|
) -> Result<()> {
|
|
UPDATE terminals
|
|
SET workspace_id = ?
|
|
WHERE workspace_id = ? AND item_id = ?
|
|
}
|
|
}
|
|
|
|
query! {
|
|
pub async fn save_working_directory(
|
|
item_id: ItemId,
|
|
workspace_id: WorkspaceId,
|
|
working_directory: PathBuf
|
|
) -> Result<()> {
|
|
INSERT OR REPLACE INTO terminals(item_id, workspace_id, working_directory)
|
|
VALUES (?, ?, ?)
|
|
}
|
|
}
|
|
|
|
query! {
|
|
pub fn get_working_directory(item_id: ItemId, workspace_id: WorkspaceId) -> Result<Option<PathBuf>> {
|
|
SELECT working_directory
|
|
FROM terminals
|
|
WHERE item_id = ? AND workspace_id = ?
|
|
}
|
|
}
|
|
|
|
pub async fn delete_unloaded_items(
|
|
&self,
|
|
workspace: WorkspaceId,
|
|
alive_items: Vec<ItemId>,
|
|
) -> Result<()> {
|
|
let placeholders = alive_items
|
|
.iter()
|
|
.map(|_| "?")
|
|
.collect::<Vec<&str>>()
|
|
.join(", ");
|
|
|
|
let query = format!(
|
|
"DELETE FROM terminals WHERE workspace_id = ? AND item_id NOT IN ({placeholders})"
|
|
);
|
|
|
|
self.write(move |conn| {
|
|
let mut statement = Statement::prepare(conn, query)?;
|
|
let mut next_index = statement.bind(&workspace, 1)?;
|
|
for id in alive_items {
|
|
next_index = statement.bind(&id, next_index)?;
|
|
}
|
|
statement.exec()
|
|
})
|
|
.await
|
|
}
|
|
}
|