o
    �õ±jn  ã                   @  sÀ   d dl mZ d dlZd dlmZmZmZmZ d dl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ÚMappingÚSequence)Ú
NamedTuple)ÚDocument)ÚFilterOrBoolÚ	to_filter)ÚAnyFormattedTextÚStyleAndTextTuplesé   )ÚCompleteEventÚ	CompleterÚ
Completion)ÚWordCompleterÚFuzzyCompleterÚFuzzyWordCompleterc                   @  sJ   e Zd ZdZ			d#d$dd„Zd%dd„Zd&dd„Zd%dd„Z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.
    FNTÚ	completerr   ÚWORDÚboolÚpatternú
str | NoneÚenable_fuzzyr	   ÚreturnÚNonec                 C  s<   |d u s|  d¡sJ ‚|| _|| _|| _|| _t|ƒ| _d S )Nú^)Ú
startswithr   r   r   r
   r   )Úselfr   r   r   r   © r   úœ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/completion/fuzzy_completer.pyÚ__init__1   s   zFuzzyCompleter.__init__Údocumentr   Úcomplete_eventr   úIterable[Completion]c                 C  s"   |   ¡ r
|  ||¡S | j ||¡S ©N)r   Ú_get_fuzzy_completionsr   Úget_completions©r   r"   r#   r   r   r    r'   @   s   zFuzzyCompleter.get_completionsÚstrc                 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_patternH   s
   zFuzzyCompleter._get_patternc              	   c  sB  � |j t |  ¡ ¡d�}t|jd |jt|ƒ … |jt|ƒ d�}t| j	 
||¡ƒ}g }|dkr8dd„ |D ƒ}nHd ttj|ƒ¡}d|› d�}t |tj¡}|D ]$}	t| |	j¡ƒ}
|
rtt|
d	d
„ d�}| tt| d¡ƒ| ¡ |	ƒ¡ qPd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>f   s    z9FuzzyCompleter._get_fuzzy_completions.<locals>.<listcomp>z.*?z(?=(z))c                 S  s   |   ¡ t|  d¡ƒfS )Nr   )ÚstartÚlenÚgroup)Úmr   r   r    Ú<lambda>o   s    z7FuzzyCompleter._get_fuzzy_completions.<locals>.<lambda>)Úkeyr   Úfuzzy_matchr.   r   útuple[int, int]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_keyt   s   z7FuzzyCompleter._get_fuzzy_completions.<locals>.sort_key)r+   Ústart_positionÚdisplay_metaÚdisplayÚstyle)r8   r.   r   r9   )Úget_word_before_cursorÚreÚcompiler*   r   r+   r,   r3   Úlistr   r'   ÚjoinÚmapÚescapeÚ
IGNORECASEÚfinditerÚminÚappendr.   r4   r2   Úsortedr   Ú
completionr=   Ú_display_metaÚ_get_displayr@   )r   r"   r#   Úword_before_cursorÚ	document2Úinner_completionsÚfuzzy_matchesÚpatÚregexr0   ÚmatchesÚbestr<   Úmatchr   r   r    r&   O   sL   €ÿþÿÿ€
ÿ

ùýz%FuzzyCompleter._get_fuzzy_completionsr8   r.   rP   r   c                   s   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}|rC| ¡ |d  ¡ krC|d7 }|d= | ||f¡ q,| d|| j| j d … f¡ |S )Nr   zclass:fuzzymatch.outsidezclass:fuzzymatch.insidez
.character)rM   r+   r;   r?   rK   r:   rD   Úlower)r5   ÚwordÚresultÚ
charactersÚcÚ	classname©r8   rP   r   r    Úget_displayŽ   s"   
ÿz0FuzzyCompleter._get_display.<locals>.get_displayN)r   r   r   )r   r8   rP   r`   r   r_   r    rO   ‡   s   !zFuzzyCompleter._get_display)FNT)
r   r   r   r   r   r   r   r	   r   r   ©r"   r   r#   r   r   r$   )r   r)   )r8   r.   rP   r)   r   r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r'   r*   r&   rO   r   r   r   r    r      s    û


8c                   @  s*   e Zd ZdZ		dddd„Z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.
    NFÚwordsú+Sequence[str] | Callable[[], Sequence[str]]Ú	meta_dictú%Mapping[str, AnyFormattedText] | Noner   r   r   r   c                 C  sB   || _ |pi | _|| _t| j | j| jd�| _t| j| jd�| _d S )N)rf   r   rh   )r   )rf   rh   r   r   Úword_completerr   Úfuzzy_completer)r   rf   rh   r   r   r   r    r!   ½   s   
ÿzFuzzyWordCompleter.__init__r"   r   r#   r   r$   c                 C  s   | j  ||¡S r%   )rk   r'   r(   r   r   r    r'   Í   s   z"FuzzyWordCompleter.get_completions)NF)rf   rg   rh   ri   r   r   r   r   ra   )rb   rc   rd   re   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;   r:   r   rM   N)rb   rc   rd   Ú__annotations__r   r   r   r    r.   Ó   s   
 r.   )Ú
__future__r   rB   Úcollections.abcr   r   r   r   Útypingr   Úprompt_toolkit.documentr   Úprompt_toolkit.filtersr	   r
   Úprompt_toolkit.formatted_textr   r   Úbaser   r   r   rj   r   Ú__all__r   r   r.   r   r   r   r    Ú<module>   s     þ !