Created
May 12, 2014 22:49
-
-
Save jimfleming/f552b31f174488510567 to your computer and use it in GitHub Desktop.
Very basic jsfmt integration for Sublime Text 3
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 subprocess | |
import sublime, sublime_plugin | |
import os | |
PLUGIN_FOLDER = os.path.dirname(os.path.realpath(__file__)) | |
SCRIPT_PATH = PLUGIN_FOLDER + '/node_modules/jsfmt/run.js' | |
NODE_PATH = '/usr/local/bin/node' | |
class FormatJavascript(sublime_plugin.TextCommand): | |
def run(self, edit): | |
if self.view.size() > 0 and self.view.file_name().endswith(".js"): | |
try: | |
region = sublime.Region(0, self.view.size()) | |
content = self.view.substr(region) | |
cmd = [NODE_PATH, SCRIPT_PATH, '--format'] | |
args = '"' + '" "'.join(cmd) + '"' | |
p = subprocess.Popen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout, stderr = p.communicate(content.encode('utf-8')) | |
if stderr: | |
print(stderr.decode('utf-8')) | |
elif stdout: | |
self.view.replace(edit, region, stdout.decode('utf-8')) | |
finally: | |
self.view.end_edit(edit) | |
class CommandOnSave(sublime_plugin.EventListener): | |
def on_pre_save(self, view): | |
view.run_command("format_javascript") |
Looks like there's a formalized version of this over in https://github.com/paulirish/sublime-jsfmt/blob/master/jsfmt-sublime.py
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick update: https://gist.github.com/hparra/af67e9a39037a3bfdbed