Example #1
0
def act(controller, bundle, options):
    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)
    
    direction = cp.get_option(options, 'direction', 'down')
    duplicate = cp.get_option(options, 'duplicate', False)
    
    text, target_range = cp.lines_and_range(context)
    select_range = cp.get_range(context)
    
    if duplicate:
        cloned = text
        
        if not cloned.endswith(line_ending):
            cloned += line_ending
        
        if direction.lower() == 'down':
            select_range = cp.new_range(select_range.location + len(cloned), select_range.length)
        
        text = cloned + text
    else:
        if direction.lower() == 'down':
            line_after = cp.get_line_after(context, target_range)
            if line_after is None: return
            
            # we care about the original length of line after, not the balanced one we'll get in a second
            len_line_after = len(line_after)
            
            line_after, text = cp.balance_line_endings(line_after, text, line_ending)
            line_delta = len(line_after) - len_line_after
            
            select_start = select_range.location + len(line_after)
            select_end = min(select_start + select_range.length, len(context.string()))
            
            text = line_after + text
            
            select_range = cp.new_range(select_start, max(0,select_end - select_start))
            target_range = cp.new_range(target_range.location, max(0, target_range.length + len(line_after) - (len(line_after) - len_line_after)))
        else:
            line_before = cp.get_line_before(context, target_range)
            if line_before is None: return
            
            # we care about the original length of line before, not the balanced one we'll get in a second
            len_line_before = len(line_before)
            
            text, line_before = cp.balance_line_endings(text, line_before, line_ending)
            
            text = text + line_before
            select_range = cp.new_range(select_range.location - len_line_before, select_range.length)
            target_range = cp.new_range(target_range.location - len_line_before, target_range.length + len_line_before)
    
    cp.insert_text_and_select(context, text, target_range, select_range)
def act(controller, bundle, options):
    '''
    Required action method
    
    if supplied, message will be written instead of Hello World
    
    Setting replace=True replace the current selection instead of inserting
    '''
    
    context = cp.get_context(controller)
    
    message = cp.get_option(options, 'message', 'Hello World')
    replace_selection = cp.get_option(options, 'replace', False)
    
    range = cp.get_range(context)
    
    if not replace_selection:
        range = cp.new_range(range.location, 0)
    
    cp.insert_text(context, message, range)
Example #3
0
def act(controller, bundle, options):
    '''
    Required action method
    '''

    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)

    select_range = range = cp.get_range(context)
    if range.length == 0:
        range = cp.new_range(0, len(context.string()))

    lines, range = cp.lines_and_range(context, range)

    newlines = line_ending.join([re.sub("^(.*?)\s*([;,]?)\s*$", '\\1\\2', x) for x in lines.split(line_ending)])

    if select_range.length == 0:
        prefix = line_ending.join([re.sub("^(.*?)\s*([;,]?)\s*$", '\\1\\2', x) for x in lines[0:select_range.location].split(line_ending)])
        cp.insert_text_and_select(context, newlines, range, cp.new_range(len(prefix), 0))
    else:
        cp.insert_text_and_select(context, newlines, range, cp.new_range(range.location, len(newlines)))