Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Deserialize untitled editors - #9968

Merged
as-cii merged 8 commits into
masterfrom
as-deserialize-untitled-editors
Dec 9, 2015
Merged

as-cii merged 8 commits into
masterfrom
as-deserialize-untitled-editors

Conversation

@as-cii

@as-cii as-cii commented Dec 7, 2015

Copy link
Copy Markdown
Contributor

Depends on atom/text-buffer#116.
Fixes #942.

Our serialization code already supported saving state for untitled TextBuffer instances, but the problem was that we used the file's path to identify and search for buffers in TokenizedBuffer and atom.project. This PR changes it so that, in addition to paths, we can make use of a uniquely generated id for each TextBuffer.

If the TokenizedBuffer's state has a buffer path, we deserialize it exactly as before. Otherwise, we try to search by buffer id among all the project's buffers. It might be tempting to use IDs even when we have a path, but it still seems quite reasonable to just use them as a fallback for two potential problems:

  1. Possible weird mismatches or false duplicates (e.g. two buffers have the same path, but different IDs)
  2. Even if a buffer is not present in atom.project, we still want re-open a buffer at that location when deserializing a TokenizedBuffer (I am not sure we have any guarantee that this never happens).

Feedback welcome! 🎈

/cc: @nathansobo @maxbrunsfeld @atom/feedback

Comment thread src/text-editor.coffee

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the comments I read on #942, we now avoid to prompt the user when a window close request is in place. What are your thoughts on this, @atom/feedback? 💭

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested with Notepad++ and it doesn't prompt to save on close. I really don't see any downside to this because even if someone closed Atom accidentally, they can just open it up again?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would these buffers be deserialized? Would it only be when atom is opened in the same project context? i.e. What would happen in this case:

  • open atom via atom /somepath
  • edit untitled buffers
  • kill atom
  • open atom via atom /anotherpath

Will the scratch buffers be in this new atom window? Or only if I started it via atom /somepath?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be tied to the workspace state, which is keyed on the project paths. Making it independent of the workspace state is a substantial increase in scope.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, so following the above reproduction steps would only reopen the untitled buffers when starting it via atom /somepath.

I feel like this would be a nice byproduct, as it would allow users to have several untitled editors based on the project they're working on. I cannot think of any other drawback in not prompting to save when closing a window, 'cause serializing the state acts a nice safety net in case users want to restore their changes.

I think I should clearly state here that this will affect also editors with a path (i.e. non-untitled), although I think this is actually fine and doesn't change the thoughts I expressed above.

What do you think?

@winstliu

winstliu commented Dec 7, 2015

Copy link
Copy Markdown
Contributor

Fixes #942.

Yessssssss

@nathansobo

Copy link
Copy Markdown
Contributor

It might be tempting to use IDs even when we have a path

I'm unclear on whether either of the concerns you raise can actually happen in a real deserialization scenario. Problems with either would emerge from the workspace getting into an inconsistent state...

We ensure we don't duplicate buffers every time we open a path. So long as we only deserialize from workspaces in which that rule was followed for every open, we shouldn't get duplicates. Also, we pretty explicitly guarantee we serializing a workspace that any buffer we reference in panes is present in our overall set of buffers.

Unless I'm missing something, I think the code would be simpler if we just went all the way with ids. I do think using a random string is smart to avoid needing to update an incrementing counter after deserializing.

@as-cii

as-cii commented Dec 7, 2015

Copy link
Copy Markdown
Contributor Author

Problems with either would emerge from the workspace getting into an inconsistent state...

Exactly, I wasn't sure about which exact guarantees we had for the deserialization path and went down the safe, defensive path instead. Like, one of my concerns was having a scenario like the following:

  1. Open an untitled editor, say Untitled 1.
  2. Open an untitled editor, say Untitled 2.
  3. Save Untitled 1 into a.txt.
  4. Save Untitled 2 into a.txt, thus replacing the previously created file.

