forked from sustained/CoffeeScript-Sublime-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoffeeScript.py
More file actions
executable file
·120 lines (102 loc) · 3.1 KB
/
CoffeeScript.py
File metadata and controls
executable file
·120 lines (102 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import sublime
from os import path
from subprocess import Popen, PIPE
from sublime_plugin import TextCommand, WindowCommand
settings = sublime.load_settings('CoffeeScript.sublime-settings')
def run(cmd, args = [], cwd = None, env = None):
if not type(args) is list:
args = [args]
if env is None:
env = {"PATH": settings.get('binDir', '/usr/local/bin')}
proc = Popen([cmd]+args, env=env, cwd=cwd, stdout=PIPE, stderr=PIPE)
stat = proc.communicate()
okay = proc.returncode == 0
return {"okay": okay, "out": stat[0], "err": stat[1]}
def brew(args):
return run("coffee", args=args)
def cake(task, cwd):
return run("cake", args=task, cwd=cwd)
def isCoffee(view = None):
if view is None:
view = sublime.active_window().active_view()
return 'source.coffee' in view.scope_name(0)
class Text():
@staticmethod
def all(view):
return view.substr(sublime.Region(0, view.size()))
@staticmethod
def sel(view):
text = []
for region in view.sel():
if region.empty():
continue
text.append(view.substr(region))
return "".join(text)
@staticmethod
def get(view):
text = Text.sel(view)
if len(text) > 0:
return text
return Text.all(view)
class CompileAndDisplayCommand(TextCommand):
def is_enabled(self):
return isCoffee(self.view)
def run(self, edit, **kwargs):
opt = kwargs["opt"]
res = brew(['-e', '-b', opt, Text.get(self.view)])
output = self.view.window().new_file()
output.set_scratch(True)
if opt == '-p':
output.set_syntax_file('Packages/JavaScript/JavaScript.tmLanguage')
if res["okay"] is True:
output.insert(edit, 0, res["out"])
else:
output.insert(edit, 0, res["err"].split("\n")[0])
class CheckSyntaxCommand(TextCommand):
def is_enabled(self):
return isCoffee(self.view)
def run(self, edit):
res = brew(['-e', '-b', '-p', Text.get(self.view)])
if res["okay"] is True:
status = 'Valid'
else:
status = res["err"].split("\n")[0]
sublime.status_message('Syntax %s' % status)
class RunScriptCommand(WindowCommand):
def finish(self, text):
if text == '':
return
text = "{puts, print} = require 'util'\n" + text
res = brew(['-e', '-b', text])
if res["okay"] is True:
output = self.window.new_file()
output.set_scratch(True)
edit = output.begin_edit()
output.insert(edit, 0, res["out"])
output.end_edit(edit)
else:
sublime.status_message('Syntax %s' % res["err"].split("\n")[0])
def run(self):
sel = Text.sel(sublime.active_window().active_view())
if len(sel) > 0:
if not isCoffee(): return
self.finish(sel)
else:
self.window.show_input_panel('Coffee >', '', self.finish, None, None)
class RunCakeTaskCommand(WindowCommand):
def finish(self, task):
if task == '':
return
if not path.exists(path.join(self.window.folders()[0], 'Cakefile')):
return sublime.status_message("Cakefile not found.")
res = cake(task, self.window.folders()[0])
if res["okay"] is True:
if "No such task" in res["out"]:
msg = "doesn't exist"
else:
msg = "suceeded"
else:
msg = "failed"
sublime.status_message("Task %s - %s." % (task, msg))
def run(self):
self.window.show_input_panel('Cake >', '', self.finish, None, None)