def _buf_enter(nvim: Nvim, stack: Stack) -> None: state(commit_id=uuid4()) with suppress(NvimError): buf = cur_buf(nvim) listed = buf_get_option(nvim, buf=buf, key="buflisted") buf_type: str = buf_get_option(nvim, buf=buf, key="buftype") if listed and buf_type != "terminal": nvim.api.buf_attach(buf, True, {})
def _term_open(nvim: Nvim, *args: str, opts: TermOpts = {}) -> None: buf = _ensure_marked_buf(nvim) buf_type: str = buf_get_option(nvim, buf=buf, key="buftype") is_term_buf = buf_type == "terminal" open_float_win(nvim, margin=0, relsize=0.95, buf=buf) if not is_term_buf: cmds = args or (environ["SHELL"], ) nvim.funcs.termopen(cmds, opts) nvim.command("startinsert")
def _term_open(nvim: Nvim, prog: AnyPath, *args: AnyPath, opts: TermOpts = {}) -> None: buf = _ensure_marked_buf(nvim) buf_type: str = buf_get_option(nvim, buf=buf, key="buftype") is_term_buf = buf_type == "terminal" open_float_win(nvim, margin=0, relsize=0.95, buf=buf, border="rounded") if not is_term_buf: cmds = tuple(map(str, chain((which(prog),), args))) nvim.funcs.termopen(cmds, opts) nvim.command("startinsert")
def cont() -> None: buf = cur_buf(nvim) buf_type: str = buf_get_option(nvim, buf=buf, key="buftype") if buf_type == "terminal": nvim.api.buf_detach(buf) state(nono_bufs={buf.number}) _insert_enter(nvim, stack=stack) stack.supervisor.notify_idle()
def current_ctx(nvim: Nvim) -> Tuple[str, BufContext]: cwd = get_cwd(nvim) buf = cur_buf(nvim) filename = buf_name(nvim, buf=buf) filetype = buf_filetype(nvim, buf=buf) tabsize: int = buf_get_option(nvim, buf=buf, key="tabstop") lines: Sequence[str] = buf_get_lines(nvim, buf=buf, lo=0, hi=-1) return cwd, BufContext( buf=buf, filename=filename, filetype=filetype, tabsize=tabsize, lines=lines )
def preview_preview(nvim: Nvim, stack: Stack, *_: str) -> str: win = next(_ls(nvim), None) if win: buf = win_get_buf(nvim, win=win) syntax = buf_get_option(nvim, buf=buf, key="syntax") lines = buf_get_lines(nvim, buf=buf, lo=0, hi=-1) nvim.exec_lua(f"{NAMESPACE}.{_bigger_preview.name}(...)", (syntax, lines)) escaped: str = nvim.api.replace_termcodes("<c-e>", True, False, True) return escaped
def _indent(nvim: Nvim) -> None: win = cur_win(nvim) buf = win_get_buf(nvim, win) row, _ = win_get_cursor(nvim, win) tabsize: int = buf_get_option(nvim, buf=buf, key="tabstop") lines = buf_get_lines(nvim, buf=buf, lo=0, hi=-1) before, curr, after = lines[:row], lines[row], lines[row + 1:] init_lv = p_indent(curr, tabsize=tabsize) top = row - _p_inside(init_lv, tabsize=tabsize, lines=reversed(before)) btm = row + _p_inside(init_lv, tabsize=tabsize, lines=after) set_visual_selection(nvim, win=win, mode="V", mark1=(top, 0), mark2=(btm, 0))
def _indent(nvim: Nvim) -> None: win = cur_win(nvim) buf = win_get_buf(nvim, win) row, _ = win_get_cursor(nvim, win) tabsize: int = buf_get_option(nvim, buf=buf, key="tabstop") lines = buf_get_lines(nvim, buf=buf, lo=0, hi=-1) before, curr, after = lines[:row], lines[row], lines[row + 1:] init_lv = p_indent(curr, tabsize=tabsize) top = row - _p_inside(init_lv, tabsize=tabsize, lines=reversed(before)) btm = row + _p_inside(init_lv, tabsize=tabsize, lines=after) lines = deque(buf_get_lines(nvim, buf=buf, lo=top, hi=btm + 1)) while lines: if line := lines.popleft(): lines.appendleft(line) break else: top += 1
def _win_nochange(nvim: Nvim, win: Window) -> bool: buf = win_get_buf(nvim, win=win) modified: bool = buf_get_option(nvim, buf=buf, key="modified") return not modified
def _parse_comment_str(nvim: Nvim, buf: Buffer) -> Tuple[str, str]: comment_str: str = buf_get_option(nvim, buf=buf, key="commentstring") assert len(comment_str.splitlines()) == 1 lhs, _, rhs = comment_str.partition("%s") return lhs, rhs