a
    õ£Be(  ã                   @  s°   d dl mZ d dlZd dlmZmZmZ d dlmZ d dl	m
Z
mZ d dlmZmZ ddlmZmZmZ dd	lmZ d
dgZG dd
„ d
eƒZG dd„ deƒZG dd„ deƒZdS )é    )ÚannotationsN)ÚCallableÚIterableÚ
NamedTuple)ÚDocument)ÚFilterOrBoolÚ	to_filter)ÚAnyFormattedTextÚStyleAndTextTuplesé   )ÚCompleteEventÚ	CompleterÚ
Completion)ÚWordCompleterÚFuzzyCompleterÚFuzzyWordCompleterc                   @  sl   e Zd ZdZdddddd	d
œdd„Zddddœdd„Zddœdd„Zddddœdd„Zddddœdd„ZdS )r   a  
    Fuzzy completion.
    This wraps any other completer and turns it into a fuzzy completer.

    If the list of words is: ["leopard" , "gorilla", "dinosaur", "cat", "bee"]
    Then trying to complete "oar" would yield "leopard" and "dinosaur", but not
    the others, because they match the regular expression 'o.*a.*r'.
    Similar, in another application "djm" could expand to "django_migrations".

    The results are sorted by relevance, which is defined as the start position
    and the length of the match.

    Notice that this is not really a tool to work around spelling mistakes,
    like what would be possible with difflib. The purpose is rather to have a
    quicker or more intuitive way to filter the given completions, especially
    when many completions have a common prefix.

    Fuzzy algorithm is based on this post:
    https://blog.amjith.com/fuzzyfinder-in-10-lines-of-python

    :param completer: A :class:`~.Completer` instance.
    :param WORD: When True, use WORD characters.
    :param pattern: Regex pattern which selects the characters before the
        cursor that are considered for the fuzzy matching.
    :param enable_fuzzy: (bool or `Filter`) Enabled the fuzzy behavior. For
        easily turning fuzzyness on or off according to a certain condition.
    FNTr   Úboolz
str | Noner   ÚNone)Ú	completerÚWORDÚpatternÚenable_fuzzyÚreturnc                 C  s<   |d u s|  d¡sJ ‚|| _|| _|| _|| _t|ƒ| _d S )Nú^)Ú
startswithr   r   r   r   r   )Úselfr   r   r   r   © r   úl/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/completion/fuzzy_completer.pyÚ__init__0   s    zFuzzyCompleter.__init__r   r   úIterable[Completion]©ÚdocumentÚcomplete_eventr   c                 C  s&   |   ¡ r|  ||¡S | j ||¡S d S ©N)r   Ú_get_fuzzy_completionsr   Úget_completions©r   r!   r"   r   r   r   r%   ?   s    zFuzzyCompleter.get_completionsÚstr©r   c                 C  s   | j r| j S | jrdS dS )Nz[^\s]+z^[a-zA-Z0-9_]*)r   r   )r   r   r   r   Ú_get_patternG   s
    zFuzzyCompleter._get_patternc              	   c  sH  |j t |  ¡ ¡d�}t|jd |jt|ƒ … |jt|ƒ d�}t| j	 
