Skip to content

Commit

Permalink
Avoid closing wrong panels (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
niosus authored Jan 10, 2021
1 parent 8b66d57 commit fb3f536
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
16 changes: 9 additions & 7 deletions plugin/utils/output_panel_handler.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
"""Handle everything related to the output panel."""
import time
import sublime


class OutputPanelHandler():
"""Handle the output panel."""
_PANEL_TAG = "ECC"
PANEL_TAG = "ECC"
PANEL_NAME = "output." + PANEL_TAG

@staticmethod
def hide_panel():
"""Hide the output panel."""
window = sublime.active_window()
window.run_command(
"hide_panel", {"panel": "output." + OutputPanelHandler._PANEL_TAG})
if window.active_panel() == OutputPanelHandler.PANEL_NAME:
window.run_command(
"hide_panel", {"panel": OutputPanelHandler.PANEL_NAME})

@staticmethod
def show(text):
"""Show the panel with text."""
import time
window = sublime.active_window()
window.destroy_output_panel(OutputPanelHandler._PANEL_TAG)
window.destroy_output_panel(OutputPanelHandler.PANEL_TAG)
panel_view = window.create_output_panel(
OutputPanelHandler._PANEL_TAG)
OutputPanelHandler.PANEL_TAG)
while panel_view.is_loading():
time.sleep(0.1)
panel_view.run_command("select_all")
Expand All @@ -34,5 +36,5 @@ def show(text):
panel_view.show(panel_view.size())

window.run_command(
"show_panel", {"panel": "output." + OutputPanelHandler._PANEL_TAG})
"show_panel", {"panel": OutputPanelHandler.PANEL_NAME})
panel_view.set_read_only(True)
2 changes: 1 addition & 1 deletion tests/test_bazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_bad_project(self):
future = MockFuture(output)
Bazel.compdb_generated(future)
window = sublime.active_window()
panel_view = window.find_output_panel(OutputPanelHandler._PANEL_TAG)
panel_view = window.find_output_panel(OutputPanelHandler.PANEL_TAG)
panel_content = panel_view.substr(sublime.Region(0, panel_view.size()))
split_output = output.split('\n')
split_panel_content = panel_content.split('\n')
Expand Down
30 changes: 25 additions & 5 deletions tests/test_output_panel_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from EasyClangComplete.plugin.utils import output_panel_handler
from unittest import TestCase

import sublime
import time
import imp
import sublime

imp.reload(output_panel_handler)

Expand All @@ -24,15 +25,34 @@ def test_panel_creation(self):
OutputPanelHandler.show("hello world")
window = sublime.active_window()
self.assertIsNotNone(window.active_panel())
self.assertEquals(window.active_panel(), "output.ECC")
panel_view = window.find_output_panel(OutputPanelHandler._PANEL_TAG)
self.assertEqual(window.active_panel(), OutputPanelHandler.PANEL_NAME)
panel_view = window.find_output_panel(OutputPanelHandler.PANEL_TAG)
contents = panel_view.substr(sublime.Region(0, panel_view.size()))
self.assertEquals(contents, "hello world")
self.assertEqual(contents, "hello world")

def test_panel_closing(self):
"""Test that we can close a panel."""
OutputPanelHandler.show("hello world")
window = sublime.active_window()
self.assertEquals(window.active_panel(), "output.ECC")
self.assertEqual(window.active_panel(), OutputPanelHandler.PANEL_NAME)
OutputPanelHandler.hide_panel()
self.assertIsNone(window.active_panel())

def test_not_closing_panel(self):
"""Test that we don't close a panel with a wrong name."""
window = sublime.active_window()
test_panel_name = "test_panel"
panel_view = window.create_output_panel(test_panel_name)
while panel_view.is_loading():
time.sleep(0.1)
if not window.active_panel():
# We cannot initialize a panel probably because of a CI quirk.
return
self.assertIsNotNone(window.active_panel())
self.assertNotEqual(window.active_panel(),
OutputPanelHandler.PANEL_NAME)
OutputPanelHandler.hide_panel()
self.assertIsNotNone(window.active_panel())
self.assertNotEqual(window.active_panel(),
OutputPanelHandler.PANEL_NAME)
window.destroy_output_panel(test_panel_name)

0 comments on commit fb3f536

Please sign in to comment.