Skip to content

Instantly share code, notes, and snippets.

@hparra
Forked from jimfleming/jsfmt-sublime.py
Last active August 29, 2015 14:06
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 hparra/af67e9a39037a3bfdbed to your computer and use it in GitHub Desktop.
Save hparra/af67e9a39037a3bfdbed to your computer and use it in GitHub Desktop.
import subprocess
import sublime, sublime_plugin
import os
PLUGIN_FOLDER = os.path.dirname(os.path.realpath(__file__))
SCRIPT_PATH = PLUGIN_FOLDER + '/node_modules/jsfmt/lib/run.js'
NODE_PATH = '/usr/local/bin/node' # Change to your node location (How can we `/usr/bin/env node` here?)
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]
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment