def unpack_tuple_to_one(editor=wingapi.kArgEditor): ''' Turn `things` into `(thing,)`. Useful for writing things like: (thing,) == things See this blog post for more context: http://blog.ram.rachum.com/post/1198230058/python-idiom-for-taking-the-single-item-from-a-list Suggested key combination: `Insert U` ''' assert isinstance(editor, wingapi.CAPIEditor) document = editor.GetDocument() assert isinstance(document, wingapi.CAPIDocument) with shared.UndoableAction(document): start, end = shared.select_current_word(editor) plural_word = document.GetCharRange(start, end) if not plural_word.endswith('s'): return singular_word = shared.plural_word_to_singular_word(plural_word) segment_to_insert = '(%s,)' % singular_word document.DeleteChars(start, end - 1) document.InsertChars(start, segment_to_insert) new_position = start + len(segment_to_insert) editor.SetSelection(new_position, new_position)
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 arg_to_attr(editor=wingapi.kArgEditor): ''' Turn an argument to `__init__` into an instance attribute. For example, you have just typed this: class MyObject(object): def __init__(self, crunchiness): <Cursor is here> (Of course, you can substitute `crunchiness` for whatever your argument's name is.) Now, you might want to put a `self.crunchiness = crunchiness` line in that `__init__` method. But that would take so much typing, because `self.crunchiness` doesn't exist yet, and won't be autocompleted. That would require you to make around 20 keystrokes. I don't know about you, but I'm just not ready for that kind of a commitment. Instead, type `crunchiness`. (You'll get autocompletion because it exists as an argument.) Then run this `arg_to_attr` script. (I personally use `Insert A` for it.) The final result is that you'll get a `self.crunchiness = crunchiness` line and have the cursor ready in the next line. Suggested key combination: `Insert A` ''' assert isinstance(editor, wingapi.CAPIEditor) document = editor.GetDocument() assert isinstance(document, wingapi.CAPIDocument) with shared.UndoableAction(document): start, end = shared.select_current_word(editor) variable_name = document.GetCharRange(start, end) result_string = 'self.%s = %s' % (variable_name, variable_name) document.DeleteChars(start, end - 1) document.InsertChars(start, result_string) editor.SetSelection(start + len(result_string), start + len(result_string)) editor.ExecuteCommand('new-line')