-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoffeeToJS.py
127 lines (106 loc) · 2.74 KB
/
CoffeeToJS.py
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
121
122
123
124
125
126
127
import subprocess
import sublime, sublime_plugin
from itertools import chain
from functools import wraps
PLUGIN_SETTINGS_FILE = 'coffeetojs.sublime_settings'
PROJECT_SETTINGS = 'coffeetojs'
def get_settings():
settings = {
'coffeeCommand': 'coffee',
'inlineArgs': ['--no-header', '--bare'],
'fileArgs': ['--no-header'],
'debug': False,
}
ud_settings = sublime.load_settings(PLUGIN_SETTINGS_FILE) or {}
project_settings = sublime.active_window().active_view().settings().get(PROJECT_SETTINGS) or {}
return {
'coffeeCommand': ud_settings.get(
'coffeeCommand',
project_settings.get(
'coffeeCommand',
settings.get('coffeeCommand')
)
),
'inlineArgs': ud_settings.get(
'inlineArgs',
project_settings.get(
'inlineArgs',
settings.get('inlineArgs')
)
),
'fileArgs': ud_settings.get(
'fileArgs',
project_settings.get(
'fileArgs',
settings.get('fileArgs')
)
),
'debug': ud_settings.get(
'debug',
project_settings.get(
'debug',
settings.get('debug')
)
),
}
def debug(func):
@wraps(func)
def wrapper(*args, **kwargs):
response = func(*args, **kwargs)
print('DEBUG COMMAND ::', response)
return response
return wrapper
class Command:
@staticmethod
@debug
def selectCommand(code):
settings = get_settings()
#chain flattens the list
command = chain.from_iterable(
[[settings['coffeeCommand']], settings['inlineArgs'], ['-ec', code]]
)
return list(command)
@staticmethod
@debug
def fileCommand(file):
settings = get_settings()
command = chain.from_iterable(
[[settings['coffeeCommand']], settings['fileArgs'], ['-c', file]]
)
return list(command)
@staticmethod
@debug
def clipboardCommand(code):
return Command.selectCommand(code)
class CoffeeToJsSelectedCommand(sublime_plugin.TextCommand):
def run(self, edit):
selects = self.view.sel()
for select in selects:
code = self.view.substr(select)
if not code:
continue
process = subprocess.Popen(Command.selectCommand(code), stdout=subprocess.PIPE)
out, err = process.communicate()
self.view.insert(edit, select.begin(), out.decode())
def is_enabled(self):
return True
class CoffeeToJsFromFileCommand(sublime_plugin.TextCommand):
def run(self, edit):
source = self.view.file_name()
subprocess.call(Command.fileCommand(source))
def is_enabled(self):
return True
class CoffeeToJsFromClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
code = sublime.get_clipboard()
process = subprocess.Popen(
Command.clipboardCommand(code),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, err = process.communicate()
if err:
self.write_to_console(err)
self.view.replace(edit, self.view.sel()[0], out.decode())
def is_enabled(self):
return True