|
36 | 36 |
|
37 | 37 | PHANTOMS_MARKER = 'PyTestRunner'
|
38 | 38 |
|
39 |
| -class Annotator: |
40 | 39 |
|
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_): |
43 | 42 |
|
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 |
47 | 46 |
|
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: |
57 | 53 | return
|
58 |
| - |
59 |
| - self._draw_regions(view, errs) |
60 |
| - self._draw_phantoms(view, errs, mode, phantom_sets) |
| 54 | + view.erase_regions(REGIONS_MARKER) |
61 | 55 | 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() |
62 | 64 |
|
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())] |
66 | 67 |
|
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) |
71 | 70 |
|
72 |
| - def _draw_phantoms(self, view, errs, mode='auto', phantom_sets=set()): |
73 |
| - formatter = formatters.TB_MODES[mode] |
74 |
| - phantoms = [] |
75 | 71 |
|
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] |
81 | 75 |
|
82 |
| - if text == '': |
83 |
| - continue |
| 76 | + view.add_regions(REGIONS_MARKER, regions, |
| 77 | + REGIONS_STYLE, |
| 78 | + REGIONS_ICON, |
| 79 | + sublime.DRAW_OUTLINED) |
84 | 80 |
|
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 = [] |
88 | 84 |
|
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') |
92 | 90 |
|
93 |
| - lines = text.split('<br />') |
94 |
| - text = '<br />'.join([lines[0] + focus_link] + lines[1:]) |
| 91 | + if text == '': |
| 92 | + continue |
95 | 93 |
|
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) |
104 | 97 |
|
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)) |
111 | 101 |
|
112 |
| - phantom_set.update(phantoms) |
| 102 | + lines = text.split('<br />') |
| 103 | + text = '<br />'.join([lines[0] + focus_link] + lines[1:]) |
113 | 104 |
|
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)) |
119 | 113 |
|
| 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] |
120 | 120 |
|
121 |
| - def annotate_visible_views(self, **kwargs): |
122 |
| - window = sublime.active_window() |
| 121 | + phantom_set.update(phantoms) |
123 | 122 |
|
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}) |
126 | 128 |
|
127 |
| - for view in views: |
128 |
| - self.annotate(view, **kwargs) |
129 | 129 |
|
130 | 130 |
|
131 | 131 | def get_errors_for_view(view, errors_by_view):
|
|
0 commit comments