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  qd 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      !