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
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()