-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolium-gutter.py
184 lines (155 loc) · 6.07 KB
/
solium-gutter.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
import sublime
import sublime_plugin
import os, re, codecs, subprocess, sys
try:
import commands
except ImportError:
pass
PLUGIN_FOLDER = os.path.dirname(os.path.realpath(__file__))
SETTINGS_FILE = "solium-gutter.sublime-settings"
OUTPUT_VALID = b"*** Solium results ***"
class SoliumGutterCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Make sure we're only linting Solidity files.
if self.file_unsupported():
return
# Get the current text in the buffer and save it in a temporary file.
# This allows for scratch buffers and dirty files to be linted as well.
temp_file_path = self.save_buffer_to_temp_file()
output = self.run_script_on_file(temp_file_path)
os.remove(temp_file_path)
# We're done with linting, rebuild the regions shown in the current view.
SoliumGutterStore.reset()
SoliumGutterEventListeners.reset()
self.view.erase_regions("solium_errors")
regions = []
menuitems = []
# For each line of Solium output (errors, warnings etc.) add a region
# in the view and a menuitem in a quick panel.
for line in output.splitlines():
try:
line_no, column_no, warning, message = line.split(":")
except:
continue
hint_point = self.view.text_point(int(line_no) - 1, int(column_no if column_no != "NaN" else "1") - 1)
hint_region = self.view.line(hint_point)
regions.append(hint_region)
menuitems.append(line_no + ":" + column_no + " " + message)
SoliumGutterStore.errors.append((hint_region, message))
self.add_regions(regions)
self.view.window().show_quick_panel(menuitems, self.on_quick_panel_selection)
def file_unsupported(self):
file_path = self.view.file_name()
view_settings = self.view.settings()
has_sol_extension = file_path != None and bool(re.search(r'\.sol$', file_path))
has_solidity_syntax = bool(re.search(r'Solidity', view_settings.get("syntax"), re.I))
return (not has_sol_extension and not has_solidity_syntax)
def save_buffer_to_temp_file(self):
buffer_text = self.view.substr(sublime.Region(0, self.view.size()))
temp_file_name = ".__temp__"
temp_file_path = PLUGIN_FOLDER + "/" + temp_file_name
f = codecs.open(temp_file_path, mode="w", encoding="utf-8")
f.write(buffer_text)
f.close()
return temp_file_path
def run_script_on_file(self, temp_file_path):
try:
node_path = PluginUtils.get_node_path()
script_path = PLUGIN_FOLDER + "/scripts/run.js"
file_path = self.view.file_name()
cmd = [node_path, script_path, temp_file_path, file_path or "?"]
output = SoliumGutterCommand.get_output(cmd)
print(output)
if output.find(OUTPUT_VALID) != -1:
output = output.decode('utf-8');
return output
print(output)
raise Exception(output)
except:
# Something bad happened.
print("Unexpected error({0}): {1}".format(sys.exc_info()[0], sys.exc_info()[1]))
# Usually, it's just node.js not being found. Try to alleviate the issue.
msg = "Node.js was not found in the default path. Please specify the location."
if not sublime.ok_cancel_dialog(msg):
msg = "You won't be able to use this plugin without specifying the path to node.js."
sublime.error_message(msg)
else:
PluginUtils.open_sublime_settings(self.view.window())
def add_regions(self, regions):
package_name = (PLUGIN_FOLDER.split(os.path.sep))[-1]
if int(sublime.version()) >= 3000:
icon = "Packages/" + package_name + "/warning.png"
self.view.add_regions("solium_errors", regions, "keyword", icon,
sublime.DRAW_EMPTY |
sublime.DRAW_NO_FILL |
sublime.DRAW_NO_OUTLINE |
sublime.DRAW_SQUIGGLY_UNDERLINE)
else:
icon = ".." + os.path.sep + package_name + os.path.sep + "warning"
self.view.add_regions("solium_errors", regions, "keyword", icon,
sublime.DRAW_EMPTY |
sublime.DRAW_OUTLINED)
def on_quick_panel_selection(self, index):
if index == -1:
return
# Focus the user requested region from the quick panel.
region = SoliumGutterStore.errors[index][0]
region_cursor = sublime.Region(region.begin(), region.begin())
selection = self.view.sel()
selection.clear()
selection.add(region_cursor)
self.view.show(region_cursor)
@staticmethod
def get_output(cmd):
if int(sublime.version()) < 3000:
if sublime.platform() != "windows":
# Handle Linux and OS X in Python 2.
run = '"' + '" "'.join(cmd) + '"'
return commands.getoutput(run)
else:
# Handle Windows in Python 2.
# Prevent console window from showing.
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return subprocess.Popen(cmd, \
stdout=subprocess.PIPE, \
startupinfo=startupinfo).communicate()[0]
else:
# Handle all OS in Python 3.
run = '"' + '" "'.join(cmd) + '"'
try:
return subprocess.check_output(run, stderr=subprocess.STDOUT, shell=True, env=os.environ)
except Exception as exception:
print(exception.output)
class SoliumGutterEventListeners(sublime_plugin.EventListener):
timer = None
@classmethod
def reset(self):
# Invalidate any previously set timer.
if self.timer != None:
self.timer.cancel()
self.timer = None
@staticmethod
def on_post_save(view):
view.run_command("solium_gutter")
class SoliumGutterSetNodePathCommand(sublime_plugin.TextCommand):
def run(self, edit):
PluginUtils.open_sublime_settings(self.view.window())
class SoliumGutterStore:
errors = []
@classmethod
def reset(self):
self.errors = []
class PluginUtils:
@staticmethod
def get_pref(key):
return sublime.load_settings(SETTINGS_FILE).get(key)
@staticmethod
def open_sublime_settings(window):
window.open_file(PLUGIN_FOLDER + "/" + SETTINGS_FILE)
@staticmethod
def get_node_path():
platform = sublime.platform()
node = PluginUtils.get_pref("node_path").get(platform)
print("Using node.js path on '" + platform + "': " + node)
return node