Created
September 5, 2012 18:35
-
-
Save mikamikuh/3642209 to your computer and use it in GitHub Desktop.
Run Formatter When Saving
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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