-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathevaluate_javascript_command.py
184 lines (155 loc) · 5.25 KB
/
evaluate_javascript_command.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
result_js = ""
region_selected = None
popup_is_showing = False
ej_css = """
<style>
html{
margin: 0;
padding: 0;
}
body{
color: #fff;
margin: 0;
padding: 0;
}
.container{
background-color: #202A31;
padding: 10px;
}
a{
color: #fff;
display: block;
margin: 10px 0;
}
</style>
"""
def action_result(action):
global result_js
global region_selected
view = sublime.active_window().active_view()
sel = region_selected
str_selected = view.substr(sel).strip()
if action == "copy_to_clipboard" :
sublime.set_clipboard(result_js)
elif action == "replace_text" :
view.run_command("replace_text")
elif action == "view_result_formatted":
view.run_command("view_result_formatted")
view.hide_popup()
result_js = ""
class view_result_formattedCommand(sublime_plugin.TextCommand):
def run(self, edit):
global result_js
global region_selected
sublime.active_window().show_input_panel("Result", result_js, back_to_popup, back_to_popup, back_to_popup)
class replace_textCommand(sublime_plugin.TextCommand):
def run(self, edit):
global result_js
global region_selected
view = self.view
sel = Util.trim_Region(view, region_selected)
view.replace(edit, sel, result_js)
if region_selected.a < region_selected.b :
region_selected.b = region_selected.a+len(result_js)
else :
region_selected.a = region_selected.b+len(result_js)
class ej_hide_popupEventListener(sublime_plugin.EventListener):
def on_modified_async(self, view) :
global popup_is_showing
if popup_is_showing :
view.hide_popup()
popup_is_showing = False
class evaluate_javascriptCommand(sublime_plugin.TextCommand):
def run(self, edit, is_line=False, eval_type="eval"):
global result_js
global region_selected
global popup_is_showing
view = self.view
sel = view.sel()[0]
popup_is_showing = False
str_selected = view.substr(sel).strip()
if is_line:
lines = view.lines(sel)
region_selected = lines[0]
str_selected = view.substr(region_selected)
else:
if not str_selected and region_selected :
region = get_start_end_code_highlights_eval()
region_selected = sublime.Region(region[0], region[1])
lines = view.lines(region_selected)
str_selected = ""
for line in lines:
str_selected += view.substr(view.full_line(line))
elif str_selected:
lines = view.lines(sel)
region_selected = sublime.Region if not region_selected else region_selected
region_selected = sublime.Region(lines[0].begin(), lines[-1:][0].end())
elif not str_selected :
return
if not region_selected :
return
view.run_command("show_start_end_dot_eval")
try:
from node.main import NodeJS
node = NodeJS()
result_js = node.eval(str_selected, eval_type, True)
popup_is_showing = True
view.show_popup("<html><head></head><body>"+ej_css+"""<div class=\"container\">
<p class="result">Result: """+result_js+"""</p>
<div><a href="view_result_formatted">View result formatted(\\t,\\n,...)</a></div>
<div><a href="copy_to_clipboard">Copy result to clipboard</a></div>
<div><a href="replace_text">Replace text with result</a></div>
</div>
</body></html>""", sublime.COOPERATE_WITH_AUTO_COMPLETE, -1, 400, 400, action_result)
except Exception as e:
#sublime.error_message("Error: "+traceback.format_exc())
sublime.active_window().show_input_panel("Result", "Error: "+traceback.format_exc(), lambda x: "" , lambda x: "", lambda : "")
def is_enabled(self, **args) :
view = self.view
if not Util.selection_in_js_scope(view) :
return False
return True
def is_visible(self, **args) :
view = self.view
if not Util.selection_in_js_scope(view) :
return False
return True
class show_start_end_dot_evalCommand(sublime_plugin.TextCommand) :
def run(self, edit) :
global region_selected
view = self.view
lines = view.lines(region_selected)
view.add_regions("region-dot", [lines[0], lines[-1:][0]], "code", "dot", sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE)
#view.add_regions("region-body", [region_selected], "code", "", sublime.DRAW_NO_FILL)
def is_enabled(self, **args) :
view = self.view
if not Util.selection_in_js_scope(view) :
return False
return True
def is_visible(self, **args) :
view = self.view
if not Util.selection_in_js_scope(view) :
return False
return True
class hide_start_end_dot_evalCommand(sublime_plugin.TextCommand) :
def run(self, edit) :
view = self.view
view.erase_regions("region-dot")
#view.erase_regions("region-body")
def is_enabled(self, **args) :
view = self.view
if not Util.selection_in_js_scope(view) :
return False
return True
def is_visible(self, **args) :
view = self.view
if not Util.selection_in_js_scope(view) :
return False
return True
def get_start_end_code_highlights_eval() :
view = sublime.active_window().active_view()
return [view.line(view.get_regions("region-dot")[0]).begin(), view.line(view.get_regions("region-dot")[1]).end()]
def back_to_popup(*str_arg):
view = sublime.active_window().active_view()
view.run_command("evaluate_javascript")
return