Skip to content

Improve performance of GrammarCompleter (faster deduplication of completions) #1875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/prompt_toolkit/contrib/regular_languages/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ def get_completions(
m = self.compiled_grammar.match_prefix(document.text_before_cursor)

if m:
completions = self._remove_duplicates(
yield from self._remove_duplicates(
self._get_completions_for_match(m, complete_event)
)

yield from completions

def _get_completions_for_match(
self, match: Match, complete_event: CompleteEvent
) -> Iterable[Completion]:
Expand Down Expand Up @@ -82,14 +80,21 @@ def _get_completions_for_match(
display_meta=completion.display_meta,
)

def _remove_duplicates(self, items: Iterable[Completion]) -> list[Completion]:
def _remove_duplicates(self, items: Iterable[Completion]) -> Iterable[Completion]:
"""
Remove duplicates, while keeping the order.
(Sometimes we have duplicates, because the there several matches of the
same grammar, each yielding similar completions.)
"""
result: list[Completion] = []
for i in items:
if i not in result:
result.append(i)
return result

def hash_completion(completion: Completion) -> tuple[str, int]:
return completion.text, completion.start_position

yielded_so_far: set[tuple[str, int]] = set()

for completion in items:
hash_value = hash_completion(completion)

if hash_value not in yielded_so_far:
yielded_so_far.add(hash_value)
yield completion
Loading