Exemple #1
0
            def c2() -> None:
                def it() -> Iterator[Tuple[Window, Tuple[int, int]]]:
                    wins = list_wins(nvim)
                    for win in wins:
                        buf = win_get_buf(nvim, win=win)
                        if buf == ctx.buf:
                            row, col = win_get_cursor(nvim, win)
                            yield win, (row, col)

                saved = {win: pos for win, pos in it()}

                lines = temp.read_text().split(ctx.linefeed)
                if lines:
                    l = lines.pop()
                    if l:
                        lines.append(l)
                buf_set_lines(nvim, buf=ctx.buf, lo=0, hi=-1, lines=lines)

                for win, (row, col) in saved.items():
                    new_row = min(row, len(lines) - 1)
                    with suppress(NvimError):
                        win_set_cursor(nvim, win=win, row=new_row, col=col)
                detect_tabs(nvim, buf=ctx.buf)

                prettiers = LANG("step join sep").join(attr.bin for attr in attrs)
                nice = LANG("prettier succeeded", prettiers=prettiers)
                write(nvim, nice)
Exemple #2
0
def _single_mark(
    nvim: Nvim,
    mark: ExtMark,
    marks: Sequence[ExtMark],
    ns: int,
    win: Window,
    buf: Buffer,
) -> None:
    row, col = mark.begin
    nvim.options["undolevels"] = nvim.options["undolevels"]

    try:
        apply(nvim, buf=buf, instructions=_trans("", marks=(mark, )))
        win_set_cursor(nvim, win=win, row=row, col=col)
        nvim.command("startinsert")
    except NvimError as e:
        msg = f"""
        bad mark location {mark}

        {e}
        """
        log.warn("%s", dedent(msg))
    else:
        nvim.command("startinsert")
        state(inserted_pos=(row, col))
        msg = LANG("applied mark", marks_left=len(marks))
        write(nvim, msg)
    finally:
        _del_marks(nvim, buf=buf, id=ns, marks=(mark, ))
Exemple #3
0
def _restore_pos(nvim: Nvim) -> None:
    win = cur_win(nvim)
    buf = win_get_buf(nvim, win=win)
    row, _ = win_get_cursor(nvim, win=win)
    pos: Optional[int] = buf_get_var(nvim, buf=buf, key=BUF_VAR_NAME)

    if pos is not None:
        win_set_cursor(nvim, win=win, row=row, col=pos)
Exemple #4
0
def _toggle_case(nvim: Nvim) -> None:
    win = cur_win(nvim)
    row, col = win_get_cursor(nvim, win=win)
    buf = win_get_buf(nvim, win=win)
    if writable(nvim, buf=buf):
        line, *_ = buf_get_lines(nvim, buf=buf, lo=row, hi=row + 1)
        bline = encode(line)
        before, after = bline[:col], bline[col:]
        if after:
            cur, *post = after
            pt = decode(bytes((cur, )))
            swapped = _swap_case(pt)
            new = decode(before) + swapped + decode(bytes(post))
            pos = len(before) + len(encode(swapped))
            buf_set_lines(nvim, buf=buf, lo=row, hi=row + 1, lines=(new, ))
            win_set_cursor(nvim, win=win, row=row, col=pos)
Exemple #5
0
def _toggle_case(nvim: Nvim) -> None:
    win = cur_win(nvim)
    row, col = win_get_cursor(nvim, win=win)
    buf = win_get_buf(nvim, win=win)
    if not writable(nvim, buf=buf):
        return
    else:
        line, *_ = buf_get_lines(nvim, buf=buf, lo=row, hi=row + 1)
        bline = line.encode()
        before, after = bline[:col], bline[col:]
        cur, *post = after
        pt = bytes((cur,)).decode()
        swapped = _swap_case(pt)
        new = before.decode() + swapped + bytes(post).decode()
        pos = len(before) + len(swapped.encode())
        buf_set_lines(nvim, buf=buf, lo=row, hi=row + 1, lines=(new,))
        win_set_cursor(nvim, win=win, row=row, col=pos)
Exemple #6
0
            def cont() -> None:
                def it() -> Iterator[Tuple[Window, Tuple[int, int]]]:
                    wins = list_wins(nvim)
                    for win in wins:
                        buf = win_get_buf(nvim, win=win)
                        if buf == ctx.buf:
                            row, col = win_get_cursor(nvim, win)
                            yield win, (row, col)

                saved = {win: pos for win, pos in it()}

                lines = temp.read_text().splitlines()
                buf_set_lines(nvim, buf=ctx.buf, lo=0, hi=-1, lines=lines)

                for win, (row, col) in saved.items():
                    new_row = min(row, len(lines) - 1)
                    win_set_cursor(nvim, win=win, row=new_row, col=col)
