def flip_case(editor=wingapi.kArgEditor): ''' Flip the case of the current word between undercase and camelcase. For example, if the cursor is on `something_like_this` and you activate this script, you'll get `SomethingLikeThis`. Do it again and you'll get `something_like_this` again. Suggested key combination: `Insert C` ''' assert isinstance(editor, wingapi.CAPIEditor) document = editor.GetDocument() assert isinstance(document, wingapi.CAPIDocument) with shared.UndoableAction(document): start, end = shared.select_current_word(editor) word = document.GetCharRange(start, end) is_lower_case = word.islower() is_camel_case = not is_lower_case if is_lower_case: new_word = shared.lower_case_to_camel_case(word) else: assert is_camel_case new_word = shared.camel_case_to_lower_case(word) document.DeleteChars(start, end-1) document.InsertChars(start, new_word) editor.SetSelection(start + len(new_word), start + len(new_word))
def instantiate(editor=wingapi.kArgEditor): ''' Write `my_class_name = MyClassName()`. This is used to quickly instantiate a class. Write your class name, like `CatNip`. It will usually be autocompleted. Then execute this script, and you'll have `cat_nip = CatNip()`, with the cursor positioned between the brackes. This saves a lot of typing, because normally you don't have autocompletion for the new instance name `cat_nip` because it doesn't exist yet. Note: The `()` part is added only on Windows. ''' assert isinstance(editor, wingapi.CAPIEditor) document = editor.GetDocument() assert isinstance(document, wingapi.CAPIDocument) with shared.UndoableAction(document): editor.ExecuteCommand('end-of-line') start, end = shared.select_current_word(editor) word = document.GetCharRange(start, end) if '_' in word: raise Exception("Must use `instantiate` when the current word is " "the CamelCase class name. The current word is " "`%s`, and it has an underscore in it, so it's " "not CamelCase.") lower_case_word = shared.camel_case_to_lower_case(word) segment_to_insert = '%s = ' % lower_case_word editor.ExecuteCommand('beginning-of-line-text') current_position, _ = editor.GetSelection() document.InsertChars(current_position, segment_to_insert) editor.ExecuteCommand('end-of-line') if shared.autopy_available: import autopy.key autopy.key.tap('(', autopy.key.MOD_SHIFT)
def deep_to_var(editor=wingapi.kArgEditor): ''' Create a variable from a deep expression. When you're programming, you're often writing lines like these: html_color = self._style_handler.html_color Or: location = context_data['location'] Or: event_handler = super(Foobsnicator, self).get_event_handler() Or: user_profile = models.UserProfile.objects.get(pk=pk) What's common to all these lines is that you're accessing some expression, sometimes a deep one, and then getting an object, and making a variable for that object with the same name that it has in the deep expression. What this `deep-to-var` script will do for you is save you from having to write the `html_color = ` part, which is annoying to type because you don't have autocompletion for it. Just write your deep expression, like `self._style_handler.html_color`, invoke this `deep-to-var` script, and you'll get the full line and have the caret put on the next line. Suggested key combination: `Insert E` ''' assert isinstance(editor, wingapi.CAPIEditor) document = editor.GetDocument() assert isinstance(document, wingapi.CAPIDocument) position, _ = editor.GetSelection() line_number = document.GetLineNumberFromPosition(position) line_start = document.GetLineStart(line_number) line_end = document.GetLineEnd(line_number) line = document.GetCharRange(line_start, line_end) line_stripped = line.strip() variable_name = None match = None for pattern in patterns: match = pattern.search(line_stripped) if match: if pattern in variable_name_map: variable_name = variable_name_map[pattern] else: (variable_name,) = match.groups() break if match: if variable_name != variable_name.lower(): # `variable_name` has an uppercase letter, and thus is probably # camel-case. Let's flip it to underscore: variable_name = shared.camel_case_to_lower_case(variable_name) string_to_insert = '%s = ' % variable_name actual_line_start = line_start + \ string_tools.get_n_identical_edge_characters(line, character=' ') with shared.UndoableAction(document): document.InsertChars(actual_line_start, string_to_insert) new_position = line_end + len(string_to_insert) editor.SetSelection(new_position, new_position) editor.ExecuteCommand('new-line')
def deep_to_var(editor=wingapi.kArgEditor): ''' Create a variable from a deep expression. When you're programming, you're often writing lines like these: html_color = self._style_handler.html_color Or: location = context_data['location'] Or: event_handler = super(Foobsnicator, self).get_event_handler() Or: user_profile = models.UserProfile.objects.get(pk=pk) What's common to all these lines is that you're accessing some expression, sometimes a deep one, and then getting an object, and making a variable for that object with the same name that it has in the deep expression. What this `deep-to-var` script will do for you is save you from having to write the `html_color = ` part, which is annoying to type because you don't have autocompletion for it. Just write your deep expression, like `self._style_handler.html_color`, invoke this `deep-to-var` script, and you'll get the full line and have the caret put on the next line. Suggested key combination: `Insert E` ''' assert isinstance(editor, wingapi.CAPIEditor) document = editor.GetDocument() assert isinstance(document, wingapi.CAPIDocument) position, _ = editor.GetSelection() line_number = document.GetLineNumberFromPosition(position) line_start = document.GetLineStart(line_number) line_end = document.GetLineEnd(line_number) line = document.GetCharRange(line_start, line_end) line_stripped = line.strip() variable_name = None match = None for pattern in patterns: match = pattern.search(line_stripped) if match: if pattern in variable_name_map: variable_name = variable_name_map[pattern] else: (variable_name, ) = match.groups() break if match: if variable_name != variable_name.lower(): # `variable_name` has an uppercase letter, and thus is probably # camel-case. Let's flip it to underscore: variable_name = shared.camel_case_to_lower_case(variable_name) string_to_insert = '%s = ' % variable_name actual_line_start = line_start + \ string_tools.get_n_identical_edge_characters(line, character=' ') with shared.UndoableAction(document): document.InsertChars(actual_line_start, string_to_insert) new_position = line_end + len(string_to_insert) editor.SetSelection(new_position, new_position) editor.ExecuteCommand('new-line')