Skip to content

Commit

Permalink
Improve performance of GrammarCompleter (faster deduplication of comp…
Browse files Browse the repository at this point in the history
…letions).
  • Loading branch information
jonathanslenders committed May 28, 2024
1 parent 81dcfda commit 0b72a74
Showing 1 changed file with 14 additions and 9 deletions.
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

0 comments on commit 0b72a74

Please sign in to comment.