<s> import sublime , sublime_plugin , os , re , glob , itertools , json , os . path , sys , time ; <EOL> from sublime import Region ; <EOL> try : <EOL> from . libs . ClassFunctions import ClassFunctions <EOL> from . libs . JsDuck import JsDuck <EOL> from . libs . Settings import Settings <EOL> except ValueError : <EOL> from libs . ClassFunctions import ClassFunctions <EOL> from libs . JsDuck import JsDuck <EOL> from libs . Settings import Settings <EOL> def isJavascriptFile ( view ) : <EOL> return view . score_selector ( <NUM_LIT:0> , Settings . get ( '<STR_LIT>' , '<STR_LIT>' ) ) > <NUM_LIT:0> <EOL> class ClassBase ( sublime_plugin . TextCommand ) : <EOL> def active_file_path ( self ) : <EOL> file_path = self . view . file_name ( ) <EOL> if file_path and len ( file_path ) > <NUM_LIT:0> : <EOL> return file_path <EOL> def window ( self ) : <EOL> return self . view . window ( ) ; <EOL> class ClassFuncBase ( ClassBase ) : <EOL> def run ( self , edit ) : <EOL> if not isJavascriptFile ( self . view ) : <EOL> sublime . status_message ( '<STR_LIT>' ) ; <EOL> return ; <EOL> active_file_path = self . active_file_path ( ) <EOL> self . _funcs = ClassFunctions ( sublime , active_file_path ) <EOL> if not self . _funcs . isValid ( ) : <EOL> sublime . status_message ( '<STR_LIT>' ) ; <EOL> return ; <EOL> JsDuck . checkJsDuck ( sublime , self . _funcs . appRoot ( ) ) ; <EOL> if JsDuck . isActive ( ) : <EOL> sublime . status_message ( '<STR_LIT>' ) ; <EOL> return ; <EOL> if active_file_path : <EOL> self . load ( ) ; <EOL> self . window ( ) . show_quick_panel ( self . _funcs . descriptions ( ) , self . open_file ) <EOL> else : <EOL> sublime . status_message ( "<STR_LIT>" ) <EOL> def open_file ( self , index ) : <EOL> if index >= <NUM_LIT:0> and len ( self . _funcs . descriptions ( ) ) > index : <EOL> curEntry = self . _funcs . descriptions ( ) [ index ] <EOL> self . open ( curEntry ) ; <EOL> def load ( self ) : <EOL> pass <EOL> def open ( self , curEntry ) : <EOL> pass <EOL> class ClassFunctionsCommand ( ClassFuncBase ) : <EOL> def load ( self ) : <EOL> self . _funcs . readJsDuckFunctions ( ) ; <EOL> def open ( self , curEntry ) : <EOL> curFunction = curEntry [ <NUM_LIT:0> ] ; <EOL> curClass = curEntry [ <NUM_LIT:1> ] ; <EOL> path = self . _funcs . files ( ) [ curClass + '<STR_LIT::>' + curFunction ] ; <EOL> sublime . set_timeout ( lambda : self . window ( ) . open_file ( path , sublime . ENCODED_POSITION ) ) <EOL> def _getFunctionLine ( self , filepath , name ) : <EOL> lineNumber = <NUM_LIT:0> ; <EOL> f = open ( filepath , '<STR_LIT:r>' , encoding = "<STR_LIT:utf-8>" ) ; <EOL> functionRegex = re . compile ( '<STR_LIT>' + name + '<STR_LIT>' ) ; <EOL> result = [ <NUM_LIT:0> , <NUM_LIT:0> ] ; <EOL> while ( True ) : <EOL> lineNumber += <NUM_LIT:1> ; <EOL> line = f . readline ( ) ; <EOL> if line : <EOL> match = functionRegex . search ( line ) <EOL> if match : <EOL> result = [ lineNumber , match . start ( ) + <NUM_LIT:1> ] ; <EOL> break ; <EOL> else : <EOL> break ; <EOL> f . close ( ) ; <EOL> return result ; <EOL> class ClassPropertiesCommand ( ClassFuncBase ) : <EOL> def load ( self ) : <EOL> self . _funcs . readJsDuckProperties ( ) ; <EOL> def open ( self , curEntry ) : <EOL> curProperty = curEntry [ <NUM_LIT:0> ] ; <EOL> curClass = curEntry [ <NUM_LIT:1> ] ; <EOL> path = self . _funcs . files ( ) [ curClass + '<STR_LIT::>' + curProperty ] ; <EOL> view = sublime . set_timeout ( lambda : self . window ( ) . open_file ( path , sublime . ENCODED_POSITION ) ) <EOL> def _getPropertyLine ( self , filepath , name ) : <EOL> lineNumber = <NUM_LIT:0> ; <EOL> f = open ( filepath , '<STR_LIT:r>' , encoding = "<STR_LIT:utf-8>" ) ; <EOL> propertyRegex = re . compile ( '<STR_LIT>' + name + '<STR_LIT>' ) ; <EOL> result = [ <NUM_LIT:0> , <NUM_LIT:0> ] ; <EOL> while ( True ) : <EOL> lineNumber += <NUM_LIT:1> ; <EOL> line = f . readline ( ) ; <EOL> if line : <EOL> match = propertyRegex . search ( line ) <EOL> if match : <EOL> result = [ lineNumber , match . start ( ) + <NUM_LIT:1> ] ; <EOL> break ; <EOL> else : <EOL> break ; <EOL> f . close ( ) ; <EOL> return result ; <EOL> class ClassPropertiesInsertCommand ( ClassPropertiesCommand ) : <EOL> def open ( self , curEntry ) : <EOL> curProperty = curEntry [ <NUM_LIT:0> ] ; <EOL> curClass = curEntry [ <NUM_LIT:1> ] ; <EOL> propertyType = self . _funcs . types ( ) [ curClass + '<STR_LIT::>' + curProperty ] ; <EOL> template = Settings . get ( '<STR_LIT>' + propertyType , Settings . get ( '<STR_LIT>' ) ) ; <EOL> template = '<STR_LIT:\n>' . join ( template ) ; <EOL> template = template . replace ( '<STR_LIT>' , curProperty ) ; <EOL> self . view . run_command ( '<STR_LIT>' , { "<STR_LIT>" : template } ) ; <EOL> class ClassFunctionsInsertCommand ( ClassFunctionsCommand ) : <EOL> def open ( self , curEntry ) : <EOL> curFunction = curEntry [ <NUM_LIT:0> ] ; <EOL> curClass = curEntry [ <NUM_LIT:1> ] ; <EOL> template = Settings . get ( '<STR_LIT>' ) ; <EOL> template = '<STR_LIT:\n>' . join ( template ) ; <EOL> template = template . replace ( '<STR_LIT>' , curFunction ) ; <EOL> self . view . run_command ( '<STR_LIT>' , { "<STR_LIT>" : template } ) ; <EOL> class ClassRelatedClassesCommand ( ClassFuncBase ) : <EOL> def load ( self ) : <EOL> self . _funcs . readJsDuckRelatedClasses ( ) ; <EOL> def open ( self , curEntry ) : <EOL> curClass = curEntry [ <NUM_LIT:0> ] ; <EOL> curGroup = curEntry [ <NUM_LIT:1> ] ; <EOL> filepath = self . _funcs . classNameToPath ( curClass ) ; <EOL> if not os . path . exists ( filepath ) : <EOL> sublime . status_message ( '<STR_LIT>' ) ; <EOL> return ; <EOL> view = sublime . set_timeout ( lambda : self . window ( ) . open_file ( filepath , <NUM_LIT:0> ) ) <EOL> class RebuildJsduckCommand ( ClassBase ) : <EOL> def run ( self , edit ) : <EOL> active_file_path = self . active_file_path ( ) ; <EOL> root = JsDuck . detectRoot ( sublime , active_file_path ) ; <EOL> if not root : <EOL> sublime . status_message ( '<STR_LIT>' ) ; <EOL> return ; <EOL> if JsDuck . isActive ( ) : <EOL> sublime . status_message ( '<STR_LIT>' ) ; <EOL> return ; <EOL> JsDuck . buildJsDuck ( sublime , root ) ; <EOL> class RestartSublimeCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> curdir = os . path . dirname ( os . path . realpath ( __file__ ) ) ; <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> os . execl ( sys . executable , '<STR_LIT>' ) ; <EOL> else : <EOL> if sys . platform == '<STR_LIT>' : <EOL> os . chmod ( os . path . join ( curdir , '<STR_LIT>' ) , <NUM_LIT> ) ; <EOL> os . execl ( os . path . join ( curdir , '<STR_LIT>' ) , '<STR_LIT>' ) ; <EOL> else : <EOL> os . chmod ( os . path . join ( curdir , '<STR_LIT>' ) , <NUM_LIT> ) ; <EOL> os . execl ( os . path . join ( curdir , '<STR_LIT>' ) , '<STR_LIT>' ) ; <EOL> lastRun = time . time ( ) ; <EOL> scheduled = False ; <EOL> scheduleAgain = False ; <EOL> class ChangeListener ( sublime_plugin . EventListener ) : <EOL> def __timeTillNext ( self ) : <EOL> global lastRun ; <EOL> if lastRun > ( time . time ( ) - Settings . get ( '<STR_LIT>' , <NUM_LIT> ) ) : <EOL> return lastRun - ( time . time ( ) - Settings . get ( '<STR_LIT>' , <NUM_LIT> ) ) ; <EOL> else : <EOL> return <NUM_LIT:0> ; <EOL> def on_pre_save ( self , view ) : <EOL> global scheduled , lastRun ; <EOL> print ( '<STR_LIT>' , view . file_name ( ) ) ; <EOL> if not view . is_dirty ( ) : <EOL> print ( '<STR_LIT>' ) ; <EOL> return ; <EOL> if not isJavascriptFile ( view ) : <EOL> print ( '<STR_LIT>' ) ; <EOL> return ; <EOL> if not Settings . get ( '<STR_LIT>' , False ) : <EOL> print ( '<STR_LIT>' ) ; <EOL> return ; <EOL> filePath = view . file_name ( ) ; <EOL> func = ClassFunctions ( sublime , filePath ) ; <EOL> if not func . isValid ( ) : <EOL> print ( '<STR_LIT>' ) <EOL> return ; <EOL> print ( '<STR_LIT>' ) ; <EOL> if scheduled : <EOL> return ; <EOL> elif JsDuck . isActive ( ) : <EOL> scheduled = True ; <EOL> lastRun = time . time ( ) ; <EOL> sublime . set_timeout ( lambda : self . __doUpdate ( filePath ) , self . __timeTillNext ( ) * <NUM_LIT:1000> ) ; <EOL> print ( '<STR_LIT>' ) ; <EOL> else : <EOL> print ( '<STR_LIT>' ) ; <EOL> self . __doUpdate ( filePath ) ; <EOL> def __doUpdate ( self , filePath ) : <EOL> global lastRun ; <EOL> lastRun = time . time ( ) ; <EOL> root = JsDuck . detectRoot ( sublime , filePath ) ; <EOL> sublime . set_timeout ( lambda : JsDuck . buildJsDuck ( sublime , root ) , <NUM_LIT:10> ) </s>
