diff --git a/coq/clients/cache/worker.py b/coq/clients/cache/worker.py index 80df6ba1..be02140b 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( - inline_shift: bool, cursor: Cursors, comp: Completion, sort_by: Optional[str] + cursor: Cursors, comp: Completion, sort_by: Optional[str] ) -> Optional[Completion]: - if edit := sanitize(inline_shift, cursor, edit=comp.primary_edit): + if edit := sanitize(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, inline_shift: bool + self, context: Context, always: bool ) -> Tuple[bool, AbstractSet[str], Iterator[Completion]]: cache_ctx = self._cache_ctx row, col = context.position @@ -165,7 +165,9 @@ def get() -> Iterator[Completion]: for key, sort_by in selected: if (comp := self._cached.get(key)) and ( cached := sanitize_cached( - inline_shift, cursor=context.cursor, comp=comp, sort_by=sort_by + 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 ee3eb3bf..fd3ce85c 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, inline_shift=True) + _, _, cached = self._cache.apply_cache(context, always=True) 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 2b5e302a..2004d02d 100644 --- a/coq/clients/lsp/worker.py +++ b/coq/clients/lsp/worker.py @@ -143,7 +143,6 @@ async def cont() -> None: await self._with_interrupt(cont()) async def _work(self, context: Context) -> AsyncIterator[Completion]: - inline_shift = False limit = ( BIGGEST_INT if context.manual @@ -153,7 +152,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, inline_shift=inline_shift + context, always=False ) if not use_cache: self._local_cached.pre.clear() @@ -182,7 +181,6 @@ async def stream() -> AsyncIterator[Tuple[_Src, LSPcomp]]: for item in cached_items if ( cached := sanitize_cached( - inline_shift=inline_shift, cursor=context.cursor, comp=item, sort_by=None, diff --git a/coq/server/registrants/repeat.py b/coq/server/registrants/repeat.py index 5c3e1657..f909799e 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(True, cursor=(-1, -1, -1, -1), edit=prev) + sanitized = sanitize(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 93a2e818..7f242a74 100644 --- a/coq/shared/repeat.py +++ b/coq/shared/repeat.py @@ -18,7 +18,9 @@ ) -def _shift(cursor: Cursors, edit: BaseRangeEdit) -> Tuple[WTF8Pos, WTF8Pos]: +def _shift_or_reject( + cursor: Cursors, edit: BaseRangeEdit +) -> Optional[Tuple[WTF8Pos, WTF8Pos]]: row, u8, u16, u32 = cursor if edit.encoding == UTF16: col = u16 @@ -30,6 +32,9 @@ def _shift(cursor: Cursors, edit: BaseRangeEdit) -> Tuple[WTF8Pos, WTF8Pos]: never(edit.encoding) (b_row, b_col), (e_row, e_col) = edit.begin, edit.end + if e_col < col: + return None + edit_col = edit.cursor_pos diff = col - edit_col @@ -53,7 +58,7 @@ def _shift(cursor: Cursors, edit: BaseRangeEdit) -> Tuple[WTF8Pos, WTF8Pos]: return new_begin, new_end -def sanitize(inline_shift: bool, cursor: Cursors, edit: Edit) -> Optional[Edit]: +def sanitize(cursor: Cursors, edit: Edit) -> Optional[Edit]: row, *_ = cursor if isinstance(edit, SnippetRangeEdit): if row == -1: @@ -67,17 +72,17 @@ def sanitize(inline_shift: bool, 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: - begin, end = _shift(cursor, edit=edit) + elif shift := _shift_or_reject(cursor, edit=edit): + begin, end = shift return replace(edit, begin=begin, end=end) + else: + return None elif isinstance(edit, RangeEdit): - if inline_shift: - begin, end = _shift(cursor, edit=edit) - return replace(edit, begin=begin, end=end) - elif fallback := edit.fallback: + if fallback := edit.fallback: return Edit(new_text=fallback) - elif not requires_snip(edit.new_text): - return Edit(new_text=edit.new_text) + elif shift := _shift_or_reject(cursor, edit=edit): + begin, end = shift + return replace(edit, begin=begin, end=end) else: return None elif isinstance(edit, SnippetEdit):