<s> import sublime <EOL> import sublime_plugin <EOL> from . . settings import get_setting <EOL> class InsertPhpConstructorPropertyCommand ( sublime_plugin . TextCommand ) : <EOL> '<STR_LIT>' <EOL> placeholder = '<STR_LIT>' <EOL> def description ( self ) : <EOL> '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> def is_enabled ( self ) : <EOL> '<STR_LIT>' <EOL> return '<STR_LIT>' in self . view . settings ( ) . get ( '<STR_LIT>' ) . lower ( ) <EOL> def run ( self , edit ) : <EOL> '<STR_LIT>' <EOL> self . edit = edit <EOL> self . regions = [ ] <EOL> self . visibility = get_setting ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . add_property ( self . placeholder ) <EOL> self . add_constructor ( self . placeholder ) <EOL> sel = self . view . sel ( ) <EOL> sel . clear ( ) <EOL> sel . add_all ( self . regions ) <EOL> self . view . show ( sel ) <EOL> def add_property ( self , prop_name ) : <EOL> '<STR_LIT>' <EOL> text = prop_name + "<STR_LIT:;>" <EOL> properties = self . find_properties ( ) <EOL> if properties : <EOL> pos = properties [ - <NUM_LIT:1> ] . end ( ) <EOL> else : <EOL> pos = self . find_class_opening_bracket ( ) <EOL> text += "<STR_LIT:\n>" <EOL> pos += self . view_insert ( pos , "<STR_LIT>" + self . visibility + "<STR_LIT>" ) <EOL> self . view_insert ( pos , text ) <EOL> cursor_start = pos <EOL> cursor_end = cursor_start + len ( prop_name ) <EOL> self . add_region ( cursor_start , cursor_end ) <EOL> def add_constructor ( self , prop_name ) : <EOL> '<STR_LIT>' <EOL> constructor = self . view . find ( r'<STR_LIT>' , <NUM_LIT:0> ) <EOL> if constructor : <EOL> constructor_start = constructor . end ( ) <EOL> constructor_end = self . view . find ( r'<STR_LIT>' , constructor_start ) . begin ( ) <EOL> else : <EOL> text = "<STR_LIT>" <EOL> properties = self . find_properties ( ) <EOL> if properties : <EOL> pos = properties [ - <NUM_LIT:1> ] . end ( ) <EOL> text = "<STR_LIT:\n>" + text <EOL> else : <EOL> pos = self . find_class_opening_bracket ( ) <EOL> self . view_insert ( pos , text ) <EOL> constructor = self . view . find ( r'<STR_LIT>' , <NUM_LIT:0> ) <EOL> constructor_start = constructor_end = constructor . end ( ) - <NUM_LIT:1> <EOL> constructor_args = self . view . substr ( sublime . Region ( constructor_start , constructor_end ) ) <EOL> arg_pos = constructor_end <EOL> text = "<STR_LIT:$>" + prop_name <EOL> last_newline = self . view . find_by_class ( constructor_end , False , sublime . CLASS_LINE_END ) <EOL> last_word = self . view . find_by_class ( constructor_end , False , sublime . CLASS_SUB_WORD_START ) <EOL> is_multiline_constructor = last_newline > last_word <EOL> if is_multiline_constructor : <EOL> arg_pos = last_newline <EOL> if constructor_args . strip ( ) != '<STR_LIT>' : <EOL> arg_pos += self . view_insert ( arg_pos , "<STR_LIT:U+002C>" ) <EOL> arg_pos += self . view_insert ( arg_pos , "<STR_LIT>" ) <EOL> cursor_start = arg_pos + <NUM_LIT:1> <EOL> else : <EOL> cursor_start = constructor_end + <NUM_LIT:1> <EOL> if constructor_args . strip ( ) != '<STR_LIT>' : <EOL> text = "<STR_LIT:U+002CU+0020>" + text <EOL> cursor_start += <NUM_LIT:2> <EOL> self . view_insert ( arg_pos , text ) <EOL> cursor_end = cursor_start + len ( prop_name ) <EOL> self . add_region ( cursor_start , cursor_end ) <EOL> constructor_close = self . view . find ( r'<STR_LIT>' , constructor_end ) . begin ( ) <EOL> last_newline = self . view . find_by_class ( constructor_close , False , sublime . CLASS_LINE_START ) <EOL> cursor_start = last_newline + self . view_insert ( last_newline , "<STR_LIT>" ) <EOL> self . view_insert ( cursor_start , prop_name + '<STR_LIT>' + prop_name + "<STR_LIT>" ) <EOL> cursor_end = cursor_start + len ( prop_name ) <EOL> self . add_region ( cursor_start , cursor_end ) <EOL> cursor_start = cursor_end + <NUM_LIT:4> <EOL> cursor_end = cursor_start + len ( prop_name ) <EOL> self . add_region ( cursor_start , cursor_end ) <EOL> def find_class_opening_bracket ( self ) : <EOL> '<STR_LIT>' <EOL> pos = self . view . find ( r'<STR_LIT>' , <NUM_LIT:0> ) . end ( ) <EOL> return self . view . find ( r'<STR_LIT>' , pos ) . end ( ) <EOL> def find_properties ( self ) : <EOL> '<STR_LIT>' <EOL> return self . view . find_all ( r'<STR_LIT>' ) <EOL> def add_region ( self , start , end ) : <EOL> '<STR_LIT>' <EOL> self . regions . append ( sublime . Region ( start , end ) ) <EOL> def view_insert ( self , pos , text ) : <EOL> '<STR_LIT>' <EOL> return self . view . insert ( self . edit , pos , text ) </s>