I am quite sure that the above steps cause Project to serialize two different buffers and, as a result, we would introduce a different behavior with this PR: that is, by using IDs we would use both buffers when deserializing, as opposed to reusing the same one by using paths.

I guess it's not the end of the world, but more generally speaking, I was concerned about the potential edge cases such as the one above that we might introduce by changing the logical key we use to refer to buffers. Thoughts? Maybe we should just fix those inconsistencies in Project::addBuffer. Anyways, I was tempted to use IDs only as well and I am 🆒 with doing so after hearing what you think.

Thanks!

@nathansobo

Copy link
Copy Markdown
Contributor

having a scenario like the following:

Interesting. I hadn't considered this edge case. I actually think our current behavior is probably wrong, in that we don't fully enforce one buffer per physical file on disk. Fixing this would probably require some weird juggling where we merge two buffers into one when paths get changed...

I agree this is a weird case, but presumably having two duplicate buffers after a serialization wouldn't make it any worse, right? It's actually a bit weird that we "fix" this edge case with deserialization at this point. Ideally, deserialization reproduces the state of the workspace as faithfully as possible and we fix this at a different layer.

Making a smaller change and leaving the ids in place introduces less risk, but I also think we should consider the total complexity of the system to a minimum. So that leans me toward just using ids.

Another thing: Did you consider backwards compatibility? What happens when we deserialize workspaces created with older versions. Conversely, what happens when we fall back a version. Might be nice to make sure that's smooth if it's cheap to do.

@as-cii

as-cii commented Dec 7, 2015

Copy link
Copy Markdown
Contributor Author

I agree this is a weird case, but presumably having two duplicate buffers after a serialization wouldn't make it any worse, right? It's actually a bit weird that we "fix" this edge case with deserialization at this point. Ideally, deserialization reproduces the state of the workspace as faithfully as possible and we fix this at a different layer.

Agreed. Hopefully we're not missing other edge cases similar to the one above but I think using IDs will be just fine.

Another thing: Did you consider backwards compatibility? What happens when we deserialize workspaces created with different versions. Conversely, what happens when we fall back a version. Might be nice to make sure that's smooth if it's cheap to do.

Good point. Using IDs requires introducing a conditional where we check for id presence first, falling back to path when we don't find it in the serialized state. Other than that, it seems to work just fine across multiple versions. 👍

Please, merge at will after one last 👀. Thank you!

This made the build fail because we were checking that a TextEditor couldn't
have been serialized when the path didn't exist. This is exactly the opposite we
want to do for restoring untitled editors, therefore I think it's safe to delete
this test.

/cc: @nathansobo for extra 👀
@benogle

benogle commented Dec 7, 2015

Copy link
Copy Markdown
Contributor

Thanks for taking this on!

@as-cii

as-cii commented Dec 8, 2015

Copy link
Copy Markdown
Contributor Author

@benogle: 💙 💚 ❤️

@nathansobo: I just noticed an inconsistency in TextBuffer::isModified, which returns false when deserializing an unsaved buffer. I fixed it in atom/text-buffer#118, so we may probably want to merge that first and ⬆️ text-buffer here before 🚢ping this.

@as-cii
as-cii force-pushed the as-deserialize-untitled-editors branch from 654b14b to 769b7bc Compare December 9, 2015 07:49
@as-cii

as-cii commented Dec 9, 2015

Copy link
Copy Markdown
Contributor Author

Everything is 💚 and this seems to work quite nicely after a test-drive, so I'll go ahead and :shipit: (we can refine this in a later PR if needed).

Thanks everyone for the feedback! 🙏

as-cii added a commit that referenced this pull request Dec 9, 2015
@as-cii
as-cii merged commit ae90db0 into master Dec 9, 2015
@as-cii
as-cii deleted the as-deserialize-untitled-editors branch December 9, 2015 11:00
@bhadra

bhadra commented Dec 11, 2015

Copy link
Copy Markdown

