From 73ef7287c8f9d59a46756d03cd61620826c54131 Mon Sep 17 00:00:00 2001 From: dogisgreat Date: Fri, 6 Dec 2024 07:52:29 -0500 Subject: [PATCH] do not shift inline completions --- coq/clients/cache/worker.py | 8 ++++---- coq/clients/inline/worker.py | 2 +- coq/clients/lsp/worker.py | 8 ++++++-- coq/server/registrants/repeat.py | 2 +- coq/shared/repeat.py | 6 ++++-- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/coq/clients/cache/worker.py b/coq/clients/cache/worker.py index 1946a38c6..49c619a0f 100644 --- a/coq/clients/cache/worker.py +++ b/coq/clients/cache/worker.py @@ -60,9 +60,9 @@ def _overlap(row: int, edit: BaseRangeEdit) -> bool: def sanitize_cached( - cursor: Cursors, comp: Completion, sort_by: Optional[str] + shift: bool, cursor: Cursors, comp: Completion, sort_by: Optional[str] ) -> Optional[Completion]: - if edit := sanitize(cursor, edit=comp.primary_edit): + if edit := sanitize(shift, cursor, edit=comp.primary_edit): row, *_ = cursor cached = replace( comp, @@ -126,7 +126,7 @@ def cont() -> Iterator[Tuple[bytes, str]]: self._cached.update(new_comps) def apply_cache( - self, context: Context, always: bool + self, context: Context, always: bool, shift: bool ) -> Tuple[bool, AbstractSet[str], Iterator[Completion]]: cache_ctx = self._cache_ctx row, col = context.position @@ -165,7 +165,7 @@ def get() -> Iterator[Completion]: for key, sort_by in selected: if (comp := self._cached.get(key)) and ( cached := sanitize_cached( - context.cursor, comp=comp, sort_by=sort_by + shift, cursor=context.cursor, comp=comp, sort_by=sort_by ) ): if ( diff --git a/coq/clients/inline/worker.py b/coq/clients/inline/worker.py index fd3ce85c3..6d6092f79 100644 --- a/coq/clients/inline/worker.py +++ b/coq/clients/inline/worker.py @@ -71,7 +71,7 @@ async def cont() -> None: async def _work(self, context: Context) -> AsyncIterator[Completion]: async with self._work_lock, self._working: try: - _, _, cached = self._cache.apply_cache(context, always=True) + _, _, cached = self._cache.apply_cache(context, always=True, shift=False) lsp_stream = ( self._request(context) if self._options.live_pulling diff --git a/coq/clients/lsp/worker.py b/coq/clients/lsp/worker.py index 69f2dfaa0..dbdb46396 100644 --- a/coq/clients/lsp/worker.py +++ b/coq/clients/lsp/worker.py @@ -143,6 +143,7 @@ async def cont() -> None: await self._with_interrupt(cont()) async def _work(self, context: Context) -> AsyncIterator[Completion]: + shift = True limit = ( BIGGEST_INT if context.manual @@ -152,7 +153,7 @@ async def _work(self, context: Context) -> AsyncIterator[Completion]: async with self._work_lock, self._working: try: use_cache, cached_clients, cached = self._cache.apply_cache( - context, always=False + context, always=False, shift=shift ) if not use_cache: self._local_cached.pre.clear() @@ -181,7 +182,10 @@ async def stream() -> AsyncIterator[Tuple[_Src, LSPcomp]]: for item in cached_items if ( cached := sanitize_cached( - context.cursor, comp=item, sort_by=None + shift=shift, + cursor=context.cursor, + comp=item, + sort_by=None, ) ) ) diff --git a/coq/server/registrants/repeat.py b/coq/server/registrants/repeat.py index e71763e0d..5c3e16579 100644 --- a/coq/server/registrants/repeat.py +++ b/coq/server/registrants/repeat.py @@ -11,7 +11,7 @@ def _edit(prev: Edit) -> Optional[Edit]: - sanitized = sanitize((-1, -1, -1, -1), edit=prev) + sanitized = sanitize(True, cursor=(-1, -1, -1, -1), edit=prev) new_edit = ( ContextualEdit( new_text=sanitized.new_text, old_prefix="", new_prefix=sanitized.new_text diff --git a/coq/shared/repeat.py b/coq/shared/repeat.py index edbd858f2..16d55fb23 100644 --- a/coq/shared/repeat.py +++ b/coq/shared/repeat.py @@ -53,7 +53,7 @@ def _shift(cursor: Cursors, edit: BaseRangeEdit) -> Tuple[WTF8Pos, WTF8Pos]: return new_begin, new_end -def sanitize(cursor: Cursors, edit: Edit) -> Optional[Edit]: +def sanitize(shift: bool, cursor: Cursors, edit: Edit) -> Optional[Edit]: row, *_ = cursor if isinstance(edit, SnippetRangeEdit): if row == -1: @@ -67,9 +67,11 @@ def sanitize(cursor: Cursors, edit: Edit) -> Optional[Edit]: return SnippetEdit(grammar=edit.grammar, new_text=fallback) elif not requires_snip(edit.new_text): return Edit(new_text=edit.new_text) - else: + elif shift: begin, end = _shift(cursor, edit=edit) return replace(edit, begin=begin, end=end) + else: + return edit elif isinstance(edit, RangeEdit): if fallback := edit.fallback: return Edit(new_text=fallback)