Skip to content

Commit d9807b4

Browse files
author
deathaxe
authored
Refactor OutputPanel (#251)
1. Pass owning `window` when initializing object. 2. Replace deprecated `get_output_panel()` API call. 3. Reduce API calls when determining base_dir. 4. Use normal build output panel ("exec"). 5. format code using black.
1 parent e860c4e commit d9807b4

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

unittesting/mixin.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ def load_stream(self, package, settings):
106106
output = settings["output"]
107107
if not output or output == "<panel>":
108108
output_panel = OutputPanel(
109-
'UnitTesting', file_regex=r'File "([^"]*)", line (\d+)')
109+
sublime.active_window(),
110+
'exec',
111+
file_regex=r'File "([^"]*)", line (\d+)'
112+
)
110113
output_panel.show()
111114
stream = output_panel
112115
else:

unittesting/utils/output_panel.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
import collections
22
import os
3-
import sublime
43
import threading
54

65

76
class OutputPanel:
8-
97
def __init__(
10-
self, name, file_regex='', line_regex='', base_dir=None,
11-
word_wrap=False, line_numbers=False, gutter=False,
12-
scroll_past_end=False
8+
self,
9+
window,
10+
name,
11+
file_regex="",
12+
line_regex="",
13+
base_dir=None,
14+
word_wrap=False,
15+
line_numbers=False,
16+
gutter=False,
17+
scroll_past_end=False,
1318
):
1419
self.name = name
15-
self.window = sublime.active_window()
16-
self.output_view = self.window.get_output_panel(name)
20+
self.window = window
21+
self.output_view = window.create_output_panel(name)
1722

1823
# default to the current file directory
19-
if not base_dir and self.window.active_view() and \
20-
self.window.active_view().file_name():
21-
base_dir = os.path.dirname(self.window.active_view().file_name())
24+
if not base_dir:
25+
view = window.active_view()
26+
if view:
27+
file_name = view.file_name()
28+
if file_name:
29+
base_dir = os.path.dirname(file_name)
2230

2331
settings = self.output_view.settings()
2432
settings.set("result_file_regex", file_regex)
@@ -29,7 +37,9 @@ def __init__(
2937
settings.set("gutter", gutter)
3038
settings.set("scroll_past_end", scroll_past_end)
3139

32-
self.output_view.assign_syntax("Packages/UnitTesting/res/unit-testing-test-result.sublime-syntax")
40+
# make sure to apply settings
41+
self.output_view = window.create_output_panel(name)
42+
self.output_view.assign_syntax("unit-testing-test-result.sublime-syntax")
3343
self.output_view.set_read_only(True)
3444
self.closed = False
3545

@@ -44,15 +54,12 @@ def writeln(self, s):
4454
self.write(s + "\n")
4555

4656
def _write(self):
57+
text = ""
4758
with self.text_queue_lock:
48-
text = ''
4959
while self.text_queue:
5060
text += self.text_queue.popleft()
5161

52-
self.output_view.run_command(
53-
'append',
54-
{'characters': text, 'force': True}
55-
)
62+
self.output_view.run_command("append", {"characters": text, "force": True})
5663
self.output_view.show(self.output_view.size())
5764

5865
def flush(self):

0 commit comments

Comments
 (0)