<s> import sublime <EOL> import sublime_plugin <EOL> import re <EOL> import os <EOL> import shutil <EOL> import tarfile <EOL> import webbrowser <EOL> import time <EOL> import urllib <EOL> from Default import symbol as sublime_symbol <EOL> from html . parser import HTMLParser <EOL> package_name = '<STR_LIT>' <EOL> setting_file = package_name + '<STR_LIT>' <EOL> docphp_languages = { } <EOL> currentView = False <EOL> currentSettings = None <EOL> openfiles = { } <EOL> entities = { <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT:html>" : False <EOL> } <EOL> language = '<STR_LIT>' <EOL> downloading = False <EOL> def plugin_loaded ( ) : <EOL> global currentSettings , language , currentView <EOL> currentSettings = sublime . load_settings ( setting_file ) <EOL> language = currentSettings . get ( '<STR_LIT>' ) <EOL> currentView = sublime . active_window ( ) . active_view ( ) <EOL> docphpPath = getDocphpPath ( ) <EOL> if not os . path . isdir ( docphpPath + '<STR_LIT>' ) : <EOL> os . makedirs ( docphpPath + '<STR_LIT>' ) <EOL> if not callable ( sublime_symbol . symbol_at_point ) or not callable ( sublime_symbol . navigate_to_symbol ) : <EOL> sublime . error_message ( '<STR_LIT>' ) <EOL> from package_control import events <EOL> if events . install ( package_name ) or not language : <EOL> currentView . run_command ( '<STR_LIT>' , { "<STR_LIT>" : True , "<STR_LIT>" : True } ) <EOL> def plugin_unloaded ( ) : <EOL> for k in openfiles : <EOL> try : <EOL> openfiles [ k ] . close ( ) <EOL> except Exception as e : <EOL> if getSetting ( '<STR_LIT>' ) : <EOL> print ( e ) <EOL> sublime . save_settings ( setting_file ) <EOL> from package_control import events <EOL> if events . remove ( package_name ) : <EOL> if os . path . isdir ( getDocphpPath ( ) ) : <EOL> shutil . rmtree ( getDocphpPath ( ) ) <EOL> def getSetting ( key ) : <EOL> return currentSettings . get ( key ) <EOL> def setSetting ( key , value ) : <EOL> currentSettings . set ( key , value ) <EOL> sublime . save_settings ( setting_file ) <EOL> def getAllLanguages ( ) : <EOL> return sublime . decode_value ( sublime . load_resource ( '<STR_LIT>' + package_name + '<STR_LIT>' ) ) <EOL> def getLanguageList ( languageName = None , format = '<STR_LIT:all>' , getAll = True ) : <EOL> if not getAll : <EOL> languageName = getSetting ( '<STR_LIT>' ) <EOL> if languageName == None : <EOL> dic = [ ] <EOL> elif isinstance ( languageName , str ) : <EOL> dic = [ languageName ] <EOL> else : <EOL> dic = languageName <EOL> languages = getAllLanguages ( ) <EOL> languages = [ ( k , languages [ k ] ) for k in sorted ( languages . keys ( ) ) ] <EOL> languageList = [ ] <EOL> index = None <EOL> for k , v in languages : <EOL> if languageName == None or k in dic : <EOL> index = len ( languageList ) <EOL> if format == '<STR_LIT:all>' : <EOL> languageList . append ( k + '<STR_LIT:U+0020>' + v [ '<STR_LIT:name>' ] + '<STR_LIT>' + v [ '<STR_LIT>' ] + '<STR_LIT:)>' ) <EOL> elif format == '<STR_LIT:name>' : <EOL> languageList . append ( v [ '<STR_LIT:name>' ] ) <EOL> elif format == '<STR_LIT>' : <EOL> languageList . append ( v [ '<STR_LIT>' ] ) <EOL> elif format == '<STR_LIT>' : <EOL> v [ '<STR_LIT>' ] = k <EOL> languageList . append ( v ) <EOL> return languageList , index <EOL> def decodeEntity ( xml , category = '<STR_LIT>' ) : <EOL> global entities <EOL> if not isinstance ( xml , str ) : <EOL> return xml <EOL> if entities [ category ] : <EOL> forward , reverse = entities [ category ] <EOL> else : <EOL> resourceMap = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:html>" : "<STR_LIT>" , <EOL> } <EOL> forward = sublime . decode_value ( sublime . load_resource ( '<STR_LIT>' + package_name + '<STR_LIT:/>' + resourceMap [ category ] ) ) <EOL> reverse = dict ( ( v , k ) for k , v in forward . items ( ) ) <EOL> entities [ category ] = ( forward , reverse ) <EOL> def parseEntity ( match ) : <EOL> entity = match . group ( <NUM_LIT:1> ) <EOL> try : <EOL> if entity . isdigit ( ) : <EOL> return reverse [ int ( entity ) ] <EOL> else : <EOL> return chr ( forward [ entity ] ) <EOL> except : <EOL> return match . group ( <NUM_LIT:0> ) <EOL> xml = re . sub ( '<STR_LIT>' , parseEntity , xml ) <EOL> return xml <EOL> def getDocphpPath ( ) : <EOL> return sublime . cache_path ( ) + '<STR_LIT:/>' + package_name + '<STR_LIT:/>' <EOL> def getTarGzPath ( ) : <EOL> return getDocphpPath ( ) + '<STR_LIT>' + language + '<STR_LIT>' <EOL> def getI18nCachePath ( languageName = None ) : <EOL> if not languageName : <EOL> languageName = language <EOL> return getDocphpPath ( ) + '<STR_LIT>' + languageName + '<STR_LIT:/>' <EOL> def getTarHandler ( ) : <EOL> tarGzPath = getTarGzPath ( ) <EOL> try : <EOL> tar = openfiles [ tarGzPath ] <EOL> except KeyError : <EOL> tar = tarfile . open ( tarGzPath ) <EOL> openfiles [ tarGzPath ] = tar <EOL> return tar <EOL> def loadLanguage ( ) : <EOL> global docphp_languages <EOL> tarGzPath = getTarGzPath ( ) <EOL> if not os . path . isfile ( tarGzPath ) : <EOL> return False <EOL> tar = getTarHandler ( ) <EOL> tar . getmembers ( ) <EOL> def generate ( ) : <EOL> symbols = { } <EOL> for tarinfo in tar : <EOL> m = re . search ( '<STR_LIT>' , tarinfo . name ) <EOL> if m : <EOL> symbols [ m . group ( <NUM_LIT:1> ) ] = m . group ( <NUM_LIT:0> ) <EOL> return symbols <EOL> symbols = getJsonOrGenerate ( '<STR_LIT>' , generate ) <EOL> docphp_languages [ language ] = { "<STR_LIT>" : symbols , "<STR_LIT>" : { } } <EOL> return True <EOL> def getJsonOrGenerate ( name , callback ) : <EOL> filename = getI18nCachePath ( ) + name + '<STR_LIT>' <EOL> if os . path . exists ( filename ) : <EOL> with open ( filename , '<STR_LIT:r>' , encoding = '<STR_LIT:utf8>' ) as f : <EOL> json = f . read ( <NUM_LIT> ) <EOL> content = sublime . decode_value ( json ) <EOL> else : <EOL> content = callback ( ) <EOL> dirname = os . path . dirname ( filename ) <EOL> if not os . path . isdir ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> with open ( filename , '<STR_LIT:w>' , encoding = "<STR_LIT:utf8>" ) as f : <EOL> f . write ( sublime . encode_value ( content ) ) <EOL> return content <EOL> def languageExists ( languageName = None , fallback = False ) : <EOL> if not languageName : <EOL> languageName = language <EOL> if not language : <EOL> currentView . run_command ( '<STR_LIT>' , { "<STR_LIT>" : True , "<STR_LIT>" : True } ) <EOL> return False <EOL> if languageName not in docphp_languages and not loadLanguage ( ) : <EOL> if fallback : <EOL> begin = '<STR_LIT>' <EOL> else : <EOL> begin = '<STR_LIT>' <EOL> print ( getAllLanguages ( ) ) <EOL> show_name = getAllLanguages ( ) [ languageName ] [ '<STR_LIT:name>' ] <EOL> sublime . error_message ( begin + '<STR_LIT>' + show_name + <EOL> '<STR_LIT>' ) <EOL> return False <EOL> return True <EOL> def getSymbolDescription ( symbol , use_language = False , fallback = False ) : <EOL> if not use_language : <EOL> global language <EOL> else : <EOL> language = use_language <EOL> if not languageExists ( language , fallback ) : <EOL> return None , False <EOL> symbol = symbol . lower ( ) <EOL> symbolList = docphp_languages [ language ] [ "<STR_LIT>" ] <EOL> if not fallback : <EOL> for prefix in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if prefix + symbol in symbolList : <EOL> symbol = prefix + symbol <EOL> break <EOL> if symbol not in symbolList : <EOL> if not fallback and getSetting ( '<STR_LIT>' ) : <EOL> return getSymbolDescription ( symbol , getSetting ( '<STR_LIT>' ) , True ) <EOL> else : <EOL> return None , None <EOL> elif symbol not in docphp_languages [ language ] [ "<STR_LIT>" ] : <EOL> output = getSymbolFromHtml ( symbol ) <EOL> docphp_languages [ language ] [ "<STR_LIT>" ] [ symbol ] = output <EOL> return symbol , docphp_languages [ language ] [ "<STR_LIT>" ] [ symbol ] <EOL> def getSymbolFromHtml ( symbol ) : <EOL> tar = getTarHandler ( ) <EOL> member = tar . getmember ( docphp_languages [ language ] [ "<STR_LIT>" ] [ symbol ] ) <EOL> f = tar . extractfile ( member ) <EOL> output = f . read ( ) . decode ( errors = '<STR_LIT:ignore>' ) <EOL> dic = { <EOL> '<STR_LIT>' : chr ( <NUM_LIT> ) , <EOL> '<STR_LIT>' : '<STR_LIT:">' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : "<STR_LIT:'>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : chr ( <NUM_LIT> ) , <EOL> } <EOL> pattern = "<STR_LIT:|>" . join ( map ( re . escape , dic . keys ( ) ) ) <EOL> output = re . sub ( pattern , lambda m : dic [ m . group ( ) ] , output ) <EOL> return output <EOL> class DocphpShowDefinitionCommand ( sublime_plugin . TextCommand ) : <EOL> history = [ ] <EOL> currentSymbol = '<STR_LIT>' <EOL> projectSymbols = [ ] <EOL> window = False <EOL> projectView = False <EOL> def is_enabled ( self , ** args ) : <EOL> selection = self . view . sel ( ) <EOL> force = args . get ( '<STR_LIT>' ) <EOL> if force or self . view . score_selector ( selection [ <NUM_LIT:0> ] . a , '<STR_LIT>' ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def want_event ( self ) : <EOL> return True <EOL> def run ( self , edit , event = None , symbol = None , force = False ) : <EOL> global language , currentView <EOL> view = self . view <EOL> currentView = view <EOL> pt = False <EOL> language = getSetting ( '<STR_LIT>' ) <EOL> if not language : <EOL> view . window ( ) . run_command ( '<STR_LIT>' ) <EOL> return <EOL> if symbol == None : <EOL> if event : <EOL> pt = view . window_to_text ( ( event [ "<STR_LIT:x>" ] , event [ "<STR_LIT:y>" ] ) ) <EOL> else : <EOL> pt = view . sel ( ) [ <NUM_LIT:0> ] <EOL> self . pt = pt <EOL> symbol , locations = sublime_symbol . symbol_at_point ( view , pt ) <EOL> translatedSymbol = symbol . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> translatedSymbol , symbolDescription = getSymbolDescription ( translatedSymbol ) <EOL> if symbolDescription == None : <EOL> if self . search_in_scope ( symbol ) : <EOL> return <EOL> if getSetting ( '<STR_LIT>' ) and len ( locations ) : <EOL> sublime_symbol . navigate_to_symbol ( view , symbol , locations ) <EOL> return <EOL> if getSetting ( '<STR_LIT>' ) : <EOL> view . show_popup ( '<STR_LIT>' , sublime . COOPERATE_WITH_AUTO_COMPLETE ) <EOL> return <EOL> if symbolDescription == False : <EOL> return <EOL> if getSetting ( '<STR_LIT>' ) == False : <EOL> self . show_popup ( translatedSymbol , symbolDescription ) <EOL> else : <EOL> self . show_panel ( translatedSymbol , symbolDescription , edit ) <EOL> def search_in_scope ( self , symbol ) : <EOL> search_str = self . view . substr ( self . view . line ( self . pt ) ) <EOL> if re . search ( "<STR_LIT>" + re . escape ( symbol ) , search_str ) != None : <EOL> lower = symbol . lower ( ) <EOL> for scopeSymbol in self . view . symbols ( ) : <EOL> if scopeSymbol [ <NUM_LIT:1> ] . lower ( ) == lower : <EOL> self . view . sel ( ) . clear ( ) <EOL> self . view . sel ( ) . add ( scopeSymbol [ <NUM_LIT:0> ] ) <EOL> self . view . show ( scopeSymbol [ <NUM_LIT:0> ] ) <EOL> return True <EOL> return False <EOL> def show_popup ( self , symbol , symbolDescription ) : <EOL> output = symbolDescription <EOL> if getSetting ( '<STR_LIT>' ) : <EOL> print ( output ) <EOL> self . currentSymbol = symbol <EOL> width , height = self . view . viewport_extent ( ) <EOL> output = self . formatPopup ( output , symbol = symbol ) <EOL> output = output [ : <NUM_LIT> ] <EOL> self . view . show_popup ( <EOL> output , <EOL> flags = sublime . COOPERATE_WITH_AUTO_COMPLETE | sublime . HTML , <EOL> location = - <NUM_LIT:1> , <EOL> max_width = min ( getSetting ( '<STR_LIT>' ) , width ) , <EOL> max_height = min ( getSetting ( '<STR_LIT>' ) , height - <NUM_LIT:100> ) , <EOL> on_navigate = self . on_navigate , <EOL> on_hide = self . on_hide <EOL> ) <EOL> def show_panel ( self , symbol , symbolDescription , edit ) : <EOL> output = self . formatPanel ( symbolDescription ) <EOL> name = '<STR_LIT>' <EOL> window = self . view . window ( ) <EOL> panel = window . get_output_panel ( name ) <EOL> window . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" + name } ) <EOL> panel . set_read_only ( False ) <EOL> panel . insert ( edit , panel . size ( ) , output + '<STR_LIT:\n>' ) <EOL> panel . set_read_only ( True ) <EOL> def on_hide ( self ) : <EOL> self . currentSymbol = '<STR_LIT>' <EOL> self . history = [ ] <EOL> def on_navigate ( self , url ) : <EOL> if re . search ( '<STR_LIT>' , url ) : <EOL> webbrowser . open_new ( url ) <EOL> return True <EOL> m = re . search ( '<STR_LIT>' , url ) <EOL> if m : <EOL> symbol , content = getSymbolDescription ( self . currentSymbol , m . group ( <NUM_LIT:1> ) ) <EOL> else : <EOL> if url == '<STR_LIT>' : <EOL> symbol = self . history . pop ( ) <EOL> self . currentSymbol = symbol <EOL> else : <EOL> self . history . append ( self . currentSymbol ) <EOL> symbol = url [ : url . find ( '<STR_LIT>' ) ] <EOL> self . currentSymbol = symbol <EOL> symbol , content = getSymbolDescription ( symbol ) <EOL> if content == False : <EOL> return False <EOL> content = self . formatPopup ( content , symbol = symbol , can_back = len ( self . history ) > <NUM_LIT:0> ) <EOL> content = content [ : <NUM_LIT> ] <EOL> self . view . update_popup ( content ) <EOL> def formatPopup ( self , content , symbol , can_back = False ) : <EOL> if not isinstance ( content , str ) : <EOL> return <EOL> content = decodeEntity ( content ) <EOL> parser = PopupHTMLParser ( symbol , language , can_back ) <EOL> try : <EOL> parser . feed ( content ) <EOL> except FinishError : <EOL> pass <EOL> content = parser . output <EOL> content = '<STR_LIT>' + sublime . load_resource ( '<STR_LIT>' + package_name + '<STR_LIT>' ) + '<STR_LIT>' + content + "<STR_LIT>" <EOL> return content <EOL> def formatPanel ( self , content ) : <EOL> if not isinstance ( content , str ) : <EOL> return <EOL> content = decodeEntity ( content ) <EOL> content = re . sub ( '<STR_LIT>' , '<STR_LIT:U+0020>' , content ) <EOL> content = re . sub ( '<STR_LIT>' , '<STR_LIT:\n>' , content ) <EOL> content = re . sub ( '<STR_LIT>' , '<STR_LIT>' , content ) <EOL> content = re . sub ( '<STR_LIT>' , '<STR_LIT>' , content ) <EOL> content = re . sub ( '<STR_LIT>' , '<STR_LIT>' , content , count = <NUM_LIT:1> ) <EOL> content = decodeEntity ( content , '<STR_LIT:html>' ) <EOL> return content <EOL> class PopupHTMLParser ( HTMLParser ) : <EOL> symbol = '<STR_LIT>' <EOL> language = '<STR_LIT>' <EOL> can_back = False <EOL> stack = [ ] <EOL> output = '<STR_LIT>' <EOL> as_div = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> strip = [ '<STR_LIT>' ] <EOL> started = False <EOL> navigate_rendered = False <EOL> navigate_up = '<STR_LIT>' <EOL> def __init__ ( self , symbol , language , can_back ) : <EOL> self . symbol = symbol <EOL> self . language = language <EOL> self . can_back = can_back <EOL> super ( ) . __init__ ( ) <EOL> def parseAttrs ( self , attrs ) : <EOL> ret = { } <EOL> for k , v in attrs : <EOL> ret [ k ] = v <EOL> return ret <EOL> def handle_starttag ( self , tag , attrs ) : <EOL> attrs = self . parseAttrs ( attrs ) <EOL> for k in attrs : <EOL> v = attrs [ k ] <EOL> if k == '<STR_LIT:id>' and v == self . symbol : <EOL> self . output = '<STR_LIT>' <EOL> if k == '<STR_LIT:class>' and v == '<STR_LIT>' : <EOL> self . output = '<STR_LIT>' <EOL> if tag in self . as_div : <EOL> if '<STR_LIT:class>' in attrs : <EOL> attrs [ '<STR_LIT:class>' ] += '<STR_LIT:U+0020>' + tag <EOL> else : <EOL> attrs [ '<STR_LIT:class>' ] = tag <EOL> tag = '<STR_LIT>' <EOL> if tag in self . strip : <EOL> return <EOL> self . stack . append ( { '<STR_LIT>' : tag , '<STR_LIT>' : attrs } ) <EOL> border = self . shall_border ( tag , attrs ) <EOL> if border : <EOL> self . output += '<STR_LIT>' + border + '<STR_LIT>' <EOL> self . output += self . get_tag_text ( tag , attrs ) <EOL> def handle_endtag ( self , tag ) : <EOL> if tag in self . as_div : <EOL> tag = '<STR_LIT>' <EOL> if tag in self . strip : <EOL> return <EOL> try : <EOL> while ( True ) : <EOL> previous = self . stack . pop ( ) <EOL> self . output += '<STR_LIT>' + tag + '<STR_LIT:>>' <EOL> if re . search ( '<STR_LIT>' , tag ) : <EOL> self . output += '<STR_LIT>' <EOL> if not self . navigate_rendered : <EOL> self . navigate_rendered = True <EOL> self . output += ( '<STR_LIT>' if self . can_back else '<STR_LIT>' ) + '<STR_LIT>' + self . language + '<STR_LIT:/>' + self . symbol + '<STR_LIT>' + '<STR_LIT>' + re . sub ( '<STR_LIT>' , '<STR_LIT>' , self . navigate_up ) <EOL> languages , _ = getLanguageList ( format = '<STR_LIT>' , getAll = False ) <EOL> if len ( languages ) > <NUM_LIT:1> : <EOL> self . output += '<STR_LIT>' <EOL> for lang in languages : <EOL> self . output += '<STR_LIT>' + lang [ '<STR_LIT>' ] + '<STR_LIT>' + lang [ '<STR_LIT>' ] + '<STR_LIT>' <EOL> if self . shall_border ( previous [ '<STR_LIT>' ] , previous [ '<STR_LIT>' ] ) : <EOL> self . output += '<STR_LIT>' <EOL> for k in previous [ '<STR_LIT>' ] : <EOL> v = previous [ '<STR_LIT>' ] [ k ] <EOL> if k == '<STR_LIT:id>' and v == self . symbol : <EOL> raise FinishError <EOL> if k == '<STR_LIT:class>' and v == '<STR_LIT>' : <EOL> self . navigate_up = self . output <EOL> if tag == previous [ '<STR_LIT>' ] : <EOL> break <EOL> except IndexError : <EOL> pass <EOL> def handle_startendtag ( self , tag , attrs ) : <EOL> if tag in self . as_div : <EOL> if '<STR_LIT:class>' in attrs : <EOL> attrs [ '<STR_LIT:class>' ] += '<STR_LIT:U+0020>' + tag <EOL> else : <EOL> attrs [ '<STR_LIT:class>' ] = tag <EOL> tag = '<STR_LIT>' <EOL> self . output += self . get_tag_text ( tag , attrs , True ) <EOL> def handle_data ( self , data ) : <EOL> self . output += data <EOL> pass <EOL> def handle_entityref ( self , name ) : <EOL> self . output += '<STR_LIT:&>' + name + '<STR_LIT:;>' <EOL> def handle_charref ( self , name ) : <EOL> self . output += '<STR_LIT:&>' + name + '<STR_LIT:;>' <EOL> def shall_border ( self , tag , attrs ) : <EOL> if tag . lower ( ) not in [ '<STR_LIT>' , '<STR_LIT:p>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return False <EOL> for k in attrs : <EOL> v = attrs [ k ] <EOL> if k == '<STR_LIT:class>' : <EOL> if re . search ( '<STR_LIT>' , v ) : <EOL> return '<STR_LIT>' <EOL> elif re . search ( '<STR_LIT>' , v ) : <EOL> return '<STR_LIT>' <EOL> elif re . search ( '<STR_LIT>' , v ) : <EOL> return '<STR_LIT>' <EOL> elif re . search ( '<STR_LIT>' , v ) : <EOL> return '<STR_LIT>' <EOL> return False <EOL> def get_tag_text ( self , tag , attrs , is_startend = False ) : <EOL> return '<STR_LIT:<>' + ( tag + '<STR_LIT:U+0020>' + '<STR_LIT:U+0020>' . join ( map ( lambda m : m + '<STR_LIT>' + re . sub ( '<STR_LIT>' , '<STR_LIT>' , attrs [ m ] ) + '<STR_LIT:">' , attrs ) ) ) . rstrip ( ) + ( '<STR_LIT>' if is_startend else '<STR_LIT:>>' ) <EOL> class DocphpCheckoutLanguageCommand ( sublime_plugin . TextCommand ) : <EOL> languageList = None <EOL> languageName = None <EOL> downloading = False <EOL> set_fallback = None <EOL> def run ( self , edit , languageName = None , set_fallback = False , is_init = False ) : <EOL> view = self . view <EOL> global currentView <EOL> currentView = view <EOL> if self . downloading : <EOL> sublime . message_dialog ( '<STR_LIT>' + self . downloading + '<STR_LIT>' ) <EOL> return <EOL> self . languageList , index = getLanguageList ( languageName ) <EOL> self . set_fallback = set_fallback <EOL> if languageName : <EOL> self . updateLanguage ( index ) <EOL> else : <EOL> currentView . window ( ) . show_quick_panel ( self . languageList , self . updateLanguage , sublime . KEEP_OPEN_ON_FOCUS_LOST ) <EOL> def updateLanguage ( self , index = None ) : <EOL> if index == - <NUM_LIT:1> or index == None : <EOL> return <EOL> languageName = re . search ( '<STR_LIT>' , self . languageList [ index ] ) . group ( <NUM_LIT:0> ) <EOL> self . languageName = languageName <EOL> sublime . set_timeout_async ( self . checkoutLanguage , <NUM_LIT:0> ) <EOL> def checkoutLanguage ( self ) : <EOL> global language <EOL> languageName = self . languageName <EOL> if not self . downloadLanguageGZ ( languageName ) : <EOL> if getSetting ( '<STR_LIT>' ) : <EOL> print ( '<STR_LIT>' ) <EOL> return False <EOL> setSetting ( '<STR_LIT>' , languageName ) <EOL> language = languageName <EOL> languageSettings = currentSettings . get ( '<STR_LIT>' ) <EOL> languageSettings [ languageName ] = '<STR_LIT>' <EOL> setSetting ( '<STR_LIT>' , languageSettings ) <EOL> if self . set_fallback : <EOL> setSetting ( '<STR_LIT>' , languageName ) <EOL> loadLanguage ( ) <EOL> sublime . message_dialog ( '<STR_LIT>' + languageName + '<STR_LIT>' ) <EOL> def downloadLanguageGZ ( self , name ) : <EOL> err = None <EOL> try : <EOL> url = '<STR_LIT>' + name + '<STR_LIT>' <EOL> filename = getDocphpPath ( ) + '<STR_LIT>' + name + '<STR_LIT>' <EOL> response = urllib . request . urlopen ( url ) <EOL> try : <EOL> totalsize = int ( response . headers [ '<STR_LIT>' ] ) <EOL> except NameError : <EOL> totalsize = None <EOL> except KeyError : <EOL> totalsize = None <EOL> outputfile = open ( filename , '<STR_LIT:wb>' ) <EOL> readsofar = <NUM_LIT:0> <EOL> chunksize = <NUM_LIT> <EOL> try : <EOL> self . downloading = name <EOL> while ( True ) : <EOL> data = response . read ( chunksize ) <EOL> if not data : <EOL> break <EOL> readsofar += len ( data ) <EOL> outputfile . write ( data ) <EOL> if totalsize : <EOL> percent = readsofar * <NUM_LIT> / totalsize <EOL> sublime . status_message ( package_name + '<STR_LIT>' % ( percent , name , ) ) <EOL> else : <EOL> kb = readsofar / <NUM_LIT> <EOL> sublime . status_message ( package_name + '<STR_LIT>' % ( kb , name , ) ) <EOL> finally : <EOL> outputfile . close ( ) <EOL> self . downloading = False <EOL> if totalsize and readsofar != totalsize : <EOL> os . unlink ( filename ) <EOL> err = '<STR_LIT>' <EOL> except ( urllib . error . HTTPError ) as e : <EOL> err = '<STR_LIT>' % ( __name__ , str ( e . code ) ) <EOL> except ( urllib . error . URLError ) as e : <EOL> err = '<STR_LIT>' % ( __name__ , str ( e . reason ) ) <EOL> except Exception as e : <EOL> err = e . __class__ . __name__ <EOL> if not err : <EOL> if os . path . isdir ( getI18nCachePath ( name ) ) : <EOL> shutil . rmtree ( getI18nCachePath ( name ) ) <EOL> newname = getDocphpPath ( ) + '<STR_LIT>' + name + '<STR_LIT>' <EOL> if os . path . isfile ( newname ) : <EOL> os . unlink ( newname ) <EOL> os . rename ( filename , newname ) <EOL> return True <EOL> print ( err ) <EOL> sublime . message_dialog ( '<STR_LIT>' + name + '<STR_LIT>' ) <EOL> return False <EOL> class DocphpSelectLanguageCommand ( sublime_plugin . TextCommand ) : <EOL> languageNameList = None <EOL> def run ( self , edit ) : <EOL> global currentView <EOL> view = self . view <EOL> currentView = view <EOL> self . languageList , _ = getLanguageList ( getAll = False ) <EOL> currentView . window ( ) . show_quick_panel ( self . languageList , self . selectLanguageCallback ) <EOL> def selectLanguageCallback ( self , index ) : <EOL> global language <EOL> if index != - <NUM_LIT:1> : <EOL> language = re . search ( '<STR_LIT>' , self . languageList [ index ] ) . group ( <NUM_LIT:0> ) <EOL> setSetting ( '<STR_LIT>' , language ) <EOL> loadLanguage ( ) <EOL> class DocphpOpenManualIndexCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> global currentView <EOL> currentView = self . view <EOL> self . view . run_command ( '<STR_LIT>' , { "<STR_LIT>" : '<STR_LIT:index>' , "<STR_LIT>" : True } ) <EOL> class DocphpSearchCommand ( sublime_plugin . TextCommand ) : <EOL> def want_event ( self ) : <EOL> return True <EOL> def run ( self , edit , event = None , at_point = False ) : <EOL> global currentView <EOL> view = self . view <EOL> window = view . window ( ) <EOL> currentView = view <EOL> if not languageExists ( ) : <EOL> return <EOL> tar = getTarHandler ( ) <EOL> symbol = None <EOL> if at_point : <EOL> symbol = view . substr ( view . word ( view . sel ( ) [ <NUM_LIT:0> ] ) ) <EOL> files = list ( docphp_languages [ language ] [ "<STR_LIT>" ] . keys ( ) ) <EOL> files . sort ( ) <EOL> def show ( index ) : <EOL> if index != - <NUM_LIT:1> : <EOL> currentView . run_command ( '<STR_LIT>' , { "<STR_LIT>" : files [ index ] , "<STR_LIT>" : True } ) <EOL> selected_index = - <NUM_LIT:1> <EOL> if event : <EOL> pt = view . window_to_text ( ( event [ "<STR_LIT:x>" ] , event [ "<STR_LIT:y>" ] ) ) <EOL> symbol , locations = sublime_symbol . symbol_at_point ( view , pt ) <EOL> for prefix in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> try : <EOL> selected_index = files . index ( prefix + symbol ) <EOL> break <EOL> except ValueError : <EOL> pass <EOL> currentView . window ( ) . show_quick_panel ( files , show , selected_index = selected_index ) <EOL> class DocPHPListener ( sublime_plugin . EventListener ) : <EOL> prevTime = <NUM_LIT:0> <EOL> delaying = False <EOL> def on_selection_modified_async ( self , view ) : <EOL> if not getSetting ( '<STR_LIT>' ) : <EOL> return <EOL> global currentView <EOL> currentView = view <EOL> self . prevTime = time . time ( ) <EOL> if not self . delaying : <EOL> sublime . set_timeout_async ( self . doAutoShow , getSetting ( '<STR_LIT>' ) + <NUM_LIT:50> ) <EOL> self . delaying = True <EOL> def doAutoShow ( self ) : <EOL> delayTime = getSetting ( '<STR_LIT>' ) <EOL> if ( time . time ( ) - self . prevTime ) * <NUM_LIT:1000> > delayTime : <EOL> self . delaying = False <EOL> if not currentView . is_popup_visible ( ) : <EOL> currentView . run_command ( '<STR_LIT>' ) <EOL> else : <EOL> sublime . set_timeout_async ( self . doAutoShow , int ( delayTime - ( time . time ( ) - self . prevTime ) * <NUM_LIT:1000> ) + <NUM_LIT:50> ) <EOL> class FinishError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
