<s> import sublime <EOL> import sublime_plugin <EOL> import os <EOL> import re <EOL> import threading <EOL> import subprocess <EOL> import datetime <EOL> import sys <EOL> import socket <EOL> import traceback <EOL> try : <EOL> import queue <EOL> coronaQueue = queue <EOL> except : <EOL> import Queue <EOL> coronaQueue = Queue <EOL> consoleOutputQ = None <EOL> variablesOutputQ = None <EOL> luaStackOutputQ = None <EOL> debuggerCmdQ = None <EOL> try : <EOL> from . import _corona_utils <EOL> except : <EOL> import _corona_utils <EOL> def is_string_instance ( obj ) : <EOL> try : <EOL> return isinstance ( obj , basestring ) <EOL> except NameError : <EOL> return isinstance ( obj , str ) <EOL> statusRegion = None <EOL> corona_sdk_version = None <EOL> corona_sdk_debug = _corona_utils . GetSetting ( "<STR_LIT>" , False ) <EOL> debugFP = None <EOL> def debug ( s ) : <EOL> global debugFP <EOL> global corona_sdk_debug <EOL> try : <EOL> if not debugFP and corona_sdk_debug : <EOL> if not os . path . isdir ( _corona_utils . PACKAGE_USER_DIR ) : <EOL> os . makedirs ( _corona_utils . PACKAGE_USER_DIR ) <EOL> debugFP = open ( os . path . normpath ( os . path . join ( _corona_utils . PACKAGE_USER_DIR , "<STR_LIT>" ) ) , "<STR_LIT:w>" , <NUM_LIT:1> ) <EOL> except : <EOL> pass <EOL> thread_id = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , str ( threading . current_thread ( ) ) ) <EOL> log_line = str ( datetime . datetime . now ( ) ) + "<STR_LIT>" + str ( thread_id ) + "<STR_LIT>" + str ( s ) <EOL> if debugFP : <EOL> debugFP . write ( log_line + "<STR_LIT:\n>" ) <EOL> _corona_utils . debug ( log_line ) <EOL> def debug_with_stacktrace ( s ) : <EOL> debug ( s ) <EOL> for line in traceback . format_list ( traceback . extract_stack ( ) ) : <EOL> debug ( "<STR_LIT:U+0020>" + line . strip ( ) ) <EOL> HOST = '<STR_LIT>' <EOL> PORT = <NUM_LIT> <EOL> coronaDbg = None <EOL> coronaDbgThread = None <EOL> coronaBreakpointsSettings = None <EOL> coronaBreakpoints = { } <EOL> class CoronaDebuggerThread ( threading . Thread ) : <EOL> def __init__ ( self , projectDir , completionCallback , threadID = <NUM_LIT:1> ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . threadID = threadID <EOL> self . projectDir = projectDir <EOL> self . completionCallback = completionCallback <EOL> self . debugger_running = False <EOL> self . conn = None <EOL> self . socket = None <EOL> self . recvFP = None <EOL> self . sendFP = None <EOL> def stop ( self ) : <EOL> self . debugger_running = False <EOL> def isRunning ( self ) : <EOL> return self . debugger_running <EOL> def setup ( self ) : <EOL> self . debugger_running = True <EOL> self . socket = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> debug ( '<STR_LIT>' ) <EOL> self . socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> debug ( '<STR_LIT>' ) <EOL> try : <EOL> self . socket . bind ( ( HOST , PORT ) ) <EOL> except socket . error as msg : <EOL> debug ( '<STR_LIT>' + str ( msg ) ) <EOL> sublime . error_message ( "<STR_LIT>" + str ( msg ) + "<STR_LIT>" ) <EOL> return False <EOL> else : <EOL> debug ( '<STR_LIT>' ) <EOL> self . socket . listen ( <NUM_LIT:1> ) <EOL> debug ( '<STR_LIT>' ) <EOL> return True <EOL> def initPUTComms ( self ) : <EOL> if _corona_utils . SUBLIME_VERSION < <NUM_LIT> : <EOL> self . recvFP = self . conn . makefile ( '<STR_LIT:r>' , <NUM_LIT:1> ) <EOL> self . sendFP = self . conn . makefile ( '<STR_LIT:w>' , <NUM_LIT:1> ) <EOL> else : <EOL> self . recvFP = self . conn . makefile ( mode = '<STR_LIT:rb>' , buffering = <NUM_LIT:0> , newline = '<STR_LIT:\n>' ) <EOL> self . sendFP = self . conn . makefile ( mode = '<STR_LIT:wb>' , buffering = <NUM_LIT:0> , newline = '<STR_LIT:\n>' ) <EOL> def closePUTComms ( self ) : <EOL> if self . sendFP is not None : <EOL> self . sendFP . close ( ) <EOL> self . sendFP = None <EOL> if self . recvFP is not None : <EOL> self . recvFP . close ( ) <EOL> self . recvFP = None <EOL> def writeToPUT ( self , s ) : <EOL> try : <EOL> return self . sendFP . write ( str . encode ( s , '<STR_LIT:utf-8>' ) ) <EOL> except TypeError : <EOL> return self . sendFP . write ( s ) <EOL> def readFromPUT ( self , n = None ) : <EOL> try : <EOL> if n is not None : <EOL> result = self . recvFP . read ( n ) <EOL> else : <EOL> result = self . recvFP . readline ( ) <EOL> except Exception as e : <EOL> debug ( "<STR_LIT>" + str ( e ) ) <EOL> else : <EOL> return result . decode ( '<STR_LIT:utf-8>' ) <EOL> def run ( self ) : <EOL> self . socket . settimeout ( <NUM_LIT:1> ) <EOL> debug ( "<STR_LIT>" ) <EOL> while self . socket is not None : <EOL> try : <EOL> self . conn , addr = self . socket . accept ( ) <EOL> self . conn . settimeout ( None ) <EOL> except socket . error as msg : <EOL> debug ( '<STR_LIT>' + str ( msg ) ) <EOL> else : <EOL> debug ( '<STR_LIT>' ) <EOL> break <EOL> if self . socket is None : <EOL> return <EOL> debug ( '<STR_LIT>' + addr [ <NUM_LIT:0> ] + '<STR_LIT::>' + str ( addr [ <NUM_LIT:1> ] ) ) <EOL> self . initPUTComms ( ) <EOL> self . writeToPUT ( "<STR_LIT>" ) <EOL> data = self . readFromPUT ( ) <EOL> debug ( '<STR_LIT>' + str ( data ) ) <EOL> bpResponse = self . readFromPUT ( ) <EOL> debug ( '<STR_LIT>' + bpResponse ) <EOL> bpMatches = re . search ( r'<STR_LIT>' , bpResponse . strip ( ) ) <EOL> if bpMatches is not None : <EOL> filename = bpMatches . group ( <NUM_LIT:1> ) <EOL> line = bpMatches . group ( <NUM_LIT:2> ) <EOL> debug ( "<STR_LIT>" . format ( filename , line ) ) <EOL> if not filename . endswith ( "<STR_LIT>" ) : <EOL> debugger_status ( "<STR_LIT>" ) <EOL> on_main_thread ( lambda : sublime . error_message ( "<STR_LIT>" ) ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" . format ( line , filename ) ) <EOL> on_main_thread ( lambda : self . showSublimeContext ( filename , int ( line ) ) ) <EOL> else : <EOL> errMatches = re . search ( r'<STR_LIT>' , bpResponse . strip ( ) ) <EOL> if errMatches is not None : <EOL> size = errMatches . group ( <NUM_LIT:1> ) <EOL> console_output ( "<STR_LIT>" ) <EOL> console_output ( self . readFromPUT ( size ) ) <EOL> else : <EOL> print ( "<STR_LIT>" , bpResponse ) <EOL> on_main_thread ( lambda : sublime . error_message ( "<STR_LIT>" + str ( bpResponse ) + "<STR_LIT>" ) ) <EOL> on_main_thread ( lambda : self . restore_breakpoints ( ) ) <EOL> self . doCommand ( '<STR_LIT>' ) <EOL> if corona_sdk_version and ( int ( corona_sdk_version ) >= <NUM_LIT> and int ( corona_sdk_version ) < <NUM_LIT> ) : <EOL> variables_output ( "<STR_LIT>" + corona_sdk_version + "<STR_LIT>" ) <EOL> else : <EOL> self . doCommand ( '<STR_LIT>' ) <EOL> while self . debugger_running : <EOL> cmd = debuggerCmdQ . get ( ) <EOL> self . performCommand ( cmd ) <EOL> debuggerCmdQ . task_done ( ) <EOL> on_main_thread ( lambda : self . completionCallback ( self . threadID ) ) <EOL> debug ( '<STR_LIT>' ) <EOL> def restore_breakpoints ( self ) : <EOL> global coronaBreakpointsSettings <EOL> if coronaBreakpointsSettings is None : <EOL> coronaBreakpointsSettings = sublime . load_settings ( _corona_utils . PACKAGE_NAME + "<STR_LIT>" ) <EOL> if coronaBreakpointsSettings is not None : <EOL> debug ( "<STR_LIT>" + str ( coronaBreakpointsSettings ) ) <EOL> if coronaBreakpointsSettings . get ( '<STR_LIT>' ) is not None : <EOL> breakpoints = coronaBreakpointsSettings . get ( '<STR_LIT>' ) <EOL> debug ( "<STR_LIT>" + str ( breakpoints ) ) <EOL> for filename in breakpoints : <EOL> for view in sublime . active_window ( ) . views ( ) : <EOL> debug ( "<STR_LIT>" + str ( view . file_name ( ) ) + "<STR_LIT>" + filename ) <EOL> if view . file_name ( ) == filename : <EOL> breakpoints [ filename ] = sorted ( set ( breakpoints [ filename ] ) ) <EOL> for line in breakpoints [ filename ] : <EOL> sublime . active_window ( ) . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : filename , "<STR_LIT>" : line , "<STR_LIT>" : False } ) <EOL> def getBreakpointParameters ( self , cmdLine ) : <EOL> cmd = "<STR_LIT>" <EOL> filename = "<STR_LIT>" <EOL> linenum = <NUM_LIT:0> <EOL> bpMatches = re . search ( r'<STR_LIT>' , cmdLine ) <EOL> if bpMatches is not None : <EOL> cmd = bpMatches . group ( <NUM_LIT:1> ) <EOL> filename = bpMatches . group ( <NUM_LIT:2> ) <EOL> linenum = bpMatches . group ( <NUM_LIT:3> ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" + cmdLine ) <EOL> return cmd , filename , linenum <EOL> def getParameters ( self , cmdLine ) : <EOL> cmd = "<STR_LIT>" <EOL> parameter = "<STR_LIT>" <EOL> cmdMatches = re . search ( r'<STR_LIT>' , cmdLine ) <EOL> if cmdMatches is not None : <EOL> cmd = cmdMatches . group ( <NUM_LIT:1> ) <EOL> parameter = cmdMatches . group ( <NUM_LIT:2> ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" + cmdLine ) <EOL> return cmd , parameter <EOL> def getAck ( self , cmd ) : <EOL> ack = None <EOL> try : <EOL> self . writeToPUT ( cmd . upper ( ) + "<STR_LIT:\n>" ) <EOL> ack = self . readFromPUT ( ) . strip ( ) <EOL> except Exception as e : <EOL> debug ( "<STR_LIT>" + str ( e ) ) <EOL> else : <EOL> debug ( "<STR_LIT>" + ack ) <EOL> if ack != "<STR_LIT>" : <EOL> debug ( "<STR_LIT>" . format ( cmd , ack ) ) <EOL> return ack <EOL> def doCommand ( self , cmd ) : <EOL> debuggerCmdQ . put ( cmd , <NUM_LIT:1> ) <EOL> def performCommand ( self , cmd ) : <EOL> try : <EOL> verb = cmd . partition ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:0> ] . lower ( ) <EOL> if verb in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> self . doContinue ( cmd ) <EOL> elif verb in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> self . doGetData ( cmd ) <EOL> elif verb in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> self . doSetBreakpoint ( cmd ) <EOL> elif verb in [ "<STR_LIT>" ] : <EOL> self . doDump ( cmd ) <EOL> elif verb in [ "<STR_LIT>" ] : <EOL> self . doExit ( cmd ) <EOL> elif verb in [ "<STR_LIT>" ] : <EOL> debugger_status ( "<STR_LIT>" + verb + "<STR_LIT>" ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" . format ( cmd ) ) <EOL> except Exception as e : <EOL> debug ( "<STR_LIT>" + str ( e ) ) <EOL> type_ , value_ , traceback_ = sys . exc_info ( ) <EOL> for line in traceback . format_tb ( traceback_ ) : <EOL> debug ( "<STR_LIT:U+0020>" + line . strip ( ) ) <EOL> def doDump ( self , cmd ) : <EOL> cmdtype , variable_name = self . getParameters ( cmd ) <EOL> debug ( "<STR_LIT>" + cmdtype + "<STR_LIT:U+0020>" + variable_name ) <EOL> if variable_name : <EOL> self . writeToPUT ( "<STR_LIT>" + variable_name + "<STR_LIT>" ) <EOL> dmpResponse = self . readFromPUT ( ) . strip ( ) <EOL> debug ( "<STR_LIT>" + dmpResponse ) <EOL> dataMatches = re . search ( r'<STR_LIT>' , dmpResponse ) <EOL> if dataMatches is not None : <EOL> status = dataMatches . group ( <NUM_LIT:1> ) <EOL> length = int ( dataMatches . group ( <NUM_LIT:2> ) ) <EOL> if status == "<STR_LIT>" : <EOL> if length == <NUM_LIT:0> : <EOL> debugger_status ( "<STR_LIT>" + cmd ) <EOL> else : <EOL> dataStr = "<STR_LIT>" <EOL> while len ( dataStr ) < length : <EOL> dataStr += self . readFromPUT ( int ( length - len ( dataStr ) ) ) <EOL> dataStr = dataStr <EOL> debug ( '<STR_LIT>' + dataStr ) <EOL> sublime . message_dialog ( dataStr ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" + dmpResponse ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" ) <EOL> def doSetBreakpoint ( self , cmd ) : <EOL> global coronaBreakpoints <EOL> cmdtype , filename , linenum = self . getBreakpointParameters ( cmd ) <EOL> if filename and linenum : <EOL> self . writeToPUT ( cmdtype . upper ( ) + "<STR_LIT:U+0020>" + filename + "<STR_LIT:U+0020>" + linenum + "<STR_LIT:\n>" ) <EOL> bpResponse = self . readFromPUT ( ) . strip ( ) <EOL> debug ( "<STR_LIT>" + bpResponse ) <EOL> if bpResponse == "<STR_LIT>" : <EOL> action = "<STR_LIT>" if cmdtype . upper ( ) == "<STR_LIT>" else "<STR_LIT>" <EOL> debugger_status ( "<STR_LIT>" . format ( filename , linenum , action ) ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" + bpResponse ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" ) <EOL> def doExit ( self , cmd ) : <EOL> debug ( "<STR_LIT>" ) <EOL> try : <EOL> if self . conn is not None : <EOL> self . conn . close ( ) <EOL> self . conn = None <EOL> if self . socket is not None : <EOL> self . socket . close ( ) <EOL> self . socket = None <EOL> except Exception as e : <EOL> debug ( "<STR_LIT>" + str ( e ) ) <EOL> exc_type , exc_obj , exc_tb = sys . exc_info ( ) <EOL> fname = os . path . split ( exc_tb . tb_frame . f_code . co_filename ) [ <NUM_LIT:1> ] <EOL> print ( exc_type , fname , exc_tb . tb_lineno ) <EOL> def doGetData ( self , cmd ) : <EOL> self . writeToPUT ( cmd . upper ( ) + "<STR_LIT:\n>" ) <EOL> dataResponse = self . readFromPUT ( ) . strip ( ) <EOL> debug ( "<STR_LIT>" + dataResponse ) <EOL> dataMatches = re . search ( r'<STR_LIT>' , dataResponse ) <EOL> if dataMatches is not None : <EOL> status = dataMatches . group ( <NUM_LIT:1> ) <EOL> length = int ( dataMatches . group ( <NUM_LIT:2> ) ) <EOL> if status == "<STR_LIT>" : <EOL> if length == <NUM_LIT:0> : <EOL> debugger_status ( "<STR_LIT>" + cmd ) <EOL> else : <EOL> dataStr = "<STR_LIT>" <EOL> while len ( dataStr ) < length : <EOL> dataStr += self . readFromPUT ( int ( length - len ( dataStr ) ) ) <EOL> if cmd == '<STR_LIT>' : <EOL> if dataStr . find ( '<STR_LIT>' ) != - <NUM_LIT:1> or dataStr . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> dataStr = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , dataStr ) <EOL> dataStr = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , dataStr ) <EOL> dataStr = re . sub ( "<STR_LIT>" + re . escape ( '<STR_LIT:@>' + self . projectDir + os . path . sep ) , '<STR_LIT>' , dataStr ) <EOL> stack_output ( cmd . title ( ) + "<STR_LIT>" + dataStr ) <EOL> else : <EOL> dataStr = re . sub ( "<STR_LIT>" + re . escape ( '<STR_LIT:@>' + self . projectDir + os . path . sep ) , '<STR_LIT>' , dataStr ) <EOL> variables_output ( cmd . title ( ) + "<STR_LIT>" + dataStr ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" + cmd + "<STR_LIT>" + dataResponse + "<STR_LIT:)>" ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" + cmd + "<STR_LIT>" + dataResponse + "<STR_LIT:)>" ) <EOL> def activateViewWithFile ( self , filename , line ) : <EOL> debug ( "<STR_LIT>" + str ( filename ) + "<STR_LIT::>" + str ( line ) ) <EOL> window = sublime . active_window ( ) <EOL> for view in window . views ( ) : <EOL> if view . name ( ) == filename : <EOL> window . focus_view ( view ) <EOL> break <EOL> if window . active_view ( ) . file_name ( ) != filename : <EOL> filename = filename + "<STR_LIT::>" + str ( line ) <EOL> view = window . open_file ( filename , sublime . ENCODED_POSITION ) <EOL> window . focus_view ( view ) <EOL> def showSublimeContext ( self , filename , line ) : <EOL> debug ( "<STR_LIT>" + str ( filename ) + "<STR_LIT>" + str ( line ) ) <EOL> console_output ( "<STR_LIT>" + str ( filename . replace ( self . projectDir + "<STR_LIT:/>" , "<STR_LIT>" ) ) + "<STR_LIT::>" + str ( line ) + "<STR_LIT>" ) <EOL> window = sublime . active_window ( ) <EOL> if window : <EOL> window . focus_group ( <NUM_LIT:0> ) <EOL> view = window . active_view ( ) <EOL> if view is not None and view . size ( ) >= <NUM_LIT:0> : <EOL> filename = os . path . join ( self . projectDir , filename ) <EOL> if view . file_name ( ) != filename : <EOL> self . activateViewWithFile ( filename , line ) <EOL> window . run_command ( "<STR_LIT>" , { "<STR_LIT>" : line } ) <EOL> view = window . active_view ( ) <EOL> mark = [ view . line ( view . text_point ( line - <NUM_LIT:1> , <NUM_LIT:0> ) ) ] <EOL> view . erase_regions ( "<STR_LIT>" ) <EOL> view . add_regions ( "<STR_LIT>" , mark , "<STR_LIT>" , "<STR_LIT>" , sublime . DRAW_OUTLINED ) <EOL> else : <EOL> debug ( "<STR_LIT>" ) <EOL> def doContinue ( self , cmd ) : <EOL> if cmd == "<STR_LIT>" : <EOL> stack_output ( "<STR_LIT>" ) <EOL> variables_output ( "<STR_LIT>" ) <EOL> debugger_status ( "<STR_LIT>" ) <EOL> on_main_thread ( lambda : sublime . active_window ( ) . active_view ( ) . erase_regions ( "<STR_LIT>" ) ) <EOL> on_main_thread ( lambda : console_output ( "<STR_LIT>" ) ) <EOL> self . getAck ( cmd ) <EOL> response = self . readFromPUT ( ) <EOL> if response is None or response == "<STR_LIT>" : <EOL> debugger_status ( "<STR_LIT>" ) <EOL> self . debugger_running = False <EOL> return <EOL> statusMatches = re . search ( r'<STR_LIT>' , response . strip ( ) ) <EOL> status = statusMatches . group ( <NUM_LIT:0> ) <EOL> debug ( "<STR_LIT>" + status ) <EOL> if status == "<STR_LIT>" : <EOL> bpMatches = re . search ( r'<STR_LIT>' , response ) <EOL> if bpMatches is not None : <EOL> label = bpMatches . group ( <NUM_LIT:1> ) <EOL> filename = bpMatches . group ( <NUM_LIT:2> ) <EOL> line = bpMatches . group ( <NUM_LIT:3> ) <EOL> debug ( "<STR_LIT>" . format ( label , filename , line , response ) ) <EOL> if label == "<STR_LIT>" : <EOL> label = "<STR_LIT>" <EOL> if filename and line : <EOL> if filename == "<STR_LIT>" or filename == "<STR_LIT>" or filename == "<STR_LIT>" : <EOL> debugger_status ( label + "<STR_LIT>" ) <EOL> else : <EOL> debugger_status ( label + "<STR_LIT>" + line + "<STR_LIT>" + filename ) <EOL> on_main_thread ( lambda : self . showSublimeContext ( filename , int ( line ) ) ) <EOL> else : <EOL> debugger_status ( label + "<STR_LIT>" + response ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" + response ) <EOL> elif status == "<STR_LIT>" : <EOL> bpwMatches = re . search ( r'<STR_LIT>' , response ) <EOL> if bpwMatches is not None : <EOL> file = bpwMatches . group ( <NUM_LIT:1> ) <EOL> line = bpwMatches . group ( <NUM_LIT:2> ) <EOL> watchIndex = bpwMatches . group ( <NUM_LIT:3> ) <EOL> if file and line and watchIndex : <EOL> print ( _corona_utils . PACKAGE_NAME + "<STR_LIT>" ) <EOL> else : <EOL> debugger_status ( "<STR_LIT>" + response ) <EOL> elif status == "<STR_LIT>" : <EOL> errMatches = re . search ( r'<STR_LIT>' , response ) <EOL> if errMatches : <EOL> size = errMatches . group ( <NUM_LIT:1> ) <EOL> if size : <EOL> console_output ( "<STR_LIT>" ) <EOL> console_output ( self . readFromPUT ( int ( size ) ) ) <EOL> def doRun ( self ) : <EOL> self . doContinue ( "<STR_LIT>" ) <EOL> def doStep ( self ) : <EOL> self . doContinue ( "<STR_LIT>" ) <EOL> class CoronaDebuggerListener ( sublime_plugin . EventListener ) : <EOL> def on_post_save ( self , view ) : <EOL> debug ( "<STR_LIT>" + view . file_name ( ) ) <EOL> if ( coronaDbg is not None and coronaDbg . isRunning ( ) ) and view . file_name ( ) . endswith ( "<STR_LIT>" ) : <EOL> if sublime . ok_cancel_dialog ( view . file_name ( ) + "<STR_LIT>" , "<STR_LIT>" ) : <EOL> sublime . set_timeout ( lambda : sublime . active_window ( ) . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) , <NUM_LIT:0> ) <EOL> class CoronaDebuggerCommand ( sublime_plugin . WindowCommand ) : <EOL> view = None <EOL> def is_enabled ( self ) : <EOL> view = self . window . active_view ( ) <EOL> if view is not None : <EOL> s = view . sel ( ) [ <NUM_LIT:0> ] <EOL> return view . match_selector ( s . a , "<STR_LIT>" ) <EOL> else : <EOL> return False <EOL> def run ( self , cmd = None , arg_filename = None , arg_lineno = None , arg_toggle = True ) : <EOL> debug ( "<STR_LIT>" + cmd ) <EOL> global coronaDbg <EOL> global corona_sdk_version <EOL> global corona_sdk_debug <EOL> self . view = self . window . active_view ( ) <EOL> if self . view is None : <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> return <EOL> if ( coronaDbg is None or not coronaDbg . isRunning ( ) ) and cmd in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> cmd = "<STR_LIT:start>" <EOL> if cmd == "<STR_LIT:start>" : <EOL> if corona_sdk_debug : <EOL> self . window . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . window . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> if coronaDbg is not None : <EOL> debug ( "<STR_LIT>" ) <EOL> coronaDbg . join ( ) <EOL> coronaDbg = None <EOL> self . saved_layout = self . window . get_layout ( ) <EOL> filename = self . window . active_view ( ) . file_name ( ) <EOL> if filename is None or not filename . endswith ( "<STR_LIT>" ) : <EOL> filename = None <EOL> for view in self . window . views ( ) : <EOL> if view . file_name ( ) and view . file_name ( ) . endswith ( "<STR_LIT>" ) : <EOL> if filename is None or not filename . endswith ( "<STR_LIT>" ) : <EOL> filename = view . file_name ( ) <EOL> if filename is None : <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> return <EOL> mainlua = _corona_utils . ResolveMainLua ( filename ) <EOL> if mainlua is None : <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> return <EOL> self . window . open_file ( mainlua ) <EOL> projectDir = os . path . dirname ( mainlua ) <EOL> if not projectDir : <EOL> sublime . error_message ( "<STR_LIT>" + self . view . file_name ( ) + "<STR_LIT>" ) <EOL> return <EOL> dbg_path , dbg_flags , dbg_version = _corona_utils . GetSimulatorCmd ( mainlua , True ) <EOL> dbg_cmd = [ dbg_path ] <EOL> dbg_cmd += dbg_flags <EOL> dbg_cmd . append ( mainlua ) <EOL> debug ( "<STR_LIT>" + str ( dbg_cmd ) ) <EOL> debug ( "<STR_LIT>" + str ( dbg_version ) ) <EOL> if dbg_version : <EOL> corona_sdk_version = dbg_version . rpartition ( "<STR_LIT:.>" ) [ <NUM_LIT:2> ] <EOL> debug ( "<STR_LIT>" + str ( corona_sdk_version ) ) <EOL> global consoleOutputQ , variablesOutputQ , luaStackOutputQ , debuggerCmdQ <EOL> consoleOutputQ = coronaQueue . Queue ( ) <EOL> variablesOutputQ = coronaQueue . Queue ( ) <EOL> luaStackOutputQ = coronaQueue . Queue ( ) <EOL> debuggerCmdQ = coronaQueue . Queue ( ) <EOL> coronaDbg = CoronaDebuggerThread ( projectDir , self . debuggerFinished ) <EOL> if coronaDbg . setup ( ) : <EOL> if self . window . num_groups ( ) == <NUM_LIT:1> : <EOL> self . initializeWindowPanes ( ) <EOL> else : <EOL> variables_output ( '<STR_LIT:U+0020>' ) <EOL> stack_output ( '<STR_LIT:U+0020>' ) <EOL> self . window . focus_group ( <NUM_LIT:0> ) <EOL> RunSubprocess ( dbg_cmd , self . window ) <EOL> coronaDbg . start ( ) <EOL> elif cmd == "<STR_LIT>" : <EOL> if coronaDbg is not None : <EOL> self . window . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> sublime . set_timeout ( lambda : self . window . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT:start>" } ) , <NUM_LIT:0> ) <EOL> elif cmd == "<STR_LIT>" : <EOL> debugger_status ( "<STR_LIT>" ) <EOL> StopSubprocess ( ) <EOL> if coronaDbg is None : <EOL> self . closeWindowPanes ( ) <EOL> else : <EOL> coronaDbg . doCommand ( cmd ) <EOL> coronaDbg . stop ( ) <EOL> coronaDbg . join ( ) <EOL> coronaDbg = None <EOL> elif cmd in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> coronaDbg . doCommand ( cmd ) <EOL> coronaDbg . doCommand ( '<STR_LIT>' ) <EOL> if not corona_sdk_version or ( int ( corona_sdk_version ) < <NUM_LIT> or int ( corona_sdk_version ) > <NUM_LIT> ) : <EOL> coronaDbg . doCommand ( '<STR_LIT>' ) <EOL> elif cmd == "<STR_LIT>" : <EOL> self . dumpVariable ( ) <EOL> elif cmd == "<STR_LIT>" : <EOL> if arg_filename is None : <EOL> filename = self . view . file_name ( ) <EOL> ( lineno , col ) = self . view . rowcol ( self . view . sel ( ) [ <NUM_LIT:0> ] . begin ( ) ) <EOL> lineno += <NUM_LIT:1> <EOL> else : <EOL> filename = arg_filename <EOL> lineno = int ( arg_lineno ) <EOL> if self . toggle_breakpoint ( filename , lineno , arg_toggle ) : <EOL> cmd = "<STR_LIT>" <EOL> else : <EOL> cmd = "<STR_LIT>" <EOL> cmd += "<STR_LIT:U+0020>" + '<STR_LIT:">' + filename + '<STR_LIT:">' <EOL> cmd += "<STR_LIT:U+0020>" + str ( lineno ) <EOL> debug ( "<STR_LIT>" + cmd ) <EOL> if coronaDbg is not None : <EOL> coronaDbg . doCommand ( cmd ) <EOL> else : <EOL> print ( "<STR_LIT>" + cmd ) <EOL> def debuggerFinished ( self , threadId ) : <EOL> debug ( "<STR_LIT>" + str ( threadId ) ) <EOL> self . closeWindowPanes ( ) <EOL> def dumpVariable ( self ) : <EOL> selection = self . view . sel ( ) [ <NUM_LIT:0> ] <EOL> if selection : <EOL> selected_word = self . view . substr ( self . view . word ( selection ) ) <EOL> if selected_word and selected_word != "<STR_LIT>" : <EOL> self . doDumpVariable ( selected_word ) <EOL> else : <EOL> self . window . show_input_panel ( "<STR_LIT>" , "<STR_LIT>" , self . doDumpVariable , None , None ) <EOL> def doDumpVariable ( self , variable_name ) : <EOL> if coronaDbg is not None : <EOL> coronaDbg . doCommand ( "<STR_LIT>" + variable_name ) <EOL> else : <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> def toggle_breakpoint ( self , filename , lineno , toggle = True ) : <EOL> global coronaBreakpointsSettings <EOL> global coronaBreakpoints <EOL> result = True <EOL> if coronaBreakpointsSettings is None : <EOL> coronaBreakpointsSettings = sublime . load_settings ( _corona_utils . PACKAGE_NAME + "<STR_LIT>" ) <EOL> bpId = self . new_breakpoint_id ( filename , lineno ) <EOL> debug ( "<STR_LIT>" + bpId ) <EOL> if filename not in coronaBreakpoints : <EOL> coronaBreakpoints [ filename ] = [ ] <EOL> if lineno in coronaBreakpoints [ filename ] and toggle : <EOL> debug ( "<STR_LIT>" ) <EOL> coronaBreakpoints [ filename ] . remove ( lineno ) <EOL> view = self . view_for_file ( filename ) <EOL> if view is not None : <EOL> view . erase_regions ( bpId ) <EOL> result = False <EOL> else : <EOL> debug ( "<STR_LIT>" + filename + "<STR_LIT>" + str ( lineno ) ) <EOL> if lineno not in coronaBreakpoints [ filename ] : <EOL> coronaBreakpoints [ filename ] . append ( int ( lineno ) ) <EOL> view = self . view_for_file ( filename ) <EOL> if view is not None : <EOL> mark = [ view . line ( view . text_point ( lineno - <NUM_LIT:1> , <NUM_LIT:0> ) ) ] <EOL> if _corona_utils . SUBLIME_VERSION < <NUM_LIT> : <EOL> view . add_regions ( bpId , mark , "<STR_LIT>" , "<STR_LIT>" + _corona_utils . PACKAGE_NAME + "<STR_LIT>" , sublime . HIDDEN ) <EOL> else : <EOL> view . add_regions ( bpId , mark , "<STR_LIT>" , "<STR_LIT>" + _corona_utils . PACKAGE_NAME + "<STR_LIT>" , sublime . HIDDEN ) <EOL> result = True <EOL> debug ( "<STR_LIT>" + str ( coronaBreakpoints ) ) <EOL> coronaBreakpointsSettings . set ( "<STR_LIT>" , coronaBreakpoints ) <EOL> sublime . save_settings ( _corona_utils . PACKAGE_NAME + "<STR_LIT>" ) <EOL> return result <EOL> def view_for_file ( self , filename ) : <EOL> for view in sublime . active_window ( ) . views ( ) : <EOL> if view . file_name ( ) == filename : <EOL> return view <EOL> return None <EOL> def new_breakpoint_id ( self , filename , lineno ) : <EOL> return filename + str ( lineno ) <EOL> def initializeWindowPanes ( self ) : <EOL> self . window . set_layout ( { "<STR_LIT>" : [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:1> ] , "<STR_LIT>" : [ <NUM_LIT:0> , <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT:1> ] , "<STR_LIT>" : [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> ] , [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:3> ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> ] ] } ) <EOL> self . window_panes = [ { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : '<STR_LIT:code>' , '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:title>' : '<STR_LIT>' } ] <EOL> for w in self . window_panes : <EOL> views = self . window . views_in_group ( w [ '<STR_LIT>' ] ) <EOL> if len ( views ) is <NUM_LIT:0> : <EOL> view = self . window . new_file ( ) <EOL> view . set_name ( w [ '<STR_LIT:title>' ] ) <EOL> view . settings ( ) . set ( '<STR_LIT>' , True ) <EOL> view . settings ( ) . set ( '<STR_LIT>' , True ) <EOL> if view . name ( ) != '<STR_LIT>' : <EOL> view . set_syntax_file ( '<STR_LIT>' + _corona_utils . PACKAGE_NAME + '<STR_LIT>' ) <EOL> view . set_read_only ( True ) <EOL> view . set_scratch ( True ) <EOL> view . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . window . set_view_index ( view , w [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> def closeWindowPanes ( self ) : <EOL> closed_panes = False <EOL> if self . window . num_groups ( ) > <NUM_LIT:1> : <EOL> for view in self . window . views ( ) : <EOL> if not view . settings ( ) . get ( '<STR_LIT>' ) : <EOL> continue <EOL> closed_panes = True <EOL> group , index = self . window . get_view_index ( view ) <EOL> if group > <NUM_LIT:0> : <EOL> debug ( "<STR_LIT>" + view . name ( ) ) <EOL> self . window . focus_view ( view ) <EOL> self . window . run_command ( "<STR_LIT>" ) <EOL> if closed_panes : <EOL> self . window . run_command ( "<STR_LIT>" , { "<STR_LIT>" : [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ] , "<STR_LIT>" : [ <NUM_LIT:0.0> , <NUM_LIT:1.0> ] , "<STR_LIT>" : [ <NUM_LIT:0.0> , <NUM_LIT:1.0> ] } ) <EOL> self . view . erase_regions ( "<STR_LIT>" ) <EOL> def debugger_status ( msg ) : <EOL> debug ( "<STR_LIT>" + msg ) <EOL> sublime . set_timeout ( lambda : sublime . status_message ( msg ) , <NUM_LIT:0> ) <EOL> def on_main_thread ( callee ) : <EOL> sublime . set_timeout ( callee , <NUM_LIT:0> ) <EOL> def console_output ( text ) : <EOL> if consoleOutputQ is not None : <EOL> if text [ - <NUM_LIT:1> ] != "<STR_LIT:\n>" : <EOL> text += "<STR_LIT:\n>" <EOL> text = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , text . replace ( "<STR_LIT:\r>" , "<STR_LIT>" ) , <NUM_LIT:1> ) <EOL> consoleOutputQ . put ( text , <NUM_LIT:1> ) <EOL> sublime . set_timeout ( lambda : outputToPane ( '<STR_LIT>' , None , False ) , <NUM_LIT:0> ) <EOL> def variables_output ( text ) : <EOL> if variablesOutputQ is not None : <EOL> if text [ - <NUM_LIT:1> ] != "<STR_LIT:\n>" : <EOL> text += "<STR_LIT:\n>" <EOL> variablesOutputQ . put ( text , <NUM_LIT:1> ) <EOL> sublime . set_timeout ( lambda : outputToPane ( '<STR_LIT>' , None , True ) , <NUM_LIT:0> ) <EOL> def stack_output ( text ) : <EOL> if luaStackOutputQ is not None : <EOL> if text [ - <NUM_LIT:1> ] != "<STR_LIT:\n>" : <EOL> text += "<STR_LIT:\n>" <EOL> luaStackOutputQ . put ( text , <NUM_LIT:1> ) <EOL> sublime . set_timeout ( lambda : outputToPane ( '<STR_LIT>' , None , True ) , <NUM_LIT:0> ) <EOL> def outputToPane ( name , text , erase = True ) : <EOL> debug ( "<STR_LIT>" + name + "<STR_LIT>" + str ( text ) ) <EOL> global statusRegion <EOL> queueing = False <EOL> if text is None : <EOL> if name == "<STR_LIT>" : <EOL> subProcOutputQ = variablesOutputQ <EOL> elif name == "<STR_LIT>" : <EOL> subProcOutputQ = luaStackOutputQ <EOL> else : <EOL> subProcOutputQ = consoleOutputQ <EOL> text = subProcOutputQ . get ( ) <EOL> queueing = True <EOL> debug ( "<STR_LIT>" + name + "<STR_LIT>" + text ) <EOL> window = sublime . active_window ( ) <EOL> for view in window . views ( ) : <EOL> if view . name ( ) == name and view . substr ( sublime . Region ( <NUM_LIT:0> , view . size ( ) ) ) != text : <EOL> view . set_read_only ( False ) <EOL> if statusRegion and "<STR_LIT>" in text : <EOL> view . sel ( ) . clear ( ) <EOL> view . sel ( ) . add ( view . full_line ( statusRegion ) ) <EOL> view . run_command ( "<STR_LIT>" ) <EOL> statusRegion = None <EOL> if _corona_utils . SUBLIME_VERSION < <NUM_LIT> : <EOL> edit = view . begin_edit ( ) <EOL> if erase : <EOL> view . erase ( edit , sublime . Region ( <NUM_LIT:0> , view . size ( ) ) ) <EOL> view . insert ( edit , <NUM_LIT:0> , text ) <EOL> else : <EOL> view . insert ( edit , view . size ( ) , text ) <EOL> view . end_edit ( edit ) <EOL> else : <EOL> if erase : <EOL> view . run_command ( "<STR_LIT>" ) <EOL> view . run_command ( "<STR_LIT>" ) <EOL> view . run_command ( '<STR_LIT>' , { '<STR_LIT>' : text } ) <EOL> view . set_read_only ( True ) <EOL> view . show ( view . size ( ) , True ) <EOL> if "<STR_LIT>" in text : <EOL> line = view . rowcol ( view . size ( ) ) [ <NUM_LIT:0> ] <EOL> pt = view . text_point ( line - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> statusRegion = view . line ( sublime . Region ( pt ) ) <EOL> view . sel ( ) . clear ( ) <EOL> view . sel ( ) . add ( view . line ( statusRegion ) ) <EOL> mark = [ sublime . Region ( view . size ( ) - <NUM_LIT:1> , view . size ( ) ) ] <EOL> if _corona_utils . SUBLIME_VERSION < <NUM_LIT> : <EOL> view . add_regions ( '<STR_LIT>' , mark , "<STR_LIT>" , "<STR_LIT>" + _corona_utils . PACKAGE_NAME + "<STR_LIT>" , sublime . HIDDEN ) <EOL> else : <EOL> view . add_regions ( '<STR_LIT>' , mark , "<STR_LIT>" , "<STR_LIT>" + _corona_utils . PACKAGE_NAME + "<STR_LIT>" , sublime . HIDDEN ) <EOL> if queueing : <EOL> subProcOutputQ . task_done ( ) <EOL> class CoronaSubprocessThread ( threading . Thread ) : <EOL> def __init__ ( self , cmd , completionCallback = None , window = None , threadID = <NUM_LIT:1> ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . threadID = threadID <EOL> self . cmd = cmd <EOL> self . completionCallback = completionCallback <EOL> self . window = window <EOL> self . proc = None <EOL> def terminate ( self ) : <EOL> if self . proc . poll ( ) is None : <EOL> self . proc . terminate ( ) <EOL> def run ( self ) : <EOL> debug ( "<STR_LIT>" + str ( self . cmd ) ) <EOL> if sublime . platform ( ) == "<STR_LIT>" : <EOL> closeFDs = False <EOL> else : <EOL> closeFDs = True <EOL> self . proc = subprocess . Popen ( self . cmd , bufsize = <NUM_LIT:0> , close_fds = closeFDs , stdout = subprocess . PIPE , stderr = subprocess . STDOUT ) <EOL> while self . proc . poll ( ) is None : <EOL> try : <EOL> data = self . proc . stdout . readline ( ) . decode ( '<STR_LIT>' ) <EOL> console_output ( data ) <EOL> except IndexError as e : <EOL> break <EOL> except Exception as e : <EOL> console_output ( "<STR_LIT>" + str ( e ) ) <EOL> on_main_thread ( lambda : self . completionCallback ( self . threadID , self . window ) ) <EOL> debug ( "<STR_LIT>" + str ( self . proc . poll ( ) ) + "<STR_LIT:)>" ) <EOL> def CompleteSubprocess ( threadID , window ) : <EOL> debug ( "<STR_LIT>" + str ( threadID ) + "<STR_LIT:)>" ) <EOL> def RunSubprocess ( cmd , window ) : <EOL> global coronaDbgThread <EOL> coronaDbgThread = CoronaSubprocessThread ( cmd , CompleteSubprocess , window ) <EOL> coronaDbgThread . start ( ) <EOL> def StopSubprocess ( ) : <EOL> global coronaDbgThread <EOL> debug ( "<STR_LIT>" + str ( coronaDbgThread ) ) <EOL> if coronaDbgThread is not None and coronaDbgThread . is_alive ( ) : <EOL> coronaDbgThread . terminate ( ) <EOL> coronaDbgThread . join ( ) </s>
