Skip to content

Instantly share code, notes, and snippets.

@jscheel
Created December 16, 2011 06:56
Show Gist options
  • Save jscheel/1484866 to your computer and use it in GitHub Desktop.
Save jscheel/1484866 to your computer and use it in GitHub Desktop.
coffee2js sublime text 2 plugin
# not perfect yet (env. not set properly, and still not 100% about the escaping)
import sublime, sublime_plugin
from subprocess import Popen, PIPE, STDOUT
import sublime, sublime_plugin
from subprocess import Popen, PIPE, STDOUT
class CoffeescriptToJsCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
command = quote_argument(self.view.substr(region))
p = subprocess.Popen('/usr/local/bin/coffee -bpe ' + command, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
self.scratch(err + "\n" + out, title="Coffeescript to JS", syntax="Packages/JavaScript/JavaScript.tmLanguage")
# scratch code from some other project referenced in sublime forums
def scratch(self, output, title=False, **kwargs):
scratch_file = self.get_window().new_file()
if title:
scratch_file.set_name(title)
scratch_file.set_scratch(True)
self._output_to_view(scratch_file, output, **kwargs)
scratch_file.set_read_only(True)
return scratch_file
def get_window(self):
return self.view.window()
def _output_to_view(self, output_file, output, clear=False, syntax="Packages/Diff/Diff.tmLanguage"):
output_file.set_syntax_file(syntax)
edit = output_file.begin_edit()
if clear:
region = sublime.Region(0, self.output_view.size())
output_file.erase(edit, region)
output_file.insert(edit, 0, output)
output_file.end_edit(edit)
def quote_argument(argument):
return '"%s"' % (
argument
.replace('\\', '\\\\')
.replace('"', '\\"')
.replace('$', '\$')
.replace('`', '\`')
)
[
{ "caption": "Coffeescript: Convert selection to JS", "command": "coffeescript_to_js" }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment