@@ -38,12 +38,10 @@ def get_completions(
38
38
m = self .compiled_grammar .match_prefix (document .text_before_cursor )
39
39
40
40
if m :
41
- completions = self ._remove_duplicates (
41
+ yield from self ._remove_duplicates (
42
42
self ._get_completions_for_match (m , complete_event )
43
43
)
44
44
45
- yield from completions
46
-
47
45
def _get_completions_for_match (
48
46
self , match : Match , complete_event : CompleteEvent
49
47
) -> Iterable [Completion ]:
@@ -82,14 +80,21 @@ def _get_completions_for_match(
82
80
display_meta = completion .display_meta ,
83
81
)
84
82
85
- def _remove_duplicates (self , items : Iterable [Completion ]) -> list [Completion ]:
83
+ def _remove_duplicates (self , items : Iterable [Completion ]) -> Iterable [Completion ]:
86
84
"""
87
85
Remove duplicates, while keeping the order.
88
86
(Sometimes we have duplicates, because the there several matches of the
89
87
same grammar, each yielding similar completions.)
90
88
"""
91
- result : list [Completion ] = []
92
- for i in items :
93
- if i not in result :
94
- result .append (i )
95
- return result
89
+
90
+ def hash_completion (completion : Completion ) -> tuple [str , int ]:
91
+ return completion .text , completion .start_position
92
+
93
+ yielded_so_far : set [tuple [str , int ]] = set ()
94
+
95
+ for completion in items :
96
+ hash_value = hash_completion (completion )
97
+
98
+ if hash_value not in yielded_so_far :
99
+ yielded_so_far .add (hash_value )
100
+ yield completion
0 commit comments