<s> import sublime <EOL> import sublime_plugin <EOL> import os . path <EOL> from time import time <EOL> from . helpers import is_name , is_asm <EOL> global contexts <EOL> contexts = dict ( ) <EOL> processing = dict ( ) <EOL> scanRequests = set ( ) <EOL> class Context : <EOL> def __init__ ( self , view ) : <EOL> self . locals = set ( ) <EOL> self . includes = set ( ) <EOL> self . view = view <EOL> self . mtime = <NUM_LIT:0> <EOL> self . vid = view . id ( ) <EOL> self . _fileName = None <EOL> contexts [ self . vid ] = self <EOL> def getFileName ( self ) : <EOL> return self . _fileName <EOL> def setFileName ( self , fname ) : <EOL> if self . _fileName is not None : <EOL> del contexts [ self . _fileName ] <EOL> self . _fileName = fname <EOL> contexts [ self . _fileName ] = self <EOL> def changeView ( self , view ) : <EOL> del contexts [ self . vid ] <EOL> self . view = view <EOL> self . vid = view . id ( ) <EOL> contexts [ self . vid ] = self <EOL> def checkintersction ( self , sel , line ) : <EOL> for l in sel : <EOL> selectedLine = self . view . line ( l ) <EOL> if selectedLine . intersects ( line ) : <EOL> return True <EOL> return False <EOL> def ensureFresh ( self ) : <EOL> if os . path . getmtime ( self . _fileName ) > self . mtime : <EOL> if self . view . file_name ( ) is None : <EOL> newView = sublime . active_window ( ) . find_open_file ( self . _fileName ) <EOL> if newView is None : <EOL> scanRequests . add ( self . _fileName ) <EOL> sublime . active_window ( ) . open_file ( self . _fileName ) <EOL> else : <EOL> self . changeView ( newView ) <EOL> self . rescan ( ) <EOL> def getLocals ( self , files ) : <EOL> if self . _fileName in files : <EOL> return set ( ) <EOL> files . add ( self . _fileName ) <EOL> localSet = self . locals <EOL> for include in self . includes : <EOL> localSet = localSet . union ( contexts [ include ] . getLocals ( files ) ) <EOL> return localSet <EOL> def rescan ( self ) : <EOL> self . mtime = time ( ) <EOL> self . locals . clear ( ) <EOL> self . includes . clear ( ) <EOL> if self . view . file_name ( ) is not None : <EOL> self . setFileName ( self . view . file_name ( ) ) <EOL> fileRegion = sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) <EOL> lines = self . view . lines ( fileRegion ) <EOL> for l in lines : <EOL> if self . checkintersction ( self . view . sel ( ) , l ) : <EOL> print ( "<STR_LIT>" ) <EOL> continue <EOL> fullLine = self . view . substr ( l ) <EOL> line = fullLine . strip ( ) <EOL> if len ( line ) == <NUM_LIT:0> : <EOL> continue <EOL> split = line . split ( "<STR_LIT:U+0020>" , maxsplit = <NUM_LIT:1> ) <EOL> if split [ <NUM_LIT:0> ] . casefold ( ) . startswith ( "<STR_LIT>" . casefold ( ) ) and self . view . file_name ( ) is not None : <EOL> print ( "<STR_LIT>" ) <EOL> try : <EOL> fstart = split [ <NUM_LIT:1> ] . index ( '<STR_LIT:">' ) <EOL> fend = split [ <NUM_LIT:1> ] . rindex ( '<STR_LIT:">' ) <EOL> fName = split [ <NUM_LIT:1> ] [ fstart + <NUM_LIT:1> : fend ] <EOL> fName = os . path . join ( os . path . dirname ( self . _fileName ) , fName ) <EOL> if os . path . isfile ( fName ) : <EOL> if fName in contexts : <EOL> print ( "<STR_LIT>" ) <EOL> self . includes . add ( fName ) <EOL> contexts [ fName ] . ensureFresh ( ) <EOL> else : <EOL> newView = sublime . active_window ( ) . find_open_file ( fName ) <EOL> if newView is None : <EOL> scanRequests . add ( fName ) <EOL> sublime . active_window ( ) . open_file ( fName ) <EOL> else : <EOL> newView . set_syntax_file ( "<STR_LIT>" ) <EOL> context = Context ( newView ) <EOL> context . rescan ( ) <EOL> self . includes . add ( fName ) <EOL> except : <EOL> pass <EOL> lskip = fullLine . index ( split [ <NUM_LIT:0> ] ) <EOL> termStart = l . begin ( ) + lskip <EOL> if is_name ( termStart , self . view ) : <EOL> self . locals . add ( ( split [ <NUM_LIT:0> ] . strip ( "<STR_LIT::>" ) , "<STR_LIT:label>" ) ) <EOL> print ( self . locals ) <EOL> class ContextManager ( sublime_plugin . EventListener ) : <EOL> def on_modified_async ( self , view ) : <EOL> global processing <EOL> if not is_asm ( view ) : <EOL> return <EOL> if view . id ( ) in processing or view . file_name ( ) in processing : <EOL> return <EOL> processing [ view . id ( ) ] = True <EOL> if view . file_name ( ) is not None : <EOL> processing [ view . file_name ( ) ] = True <EOL> def timeoutCallback ( ) : <EOL> del processing [ view . id ( ) ] <EOL> if view . file_name ( ) is not None : <EOL> del processing [ view . file_name ( ) ] <EOL> sublime . set_timeout ( timeoutCallback , <NUM_LIT:1000> ) <EOL> if view . id ( ) in contexts or view . file_name ( ) in contexts : <EOL> if view . file_name ( ) in contexts : <EOL> contexts [ view . file_name ( ) ] . changeView ( view ) <EOL> contexts [ view . file_name ( ) ] . rescan ( ) <EOL> else : <EOL> contexts [ view . id ( ) ] . rescan ( ) <EOL> else : <EOL> context = Context ( view ) <EOL> context . rescan ( ) <EOL> def on_load_async ( self , view ) : <EOL> if view . file_name ( ) in scanRequests or view . file_name ( ) in contexts : <EOL> view . set_syntax_file ( "<STR_LIT>" ) <EOL> if is_asm ( view ) : <EOL> if view . file_name ( ) not in contexts : <EOL> print ( "<STR_LIT>" ) <EOL> processing [ view . id ( ) ] = True <EOL> processing [ view . file_name ( ) ] = True <EOL> context = Context ( view ) <EOL> context . rescan ( ) <EOL> del processing [ view . id ( ) ] <EOL> del processing [ view . file_name ( ) ] <EOL> if view . file_name ( ) in scanRequests : <EOL> scanRequests . remove ( view . file_name ( ) ) <EOL> sublime . active_window ( ) . focus_view ( view ) <EOL> sublime . active_window ( ) . run_command ( "<STR_LIT>" ) <EOL> elif view . file_name ( ) in scanRequests : <EOL> processing [ view . id ( ) ] = True <EOL> processing [ view . file_name ( ) ] = True <EOL> contexts [ view . file_name ( ) ] . changeView ( view ) <EOL> contexts [ view . file_name ( ) ] . rescan ( ) <EOL> scanRequests . remove ( view . file_name ( ) ) <EOL> sublime . active_window ( ) . focus_view ( view ) <EOL> sublime . active_window ( ) . run_command ( "<STR_LIT>" ) <EOL> del processing [ view . id ( ) ] <EOL> del processing [ view . file_name ( ) ] </s>