Exemple #7
0
def _set_trimmed(nvim: Nvim, win: Window, buf: Buffer) -> None:
    row, col = win_get_cursor(nvim, win=win)
    lines = buf_get_lines(nvim, buf=buf, lo=0, hi=-1)
    new_lines = [
        decode(encode(line)[:col]) +
        decode(encode(line)[col:]).rstrip() if r == row else line.rstrip()
        for r, line in enumerate(lines)
    ]

    while new_lines:
        line = new_lines.pop()
        if line or len(new_lines) <= row:
            new_lines.append(line)
            break
    if len(new_lines) < len(lines):
        new_lines.append("")

    if new_lines != lines:
        buf_set_lines(nvim, buf=buf, lo=0, hi=-1, lines=new_lines)
        win_set_cursor(nvim, win=win, row=row, col=col)
Exemple #8
0
def _linked_marks(
    nvim: Nvim,
    mark: ExtMark,
    linked: Sequence[ExtMark],
    ns: int,
    win: Window,
    buf: Buffer,
) -> bool:
    marks = tuple(chain((mark, ), linked))
    place_holders = tuple(
        text for _, text in extmarks_text(nvim, buf=buf, marks=marks))
    texts = dumps(place_holders, check_circular=False, ensure_ascii=False)
    resp = ask(nvim, question=LANG("expand marks", texts=texts), default="")
    if resp is not None:
        row, col = mark.begin
        nvim.options["undolevels"] = nvim.options["undolevels"]
        apply(nvim, buf=buf, instructions=_trans(resp, marks=marks))
        _del_marks(nvim, buf=buf, id=ns, marks=marks)
        win_set_cursor(nvim, win=win, row=row, col=col)
        nvim.command("startinsert")
        state(inserted_pos=(row, col - 1))
        return True
    else:
        return False
Exemple #9
0
 def cont() -> None:
     new_col = col + len(lhs.encode())
     nvim.api.set_vvar("char", lhs + cast(str, rhs))
     set_cur = lambda: win_set_cursor(
         nvim, win=win, row=row, col=new_col)
     go(async_call(nvim, set_cur))
Exemple #10
0
def edit(
    nvim: Nvim,
    stack: Stack,
    state: State,
    metric: Metric,
    synthetic: bool,
) -> Optional[NvimPos]:
    win = cur_win(nvim)
    buf = win_get_buf(nvim, win=win)
    if buf.number != state.context.buf_id:
        log.warn("%s", "stale buffer")
        return None
    else:
        nvim.options["undolevels"] = nvim.options["undolevels"]

        if synthetic:
            inserted, movement = "", None
        else:
            inserted, movement = _restore(nvim,
                                          win=win,
                                          buf=buf,
                                          pos=state.context.position)

        try:
            primary, marks = _parse(nvim,
                                    buf=buf,
                                    stack=stack,
                                    state=state,
                                    comp=metric.comp)
        except (NvimError, ParseError) as e:
            primary, marks = metric.comp.primary_edit, ()
            write(nvim, LANG("failed to parse snippet"))
            log.info("%s", e)

        lo, hi = _rows_to_fetch(
            state.context,
            primary,
            *metric.comp.secondary_edits,
        )
        if lo < 0 or hi > state.context.line_count:
            log.warn("%s", pformat(("OUT OF BOUNDS", (lo, hi), metric)))
            return None
        else:
            limited_lines = buf_get_lines(nvim, buf=buf, lo=lo, hi=hi)
            lines = [*chain(repeat("", times=lo), limited_lines)]
            view = _lines(lines)

            instructions = _consolidate(*_instructions(
                state.context,
                unifying_chars=stack.settings.match.unifying_chars,
                smart=stack.settings.completion.smart,
                lines=view,
                primary=primary,
                secondary=metric.comp.secondary_edits,
            ))
            n_row, n_col = _cursor(
                state.context.position,
                instructions=instructions,
            )

            if not synthetic:
                stack.idb.inserted(metric.instance.bytes,
                                   sort_by=metric.comp.sort_by)

            m_shift = apply(nvim, buf=buf, instructions=instructions)
            if inserted:
                try:
                    buf_set_text(
                        nvim,
                        buf=buf,
                        begin=(n_row, n_col),
                        end=(n_row, n_col),
                        text=(inserted, ),
                    )
                except NvimError as e:
                    log.warn("%s", e)

            if movement is not None:
                try:
                    win_set_cursor(nvim,
                                   win=win,
                                   row=n_row,
                                   col=n_col + movement)
                except NvimError as e:
                    log.warn("%s", e)

            if marks:
                new_marks = tuple(_shift_marks(m_shift, marks=marks))
                mark(nvim, settings=stack.settings, buf=buf, marks=new_marks)

            if DEBUG:
                log.debug(
                    "%s",
                    pformat((
                        (metric.comp.primary_edit,
                         *metric.comp.secondary_edits),
                        instructions,
                    )),
                )

            return n_row, n_col