Skip to content

Commit

Permalink
do not shift inline completions
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-jpq committed Dec 6, 2024
1 parent 1916485 commit 73ef728
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
8 changes: 4 additions & 4 deletions coq/clients/cache/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion coq/clients/inline/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions coq/clients/lsp/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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,
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion coq/server/registrants/repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions coq/shared/repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 73ef728

Please sign in to comment.