Skip to content

Commit e7b0ff4

Browse files
committed
Merge pull request jashkenas#72 from stephenjudkins/master
"Compile and Display JS" command for Sublime Text 2
2 parents eba5754 + 825254e commit e7b0ff4

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.cache
1+
*.cache
2+
*.pyc

Default (OSX).sublime-keymap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[{
2+
"keys": ["super+shift+r"],
3+
"command": "coffee_compile",
4+
"args": {}
5+
}]

Default.sublime-commands

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"caption": "CoffeeScript: Compile",
4+
"command": "coffee_compile"
5+
}
6+
]

coffee.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sublime
2+
import sublime_plugin
3+
import subprocess
4+
5+
class CoffeeCompileCommand(sublime_plugin.TextCommand):
6+
def run(self, edit):
7+
process = subprocess.Popen(["coffee", "-b", "-s", "-c"], stderr = subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
8+
(compiled, errors) = process.communicate(self.get_coffeescript())
9+
10+
if len(compiled) == 0:
11+
output = errors
12+
else:
13+
output = compiled
14+
15+
self.output_to_new_view(output)
16+
17+
def get_coffeescript(self):
18+
selection = self.view.sel()[0]
19+
if selection.empty():
20+
selection = sublime.Region(0, self.view.size())
21+
return self.view.substr(selection)
22+
23+
def output_to_new_view(self, output):
24+
new_view = sublime.active_window().new_file()
25+
new_view.set_name("(compiled CoffeeScript)")
26+
new_view.set_syntax_file("Packages/JavaScript/JavaScript.tmLanguage")
27+
new_view.set_scratch(True)
28+
edit = new_view.begin_edit()
29+
new_view.insert(edit, 0, output)
30+
new_view.end_edit(edit)

0 commit comments

Comments
 (0)