U
    ouf                     @  s   d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
mZmZ ddlmZ ddlmZ dgZeeeeee def f ZG d	d deZdS )
zA
Nestedcompleter for completion of hierarchical data structures.
    )annotations)AnyIterableMappingSetUnion)CompleteEvent	Completer
Completion)WordCompleter)DocumentNestedCompleterNc                   @  sX   e Zd ZdZdddddddZd	d
ddZedd dddZddddddZdS )r   a  
    Completer which wraps around several other completers, and calls any the
    one that corresponds with the first word of the input.

    By combining multiple `NestedCompleter` instances, we can achieve multiple
    hierarchical levels of autocompletion. This is useful when `WordCompleter`
    is not sufficient.

    If you need multiple levels, check out the `from_nested_dict` classmethod.
    Tzdict[str, Completer | None]boolNone)optionsignore_casereturnc                 C  s   || _ || _d S Nr   r   )selfr   r    r   D/tmp/pip-unpacked-wheel-8milen2s/prompt_toolkit/completion/nested.py__init__   s    zNestedCompleter.__init__str)r   c                 C  s   d| j d| jdS )NzNestedCompleter(z, ignore_case=)r   )r   r   r   r   __repr__$   s    zNestedCompleter.__repr__
NestedDict)datar   c                 C  s   i }|  D ]n\}}t|tr(|||< qt|trB| |||< qt|trf| dd |D ||< q|dksrtd||< q| |S )a  
        Create a `NestedCompleter`, starting from a nested dictionary data
        structure, like this:

        .. code::

            data = {
                'show': {
                    'version': None,
                    'interfaces': None,
                    'clock': None,
                    'ip': {'interface': {'brief'}}
                },
                'exit': None
                'enable': None
            }

        The value should be `None` if there is no further completion at some
        point. If all values in the dictionary are None, it is also possible to
        use a set instead.

        Values in this data structure can be a completers as well.
        c                 S  s   i | ]
}|d qS r   r   ).0itemr   r   r   
<dictcomp>G   s      z4NestedCompleter.from_nested_dict.<locals>.<dictcomp>N)items
isinstancer	   dictfrom_nested_dictsetAssertionError)clsr   r   keyvaluer   r   r   r$   '   s    




z NestedCompleter.from_nested_dictr   r   zIterable[Completion])documentcomplete_eventr   c           
      c  s   |j  }t|j t| }d|kr| d }| j|}|d k	r|t|d   }t|t| | }t||j| d}	||	|E d H  n*t	t
| j | jd}|||E d H  d S )N r   )cursor_position)r   )Ztext_before_cursorlstriplensplitr   getr   r-   get_completionsr   listkeysr   )
r   r*   r+   textZstripped_lenZ
first_termZ	completerZremaining_textZmove_cursorZnew_documentr   r   r   r2   N   s$    
 zNestedCompleter.get_completionsN)T)	__name__
__module____qualname____doc__r   r   classmethodr$   r2   r   r   r   r   r      s    &)r9   
__future__r   typingr   r   r   r   r   Zprompt_toolkit.completionr   r	   r
   Z(prompt_toolkit.completion.word_completerr   Zprompt_toolkit.documentr   __all__r   r   r   r   r   r   r   <module>   s   