def _restore(nvim: Nvim, win: Window, buf: Buffer, pos: NvimPos) -> Tuple[str, Optional[int]]: row, _ = pos ns = create_ns(nvim, ns=NS) marks = tuple(buf_get_extmarks(nvim, buf=buf, id=ns)) if len(marks) != 2: return "", 0 else: m1, m2 = marks after, *_ = buf_get_lines(nvim, buf=buf, lo=row, hi=row + 1) cur_row, cur_col = win_get_cursor(nvim, win=win) (_, lo), (_, hi) = m1.end, m2.begin binserted = encode(after)[lo:hi] inserted = decode(binserted) movement = cur_col - lo if cur_row == row and lo <= cur_col <= hi else None if inserted: buf_set_text(nvim, buf=buf, begin=m1.end, end=m2.begin, text=("", )) return inserted, movement
def _kill_win(nvim: Nvim, stack: Stack, reset: bool) -> None: if reset: state(pum_location=None, preview_id=uuid4()) buf = cur_buf(nvim) ns = create_ns(nvim, ns=_NS) clear_ns(nvim, buf=buf, id=ns) for win in _ls(nvim): win_close(nvim, win=win)
def nav_mark(nvim: Nvim, stack: Stack) -> None: ns = create_ns(nvim, ns=NS) win = cur_win(nvim) buf = win_get_buf(nvim, win=win) if marks := deque(_marks(nvim, ns=ns, win=win, buf=buf)): mark = marks.popleft() def single() -> None: _single_mark(nvim, mark=mark, marks=marks, ns=ns, win=win, buf=buf) if linked := tuple(m for m in marks if m.idx == mark.idx): edited = _linked_marks(nvim, mark=mark, linked=linked, ns=ns, win=win, buf=buf) if not edited: single()
def mark(nvim: Nvim, settings: Settings, buf: Buffer, marks: Sequence[Mark]) -> None: emarks = tuple( ExtMark( idx=mark.idx + 1, begin=mark.begin, end=mark.end, meta={"hl_group": settings.display.mark_highlight_group}, ) for mark in marks) ns = create_ns(nvim, ns=NS) clear_ns(nvim, buf=buf, id=ns) try: buf_set_extmarks(nvim, buf=buf, id=ns, marks=emarks) except NvimError: log.warn("%s", f"bad mark locations {marks}") else: msg = LANG("added marks", regions=" ".join(f"[{mark.text}]" for mark in marks)) write(nvim, msg)
def _virt_text(nvim: Nvim, ghost: GhostText, text: str) -> None: if ghost.enabled: lhs, rhs = ghost.context overlay, *_ = text.splitlines() or ("", ) virt_text = lhs + overlay + rhs ns = create_ns(nvim, ns=_NS) win = cur_win(nvim) buf = win_get_buf(nvim, win=win) row, col = win_get_cursor(nvim, win=win) mark = ExtMark( idx=1, begin=(row, col), end=(row, col), meta={ "virt_text_pos": "overlay", "hl_mode": "combine", "virt_text": ((virt_text, ghost.highlight_group), ), }, ) clear_ns(nvim, buf=buf, id=ns) buf_set_extmarks(nvim, buf=buf, id=ns, marks=(mark, ))
def _comp_done(nvim: Nvim, stack: Stack, event: Mapping[str, Any]) -> None: data = event.get("user_data") if data: try: uid = _UDECODER(data) except DecodeError: pass else: s = state() if metric := stack.metrics.get(uid): row, col = s.context.position buf = cur_buf(nvim) ns = create_ns(nvim, ns=NS) clear_ns(nvim, buf=buf, id=ns) before, *_ = buf_get_lines(nvim, buf=buf, lo=row, hi=row + 1) e1 = ExtMark( idx=1, begin=(row, 0), end=(row, col), meta={}, ) e2 = ExtMark( idx=2, begin=(row, col), end=(row, len(encode(before))), meta={}, ) buf_set_extmarks(nvim, buf=buf, id=ns, marks=(e1, e2)) async def cont() -> None: if metric: new_metric = await _resolve(nvim, stack=stack, metric=metric) async def c2() -> None: if isinstance((extern := new_metric.comp.extern), ExternLSP): await cmd(nvim, extern=extern) def c1() -> None: if new_metric.comp.uid in stack.metrics: inserted_at = edit( nvim, stack=stack, state=s, metric=new_metric, synthetic=False, ) ins_pos = inserted_at or (-1, -1) state( inserted_pos=ins_pos, last_edit=new_metric, commit_id=uuid4(), ) go(nvim, aw=c2()) else: log.warn("%s", "delayed completion") await async_call(nvim, c1) go(nvim, aw=cont())