Esempio n. 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', 'right')
    
    line_text, line_range = cp.lines_and_range(context)
    selection, select_range = cp.selection_and_range(context)
    if select_range.length == 0:
        selection, select_range = cp.words_and_range(context)
    
    cp.say(context, 'word(s)', '||%s||' % selection)
    return
    
    if direction.lower() == 'left':
        prefix = line_text[:(select_range.location - line_range.location)]
        
        if not prefix.strip():
            cp.beep()
            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)
Esempio n. 2
0
def act(controller, bundle, options):
    '''
    Required action method
    
    Supplying a lang option will override the automatic language guessing
    (which might not be such a bad thing...)
    '''
    
    context = cp.get_context(controller)

    lang = cp.get_option(options, 'lang', 'auto').lower()
    
    # get the file extension so we can guess the language.
    if lang == 'auto':
        path = context.path()
        if path is not None:
            pos = path.rfind('.')
            if pos != -1:
                lang = path[pos+1:]
    
    d = Docblock.get(lang)
    
    # get the current line
    text, target_range = cp.lines_and_range(context)
    
    # keep going until we find a non-empty line to document (up to X lines below the current line)
    tries_left = 3
    while tries_left and not text.strip():
        text, target_range = cp.get_line_after_and_range(context, target_range)
        
        if text is None:
            # we're at the end of the document?
            cp.beep()
            return
        
        tries_left -= 1
    
    insert_range = cp.new_range(target_range.location, 0)
    
    d.setLineEnding(cp.get_line_ending(context))
    docblock = d.doc(text)
    
    if docblock:
        cp.insert_text_with_insertion_point(context, docblock, insert_range)
    else:
        cp.beep()
def act(controller, bundle, options):
    '''
    Required action method
    
    Supplying a lang option will override the automatic language guessing
    (which might not be such a bad thing...)
    '''

    context = cp.get_context(controller)

    lang = cp.get_option(options, 'lang', 'auto').lower()

    # get the file extension so we can guess the language.
    if lang == 'auto':
        path = context.path()
        if path is not None:
            pos = path.rfind('.')
            if pos != -1:
                lang = path[pos + 1:]

    d = Docblock.get(lang)

    # get the current line
    text, target_range = cp.lines_and_range(context)

    # keep going until we find a non-empty line to document (up to X lines below the current line)
    tries_left = 3
    while tries_left and not text.strip():
        text, target_range = cp.get_line_after_and_range(context, target_range)

        if text is None:
            # we're at the end of the document?
            cp.beep()
            return

        tries_left -= 1

    insert_range = cp.new_range(target_range.location, 0)

    d.setLineEnding(cp.get_line_ending(context))
    docblock = d.doc(text)

    if docblock:
        cp.insert_text_with_insertion_point(context, docblock, insert_range)
    else:
        cp.beep()
Esempio n. 4
0
def act(controller, bundle, options):
    '''
    Required action method
    '''

    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)
    lines, range = cp.lines_and_range(context)

    if lines.endswith(line_ending):
        lines = lines[:-len(line_ending)]
        range = cp.new_range(range.location, range.length - len(line_ending))

    try:
        newlines = line_ending.join(wrap_comment(
            lines.split(line_ending),
            context.tabWidth(),
            cp.get_option(options, 'width'),
            cp.get_option(options, 'min_width')
        ))
        cp.insert_text_and_select(context, newlines, range, cp.new_range(range.location, len(newlines)))
    except:
        cp.beep()
        raise