-
Notifications
You must be signed in to change notification settings - Fork 2
/
cleanjs_sublime.py
113 lines (89 loc) · 3.33 KB
/
cleanjs_sublime.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
class attrdict(dict):
"""Used to easily mock modules dependencies"""
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.__dict__ = self
import sys, os
try:
import sublime, sublime_plugin
except:
# Mocking out dependencies in case of unit testing
# FIXME: How to make this more elegant ???
sublime = {}
class TextCommand:
pass
class EventListener:
pass
sublime_plugin = attrdict(TextCommand=TextCommand, EventListener=EventListener)
from parsers import fileparser
from reviewers import reviewer
REGIONS_KEY = PANEL_KEY = "CLEANJS"
class CleanjsCommand(sublime_plugin.TextCommand):
def run(self, edit):
#settings = self.view.settings()
js_code = self.view.substr(sublime.Region(0, self.view.size()))
file_data = None
try:
file_data = fileparser.get_file_data_from_content("sublime cleanjs", js_code)
except Exception as error:
self.show_messages_to_panel(str(error), PANEL_KEY)
return
result = reviewer.review(file_data, working_dir=sublime.packages_path() + os.sep + "cleanjs")
text = self.get_full_report_from_result(result)
self.show_messages_to_panel(text, PANEL_KEY)
self.show_messages_to_regions(result.message_bag.get_messages(), file_data.lines)
def get_full_report_from_result(self, result):
text = "Rate : " + result.rate + "\n"
messages = result.message_bag.get_messages()
for message in messages:
if message.reviewer != "general":
text += "\nline " + str(message.line) + " | " + message.reviewer + " " + message.type + " : " + message.content
return text
def show_messages_to_regions(self, messages, file_lines):
self.view.erase_regions(REGIONS_KEY)
regions = []
for message in messages:
if message.line:
points = self.get_points_from_line(message.line, file_lines)
regions.append(sublime.Region(points[0], points[1]))
self.view.add_regions(REGIONS_KEY, regions, "string", sublime.DRAW_OUTLINED)
def get_points_from_line(self, line_number, file_lines):
start = 0
end = 0
for index, line in enumerate(file_lines.all_lines):
if index+1 == line_number:
end = start + len(line.complete_line)
break
start += len(line.complete_line) + 1
return (start, end)
def show_messages_to_panel(self, text, panel_name = 'example'):
# get_output_panel doesn't "get" the panel, it *creates* it,
# so we should only call get_output_panel once
if not hasattr(self, 'output_view'):
self.output_view = self.view.window().get_output_panel(panel_name)
v = self.output_view
# Write this text to the output panel and display it
edit = v.begin_edit()
v.erase(edit, sublime.Region(0, v.size()))
v.insert(edit, v.size(), text)
v.end_edit(edit)
v.show(v.size())
self.view.window().run_command("show_panel", {"panel": "output." + panel_name})
class EventListener(sublime_plugin.EventListener):
def on_modified(self, view):
view.erase_regions(REGIONS_KEY)
if __name__ == "__main__":
class MockLine(object):
def __init__(self, line):
self.complete_line = line
class MockFileLines(object):
def __init__(self, lines):
self.all_lines = lines
line1 = MockLine("function test(a) {")
line2 = MockLine(" var b = 1 + 1;")
line3 = MockLine(" alert(b);")
line4 = MockLine("}")
lines = MockFileLines([line1, line2, line3, line4])
command = CleanjsCommand()
assert command.get_points_from_line(3, lines) == (35,45)
print "ALL TESTS OK"