Skip to content

Commit 0020027

Browse files
committed
Make annotator functional.
1 parent d504950 commit 0020027

File tree

2 files changed

+71
-72
lines changed

2 files changed

+71
-72
lines changed

PyTest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from . import settings
1010

1111

12-
Annotator = annotator.Annotator()
1312
Settings = settings.Settings('PyTest')
1413

1514

@@ -218,7 +217,7 @@ def on_modified_async(self, view):
218217

219218
class PytestMarkCurrentViewCommand(sublime_plugin.EventListener):
220219
def on_activated_async(self, view):
221-
Annotator.annotate(view, **State)
220+
annotator.annotate(view, **State)
222221

223222

224223
class PytestStart(sublime_plugin.WindowCommand):
@@ -255,7 +254,7 @@ def run(self, errors):
255254
'phantom_sets': {},
256255
})
257256

258-
Annotator.annotate_visible_views(**State)
257+
annotator.annotate_visible_views(**State)
259258

260259

261260
class PytestWillFail(sublime_plugin.WindowCommand):

annotator.py

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -36,96 +36,96 @@
3636

3737
PHANTOMS_MARKER = 'PyTestRunner'
3838

39-
class Annotator:
4039

41-
def annotate(self, view, errors={}, mode='auto', running=False,
42-
drawn_views=set(), phantom_sets={}, **kwargs_):
40+
def annotate(view, errors={}, mode='auto', running=False,
41+
drawn_views=set(), phantom_sets={}, **kwargs_):
4342

44-
buffer_id = view.buffer_id()
45-
if buffer_id in drawn_views:
46-
return
43+
buffer_id = view.buffer_id()
44+
if buffer_id in drawn_views:
45+
return
4746

48-
errs = get_errors_for_view(view, errors)
49-
if errs is None:
50-
# As long the tests are still running, we just don't know if
51-
# the view really is clean. To reduce visual clutter, we return
52-
# immediately.
53-
if running:
54-
return
55-
view.erase_regions(REGIONS_MARKER)
56-
drawn_views.add(buffer_id)
47+
errs = get_errors_for_view(view, errors)
48+
if errs is None:
49+
# As long the tests are still running, we just don't know if
50+
# the view really is clean. To reduce visual clutter, we return
51+
# immediately.
52+
if running:
5753
return
58-
59-
self._draw_regions(view, errs)
60-
self._draw_phantoms(view, errs, mode, phantom_sets)
54+
view.erase_regions(REGIONS_MARKER)
6155
drawn_views.add(buffer_id)
56+
return
57+
58+
_draw_regions(view, errs)
59+
_draw_phantoms(view, errs, mode, phantom_sets)
60+
drawn_views.add(buffer_id)
61+
62+
def annotate_visible_views(**kwargs):
63+
window = sublime.active_window()
6264

63-
def _draw_regions(self, view, errs):
64-
regions = [view.full_line(view.text_point(tbck['line'] - 1, 0))
65-
for tbck in errs]
65+
views = [window.active_view_in_group(group)
66+
for group in range(window.num_groups())]
6667

67-
view.add_regions(REGIONS_MARKER, regions,
68-
REGIONS_STYLE,
69-
REGIONS_ICON,
70-
sublime.DRAW_OUTLINED)
68+
for view in views:
69+
annotate(view, **kwargs)
7170

72-
def _draw_phantoms(self, view, errs, mode='auto', phantom_sets=set()):
73-
formatter = formatters.TB_MODES[mode]
74-
phantoms = []
7571

76-
show_focus_links = len(errs) > 1
77-
for tbck in errs:
78-
line = tbck['line']
79-
text = tbck['text']
80-
testcase = tbck.get('testcase')
72+
def _draw_regions(view, errs):
73+
regions = [view.full_line(view.text_point(tbck['line'] - 1, 0))
74+
for tbck in errs]
8175

82-
if text == '':
83-
continue
76+
view.add_regions(REGIONS_MARKER, regions,
77+
REGIONS_STYLE,
78+
REGIONS_ICON,
79+
sublime.DRAW_OUTLINED)
8480

85-
pt = view.text_point(line - 1, 0)
86-
indentation = get_indentation_at(view, pt)
87-
text = formatter.format_text(text, indentation)
81+
def _draw_phantoms(view, errs, mode, phantom_sets):
82+
formatter = formatters.TB_MODES[mode]
83+
phantoms = []
8884

89-
if show_focus_links and testcase:
90-
focus_link = (
91-
' <a href="focus:{}">focus test</a>'.format(testcase))
85+
show_focus_links = len(errs) > 1
86+
for tbck in errs:
87+
line = tbck['line']
88+
text = tbck['text']
89+
testcase = tbck.get('testcase')
9290

93-
lines = text.split('<br />')
94-
text = '<br />'.join([lines[0] + focus_link] + lines[1:])
91+
if text == '':
92+
continue
9593

96-
phantoms.append(sublime.Phantom(
97-
sublime.Region(pt, view.line(pt).b),
98-
('<body id=inline-error>' + STYLESHEET +
99-
'<div class="error">' +
100-
'<span class="message">' + text + '</span>' +
101-
'</div>' +
102-
'</body>'),
103-
sublime.LAYOUT_BELOW, self._on_navigate))
94+
pt = view.text_point(line - 1, 0)
95+
indentation = get_indentation_at(view, pt)
96+
text = formatter.format_text(text, indentation)
10497

105-
buffer_id = view.buffer_id()
106-
if buffer_id not in phantom_sets:
107-
phantom_set = sublime.PhantomSet(view, PHANTOMS_MARKER)
108-
phantom_sets[buffer_id] = phantom_set
109-
else:
110-
phantom_set = phantom_sets[buffer_id]
98+
if show_focus_links and testcase:
99+
focus_link = (
100+
' <a href="focus:{}">focus test</a>'.format(testcase))
111101

112-
phantom_set.update(phantoms)
102+
lines = text.split('<br />')
103+
text = '<br />'.join([lines[0] + focus_link] + lines[1:])
113104

114-
def _on_navigate(self, url):
115-
# split off 'focus:'
116-
testcase = url[6:]
117-
sublime.active_window().run_command(
118-
"pytest_auto_run", {'target': testcase})
105+
phantoms.append(sublime.Phantom(
106+
sublime.Region(pt, view.line(pt).b),
107+
('<body id=inline-error>' + STYLESHEET +
108+
'<div class="error">' +
109+
'<span class="message">' + text + '</span>' +
110+
'</div>' +
111+
'</body>'),
112+
sublime.LAYOUT_BELOW, _on_navigate))
119113

114+
buffer_id = view.buffer_id()
115+
if buffer_id not in phantom_sets:
116+
phantom_set = sublime.PhantomSet(view, PHANTOMS_MARKER)
117+
phantom_sets[buffer_id] = phantom_set
118+
else:
119+
phantom_set = phantom_sets[buffer_id]
120120

121-
def annotate_visible_views(self, **kwargs):
122-
window = sublime.active_window()
121+
phantom_set.update(phantoms)
123122

124-
views = [window.active_view_in_group(group)
125-
for group in range(window.num_groups())]
123+
def _on_navigate(url):
124+
# split off 'focus:'
125+
testcase = url[6:]
126+
sublime.active_window().run_command(
127+
"pytest_auto_run", {'target': testcase})
126128

127-
for view in views:
128-
self.annotate(view, **kwargs)
129129

130130

131131
def get_errors_for_view(view, errors_by_view):

0 commit comments

Comments
 (0)