Skip to content

Commit

Permalink
Extract clear_view to views.py
Browse files Browse the repository at this point in the history
Remove dead variable
Rename lookup_view
  • Loading branch information
danmoseley committed Jul 30, 2015
1 parent 9a1176c commit 2c9f32b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 53 deletions.
2 changes: 0 additions & 2 deletions protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ def __init__(self):
self.next_id = 0
self.commands = {}
self.notifications = {}
self.last_log_object = None

def connect(self, url, on_open=None, on_close=None):
""" Attempt to connect to the web socket """
print (('SWI: Connecting to ' + url))
websocket.enableTrace(False)
self.last_break = None
self.last_log_object = None
self.url = url
self.on_open = on_open
self.on_close = on_close
Expand Down
73 changes: 26 additions & 47 deletions swi.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def messageRepeatCountUpdated(self, data, notification):
def messagesCleared(self, data, notification):
""" Notification when console cleared (by navigate or on request) """
utils.assert_main_thread()
clear_view('console')
views.clear_view('console')

# build table of mappings from local to server
def scriptParsed(self, data, notification):
Expand Down Expand Up @@ -318,9 +318,9 @@ def resumed(self, data, notification):
"""
utils.assert_main_thread()

clear_view('stack')
clear_view('scope')
clear_view('styles')
views.clear_view('stack')
views.clear_view('scope')
views.clear_view('styles')

channel.send(webkit.Debugger.setOverlayMessage())

Expand Down Expand Up @@ -435,7 +435,7 @@ def run(self):

class SwiDebugClearConsoleCommand(sublime_plugin.WindowCommand):
def run(self):
clear_view('console')
views.clear_view('console')


class SwiDebugEvaluateCommand(sublime_plugin.WindowCommand):
Expand Down Expand Up @@ -481,7 +481,7 @@ def run(self):
utils.assert_main_thread()
active_view = self.window.active_view()

v = views.lookup_view(active_view)
v = views.wrap_view(active_view)
view_name = v.file_name();
if not view_name: # eg file mapping pane
return
Expand Down Expand Up @@ -566,7 +566,7 @@ class SwiShowFileMappingsInternalCommand(sublime_plugin.TextCommand):
""" Called internally on the file mapping view """
def run(self, edit):

clear_view('mapping')
views.clear_view('mapping')
self.view.insert(edit, 0, json.dumps(file_to_scriptId, sort_keys=True, indent=4, separators=(',', ': ')))


Expand All @@ -577,7 +577,7 @@ def update_overlays():
# loop over all views, identifying the files
# we need to draw into
for v in window.views():
v = views.lookup_view(v)
v = views.wrap_view(v)

if not v.file_name():
continue
Expand Down Expand Up @@ -620,20 +620,20 @@ def __init__(self):
self.timing = time.time()

def on_new(self, v):
views.lookup_view(v).on_new()
views.wrap_view(v).on_new()

def on_clone(self, v):
views.lookup_view(v).on_clone()
views.wrap_view(v).on_clone()

def on_load(self, v):
update_overlays()
views.lookup_view(v).on_load()
views.wrap_view(v).on_load()

def on_close(self, v):
views.lookup_view(v).on_close()
views.wrap_view(v).on_close()

def on_pre_save(self, v):
views.lookup_view(v).on_pre_save()
views.wrap_view(v).on_pre_save()

def reload_styles(self):
channel.send(webkit.Runtime.evaluate("var files = document.getElementsByTagName('link');var links = [];for (var a = 0, l = files.length; a < l; a++) {var elem = files[a];var rel = elem.rel;if (typeof rel != 'string' || rel.length === 0 || rel === 'stylesheet') {links.push({'elem': elem,'href': elem.getAttribute('href').split('?')[0],'last': false});}}for ( a = 0, l = links.length; a < l; a++) {var link = links[a];link.elem.setAttribute('href', (link.href + '?x=' + Math.random()));}"))
Expand Down Expand Up @@ -662,21 +662,21 @@ def on_post_save(self, v):
else:
sublime.set_timeout(lambda: self.reload_page(), utils.get_setting('reload_timeout'))

views.lookup_view(v).on_post_save()
views.wrap_view(v).on_post_save()

def on_modified(self, v):
views.lookup_view(v).on_modified()
views.wrap_view(v).on_modified()
#update_overlays()

def on_activated(self, v):
#todo can we move to on load?
views.lookup_view(v).on_activated()
views.wrap_view(v).on_activated()

def on_deactivated(self, v):
views.lookup_view(v).on_deactivated()
views.wrap_view(v).on_deactivated()

def on_query_context(self, v, key, operator, operand, match_all):
views.lookup_view(v).on_query_context(key, operator, operand, match_all)
views.wrap_view(v).on_query_context(key, operator, operand, match_all)

def update_stack(self, command):
""" Called on setScriptSource """
Expand All @@ -696,26 +696,11 @@ def on_reload(command):
# Console
####################################################################################


def clear_view(name):
v = views.find_existing_view(name)

if not v:
return

v.run_command('swi_clear_view_internal')
v.show(v.size())

if not window:
return

window.focus_group(0)

def clear_all_views():
clear_view('console')
clear_view('stack')
clear_view('scope')
clear_view('mapping')
views.clear_view('console')
views.clear_view('stack')
views.clear_view('scope')
views.clear_view('mapping')

def close_all_our_windows():
global window
Expand Down Expand Up @@ -781,12 +766,6 @@ def change_to_call_frame(callFrame):

open_script_and_focus_line(scriptId, line_number)

class SwiClearViewInternalCommand(sublime_plugin.TextCommand):
""" Called internally on the console view """
def run(self, edit, user_input=None):
v = views.lookup_view(self.view)
v.erase(edit, sublime.Region(0, self.view.size()))

def console_repeat_message(count):
v = views.find_or_create_view('console')

Expand Down Expand Up @@ -817,7 +796,7 @@ def console_add_evaluate(eval_object):
class SwiConsoleAddEvaluateInternalCommand(sublime_plugin.TextCommand):
""" Called internally on the console view """
def run(self, edit):
v = views.lookup_view(self.view)
v = views.wrap_view(self.view)
eval_object = eval_object_queue.pop(0)

v.insert(edit, v.size(), str(eval_object) + ' \n')
Expand All @@ -837,7 +816,7 @@ def console_add_message(message):
class SwiConsoleAddMessageInternalCommand(sublime_plugin.TextCommand):
""" Called internally on the console view """
def run(self, edit):
v = views.lookup_view(self.view)
v = views.wrap_view(self.view)
message = message_queue.pop(0)

if message.level == 'debug':
Expand Down Expand Up @@ -925,7 +904,7 @@ class SwiConsolePrintPropertiesInternalCommand(sublime_plugin.TextCommand):
""" Called internally on the console view """
def run(self, edit):

v = views.lookup_view(self.view)
v = views.wrap_view(self.view)
command = properties_queue.pop(0)

if 'name' in command.options:
Expand Down Expand Up @@ -988,7 +967,7 @@ def console_show_stack(callFrames):
class SwiConsoleShowStackInternalCommand(sublime_plugin.TextCommand):
""" Called internally on the stack view """
def run(self, edit):
v = views.lookup_view(self.view)
v = views.wrap_view(self.view)

callFrames = call_frames_queue.pop(0)

Expand Down
28 changes: 24 additions & 4 deletions views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class SwiDebugView(object):
""" The SWIDebugView wraps a normal view, adding some convenience methods.
See lookup_view.
See wrap_view.
All calls to a View should be made through an SWIDebugView,
adding more passthroughs if necessary. This makes the code flow explicit.
"""
Expand Down Expand Up @@ -202,9 +202,9 @@ def find_or_create_view(console_type, create = True):

v.set_read_only(False)

return lookup_view(v)
return wrap_view(v)

def lookup_view(v):
def wrap_view(v):
""" Convert a Sublime View into an SWIDebugView
"""
if isinstance(v, SwiDebugView):
Expand All @@ -221,6 +221,26 @@ def lookup_view(v):
return config.buffers[id]
return None

def clear_view(name):
v = find_existing_view(name)

if not v:
return

v.run_command('swi_clear_view_internal')
v.show(v.size())

window = sublime.active_window()
if not window:
return

window.focus_group(0)

class SwiClearViewInternalCommand(sublime_plugin.TextCommand):
""" Called internally on a specific view """
def run(self, edit, user_input=None):
v = wrap_view(self.view)
v.erase(edit, sublime.Region(0, self.view.size()))

class SwiMouseUpCommand(sublime_plugin.TextCommand):
""" We use this to discover a "button" has been clicked.
Expand All @@ -233,7 +253,7 @@ class SwiMouseUpCommand(sublime_plugin.TextCommand):
"""
def run(self, edit):
utils.assert_main_thread()
lookup_view(self.view).check_click()
wrap_view(self.view).check_click()

class SwiDoubleMouseUpCommand(sublime_plugin.TextCommand):
""" On a double click, we get one of each event, so
Expand Down

0 comments on commit 2c9f32b

Please sign in to comment.