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 guess_class_name(): ''' Guess the class name based on file name, and replace class name. Imagine you had a file `foo_manager.py` with a class `FooManager` defined in it, and you duplicated it, calling the new file `bar_grokker.py`. If you run `guess-class-name`, it'll replace all instances of `FooManager` in your new file to `BarGrokker`. (It finds the original class name by taking the first class defined in the file. If the file defined multiple classes, you might get the wrong results.) Suggested key combination: `Insert Ctrl-C` ''' app = wingapi.gApplication editor = app.GetActiveEditor() document = editor.GetDocument() file_name_without_extension = \ os.path.split(document.GetFilename())[-1].split('.')[0] guessed_class_name = \ shared.lower_case_to_camel_case(file_name_without_extension) document_text = shared.get_text(document) matches = tuple(re.finditer(existing_class_name_pattern, document_text)) if not matches: return match = matches[-1] existing_class_name = match.group(1) n_occurrences = document_text.count(existing_class_name ) with shared.SelectionRestorer(editor): with shared.UndoableAction(document): document.SetText( document_text.replace(existing_class_name, guessed_class_name) ) app.SetStatusMessage('Replaced %s occurrences' % n_occurrences)
def guess_class_name(): ''' Guess the class name based on file name, and replace class name. Imagine you had a file `foo_manager.py` with a class `FooManager` defined in it, and you duplicated it, calling the new file `bar_grokker.py`. If you run `guess-class-name`, it'll replace all instances of `FooManager` in your new file to `BarGrokker`. (It finds the original class name by taking the first class defined in the file. If the file defined multiple classes, you might get the wrong results.) Suggested key combination: `Insert Ctrl-C` ''' app = wingapi.gApplication editor = app.GetActiveEditor() document = editor.GetDocument() file_name_without_extension = \ os.path.split(document.GetFilename())[-1].split('.')[0] guessed_class_name = \ shared.lower_case_to_camel_case(file_name_without_extension) document_text = shared.get_text(document) matches = tuple(re.finditer(existing_class_name_pattern, document_text)) if not matches: return match = matches[-1] existing_class_name = match.group(1) n_occurrences = document_text.count(existing_class_name) with shared.SelectionRestorer(editor): with shared.UndoableAction(document): document.SetText( document_text.replace(existing_class_name, guessed_class_name)) app.SetStatusMessage('Replaced %s occurrences' % n_occurrences)