Skip to content

Instantly share code, notes, and snippets.

@jimfleming
Created May 12, 2014 22:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimfleming/f552b31f174488510567 to your computer and use it in GitHub Desktop.
Save jimfleming/f552b31f174488510567 to your computer and use it in GitHub Desktop.
Very basic jsfmt integration for Sublime Text 3
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")
@hparra
Copy link

hparra commented Sep 25, 2014

@addyosmani
Copy link

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