Skip to content

Commit

Permalink
can decide to reject
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-jpq committed Dec 6, 2024
1 parent 2e8e4b8 commit d2bcbb8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
10 changes: 6 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(
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,
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, inline_shift: bool
self, context: Context, always: bool
) -> Tuple[bool, AbstractSet[str], Iterator[Completion]]:
cache_ctx = self._cache_ctx
row, col = context.position
Expand Down Expand Up @@ -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 (
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, inline_shift=True)
_, _, cached = self._cache.apply_cache(context, always=True)
lsp_stream = (
self._request(context)
if self._options.live_pulling
Expand Down
4 changes: 1 addition & 3 deletions coq/clients/lsp/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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,
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(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
Expand Down
25 changes: 15 additions & 10 deletions coq/shared/repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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:
Expand All @@ -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):
Expand Down

0 comments on commit d2bcbb8

Please sign in to comment.