a
    õ£Be  ã                   @  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   úc/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/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u srJ ‚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   ó    z4NestedCompleter.from_nested_dict.<locals>.<dictcomp>N)ÚitemsÚ
isinstancer	   ÚdictÚfrom_nested_dictÚset)Ú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|v r’| ¡ d }| j |¡}|d u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   