<s> """<STR_LIT>""" <EOL> import sublime , sublime_plugin , re , json , os , webbrowser , subprocess <EOL> ST3 = int ( sublime . version ( ) ) >= <NUM_LIT> <EOL> class NPMInfoEvents ( sublime_plugin . EventListener ) : <EOL> def __init__ ( self ) : <EOL> self . packagePaths = { } <EOL> self . busy = False <EOL> self . loadSettings ( ) <EOL> def get_setting ( self , name , default = None ) : <EOL> v = settings . get ( name ) <EOL> if v == None : <EOL> try : <EOL> return sublime . active_window ( ) . active_view ( ) . settings ( ) . get ( name , default ) <EOL> except AttributeError : <EOL> return default <EOL> else : <EOL> return v <EOL> def loadSettings ( self ) : <EOL> self . showQuickPanelDelay = self . get_setting ( '<STR_LIT>' , <NUM_LIT> ) <EOL> self . showQuickPanel = self . get_setting ( '<STR_LIT>' , True ) <EOL> def on_load ( self , view ) : <EOL> fn = view . file_name ( ) <EOL> if fn in self . packagePaths : <EOL> del self . packagePaths [ fn ] <EOL> self . view = view <EOL> def on_post_save ( self , view ) : <EOL> self . loadSettings ( ) <EOL> def on_selection_modified ( self , view ) : <EOL> if self . busy == True : <EOL> return <EOL> self . busy = True <EOL> sublime . set_timeout ( lambda : self . doCheck ( view ) , self . showQuickPanelDelay ) <EOL> def doCheck ( self , view ) : <EOL> if view . is_scratch ( ) or ( view . file_name ( ) and view . file_name ( ) . endswith ( '<STR_LIT>' ) == False ) : <EOL> self . busy = False <EOL> return <EOL> view . set_status ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . view = view <EOL> reg = view . sel ( ) [ <NUM_LIT:0> ] <EOL> if reg . empty ( ) : <EOL> line = view . line ( reg ) <EOL> line_contents = view . substr ( line ) <EOL> matchObj = re . match ( r'<STR_LIT>' , line_contents ) <EOL> if matchObj : <EOL> npm = matchObj . group ( <NUM_LIT:2> ) <EOL> qt = matchObj . group ( <NUM_LIT:1> ) <EOL> npmStart = ( line_contents . find ( "<STR_LIT>" + qt + npm + qt + "<STR_LIT:)>" ) + <NUM_LIT:9> ) <EOL> npmEnd = npmStart + len ( npm ) + <NUM_LIT:1> <EOL> col = view . rowcol ( reg . b ) [ <NUM_LIT:1> ] <EOL> if col >= npmEnd or col < npmStart : <EOL> self . busy = False <EOL> return <EOL> dir = view . file_name ( ) <EOL> pkgPath = self . getPackagePath ( npm , dir ) <EOL> self . pkgGlobal = False <EOL> if pkgPath == False : <EOL> pkgPath = self . getPackageGlobalPath ( npm , dir ) <EOL> if pkgPath != False : <EOL> self . pkgGlobal = True <EOL> o = self . getInfo ( npm , dir , pkgPath ) <EOL> self . pkgPath = pkgPath <EOL> self . pkgJson = o <EOL> self . npm = npm <EOL> if '<STR_LIT:version>' in o : <EOL> if '<STR_LIT:description>' in o : <EOL> desc = o [ '<STR_LIT:description>' ] <EOL> else : <EOL> desc = "<STR_LIT>" <EOL> npmAndVersion = npm + "<STR_LIT:@>" + o [ '<STR_LIT:version>' ] <EOL> if self . pkgGlobal : <EOL> npmAndVersion += "<STR_LIT>" <EOL> view . set_status ( '<STR_LIT>' , npmAndVersion + "<STR_LIT>" + desc ) <EOL> self . quickPanelOptions = [ npmAndVersion , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if '<STR_LIT>' in o and '<STR_LIT:url>' in o [ '<STR_LIT>' ] and o [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] . startswith ( '<STR_LIT:http>' ) : <EOL> self . quickPanelOptions += [ '<STR_LIT>' ] <EOL> if self . showQuickPanel : <EOL> view . window ( ) . show_quick_panel ( self . quickPanelOptions , self . quickPanelSelect ) <EOL> else : <EOL> view . set_status ( '<STR_LIT>' , '<STR_LIT>' + npm + '<STR_LIT:">' ) <EOL> self . busy = False <EOL> def quickPanelSelect ( self , index ) : <EOL> if index == <NUM_LIT:0> : <EOL> self . showPackageDesc ( ) <EOL> if index == <NUM_LIT:1> : <EOL> self . listPropsAndMethods ( ) <EOL> if index == <NUM_LIT:2> : <EOL> self . view . window ( ) . open_file ( self . pkgPath ) <EOL> if index == <NUM_LIT:3> and len ( self . quickPanelOptions ) == <NUM_LIT:4> : <EOL> webbrowser . open ( self . pkgJson [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] , new = <NUM_LIT:2> , autoraise = True ) <EOL> def showPackageDesc ( self ) : <EOL> if '<STR_LIT:description>' in self . pkgJson : <EOL> sublime . message_dialog ( self . pkgJson [ '<STR_LIT:description>' ] ) <EOL> def listPropsAndMethods ( self ) : <EOL> res = [ ] <EOL> if sublime . platform ( ) == '<STR_LIT>' : <EOL> app = '<STR_LIT>' <EOL> pathToJS = sublime . packages_path ( ) + '<STR_LIT>' <EOL> cmd = [ app , pathToJS , self . pkgPath ] <EOL> startupinfo = subprocess . STARTUPINFO ( ) <EOL> startupinfo . dwFlags |= subprocess . STARTF_USESHOWWINDOW <EOL> p = subprocess . Popen ( cmd , stdout = subprocess . PIPE , stderr = subprocess . STDOUT , startupinfo = startupinfo ) <EOL> else : <EOL> pathToJS = sublime . packages_path ( ) + '<STR_LIT>' <EOL> cmd = [ '<STR_LIT>' , pathToJS , self . pkgPath ] <EOL> p = subprocess . Popen ( cmd , stdout = subprocess . PIPE , stderr = subprocess . STDOUT ) <EOL> for line in iter ( p . stdout . readline , b'<STR_LIT>' ) : <EOL> lineStrip = line . rstrip ( ) <EOL> if len ( lineStrip ) > <NUM_LIT:0> and '<STR_LIT:U+0020>' not in lineStrip : <EOL> res . append ( lineStrip ) <EOL> self . view . window ( ) . show_quick_panel ( res , self . onSelectPropAndMethod ) <EOL> def onSelectPropAndMethod ( self , index ) : <EOL> pass <EOL> def getJson ( self , pkgPath ) : <EOL> with open ( pkgPath ) as json_data : <EOL> d = json . load ( json_data ) <EOL> json_data . close ( ) <EOL> return d <EOL> def getInfo ( self , npm , dir , pkgPath ) : <EOL> if dir not in self . packagePaths : <EOL> self . packagePaths [ dir ] = { } <EOL> if pkgPath == False : <EOL> return { } <EOL> elif npm in self . packagePaths [ dir ] : <EOL> return self . getJson ( self . packagePaths [ dir ] [ npm ] ) <EOL> else : <EOL> self . packagePaths [ dir ] [ npm ] = pkgPath <EOL> return self . getJson ( pkgPath ) <EOL> def getPackageGlobalPath ( self , npm , dir ) : <EOL> if sublime . platform ( ) == '<STR_LIT>' : <EOL> fn = os . environ . get ( '<STR_LIT>' ) + "<STR_LIT>" + npm + "<STR_LIT>" <EOL> else : <EOL> fn = "<STR_LIT>" + npm + "<STR_LIT>" <EOL> if os . path . exists ( fn ) : <EOL> return fn <EOL> else : <EOL> return False <EOL> def getPackagePath ( self , npm , dir ) : <EOL> dir = os . path . dirname ( dir ) <EOL> if len ( dir ) <= <NUM_LIT:3> : <EOL> return False <EOL> if sublime . platform ( ) == '<STR_LIT>' : <EOL> fn = dir + '<STR_LIT>' + npm + '<STR_LIT>' <EOL> else : <EOL> fn = dir + '<STR_LIT>' + npm + '<STR_LIT>' <EOL> if os . path . exists ( fn ) : <EOL> return fn <EOL> else : <EOL> return self . getPackagePath ( npm , dir ) <EOL> def plugin_loaded ( ) : <EOL> global settings <EOL> settings = sublime . load_settings ( '<STR_LIT>' ) <EOL> if not ST3 : <EOL> plugin_loaded ( ) </s>
