Skip to content

Instantly share code, notes, and snippets.

@singingwolfboy
Created May 16, 2012 18:41
Show Gist options
  • Save singingwolfboy/2712918 to your computer and use it in GitHub Desktop.
Save singingwolfboy/2712918 to your computer and use it in GitHub Desktop.
Update the "Default" plugin in ST2 to allow "Trim Trailing Whitespace" as a text command
--- Default/trim_trailing_white_space.py.bak 2012-04-13 11:18:23.000000000 -0400
+++ Default/trim_trailing_white_space.py 2012-04-13 11:49:48.000000000 -0400
@@ -1,19 +1,37 @@
-import sublime, sublime_plugin
+import sublime_plugin
-class TrimTrailingWhiteSpace(sublime_plugin.EventListener):
+
+class TrimTrailingWhiteSpace(sublime_plugin.TextCommand):
+ def run(self, *args):
+ view = self.view
+ trailing_white_space = view.find_all("[\t ]+$")
+ selections = view.sel()
+ if any((len(selection) for selection in selections)) > 0:
+ trailing_white_space = filter(lambda x: selections.contains(x),
+ trailing_white_space)
+ trailing_white_space.reverse()
+ edit = view.begin_edit()
+ for r in trailing_white_space:
+ view.erase(edit, r)
+ view.end_edit(edit)
+
+
+class TrimTrailingWhiteSpaceEvent(sublime_plugin.EventListener):
def on_pre_save(self, view):
if view.settings().get("trim_trailing_white_space_on_save") == True:
- trailing_white_space = view.find_all("[\t ]+$")
- trailing_white_space.reverse()
+ view.run_command("trim_trailing_white_space")
+
+
+class EnsureNewlineAtEof(sublime_plugin.TextCommand):
+ def run(self, *args):
+ view = self.view
+ if view.size() > 0 and view.substr(view.size() - 1) != '\n':
edit = view.begin_edit()
- for r in trailing_white_space:
- view.erase(edit, r)
+ view.insert(edit, view.size(), "\n")
view.end_edit(edit)
+
class EnsureNewlineAtEof(sublime_plugin.EventListener):
def on_pre_save(self, view):
if view.settings().get("ensure_newline_at_eof_on_save") == True:
- if view.size() > 0 and view.substr(view.size() - 1) != '\n':
- edit = view.begin_edit()
- view.insert(edit, view.size(), "\n")
- view.end_edit(edit)
+ view.run_command("ensure_newline_at_eof")
--- Default/Default.sublime-commands.bak 2012-04-13 11:15:50.000000000 -0400
+++ Default/Default.sublime-commands 2012-04-13 11:37:11.000000000 -0400
@@ -20,6 +20,10 @@
"caption": "Convert Case: Swap Case",
"command": "swap_case"
},
+ {
+ "caption": "Trim Trailing Whitespace",
+ "command": "trim_trailing_white_space"
+ },
{ "command": "toggle_comment", "args": {"block": false}, "caption": "Toggle Comment" },
{ "command": "toggle_comment", "args": {"block": true}, "caption": "Toggle Block Comment" },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment