<commit_before># Copyright 2009-2011 <NAME>.
# This program is distributed under the LGPL2.1 license.

'''
This module defines the `instantiate` script.

See its documentation for more information.
'''

from __future__ import with_statement

import os.path, sys; sys.path.append(os.path.dirname(__file__))

import wingapi

import shared


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 inside the
    parentheses for you to write the arguments.
    '''
    
    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)
        
        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
            
        document.InsertChars(start, segment_to_insert)

        position = end + len(segment_to_insert)
        
        editor.SetSelection(position, position)
        editor.PasteTemplate(
            '()',
            (
                (1, 1),
            )
        )
        
        <commit_msg>Improving instantiate, not done yet<commit_after># Copyright 2009-2011 <NAME>.
# This program is distributed under the LGPL2.1 license.

'''
This module defines the `instantiate` script.

See its documentation for more information.
'''

from __future__ import with_statement

import os.path, sys; sys.path.append(os.path.dirname(__file__))

import wingapi

import shared


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 inside the
    parentheses for you to write the arguments.
    '''
    
    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')
        
        current_position, _ = editor.GetSelection()
        
        document.InsertChars(current_position, '(')
        
        
{'fraction_non_alphanumeric': 0.08471849865951743, 'fraction_numerical': 0.005898123324396783, 'mean_word_length': 2.2901234567901234, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '79734', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1015, 'n_words': 294}