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

Check is_dirty to prevent invoking auto-save when file is reloaded from disk #13

Merged
merged 1 commit into from
Apr 7, 2015

Conversation

scholer
Copy link
Contributor

@scholer scholer commented Apr 7, 2015

Hi James,
First, thank you for this wonderful ST plug-in. I use it pretty much every day when taking notes at work to make sure all changes are saved.

I noticed an issue caused by auto-save's callback being called when the file is reloaded from disk. In my case, this happens if I change the file on another computer and the file is then synced via Dropbox. But it can also happen if a local program is appending the file while it is open in Sublime Text (and auto-save is active). What happens is that when a file is changed on disk, Sublime Text will automatically reload it. When reloading a file, Sublime Text will call EventListeners' on_modified. (It actually calls it twice, although that might be a bug.)

Calling EventListeners' on_modified may be desired for other plugins. However, we do not want auto-save to save the view after the file has been reloaded. First, because the file is already in sync with the disk (since it has just been reloaded). And second, because if another program is actively updating a file, and Sublime Text then also tries to save the file every time it is reloaded, this can produce conflicting versions.

The issues can be resolved by checking whether the view actually needs to be saved using view.is_dirty(). I've also added a check for view.is_loading(), which becomes relevant for very large files (larger than 5-10 MB on my system).

I've placed the is_dirty() check both in the callback() function and in the EventListener's on_modified() method. The check in the callback() function is the most important. However, as mentioned, on_modified is called twice when a file is reloaded (at least in the current version of Sublime). The first time on_modified() is called, is_dirty() returns True. The second time, is_dirty() returns False. Checking is_dirty() before scheduling debounce_save simply prevents the second of these two calls (i.e. it is just an optimization). Once the callback is called, view.is_dirty() will return False (at least in all my tests).

@jamesfzhang
Copy link
Owner

Thanks for this fix, Rasmus, and I appreciate the explanation!

jamesfzhang pushed a commit that referenced this pull request Apr 7, 2015
Check is_dirty to prevent invoking auto-save when file is reloaded from disk
@jamesfzhang jamesfzhang merged commit 849f397 into jamesfzhang:master Apr 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants