Skip to content

Instantly share code, notes, and snippets.

@mikamikuh
Created September 5, 2012 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikamikuh/3642209 to your computer and use it in GitHub Desktop.
Save mikamikuh/3642209 to your computer and use it in GitHub Desktop.
Run Formatter When Saving
import re
import sublime, sublime_plugin
# Plugin of running command when saving file
class AutoRunner(sublime_plugin.EventListener):
current = None
# Execute a command
def execute(self, view, info):
# Get information of command
command = info.get('command')
syntaxes = info.get('syntaxes')
args = info.get('args')
# Get name of syntax mode from file path
# /AAA/BBB/ccc.tmLanguage => ccc.tmLanguage
filename = re.split('/', self.current)[-1]
# ccc.tmLanguage => ccc
current_syntax = re.split('\.', filename)[0]
# Run command if list of executable syntax mode contains current syntax mode
if syntaxes == None or current_syntax in syntaxes:
print command
edit = view.begin_edit()
view.run_command(command, args)
view.end_edit(edit)
# Execute when before saving file automatically
def on_pre_save(self, view):
# Get current syntax mode
self.current = view.settings().get('syntax')
if self.current == None:
return
# Execute command from each settings
commands = view.settings().get('auto_runner')
if commands != None:
map(lambda c :self.execute(view, c), commands)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment