def run(self, edit, mode, count, search=None, forward=True, save=True): if len(self.view.sel()) != 1: ui_bell('sneak does not support multiple cursors') return if not search: search = _get_last_sneak_search(self.view) if not search: ui_bell('no previous sneak search') return clear_search_highlighting(self.view) flags = _get_search_flags(self.view, search) s = self.view.sel()[0] start_pt = get_insertion_point_at_b(s) if forward: occurrences = view_find_all_in_range(self.view, search, start_pt + 1, self.view.size(), flags) else: occurrences = list( view_rfind_all(self.view, search, start_pt, flags)) occurrences = occurrences[count - 1:] if not occurrences: ui_bell('not found: %s' % search) return target = occurrences[0].a if mode == NORMAL: s.a = s.b = target elif mode == VISUAL: resolve_visual_target(s, target) elif mode == VISUAL_LINE: resolve_visual_line_target(self.view, s, target) elif mode == INTERNAL_NORMAL: s.b = target else: return set_selection(self.view, s) add_search_highlighting(self.view, occurrences) if save: set_last_char_search_command( self.view, 'sneak_s' if forward else 'sneak_big_s') _set_last_sneak_search(self.view, search)
def _blank_up(view, edit, count: int) -> None: new_sels = [] for sel in view.sel(): line = view.line(sel) if line.empty(): new_sels.append(line.b + count) else: new_sels.append(view.find('[^\\s]', line.begin()).begin() + count) view.insert(edit, line.begin() - 1 if line.begin() > 0 else 0, '\n' * count) if new_sels: set_selection(view, new_sels)
def filter_thru_shell(view, edit, regions, cmd: str) -> None: # Maintain text size delta as we replace each selection going forward. We # can't simply go in reverse because cursor positions will be incorrect. accumulated_delta = 0 new_points = [] for r in regions: r_shifted = Region(r.begin() + accumulated_delta, r.end() + accumulated_delta) rv = _shell.filter_region(view, view.substr(r_shifted), cmd).rstrip() + '\n' view.replace(edit, r_shifted, rv) new_points.append(r_shifted.a) accumulated_delta += len(rv) - r_shifted.size() # Switch to normal mode and move cursor(s) to beginning of replacement(s). view.run_command('_enter_normal_mode') set_selection(view, new_points)
def _blank_down(view, edit, count: int) -> None: end_point = view.size() new_sels = [] for sel in view.sel(): line = view.line(sel) if line.empty(): new_sels.append(line.b) else: new_sels.append(view.find('[^\\s]', line.begin()).begin()) view.insert(edit, line.end() + 1 if line.end() < end_point else end_point, '\n' * count) if new_sels: set_selection(view, new_sels)
def _do_cc(view, edit, mode, count=1): def f(view, s): if mode == INTERNAL_NORMAL: view.run_command('toggle_comment') if row_at(view, s.a) != row_at(view, view.size()): pt = next_non_blank(view, s.a) else: pt = next_non_blank(view, view.line(s.a).a) s.a = s.b = pt return s def _motion(view, edit, mode, count): def f(view, s): if mode == INTERNAL_NORMAL: end = view.text_point(row_at(view, s.b) + (count - 1), 0) begin = view.line(s.b).a row_at_end = row_at(view, end) row_at_size = row_at(view, view.size()) if ((row_at_end == row_at_size) and (view.substr(begin - 1) == '\n')): begin -= 1 s.a = begin s.b = view.full_line(end).b return s regions_transformer(view, f) _motion(view, edit, mode, count) line = view.line(view.sel()[0].begin()) pt = line.begin() if line.size() > 0: line = view.find('^\\s*', line.begin()) pt = line.end() regions_transformer_reversed(view, f) set_selection(view, pt)
def run(self, edit, to=None, mode=None): try: to = _ALIASES[to] except KeyError: pass try: coerce_func = _COERCIONS[to] except KeyError: return new_sels = [] for sel in self.view.sel(): sel = self.view.word(sel) new_sels.append(sel.begin()) self.view.replace(edit, sel, coerce_func(self.view.substr(sel))) if new_sels: set_selection(self.view, new_sels)
def run(self, edit, to=None, mode=None): if to in _ALIASES: to = _ALIASES[to] if to in _COERCIONS: coerce_func = _COERCIONS[to] else: raise ValueError('unknown coercion') new_sels = [] for sel in self.view.sel(): if sel.empty(): sel = self.view.word(sel) new_sels.append(sel.begin()) self.view.replace(edit, sel, coerce_func(self.view.substr(sel))) if new_sels: set_selection(self.view, new_sels)
def _focus_group(window, direction, count=1): nth_group_number = _get_group(window, direction, count) if nth_group_number is None: return # If the cursor is not visible in the view we are moving to, then move the # cursor to the top of the visible area of the view (instead of scrolling # the view to show the cursor). This prevents the view we are moving to # suddenly scrolling, which can be unexpected and is arguably bad UX. The # functionaility now works closer to how Vim works. The main difference in # Vim is that the cursor never leaves the visible areas in the first place, # it just doesn't happen e.g. when you scroll with the mouse in Vim, Vim # moves the cursor along with the scrolling visible area so the cursor is # always visible, it never disappears from the visible areas. view = window.active_view_in_group(nth_group_number) if view: visible_region = view.visible_region() if not view.visible_region().contains(view.sel()[0]): set_selection(view, visible_region.begin()) window.focus_group(nth_group_number)
def _do_c(view, edit, mode, count=1, motion=None): def f(view, s): return Region(s.begin()) if motion: run_motion(view, motion) elif mode not in (VISUAL, VISUAL_LINE): return ui_bell() view.run_command('toggle_comment', {'block': False}) regions_transformer(view, f) line = view.line(view.sel()[0].begin()) pt = line.begin() if line.size() > 0: line = view.find('^\\s*', line.begin()) pt = line.end() set_selection(view, pt) enter_normal_mode(view, mode)
def _goto_modification(action: str, view, mode: str, count: int) -> None: with wrapscan(view, forward=(action == 'next')): if int(version()) >= 3189: for i in range(count): view.run_command(action + '_modification') a = view.sel()[0].a if view.substr(a) == '\n': if not view.line(a).empty(): a += 1 set_selection(view, a) enter_normal_mode(view, mode) else: # TODO Remove DEPRECATED code, deprecated since build 3189 view.run_command('git_gutter_' + action + '_change', { 'count': count, 'wrap': False }) line = view.line(view.sel()[0].b) if line.size() > 0: pt = view.find('^\\s*', line.begin()).end() if pt != line.begin(): set_selection(view, pt)