Example #1
0
def block_smart_extend(has_selection, start, end):
    end = end.copy()
    if not end.is_end():
        end.backward_lines(1)

    start_ws = len(get_whitespace(start))
    prev_empty = start.is_start() or line_is_empty(prev_line(start))
    prev_ws = len(get_whitespace(prev_line(start)))

    end_ws = len(get_whitespace(end))
    next_empty = end.is_end() or line_is_empty(next_line(end))
    next_ws = len(get_whitespace(next_line(end)))

    newstart, newend = start.copy(), end

    if not has_selection and start.get_line() == end.get_line() and \
            ( next_empty or next_ws < end_ws ) and (prev_empty or prev_ws < start_ws):
        pass
    elif not prev_empty and not next_empty and prev_ws == start_ws == next_ws == end_ws:
        newstart = extend_without_gap(start, start_ws, -1)
        newend = extend_without_gap(end, end_ws, 1)
    elif prev_empty and not next_empty and next_ws == end_ws:
        newend = extend_without_gap(end, end_ws, 1)
    elif not prev_empty and next_empty and prev_ws == start_ws:
        newstart = extend_without_gap(start, start_ws, -1)
    elif not next_empty and next_ws > start_ws:
        newend = extend_block_without_gap(end, start_ws, 1)
    elif ( not next_empty and next_ws == start_ws ) or ( not prev_empty and prev_ws >= start_ws ):
        if not prev_empty:
            newstart = extend_without_gap(start, start_ws, -1)
        if not next_empty:
            newend = extend_without_gap(end, start_ws, 1)
    elif next_empty and prev_empty:
        newstart = extend_with_gap(start, start_ws, -1)
        newend = extend_with_gap(end, start_ws, 1)
    elif next_empty and not prev_empty and prev_ws < start_ws:
        newend = extend_with_gap(end, start_ws, 1)

    if has_selection and start.equal(newstart) and end.equal(newend):
        if not prev_empty:
            newstart.backward_lines(1)
        else:
            ne = get_next_not_empty_line(start, -1)
            if ne:
                newstart = ne

        if not next_empty and len(line_text(next_line(end)).strip()) < 5:
            newend.forward_lines(1)

    newend.forward_lines(1)
    return newstart, newend
Example #2
0
def extend_with_gap(from_iter, ws, delta):
    n = None
    for p, n in iter_lines(from_iter, delta):
        if line_is_empty(n):
            ne = get_next_not_empty_line(n, delta)
            if ne and len(get_whitespace(ne)) >= ws:
                n.set_line(ne.get_line())
            else:
                return p

        n_ws = len(get_whitespace(n))
        if n_ws < ws:
            return p

    return n if n else from_iter.copy()
Example #3
0
def wrap_text(editor):
    buf = editor.buffer
    if not buf.get_has_selection():
        editor.message('Select text block to wrap')
        return

    import textwrap, re
    from util import get_whitespace

    start, end = buf.get_selection_bounds()
    start.order(end)
    if end.starts_line():
        end.backward_visible_cursor_position()

    si = ''
    second_line = start.copy()
    second_line.set_line(start.get_line() + 1)
    if second_line.get_offset() < end.get_offset():
        si = get_whitespace(second_line)

    text = buf.get_text(start, end).decode('utf-8')
    text = re.sub('(?m)^\s+', '', text)
    text = textwrap.fill(text, subsequent_indent=si ,width=editor.view.get_right_margin_position())

    buf.begin_user_action()
    buf.place_cursor(end)
    buf.delete(start, end)
    buf.insert_at_cursor(text)
    buf.end_user_action()
Example #4
0
def extend_block_without_gap(from_iter, ws, delta):
    n = None
    for p, n in iter_lines(from_iter, delta):
        if line_is_empty(n):
            ne = get_next_not_empty_line(n, delta)
            if ne:
                n.set_line(ne.get_line())
            else:
                return p

        n_ws = len(get_whitespace(n))

        if n_ws < ws or ( n_ws == ws and len(line_text(n).strip()) > 4 ):
            return p

    return n if n else from_iter.copy()