def _go_replace(nvim: Nvim, visual: VisualTypes) -> None: buf = cur_buf(nvim) if not writable(nvim, buf=buf): return else: linefeed = buf_linefeed(nvim, buf=buf) (r1, c1), (r2, c2) = operator_marks(nvim, buf=buf, visual_type=visual) lines = buf_get_lines(nvim, buf=buf, lo=r1, hi=r2 + 1) if len(lines) > 1: h, *_, t = lines else: h, *_ = t, *_ = lines begin = (r1, min(c1, max(0, len(encode(h)) - 1))) end = (r2, min(len(encode(t)), c2 + 1)) text: str = nvim.funcs.getreg() new_lines = text.split(linefeed) if new_lines: n = new_lines.pop() if n: new_lines.append(n) nvim.options["undolevels"] = nvim.options["undolevels"] buf_set_text(nvim, buf=buf, begin=begin, end=end, text=new_lines)
def _sort_lines(nvim: Nvim, visual: VisualTypes) -> None: buf = cur_buf(nvim) if not writable(nvim, buf=buf): return else: (row1, _), (row2, _) = operator_marks(nvim, buf=buf, visual_type=visual) lines = buf_get_lines(nvim, buf=buf, lo=row1, hi=row2 + 1) new_lines = sorted(lines, key=strxfrm) buf_set_lines(nvim, buf=buf, lo=row1, hi=row2 + 1, lines=new_lines)
def _comment(nvim: Nvim, visual: VisualTypes) -> None: buf = cur_buf(nvim) if not writable(nvim, buf=buf): return else: (row1, _), (row2, _) = operator_marks(nvim, buf=buf, visual_type=visual) lines = buf_get_lines(nvim, buf=buf, lo=row1, hi=row2 + 1) lhs, rhs = buf_commentstr(nvim, buf=buf) new_lines = _toggle_comment(lhs, rhs, lines=lines) buf_set_lines(nvim, buf=buf, lo=row1, hi=row2 + 1, lines=new_lines)
def _get_selected(nvim: Nvim, buf: Buffer, visual_type: VisualTypes) -> str: (row1, col1), (row2, col2) = operator_marks(nvim, buf=buf, visual_type=visual_type) lines = buf_get_lines(nvim, buf=buf, lo=row1, hi=row2 + 1) if len(lines) == 1: return lines[0].encode()[col1:col2 + 1].decode() else: head = lines[0].encode()[col1:].decode() body = lines[1:-1] tail = lines[-1].encode()[:col2 + 1].decode() return linesep.join(chain((head, ), body, (tail, )))
def eval_snips( nvim: Nvim, stack: Stack, visual: bool, maybe_grammar: str = REPL_GRAMMAR, ) -> None: try: grammar = SnippetGrammar[maybe_grammar] except KeyError: grammar = SnippetGrammar.lsp log.warn("%s", "bad snippet grammar -- reverting to LSP") win = cur_win(nvim) buf = win_get_buf(nvim, win=win) line_count = buf_line_count(nvim, buf=buf) path = PurePath(normcase(buf_name(nvim, buf=buf))) comment_str = buf_commentstr(nvim, buf=buf) clipboard = nvim.funcs.getreg() info = ParseInfo(visual="", clipboard=clipboard, comment_str=comment_str) if visual: (lo, _), (hi, _) = operator_marks(nvim, buf=buf, visual_type=None) hi = min(line_count, hi + 1) else: lo, hi = 0, line_count lines = buf_get_lines(nvim, buf=buf, lo=lo, hi=hi) try: compiled = compile_one( stack, grammar=grammar, path=path, info=info, lines=enumerate(lines, start=lo + 1), ) except (LoadError, ParseError) as e: preview = str(e).splitlines() with hold_win_pos(nvim, win=win): set_preview(nvim, syntax="", preview=preview) write(nvim, LANG("snip parse fail")) else: preview = _pprn(compiled).splitlines() with hold_win_pos(nvim, win=win): set_preview(nvim, syntax="yaml", preview=preview) if preview: write(nvim, LANG("snip parse succ")) else: write(nvim, LANG("no snippets found"))
def redraw(nvim: Nvim, state: State, focus: Optional[PurePath]) -> None: focus_row = state.derived.path_row_lookup.get(focus) if focus else None ns = nvim.api.create_namespace(FM_NAMESPACE) use_extmarks = nvim.funcs.has("nvim-0.6") for win, buf in find_fm_windows(nvim): p_count = buf_line_count(nvim, buf=buf) n_count = len(state.derived.lines) row, col = win_get_cursor(nvim, win=win) (r1, c1), (r2, c2) = operator_marks(nvim, buf=buf, visual_type=None) try: hashed_lines = _DECODER( buf_get_var(nvim, buf=buf, key=_FM_HASH_VAR)) except DecodeError: hashed_lines = ("", ) if focus_row is not None: new_row: Optional[int] = focus_row + 1 elif row >= n_count: new_row = n_count elif p_count != n_count: new_row = row + 1 else: new_row = None a1 = Atomic() a1.buf_set_option(buf, "modifiable", True) a2 = _update( use_extmarks, buf=buf, ns=ns, derived=state.derived, hashed_lines=hashed_lines, ) a3 = Atomic() a3.buf_set_option(buf, "modifiable", False) a3.call_function("setpos", ("'<", (buf.number, r1 + 1, c1 + 1, 0))) a3.call_function("setpos", ("'>", (buf.number, r2 + 1, c2 + 1, 0))) if new_row is not None: a3.win_set_cursor(win, (new_row, col)) try: (a1 + a2 + a3).commit(nvim) except NvimError as e: raise UnrecoverableError(e)
def indices(nvim: Nvim, state: State, is_visual: bool) -> Iterator[Node]: win = cur_win(nvim) buf = win_get_buf(nvim, win=win) if not is_fm_buffer(nvim, buf=buf): return None else: row, _ = win_get_cursor(nvim, win=win) node = _row_index(state, row) if node: yield node if is_visual: (row1, _), (row2, _) = operator_marks(nvim, buf=buf, visual_type=None) for r in range(row1, row2 + 1): if r != row: node = _row_index(state, r) if node: yield node
def redraw(nvim: Nvim, state: State, focus: Optional[str]) -> None: derived, current = state.derived, state.current focus_row = derived.path_row_lookup.get(focus) if focus else None current_row = derived.path_row_lookup.get(current or "") cwin = cur_win(nvim) ns = nvim.api.create_namespace(FM_NAMESPACE) for win, buf in find_fm_windows(nvim): p_count = buf_line_count(nvim, buf=buf) n_count = len(state.derived.lines) row, col = win_get_cursor(nvim, win=win) (r1, c1), (r2, c2) = operator_marks(nvim, buf=buf, visual_type=None) if focus_row is not None: new_row: Optional[int] = focus_row + 1 elif win != cwin and current_row is not None: new_row = current_row + 1 elif row >= n_count: new_row = n_count elif p_count != n_count: new_row = row + 1 else: new_row = None a1 = Atomic() a1.buf_set_option(buf, "modifiable", True) a2 = _update(nvim, buf=buf, ns=ns, derived=derived) a3 = Atomic() a3.buf_set_option(buf, "modifiable", False) a3.call_function("setpos", ("'<", (buf.number, r1 + 1, c1 + 1, 0))) a3.call_function("setpos", ("'>", (buf.number, r2 + 1, c2 + 1, 0))) if new_row is not None: a3.win_set_cursor(win, (new_row, col)) (a1 + a2 + a3).commit(nvim)
def _visual_mv(nvim: Nvim, up: bool) -> None: win = cur_win(nvim) buf = win_get_buf(nvim, win=win) (row1, col1), (row2, col2) = operator_marks(nvim, buf=buf, visual_type=None) lines = buf_line_count(nvim, buf=buf) if not writable(nvim, buf=buf): return else: if up: if row1 <= 0: nvim.command("norm! gv") else: nvim.command(f"{row1+1},{row2+1}move {row1-1}") set_visual_selection( nvim, win=win, mode="v", mark1=(row1 - 1, col1), mark2=(row2 - 1, col2), reverse=True, ) else: if row2 >= lines - 1: nvim.command("norm! gv") else: nvim.command(f"{row1+1},{row2+1}move {row2+2}") set_visual_selection( nvim, win=win, mode="v", mark1=(row1 + 1, col1), mark2=(row2 + 1, col2), )
def _get_selected(nvim: Nvim, buf: Buffer, visual_type: VisualTypes) -> str: begin, (r2, c2) = operator_marks(nvim, buf=buf, visual_type=visual_type) return buf_get_text(nvim, buf=buf, begin=begin, end=(r2, c2 + 1))