I upgraded to 1.4.0-beta0 now. I do not see #942 fixed on Windows 8.1.

  1. Open Atom
  2. Create an untitled buffer. Type some random text.
  3. Close and reopen Atom
  4. The untitled buffer does not show up.

In case of Sublime Text 3, the untitled buffer shows up as expected.

Do I have to change any settings?

@winstliu

Copy link
Copy Markdown
Contributor

@bhadra does it still not work in safe mode (atom --safe)? If so, please create a new issue for that.

@nathansobo

Copy link
Copy Markdown
Contributor

Keep in mind that the untitled buffers are associated with the workspace, which is saved based on the open folders in the current project.

@bhadra

bhadra commented Dec 13, 2015

Copy link
Copy Markdown

@50Wliu It does not work in safe mode. I have followed the test case as described in my previous message.

@nathansobo You are right. As mentioned by @as-cii, untitled buffers are persisted if and only Atom is re-started via atom /somepath. This is significantly limited compared to Sublime Text. In Sublime Text, untitled buffers are not associated with the workspace and are persisted independent of how Sublime Text is re-started.

@schmurfy

Copy link
Copy Markdown

I know the issue is closed but I just discovered save-session was merged except I don't see any of its behavior in current Atom version...

In an open project I create a new tab, type some text and then quit Atom: it asks to save Oo
What was included exactly and how to enable it ?

@mnquintana

Copy link
Copy Markdown
Contributor

@schmurfy This is available now in the beta channel and will be available in the next stable release of Atom (1.4.0)

@schmurfy

Copy link
Copy Markdown

thanks :)

@mattpker

Copy link
Copy Markdown

This is a great new feature and I really love it, but I think more work needs to be done when you have not opened a project. A lot of the time when I need to jot down some notes I will open a new atom window (not apart of a project). When I go to close that window or restart my computer, the pop up to save the untitled notes was really great, because a lot of the time I would forget to save a note that I actually needed to keep. Because this was not a part of a project, I just lost all my notes. I think having a feature that only works on projects is a bit of an oversight, it gets you in the mindset that "It's okay if I close this without saving, it will be back when I re-open it." and then when it was not opened as a project it is gone with no prompt warning you of this.

I suggest adding the prompt back for untitled tabs that are not apart of a project, or making all non project tabs all be apart of some "no project" project or something. I hope that makes sense, let me know if you have any questions.

Edit:
After posting this I checked the Issues and noticed someone else also thought this was a bug. I posted what I posted here in that Issue because that might be a better place to discuss that.

#10474 (comment)

@asbjornu

Copy link
Copy Markdown

Why does untitled files need to be associated with a project? What about files saved outside a project? I think both should be handled the same way, perhaps through an implicit project that always exist, to hold untitled files and files outside of any explicit projects.

This implicit project and all of its files (tabs) should be opened every time Atom is started, no matter how it is started, but only in the first window. If several windows are opened after that, they should contain their own implicit project that can have files added to them.

If Atom is closed with several windows open, all windows should be re-opened the next time Atom is started (no matter how it is started). This will preserve all untitled tabs and unsaved changes to previously saved files. It may become cluttered to have several windows open like this, but that's better than losing work. It should also be as easy as dragging tabs from one window to another to move files from one implicit project to another. When the last tab is dragged from a window, its implicit project should be deleted.

And lastly, to solve the several untitled tabs problem, how about doing like Sublime and Apple's Notes app and naming the tabs after the first line of the content within them?

@splashx

splashx commented May 17, 2016 •

Copy link
Copy Markdown

Are you guys fixing untitled outside a project? I'm running 1.7.3 and currently sees this behaviour:

  • open atom, untitled opens
  • write Hello World
  • CMD+Q on Mac
  • Open atom
  • untitled is blank.

@mnquintana

Copy link
Copy Markdown
Contributor

@splashx That's being tracked in #10474

Since this has already been merged and there's already another issue open tracking further improvements, I'm going to lock this to keep noise levels down.

@atom atom locked and limited conversation to collaborators Jul 19, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants