<s> import os <EOL> import sublime <EOL> class HierarchyView ( object ) : <EOL> def __init__ ( self , name , window = None ) : <EOL> self . name = name <EOL> self . window = window if window else sublime . active_window ( ) <EOL> self . file_lines = dict ( ) <EOL> self . set_view ( ) <EOL> def set_content ( self , content ) : <EOL> self . view . set_read_only ( False ) <EOL> edit = self . view . begin_edit ( ) <EOL> self . view . insert ( edit , <NUM_LIT:0> , content ) <EOL> self . view . end_edit ( edit ) <EOL> self . fold_files ( content ) <EOL> self . view . set_scratch ( True ) <EOL> self . view . set_read_only ( True ) <EOL> def empty_view ( self ) : <EOL> self . view . set_read_only ( False ) <EOL> e = self . view . begin_edit ( ) <EOL> self . view . erase ( e , sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> self . view . end_edit ( e ) <EOL> self . view . set_read_only ( True ) <EOL> def set_view ( self ) : <EOL> for view in self . window . views ( ) : <EOL> if view . name ( ) == self . name : <EOL> self . view = view <EOL> self . empty_view ( ) <EOL> self . window . focus_view ( self . view ) <EOL> return <EOL> self . view = self . window . new_file ( ) <EOL> self . view . set_name ( self . name ) <EOL> self . view . set_syntax_file ( '<STR_LIT>' ) <EOL> def fold_files ( self , content ) : <EOL> """<STR_LIT>""" <EOL> current_class = None <EOL> for line_number , line in enumerate ( content . split ( "<STR_LIT:\n>" ) ) : <EOL> line = line . strip ( ) <EOL> if not line . startswith ( "<STR_LIT:|>" ) : <EOL> self . file_lines [ line_number ] = [ ] <EOL> current_class = self . file_lines [ line_number ] <EOL> else : <EOL> current_class . append ( line_number ) <EOL> for class_line in self . file_lines . keys ( ) : <EOL> self . toggle_class_file_lines ( class_line ) <EOL> def get_file_region ( self , row ) : <EOL> file_lines = None <EOL> try : <EOL> file_lines = self . file_lines [ row ] <EOL> except KeyError : <EOL> for lines in self . file_lines . values ( ) : <EOL> if row in lines : <EOL> file_lines = lines <EOL> break <EOL> if len ( file_lines ) == <NUM_LIT:0> : <EOL> return None <EOL> region_from = self . view . text_point ( min ( file_lines ) , <NUM_LIT:0> ) - <NUM_LIT:1> <EOL> region_to = self . view . text_point ( max ( file_lines ) + <NUM_LIT:1> , <NUM_LIT:0> ) - <NUM_LIT:1> <EOL> if region_to + <NUM_LIT:1> == self . view . size ( ) : <EOL> region_to += <NUM_LIT:1> <EOL> return sublime . Region ( region_from , region_to ) <EOL> def toggle_class_file_lines ( self , row ) : <EOL> file_region = self . get_file_region ( row ) <EOL> if file_region : <EOL> if not self . view . unfold ( file_region ) : <EOL> if row in self . file_lines . keys ( ) : <EOL> self . view . fold ( file_region ) <EOL> def move_to_file ( self , row ) : <EOL> file_path = self . get_file_path_in_row ( row ) <EOL> if file_path : <EOL> window = sublime . active_window ( ) <EOL> class_view = window . open_file ( os . path . join ( window . folders ( ) [ <NUM_LIT:0> ] , file_path ) ) <EOL> self . highlight_class_name ( class_view , self . get_class_name_for_row ( row ) ) <EOL> def get_class_name_for_row ( self , row ) : <EOL> for class_line , file_lines in self . file_lines . iteritems ( ) : <EOL> if row == class_line or row in file_lines : <EOL> return self . get_text_in_row ( class_line ) . strip ( ) <EOL> return None <EOL> def get_file_path_in_row ( self , row ) : <EOL> for lines in self . file_lines . values ( ) : <EOL> if row in lines : <EOL> return self . get_text_in_row ( row ) . strip ( ) [ <NUM_LIT:1> : ] <EOL> return "<STR_LIT>" <EOL> def get_text_in_row ( self , row ) : <EOL> return self . view . substr ( self . view . line ( self . view . text_point ( row , <NUM_LIT:0> ) ) ) <EOL> def highlight_class_name ( self , view , class_name ) : <EOL> def load_finished ( ) : <EOL> class_regex = "<STR_LIT>" % class_name <EOL> class_region = view . find ( class_regex , <NUM_LIT:0> ) <EOL> if class_region : <EOL> view . show ( class_region ) <EOL> view . add_regions ( "<STR_LIT>" , [ class_region ] , '<STR_LIT:source>' , '<STR_LIT>' , sublime . DRAW_OUTLINED ) <EOL> sublime . set_timeout ( lambda : view . erase_regions ( "<STR_LIT>" ) , <NUM_LIT> ) <EOL> def check_if_loaded ( ) : <EOL> if view . is_loading ( ) : <EOL> sublime . set_timeout ( lambda : check_if_loaded ( ) , <NUM_LIT:100> ) <EOL> else : <EOL> load_finished ( ) ; <EOL> sublime . set_timeout ( lambda : check_if_loaded ( ) , <NUM_LIT:100> ) </s>
