<s> """<STR_LIT>""" <EOL> import sublime , sublime_plugin , re <EOL> class GotoCssDeclarationCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , goto ) : <EOL> """<STR_LIT>""" <EOL> settings = sublime . load_settings ( '<STR_LIT>' ) <EOL> css_files = settings . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> win = sublime . active_window ( ) <EOL> view = self . view <EOL> self . cur_pos = view . sel ( ) [ <NUM_LIT:0> ] . a <EOL> self . scope_reg = view . extract_scope ( self . cur_pos ) <EOL> if goto == "<STR_LIT>" : <EOL> cmp = lambda x , y : x if x . a < y . a else y <EOL> else : <EOL> cmp = lambda x , y : x if x . a > y . a else y <EOL> def is_css ( view ) : <EOL> """<STR_LIT>""" <EOL> file_type = re . match ( '<STR_LIT>' , view . file_name ( ) or "<STR_LIT>" ) <EOL> file_type = file_type . group ( <NUM_LIT:1> ) if file_type else "<STR_LIT>" <EOL> return file_type in css_files <EOL> def set_settings_for_all_tabs ( ) : <EOL> """<STR_LIT:U+0020>""" <EOL> views = win . views ( ) <EOL> for view in views : <EOL> if is_css ( view ) : <EOL> view . settings ( ) . set ( '<STR_LIT>' , self . class_or_id ) <EOL> def nearest ( region1 , region2 ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if None in ( region1 , region2 ) : <EOL> return region1 or region2 <EOL> if region1 . a == - <NUM_LIT:1> and region1 . b == - <NUM_LIT:1> : <EOL> return region2 <EOL> if region2 . a == - <NUM_LIT:1> and region2 . b == - <NUM_LIT:1> : <EOL> return region1 <EOL> return cmp ( region1 , region2 ) <EOL> def goto_region ( view , region ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if region : <EOL> sel = view . sel ( ) <EOL> sel . clear ( ) <EOL> sel . add ( region ) <EOL> view . show ( region ) <EOL> return True <EOL> else : <EOL> return False <EOL> def goto_decl ( view , find_in_other = None ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if not is_css ( view ) : <EOL> return False <EOL> pos = self . cur_pos <EOL> if goto == "<STR_LIT>" : <EOL> if find_in_other : <EOL> pos = - <NUM_LIT:1> <EOL> return goto_region ( view , <EOL> nearest ( <EOL> view . find ( "<STR_LIT>" + self . class_or_id , pos + <NUM_LIT:1> ) , <EOL> view . find ( "<STR_LIT>" + self . class_or_id , pos + <NUM_LIT:1> ) <EOL> ) ) <EOL> elif goto == "<STR_LIT>" : <EOL> def previous ( regions ) : <EOL> all_prev = [ r for r in regions if r . a < pos ] <EOL> if all_prev : <EOL> return all_prev [ - <NUM_LIT:1> ] <EOL> if find_in_other : <EOL> pos = view . size ( ) <EOL> return goto_region ( view , <EOL> nearest ( <EOL> previous ( view . find_all ( "<STR_LIT>" + self . class_or_id ) ) , <EOL> previous ( view . find_all ( "<STR_LIT>" + self . class_or_id ) ) <EOL> ) ) <EOL> def goto_decl_in_other_file ( view ) : <EOL> """<STR_LIT:U+0020>""" <EOL> views = win . views ( ) <EOL> vi = [ v . id ( ) for v in views ] . index ( view . id ( ) ) <EOL> prev = views [ : vi ] <EOL> next = views [ vi + <NUM_LIT:1> : ] <EOL> if goto == "<STR_LIT>" : <EOL> views = next <EOL> views . extend ( prev ) <EOL> elif goto == "<STR_LIT>" : <EOL> views = prev <EOL> views . reverse ( ) <EOL> next . reverse ( ) <EOL> views . extend ( next ) <EOL> for view in views : <EOL> if goto_decl ( view , "<STR_LIT>" ) : <EOL> win . focus_view ( view ) <EOL> return <EOL> else : <EOL> sublime . status_message ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + self . class_or_id + '<STR_LIT:">' <EOL> ) <EOL> if is_css ( view ) : <EOL> self . class_or_id = view . settings ( ) . get ( '<STR_LIT>' ) <EOL> else : <EOL> self . class_or_id = self . get_class_or_id ( ) ; <EOL> if not self . class_or_id : <EOL> return <EOL> set_settings_for_all_tabs ( ) <EOL> if not goto_decl ( view ) : <EOL> goto_decl_in_other_file ( view ) <EOL> def get_class_or_id ( self ) : <EOL> """<STR_LIT>""" <EOL> def get_sym ( pos ) : <EOL> """<STR_LIT>""" <EOL> sym = self . view . substr ( sublime . Region ( pos , pos + <NUM_LIT:1> ) ) <EOL> return sym <EOL> rule_type = set ( "<STR_LIT>" . split ( "<STR_LIT:U+0020>" ) ) <EOL> delims = set ( [ "<STR_LIT:U+0020>" ] + "<STR_LIT>" . split ( "<STR_LIT:U+0020>" ) ) <EOL> all_delims = rule_type | delims <EOL> left = self . cur_pos <EOL> if get_sym ( left ) in rule_type : <EOL> left += <NUM_LIT:1> <EOL> if get_sym ( left ) in delims : <EOL> left -= <NUM_LIT:1> <EOL> while left >= self . scope_reg . a and get_sym ( left ) not in all_delims : <EOL> left -= <NUM_LIT:1> <EOL> left += <NUM_LIT:1> <EOL> right = self . cur_pos if self . cur_pos >= left else left <EOL> while right < self . scope_reg . b and get_sym ( right ) not in all_delims : <EOL> right += <NUM_LIT:1> <EOL> word = self . view . substr ( sublime . Region ( left , right ) ) <EOL> match = re . match ( '<STR_LIT>' , word ) <EOL> return word if match else None </s>
