Exemplo n.º 1
0
def _detect_tabs(nvim: Nvim) -> None:
    buf = cur_buf(nvim)
    count = buf_line_count(nvim, buf=buf)
    rows = min(count, 100)
    lines = buf_get_lines(nvim, buf=buf, lo=0, hi=rows)
    _set_tabsize(nvim, buf=buf, lines=lines)
    _set_usetab(nvim, buf=buf, lines=lines)
Exemplo n.º 2
0
def _entire(nvim: Nvim) -> None:
    win = cur_win(nvim)
    buf = win_get_buf(nvim, win=win)
    count = buf_line_count(nvim, buf=buf)
    last_line, *_ = buf_get_lines(nvim, buf=buf, lo=-2, hi=-1)
    mark1 = (0, 0)
    mark2 = (count - 1, len(encode(last_line)))
    set_visual_selection(nvim, win=win, mode="V", mark1=mark1, mark2=mark2)
Exemplo n.º 3
0
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"))
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
def _norm_mv(nvim: Nvim, up: bool) -> None:
    win = cur_win(nvim)
    buf = win_get_buf(nvim, win=win)
    row, _ = win_get_cursor(nvim, win=win)
    lines = buf_line_count(nvim, buf=buf)

    if not writable(nvim, buf=buf):
        return
    else:
        if up:
            if row:
                nvim.command(f"move -2")
        else:
            if row < lines - 1:
                nvim.command(f"move +1")
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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),
                )
Exemplo n.º 8
0
def detect_tabs(nvim: Nvim, buf: Buffer) -> None:
    count = buf_line_count(nvim, buf=buf)
    rows = min(count, 100)
    lines = buf_get_lines(nvim, buf=buf, lo=0, hi=rows)
    _set_tabsize(nvim, buf=buf, lines=lines)
    _set_usetab(nvim, buf=buf, lines=lines)