Skip to content

Commit

Permalink
Release 1.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesfzhang committed Jul 28, 2019
1 parent 05cb0e3 commit c1b0651
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Usage
-------
**By default, auto-save is disabled** because it is a fairly invasive plugin. To make it less invasive, you can instruct it to only auto-save changes to the file that is active when you turn on auto-save. In this mode, it will ignore changes to all other files.

To run auto-save whenever a file is modified, set `"auto_save_on_modified": true` in your user settings. To ignore certain files, set `auto_save_ignore_files` to a list of file suffices like `[".yml", "package.json"]`.

You can also instruct it to auto-backup the file instead of auto-saving it. The backup gets created in the same directory as its source file. The backup file takes the same name as its source file, with the string `.autosave` inserted directly before the file extension. When auto-save is disabled, the backup file is deleted.

There are two ways to enable it. You can press <kbd>Command + Shift + P</kbd> to bring up the Command Palette, and search for **AutoSave**. Here, there are 3 options:
Expand Down
31 changes: 15 additions & 16 deletions auto_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
delay_field = "auto_save_delay_in_seconds"
all_files_field = "auto_save_all_files"
current_file_field = "auto_save_current_file"
ignore_files_field = "auto_save_ignore_files"
backup_field = "auto_save_backup"
backup_suffix_field = "auto_save_backup_suffix"



class AutoSaveListener(sublime_plugin.EventListener):

save_queue = [] # Save queue for on_modified events.
Expand All @@ -39,40 +39,39 @@ def generate_backup_filename(filename, backup_suffix):

def on_modified(self, view):
settings = sublime.load_settings(settings_filename)
if not (settings.get(on_modified_field) and view.file_name() and view.is_dirty()):
filename = view.file_name()

if view.is_auto_complete_visible():
return

if not (settings.get(on_modified_field) and filename and view.is_dirty()):
return

delay = settings.get(delay_field)
all_files = settings.get(all_files_field)
current_file = settings.get(current_file_field)
backup = settings.get(backup_field)
backup_suffix = settings.get(backup_suffix_field)
for path in settings.get(ignore_files_field):
if filename.endswith(path):
return

if not all_files and current_file != view.file_name():
if not settings.get(all_files_field) and settings.get(current_file_field) != filename:
return


def callback():
'''
Must use this callback for ST2 compatibility
'''
if view.is_dirty() and not view.is_loading():
if not backup: # Save file
if view.is_dirty() and not view.is_loading() and not view.is_auto_complete_visible():
if not settings.get(backup_field): # Save file
view.run_command("save")
else: # Save backup file
content = view.substr(sublime.Region(0, view.size()))
try:
with open(AutoSaveListener.generate_backup_filename(
view.file_name(), backup_suffix), 'w', encoding='utf-8') as f:
view.file_name(), settings.get(backup_suffix_field)), 'w', encoding='utf-8') as f:
f.write(content)
except Exception as e:
sublime.status_message(e)
raise e

else:
print("Auto-save callback invoked, but view is",
"currently loading." if view.is_loading() else "unchanged from disk.")


def debounce_save():
'''
Expand All @@ -87,7 +86,7 @@ def debounce_save():


AutoSaveListener.save_queue.append(0) # Append to queue for every on_modified event.
Timer(delay, debounce_save).start() # Debounce save by the specified delay.
Timer(settings.get(delay_field), debounce_save).start() # Debounce save by the specified delay.



Expand Down
1 change: 1 addition & 0 deletions auto_save.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"auto_save_delay_in_seconds": 1,
"auto_save_all_files": true,
"auto_save_current_file": "",
"auto_save_ignore_files": [],
"auto_save_backup": false,
"auto_save_backup_suffix": "autosave"
}
3 changes: 2 additions & 1 deletion messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"1.0.4": "messages/1.0.4.txt",
"1.0.5": "messages/1.0.5.txt",
"1.0.6": "messages/1.0.6.txt",
"1.0.7": "messages/1.0.7.txt"
"1.0.7": "messages/1.0.7.txt",
"1.0.8": "messages/1.0.8.txt"
}
4 changes: 4 additions & 0 deletions messages/1.0.8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Improvements:

* Add option for ignoring files
* Don't save when auto-complete menu is open--otherwise, the menu closes unexpectedly

0 comments on commit c1b0651

Please sign in to comment.