Skip to content

Commit

Permalink
Merge pull request jashkenas#72 from stephenjudkins/master
Browse files Browse the repository at this point in the history
"Compile and Display JS" command for Sublime Text 2
  • Loading branch information
ttilley committed Apr 2, 2013
2 parents eba5754 + 825254e commit e7b0ff4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.cache
*.cache
*.pyc
5 changes: 5 additions & 0 deletions Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[{
"keys": ["super+shift+r"],
"command": "coffee_compile",
"args": {}
}]
6 changes: 6 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"caption": "CoffeeScript: Compile",
"command": "coffee_compile"
}
]
30 changes: 30 additions & 0 deletions coffee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sublime
import sublime_plugin
import subprocess

class CoffeeCompileCommand(sublime_plugin.TextCommand):
def run(self, edit):
process = subprocess.Popen(["coffee", "-b", "-s", "-c"], stderr = subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
(compiled, errors) = process.communicate(self.get_coffeescript())

if len(compiled) == 0:
output = errors
else:
output = compiled

self.output_to_new_view(output)

def get_coffeescript(self):
selection = self.view.sel()[0]
if selection.empty():
selection = sublime.Region(0, self.view.size())
return self.view.substr(selection)

def output_to_new_view(self, output):
new_view = sublime.active_window().new_file()
new_view.set_name("(compiled CoffeeScript)")
new_view.set_syntax_file("Packages/JavaScript/JavaScript.tmLanguage")
new_view.set_scratch(True)
edit = new_view.begin_edit()
new_view.insert(edit, 0, output)
new_view.end_edit(edit)

0 comments on commit e7b0ff4

Please sign in to comment.