Skip to content

Commit

Permalink
Added on_pre_save() event. Fixes #12. Also added more menu items and …
Browse files Browse the repository at this point in the history
…new setting: 'delete_blank_lines_on_save'
  • Loading branch information
NicholasBuse committed Apr 5, 2018
1 parent 9ae04a6 commit 0b67da7
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 10 deletions.
33 changes: 23 additions & 10 deletions DeleteBlankLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ def run( self, edit, surplus=False):
if (st_version == 2):
edit = self.view.begin_edit()

# Loop through user selections.
for currentSelection in self.view.sel():
# Strip blank lines
newSelections.append( self.strip( edit, currentSelection, surplus ) )

# Clear selections since they've been modified.
self.view.sel().clear()

for newSelection in newSelections:
self.view.sel().add( newSelection )
# If there is no (empty) selection, operate on the whole file.
# else, operate only on each selection (update the selection for changes)
if len(self.view.sel()) == 1 and self.view.substr(self.view.sel()[0]) == "":
self.strip( edit, sublime.Region(0, self.view.size()), surplus )
else:
# Loop through user selections.
for currentSelection in self.view.sel():
# Strip blank lines
newSelections.append( self.strip( edit, currentSelection, surplus ) )

# Clear selections since they've been modified.
self.view.sel().clear()

for newSelection in newSelections:
self.view.sel().add( newSelection )
# END: if len()...

# A corresponding call to end_edit() is required.
if (st_version == 2):
Expand Down Expand Up @@ -61,3 +67,10 @@ def strip( self, edit, currentSelection, surplus ):
self.view.replace( edit, currentSelection, output )

return sublime.Region( currentSelection.begin(), currentSelection.begin() + len(output) )


class DeleteBlankLines(sublime_plugin.EventListener):
def on_pre_save(self, view):
settings = sublime.load_settings('DeleteBlankLines.sublime-settings')
if settings and settings.has('delete_blank_lines_on_save') and settings.get("delete_blank_lines_on_save") is True:
view.run_command("delete_blank_lines")
3 changes: 3 additions & 0 deletions DeleteBlankLines.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"delete_blank_lines_on_save": false
}
97 changes: 97 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,102 @@
]
}
]
},

{
"caption": "Preferences",
"mnemonic": "n",
"id": "preferences",
"children":
[
{
"caption": "Package Settings",
"mnemonic": "P",
"id": "package-settings",
"children":
[
{
"caption": "DeleteBlankLines",
"children":
[
{
"args": {
"file": "${packages}/DeleteBlankLines/README.md"
},
"caption": "README",
"command": "open_file"
},
{
"caption": "-"
},
{
"command": "open_file",
"args": {"file": "${packages}/DeleteBlankLines/DeleteBlankLines.sublime-settings"},
"caption": "Settings – Default"
},
{
"command": "open_file",
"args": {"file": "${packages}/User/DeleteBlankLines.sublime-settings"},
"caption": "Settings – User"
},
{
"caption": "-"
},
{
"args": {
"file": "${packages}/DeleteBlankLines/Default (Windows).sublime-keymap",
"platform": "Windows"
},
"caption": "Key Bindings \u2013 Default",
"command": "open_file"
},
{
"args": {
"file": "${packages}/DeleteBlankLines/Default (OSX).sublime-keymap",
"platform": "OSX"
},
"caption": "Key Bindings \u2013 Default",
"command": "open_file"
},
{
"args": {
"file": "${packages}/DeleteBlankLines/Default (Linux).sublime-keymap",
"platform": "Linux"
},
"caption": "Key Bindings \u2013 Default",
"command": "open_file"
},
{
"args": {
"file": "${packages}/User/Default (Windows).sublime-keymap",
"platform": "Windows"
},
"caption": "Key Bindings \u2013 User",
"command": "open_file"
},
{
"args": {
"file": "${packages}/User/Default (OSX).sublime-keymap",
"platform": "OSX"
},
"caption": "Key Bindings \u2013 User",
"command": "open_file"
},
{
"args": {
"file": "${packages}/User/Default (Linux).sublime-keymap",
"platform": "Linux"
},
"caption": "Key Bindings \u2013 User",
"command": "open_file"
},
{
"caption": "-"
}
]
}
]
}
]
}
]

0 comments on commit 0b67da7

Please sign in to comment.