<s> import sublime , sublime_plugin , os , re <EOL> class StyleSheetSetup : <EOL> def __init__ ( self , extensions , regex , partials = None , index = None ) : <EOL> if partials is None : <EOL> self . partials = False <EOL> else : <EOL> self . partials = partials <EOL> if index is None : <EOL> self . index = False <EOL> else : <EOL> self . index = index <EOL> self . extensions = extensions <EOL> self . regex = regex <EOL> class ListStylesheetVariables ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> settings = sublime . load_settings ( '<STR_LIT>' ) <EOL> handle_imports = settings . get ( "<STR_LIT>" ) <EOL> read_all_views = settings . get ( "<STR_LIT>" ) <EOL> less_setup = StyleSheetSetup ( ( b'<STR_LIT>' , b'<STR_LIT>' ) , "<STR_LIT>" ) <EOL> sass_setup = StyleSheetSetup ( ( b'<STR_LIT>' , b'<STR_LIT>' ) , "<STR_LIT>" , True ) <EOL> stylus_setup = StyleSheetSetup ( ( b'<STR_LIT>' , ) , "<STR_LIT>" , False , True ) <EOL> sass_erb_setup = StyleSheetSetup ( ( b'<STR_LIT>' , b'<STR_LIT>' ) , "<STR_LIT>" , True ) <EOL> setups = ( less_setup , sass_setup , stylus_setup , sass_erb_setup ) <EOL> chosen_setup = None <EOL> self . edit = edit <EOL> fn = self . view . file_name ( ) . encode ( "<STR_LIT>" ) <EOL> for setup in setups : <EOL> for ext in setup . extensions : <EOL> if fn . endswith ( ext ) : <EOL> chosen_setup = setup <EOL> if chosen_setup == None : <EOL> return <EOL> imports = [ ] <EOL> imported_vars = [ ] <EOL> compiled_regex = re . compile ( chosen_setup . regex , re . MULTILINE ) <EOL> if handle_imports : <EOL> self . view . find_all ( "<STR_LIT>" , <NUM_LIT:0> , "<STR_LIT>" , imports ) <EOL> file_dir = os . path . dirname ( fn ) . decode ( "<STR_LIT:utf-8>" ) <EOL> for i , filename in enumerate ( imports ) : <EOL> has_extension = False <EOL> for ext in chosen_setup . extensions : <EOL> if filename . endswith ( ext . decode ( "<STR_LIT:utf-8>" ) ) : <EOL> has_extension = True <EOL> if has_extension == False : <EOL> for ext in chosen_setup . extensions : <EOL> ext = ext . decode ( "<STR_LIT:utf-8>" ) <EOL> if os . path . isfile ( os . path . normpath ( file_dir + '<STR_LIT:/>' + filename + ext ) ) : <EOL> filename += ext <EOL> break <EOL> if chosen_setup . partials : <EOL> fn_split = os . path . split ( filename ) <EOL> partial_filename = fn_split [ <NUM_LIT:0> ] + "<STR_LIT>" + fn_split [ <NUM_LIT:1> ] <EOL> if os . path . isfile ( os . path . normpath ( file_dir + partial_filename + ext ) ) : <EOL> filename = "<STR_LIT:_>" + filename + ext <EOL> break <EOL> if chosen_setup . index and os . path . isfile ( os . path . normpath ( file_dir + "<STR_LIT:/>" + filename + "<STR_LIT>" + ext ) ) : <EOL> filename += "<STR_LIT>" + ext <EOL> try : <EOL> f = open ( os . path . normpath ( file_dir + '<STR_LIT:/>' + filename ) , '<STR_LIT:r>' ) <EOL> contents = f . read ( ) <EOL> f . close ( ) <EOL> m = re . findall ( compiled_regex , contents ) <EOL> imported_vars = imported_vars + m <EOL> except : <EOL> print ( '<STR_LIT>' + filename ) <EOL> imported_vars = [ list ( item ) for item in imported_vars ] <EOL> self . variables = [ ] <EOL> vars_from_views = [ ] <EOL> if read_all_views : <EOL> for view in self . view . window ( ) . views ( ) : <EOL> viewfn = self . view . file_name ( ) . encode ( "<STR_LIT:utf-8>" ) <EOL> compatible_view = False <EOL> for ext in chosen_setup . extensions : <EOL> if viewfn . endswith ( ext ) : <EOL> viewvars = [ ] <EOL> view . find_all ( chosen_setup . regex , <NUM_LIT:0> , "<STR_LIT>" , viewvars ) <EOL> vars_from_views += viewvars <EOL> break ; <EOL> else : <EOL> self . view . find_all ( chosen_setup . regex , <NUM_LIT:0> , "<STR_LIT>" , self . variables ) <EOL> self . variables += vars_from_views <EOL> self . variables = list ( set ( self . variables ) ) <EOL> for i , val in enumerate ( self . variables ) : <EOL> self . variables [ i ] = val . split ( "<STR_LIT:|>" ) <EOL> self . variables = imported_vars + self . variables <EOL> self . variables . sort ( ) <EOL> self . view . window ( ) . show_quick_panel ( self . variables , self . insert_variable , sublime . MONOSPACE_FONT ) <EOL> def insert_variable ( self , choice ) : <EOL> if choice == - <NUM_LIT:1> : <EOL> return <EOL> self . view . run_command ( '<STR_LIT>' , { '<STR_LIT:string>' : self . variables [ choice ] [ <NUM_LIT:0> ] } ) <EOL> class InsertText ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , string = '<STR_LIT>' ) : <EOL> for selection in self . view . sel ( ) : <EOL> self . view . insert ( edit , selection . begin ( ) , string ) </s>
