U
    bh                     @  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                   @  sV   e Zd ZdZddddddZdd	d
dddZdd	d
dddZd
d
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.
    r
   zdict[str, Completer]None)compiled_grammar
completersreturnc                 C  s   || _ || _d S N)r   r   )selfr   r    r   W/tmp/pip-unpacked-wheel-9s95tbmv/prompt_toolkit/contrib/regular_languages/completion.py__init__   s    zGrammarCompleter.__init__r   r   zIterable[Completion])documentcomplete_eventr   c                 c  s.   | j |j}|r*| | ||E d H  d S r   )r   Zmatch_prefixZtext_before_cursor_remove_duplicates_get_completions_for_match)r   r   r   mr   r   r   get_completions#   s
    
z GrammarCompleter.get_completionsr	   )matchr   r   c                 c  s   |  D ]}|j}|j}| j|}|r|j}| j||}t|t	|}	|
|	|D ]J}
|dt	||
j  |
j }t| j|||t	|j |
j|
jdV  qVq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)Z	end_nodesvarnamestartr   getvaluer   unescaper   lenr   r   r   r   escapestringr   r   )r   r   r   Zmatch_variabler    r!   Z	completerr   Zunwrapped_textr   
completionZnew_textr   r   r   r   -   s&    z+GrammarCompleter._get_completions_for_match)itemsr   c                 c  sD   ddddd}t  }|D ]$}||}||k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.)
        r   ztuple[str, int])r(   r   c                 S  s   | j | jfS r   )r   r   )r(   r   r   r   hash_completionZ   s    z<GrammarCompleter._remove_duplicates.<locals>.hash_completionN)setadd)r   r)   r*   Zyielded_so_farr(   Z
hash_valuer   r   r   r   S   s    
z#GrammarCompleter._remove_duplicatesN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s
   	
&N)r0   
__future__r   typingr   Zprompt_toolkit.completionr   r   r   Zprompt_toolkit.documentr   compilerr	   r
   __all__r   r   r   r   r   <module>   s   