Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore unsaved buffers on restart #13546

Merged
merged 39 commits into from
Jul 17, 2024
Merged

Restore unsaved buffers on restart #13546

merged 39 commits into from
Jul 17, 2024

Conversation

mrnugget
Copy link
Member

@mrnugget mrnugget commented Jun 26, 2024

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

demo_unsaved_buffers.mp4

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.

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}}.

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Jun 26, 2024
@mrnugget mrnugget force-pushed the persist-unsaved-buffers branch from 4d9b124 to 3404073 Compare July 11, 2024 07:49
@mrnugget mrnugget changed the title WIP: Backup unpersisted changes in in buffers WIP: Backup unpersisted changes in buffers Jul 11, 2024
@mrnugget mrnugget changed the title WIP: Backup unpersisted changes in buffers WIP: Backup unsaved changes in buffers Jul 11, 2024
@mrnugget mrnugget force-pushed the persist-unsaved-buffers branch from 24452d2 to 2e5f45a Compare July 15, 2024 08:34
@mrnugget mrnugget marked this pull request as ready for review July 17, 2024 08:11
@mvp
Copy link

mvp commented Dec 3, 2024

I have installed Zed 0.163.3 67d405d, which seems quite recent.
However, restoring unsaved buffers on restart still does not seem to work.
I read the code, and it should be gated by setting restore_unsaved_buffers (defaults to true. I even added this setting explicitly to my config, but it still does not work.
Reading Zed book https://zed.dev/docs/configuring-zed, it does not mention this option at all.
What am I missing?
Basically, I am looking to be able to open few untitled/unsaved windows/scratchpads and have them survive restart or quitting the editor.

@ConradIrwin
Copy link
Member

@mvp do you see any errors in your zed log? I've seen the sqlite database we use get corrupted occasionally

@AndydeCleyre
Copy link

@mvp If I understand the current state -- and just in case you missed this -- it only works if you launch zed by opening a folder (not a file), and subsequently launch it by opening the very same folder. It's not yet implemented completely, only for "projects."

@mvp
Copy link

mvp commented Dec 4, 2024

I cannot find any sqlite db under ~/.config/zed. Is there any other place?

Not supporting this transparently is very unfortunate.
Even if I am saving all files, quitting zed and starting it again (or rebooting my computer) doesn't load the same files again, which is even worse.

I guess back to Sublime Text :-(

@notpeter
Copy link
Member

notpeter commented Dec 4, 2024

On MacOS the workspace is under ~/Library/Application Support/Zed

@mvp
Copy link

mvp commented Dec 4, 2024

Opening a directory and then adding unsaved file works as expected, and it preserves last opened directory upon restart, as well does not ask to save unsaved files upon exit.

However, opening random files or scratches does not keep unsaved files, and does not remember list of previously opened files upon restart...

@mrnugget
Copy link
Member Author

mrnugget commented Dec 5, 2024

Yeah, that's a current limitation. See this note above in the release notes:

One current limitation is that this only works when having projects open, not single files or empty windows with unsaved buffers.

@AndydeCleyre
Copy link

AndydeCleyre commented Dec 5, 2024

For completeness, the current tracking issue is:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants