Skip to content

Commit

Permalink
extract complain into utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
tomv564 committed Oct 21, 2015
1 parent 408e7a2 commit ab42a78
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
14 changes: 2 additions & 12 deletions stack_ide.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

sys.path.append(os.path.dirname(os.path.realpath(__file__)))

from utility import first_folder
from utility import first_folder, complain
from req import Req
from log import Log
from win import Win
Expand All @@ -26,7 +26,6 @@

class StackIDE:

complaints_shown = set()

def __init__(self, window, settings, backend=None):
self.window = window
Expand Down Expand Up @@ -68,15 +67,6 @@ def send_request(self, request, response_handler = None):
else:
Log.error("Couldn't send request, no process!", request)

@classmethod
def complain(cls,complaint_id,msg):
"""
we don't do it again (until reset)
"""
if complaint_id not in cls.complaints_shown:
cls.complaints_shown.add(complaint_id)
sublime.error_message(msg)


def load_initial_targets(self):
"""
Expand Down Expand Up @@ -142,7 +132,7 @@ def handle_response(self, data):
version_got = tuple(contents) if type(contents) is list else contents
if expected_version > version_got:
Log.error("Old stack-ide protocol:", version_got, '\n', 'Want version:', expected_version)
StackIDE.complain("wrong-stack-ide-version",
complain("wrong-stack-ide-version",
"Please upgrade stack-ide to a newer version.")
elif expected_version < version_got:
Log.warning("stack-ide protocol may have changed:", version_got)
Expand Down
18 changes: 3 additions & 15 deletions stack_ide_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from stack_ide import StackIDE
from log import Log
from utility import first_folder,expected_cabalfile,has_cabal_file, is_stack_project
from utility import first_folder,expected_cabalfile,has_cabal_file, is_stack_project, complain, reset_complaints
try:
import sublime
except ImportError:
Expand Down Expand Up @@ -55,7 +55,7 @@ def configure_instance(window, settings):
except FileNotFoundError as e:
instance = NoStackIDE("instance init failed -- stack not found")
Log.error(e)
StackIDEManager.complain('stack-not-found',
complain('stack-not-found',
"Could not find program 'stack'!\n\n"
"Make sure that 'stack' and 'stack-ide' are both installed. "
"If they are not on the system path, edit the 'add_to_PATH' "
Expand All @@ -71,7 +71,6 @@ def configure_instance(window, settings):

class StackIDEManager:
ide_backend_instances = {}
complaints_shown = set()
settings = None

@classmethod
Expand Down Expand Up @@ -146,24 +145,13 @@ def reset(cls, settings):
"""
Log.normal("Resetting StackIDE")
cls.kill_all()
cls.complaints_shown = set()
reset_complaints()
cls.settings = settings

@classmethod
def configure(cls, settings):
cls.settings = settings

@classmethod
def complain(cls,complaint_id,msg):
"""
Show the msg as an error message (on a modal pop-up). The complaint_id is
used to decide when we have already complained about something, so that
we don't do it again (until reset)
"""
if complaint_id not in cls.complaints_shown:
cls.complaints_shown.add(complaint_id)
sublime.error_message(msg)


class NoStackIDE:
"""
Expand Down
11 changes: 11 additions & 0 deletions test/test_utility.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from test.mocks import mock_view, mock_window, cur_dir
import utility
from .stubs import sublime

class UtilTests(unittest.TestCase):

Expand All @@ -23,3 +24,13 @@ def test_span_from_view_selection(self):
self.assertEqual(1, span['spanFromColumn'])
self.assertEqual(1, span['spanToColumn'])
self.assertEqual('src/Main.hs', span['spanFilePath'])

def test_complaints_not_repeated(self):
utility.complain('complaint', 'waaaah')
self.assertEqual(sublime.current_error, 'waaaah')
utility.complain('complaint', 'waaaah 2')
self.assertEqual(sublime.current_error, 'waaaah')
utility.reset_complaints()
utility.complain('complaint', 'waaaah 2')
self.assertEqual(sublime.current_error, 'waaaah 2')

16 changes: 16 additions & 0 deletions utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@

from log import Log

complaints_shown = set()
def complain(id, text):
"""
Show the msg as an error message (on a modal pop-up). The complaint_id is
used to decide when we have already complained about something, so that
we don't do it again (until reset)
"""
if id not in complaints_shown:
complaints_shown.add(id)
sublime.error_message(text)

def reset_complaints():
global complaints_shown
complaints_shown = set()


def first_folder(window):
"""
We only support running one stack-ide instance per window currently,
Expand Down

0 comments on commit ab42a78

Please sign in to comment.