||¡ƒ}g }|dkrndd„ |D ƒ}n–d ttj|ƒ¡}d|› d�}t |tj¡}|D ]H}	t| |	j¡ƒ}
|
ržt|
d	d
„ d�}| tt| d¡ƒ| ¡ |	ƒ¡ qždddœdd„}t||d�}|D ]:}t|jj|jjt|ƒ |jj|  ||¡|jjd�V  �qd S )N)r   )ÚtextÚcursor_positionÚ c                 S  s   g | ]}t d d |ƒ‘qS )r   )Ú_FuzzyMatch)Ú.0Úcomplr   r   r   Ú
<listcomp>e   ó    z9FuzzyCompleter._get_fuzzy_completions.<locals>.<listcomp>z.*?z(?=(z))c                 S  s   |   ¡ t|  d¡ƒfS )Nr   )ÚstartÚlenÚgroup)Úmr   r   r   Ú<lambda>n   r1   z7FuzzyCompleter._get_fuzzy_completions.<locals>.<lambda>)Úkeyr   r-   ztuple[int, int])Úfuzzy_matchr   c                 S  s   | j | jfS )z8Sort by start position, then by the length of the match.)Ú	start_posÚmatch_length)r8   r   r   r   Úsort_keys   s    z7FuzzyCompleter._get_fuzzy_completions.<locals>.sort_key)r*   Ústart_positionZdisplay_metaÚdisplayÚstyle)Zget_word_before_cursorÚreÚcompiler)   r   r*   r+   r3   Úlistr   r%   ÚjoinÚmapÚescapeÚ
IGNORECASEÚfinditerÚminÚappendr-   r4   r2   Úsortedr   Ú
completionr<   Z_display_metaÚ_get_displayr>   )r   r!   r"   Úword_before_cursorZ	document2Zinner_completionsZfuzzy_matchesÚpatÚregexr/   ÚmatchesÚbestr;   Úmatchr   r   r   r$   N   sF    ÿþÿÿÿ
ùz%FuzzyCompleter._get_fuzzy_completionsr-   r	   )r8   rL   r   c                   s   ddœ‡ ‡fdd„}|ƒ S )z@
        Generate formatted text for the display label.
        r	   r(   c                    s¸   ˆ } | j j}| jdkr| j jS g }| d|d | j… f¡ tˆƒ}|| j| j| j … D ]<}d}|r†| ¡ |d  ¡ kr†|d7 }|d= | ||f¡ qX| d|| j| j d … f¡ |S )Nr   zclass:fuzzymatch.outsidezclass:fuzzymatch.insidez
.character)rJ   r*   r:   r=   rH   r9   rA   Úlower)r5   ÚwordÚresultÚ
charactersÚcÚ	classname©r8   rL   r   r   Úget_display�   s"    
ÿz0FuzzyCompleter._get_display.<locals>.get_displayr   )r   r8   rL   rY   r   rX   r   rK   †   s    !zFuzzyCompleter._get_display)FNT)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r)   r$   rK   r   r   r   r   r      s      û8c                   @  s8   e Zd ZdZddddddœd	d
„Zddddœdd„ZdS )r   aA  
    Fuzzy completion on a list of words.

    (This is basically a `WordCompleter` wrapped in a `FuzzyCompleter`.)

    :param words: List of words or callable that returns a list of words.
    :param meta_dict: Optional dict mapping words to their meta-information.
    :param WORD: When True, use WORD characters.
    NFz#list[str] | Callable[[], list[str]]zdict[str, str] | Noner   r   )ÚwordsÚ	meta_dictr   r   c                 C  sB   || _ |pi | _|| _t| j | j| jd�| _t| j| jd�| _d S )N)r^   r   r_   )r   )r^   r_   r   r   Úword_completerr   Úfuzzy_completer)r   r^   r_   r   r   r   r   r   ¼   s    
ÿzFuzzyWordCompleter.__init__r   r   r   r    c                 C  s   | j  ||¡S r#   )ra   r%   r&   r   r   r   r%   Ì   s    z"FuzzyWordCompleter.get_completions)NF)rZ   r[   r\   r]   r   r%   r   r   r   r   r   ±   s
     üc                   @  s&   e Zd ZU ded< ded< ded< dS )r-   Úintr:   r9   r   rJ   N)rZ   r[   r\   Ú__annotations__r   r   r   r   r-   Ò   s   
r-   )Ú
__future__r   r?   Útypingr   r   r   Zprompt_toolkit.documentr   Zprompt_toolkit.filtersr   r   Zprompt_toolkit.formatted_textr	   r
   Úbaser   r   r   r`   r   Ú__all__r   r   r-   r   r   r   r   Ú<module>   s   þ !