def a_word(view, pt, inclusive=True, count=1): # type: (...) -> Region assert count > 0 start = current_word_start(view, pt) end = pt if inclusive: end = word_starts(view, start, count=count, internal=True) # If there is no space at the end of our word text object, include any # preceding spaces. (Follows Vim behavior.) if (not view.substr(end - 1).isspace() and view.substr(start - 1).isspace()): start = prev_non_blank(view, start - 1) + 1 # Vim does some inconsistent stuff here... if count > 1 and view.substr(end) == '\n': end += 1 return Region(start, end) for x in range(count): end = current_word_end(view, end) return Region(start, end)
def resolve_indent_text_object(view, s: Region, inclusive: bool = True, big: bool = False): # Look for the minimum indentation in the current visual region. idnt = 1000 idnt_pt = None for line in view.lines(s): if not re.match('^\\s*$', view.substr(line)): level = view.indentation_level(line.a) if level < idnt: idnt = min(idnt, level) idnt_pt = line.a # If the selection has no indentation at all, find which indentation level # is the largest, the previous non blank before tphe cursor or the next non # blank after the cursor, and start the selection from that point. if idnt == 1000: pnb_pt = prev_non_ws(view, s.begin()) pnb_indent_level = view_indentation_level(view, pnb_pt) nnb_pt = next_non_ws(view, s.end()) nnb_indent_level = view_indentation_level(view, nnb_pt) if pnb_indent_level > nnb_indent_level: idnt_pt = s.a = s.b = pnb_pt elif nnb_indent_level > pnb_indent_level: idnt_pt = s.a = s.b = nnb_pt else: idnt_pt = pnb_pt if idnt == 0 and idnt_pt is not None: expanded = view.expand_by_class(s, CLASS_EMPTY_LINE) s.a = expanded.a s.b = expanded.b if not inclusive: # Case: ii and iI. Strip any leading whitespace. leading_ws = view_find(view, '\\s*', s.a) if leading_ws is not None: s.a = view.line(leading_ws.b).a s.b = prev_non_blank(view, s.b) elif big: # Case: aI. Add a line below. if view.substr(s.b) == '\n': s.b += 1 elif idnt > 0 and idnt_pt is not None: indented_region = view_indented_region(view, idnt_pt, inclusive) if indented_region.begin() < s.begin(): s.a = indented_region.begin() if indented_region.end() > s.end(): s.b = indented_region.end() if inclusive: # Case: ai. Add a line above. s.a = view.line(view.text_point(view.rowcol(s.a)[0] - 1, 0)).a # Case: aI. Add a line below. if big: s.b = view.full_line(view.text_point(view.rowcol(s.b - 1)[0] + 1, 0)).b return s