-
Notifications
You must be signed in to change notification settings - Fork 11
/
CoalaCommand.py
54 lines (45 loc) · 1.76 KB
/
CoalaCommand.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
import sublime_plugin
import sublime
import json
from .CoalaThread import CoalaThread
from .Utils import log, COALA_KEY
def show_output(view):
output_str = view.settings().get(COALA_KEY + ".output_str")
if not output_str:
return
output = json.loads(output_str)
region_flag = sublime.DRAW_OUTLINED
regions = []
for section_name, section_results in output["results"].items():
for result in section_results:
if not result["affected_code"]:
continue
for code_region in result["affected_code"]:
line = view.line(
view.text_point(code_region["start"]["line"]-1, 0))
regions.append(line)
view.add_regions(COALA_KEY, regions, COALA_KEY, "dot", region_flag)
class CoalaCommand(sublime_plugin.TextCommand):
"""
The CoalaCommand inherits the TextCommand from sublime_plugin and can be
executed using `view.run_command("coala")` - which executes the `run()`
function by default.
"""
def run(self, edit, **kwargs):
file_name = self.view.file_name()
log("Trying to run coala on", file_name)
if file_name:
thread = CoalaThread(self.view, show_output)
thread.start()
self.progress_tracker(thread)
else:
sublime.status_message("Save the file to run coala on it")
def progress_tracker(self, thread, i=0):
""" Display spinner while coala is running """
icons = [u"◐", u"◓", u"◑", u"◒"]
sublime.status_message("Running coala %s" % icons[i])
if thread and thread.is_alive():
i = (i + 1) % 4
sublime.set_timeout(lambda: self.progress_tracker(thread, i), 100)
else:
sublime.status_message("")