o
    �õ±j•  ã                   @  sf   d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZ dgZG d	d„ deƒZd
S )z"
Completer for a regular grammar.
é    )Úannotations)ÚIterable)ÚCompleteEventÚ	CompleterÚ
Completion)ÚDocumenté   )ÚMatchÚ_CompiledGrammarÚGrammarCompleterc                   @  s8   e Zd ZdZddd	„Zddd„Zddd„Zddd„ZdS )r   af  
    Completer which can be used for autocompletion according to variables in
    the grammar. Each variable can have a different autocompleter.

    :param compiled_grammar: `GrammarCompleter` instance.
    :param completers: `dict` mapping variable names of the grammar to the
                       `Completer` instances to be used for each variable.
    Úcompiled_grammarr
   Ú
completersúdict[str, Completer]ÚreturnÚNonec                 C  s   || _ || _d S ©N)r   r   )Úselfr   r   © r   ú¦/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/contrib/regular_languages/completion.pyÚ__init__   s   
zGrammarCompleter.__init__Údocumentr   Úcomplete_eventr   úIterable[Completion]c                 c  s4   � | j  |j¡}|r|  |  ||¡¡E d H  d S d S r   )r   Úmatch_prefixÚtext_before_cursorÚ_remove_duplicatesÚ_get_completions_for_match)r   r   r   Úmr   r   r   Úget_completions#   s   €
ÿÿz GrammarCompleter.get_completionsÚmatchr	   c                 c  sª   � |  ¡ D ]M}|j}|j}| j |¡}|rR|j}| j ||¡}t|t	|ƒƒ}	| 
|	|¡D ]%}
|dt	|ƒ|
j … |
j }t| j ||¡|t	|jƒ |
j|
jd�V  q,qdS )z°
        Yield all the possible completions for this input string.
        (The completer assumes that the cursor position was at the end of the
        input string.)
        N)ÚtextÚstart_positionÚdisplayÚdisplay_meta)Ú	end_nodesÚvarnameÚstartr   ÚgetÚvaluer   Úunescaper   Úlenr   r!   r    r   ÚescapeÚstringr"   r#   )r   r   r   Úmatch_variabler%   r&   Ú	completerr    Úunwrapped_textr   Ú
completionÚnew_textr   r   r   r   -   s,   €ÿÿ
ü€éz+GrammarCompleter._get_completions_for_matchÚitemsc                 c  s@   � ddd„}t ƒ }|D ]}||ƒ}||vr| |¡ |V  qdS )	zÇ
        Remove duplicates, while keeping the order.
        (Sometimes we have duplicates, because the there several matches of the
        same grammar, each yielding similar completions.)
        r0   r   r   útuple[str, int]c                 S  s   | j | jfS r   )r    r!   )r0   r   r   r   Úhash_completionZ   s   z<GrammarCompleter._remove_duplicates.<locals>.hash_completionN)r0   r   r   r3   )ÚsetÚadd)r   r2   r4   Úyielded_so_farr0   Ú
hash_valuer   r   r   r   S   s   €

€ûz#GrammarCompleter._remove_duplicatesN)r   r
   r   r   r   r   )r   r   r   r   r   r   )r   r	   r   r   r   r   )r2   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r      s    
	


&N)r<   Ú
__future__r   Úcollections.abcr   Úprompt_toolkit.completionr   r   r   Úprompt_toolkit.documentr   Úcompilerr	   r
   Ú__all__r   r   r   r   r   Ú<module>   s    ÿ