
     hL                        d dl mZ d dlZd dlmZmZmZmZmZ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
e          Z G d de          Z G d de          ZdS )    )annotationsN)CallableDictIterableList
NamedTupleOptionalTupleUnion)Document)FilterOrBool	to_filter)AnyFormattedTextStyleAndTextTuples   )CompleteEvent	Completer
Completion)WordCompleterFuzzyCompleterFuzzyWordCompleterc                  B    e Zd ZdZ	 	 	 dddZd dZd!dZd dZ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                    ||                     d          sJ || _        || _        || _        || _        t	          |          | _        d S )N^)
startswithr   r   r   r   r   )selfr   r   r   r   s        e/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/prompt_toolkit/completion/fuzzy_completer.py__init__zFuzzyCompleter.__init__0   sR     '"4"4S"9"99"	%l33    documentr   complete_eventr   Iterable[Completion]c                    |                                  r|                     ||          S | j                            ||          S N)r   _get_fuzzy_completionsr   get_completionsr$   r(   r)   s      r%   r.   zFuzzyCompleter.get_completions?   sG      	L..xHHH>11(NKKKr'   strc                4    | j         r| j         S | j        rdS dS )Nz[^\s]+z^[a-zA-Z0-9_]*)r   r   )r$   s    r%   _get_patternzFuzzyCompleter._get_patternG   s)    < 	 <9 	9r'   c           
   #  P  K   |                     t          j        |                                                     }t	          |j        d |j        t          |          z
           |j        t          |          z
            }t          | j	        
                    ||                    }g }|dk    rd |D             }nd                    t          t          j        |                    }d| d}t          j        |t          j                  }|D ]}	t          |                    |	j                            }
|
rht!          |
d 	          }|                    t%          t          |                    d
                    |                                |	                     dd}t+          ||	          }|D ]d}t-          |j        j        |j        j        t          |          z
  |j        j        |                     ||          |j        j                  V  ed S )N)r   )textcursor_position c                0    g | ]}t          d d |          S )r   )_FuzzyMatch).0compls     r%   
<listcomp>z9FuzzyCompleter._get_fuzzy_completions.<locals>.<listcomp>e   s$    UUU%[Au55UUUr'   z.*?z(?=(z))c                n    |                                  t          |                     d                    fS )Nr   )startlengroup)ms    r%   <lambda>z7FuzzyCompleter._get_fuzzy_completions.<locals>.<lambda>n   s#    qwwyy#aggajj//6R r'   )keyr   fuzzy_matchr8   r   tuple[int, int]c                    | j         | j        fS )z8Sort by start position, then by the length of the match.)	start_posmatch_length)rC   s    r%   sort_keyz7FuzzyCompleter._get_fuzzy_completions.<locals>.sort_keys   s    ",k.FFFr'   )r4   start_positiondisplay_metadisplaystyle)rC   r8   r   rD   )get_word_before_cursorrecompiler2   r   r4   r5   r>   listr   r.   joinmapescape
IGNORECASEfinditerminappendr8   r?   r=   sortedr   
completionrI   _display_meta_get_displayrL   )r$   r(   r)   word_before_cursor	document2inner_completionsfuzzy_matchespatregexr:   matchesbestrH   matchs                 r%   r-   z%FuzzyCompleter._get_fuzzy_completionsN   sY      &<<Jt002233 = 
 

 S!9C@R<S<S!SST$4s;M7N7NN
 
 
	
 !N**9nEE
 
 ,.## VUCTUUUMM**S,>??@@C ...CJsBM22E*  u~~ej99:: w,R,RSSSD!((#C

1$6$6

eLL  G G G G #=h???M" 	 	E %*$/>()) * #-;))%1CDD&,      	 	r'   rC   r8   r\   r   c                (    dfd} |            S )z@
        Generate formatted text for the display label.
        r   r   c                    } | j         j        }| j        dk    r| j         j        S g }|                    d|d | j                 f           t                    }|| j        | j        | j        z            D ]U}d}|r8|                                |d                                         k    r|dz  }|d= |                    ||f           V|                    d|| j        | j        z   d          f           |S )Nr   zclass:fuzzymatch.outsidezclass:fuzzymatch.insidez
.character)rY   r4   rG   rK   rW   rF   rP   lower)r@   wordresult
charactersc	classnamerC   r\   s         r%   get_displayz0FuzzyCompleter._get_display.<locals>.get_display   s   A<$D~"" |++)+F MM5tMakM7JKLLL 011J!+an(DDE . .5	 &!''))z!}/B/B/D/D"D"D-I"1y!n---- MM+T!+2N2P2P-QR   Mr'   )r   r    )r$   rC   r\   rm   s    `` r%   r[   zFuzzyCompleter._get_display   s6    	 	 	 	 	 	 	B {}}r'   )FNT)
r   r   r   r   r   r   r   r   r   r    r(   r   r)   r   r   r*   )r   r0   )rC   r8   r\   r0   r   r   )	__name__
__module____qualname____doc__r&   r.   r2   r-   r[   rn   r'   r%   r   r      s         > "%)4 4 4 4 4L L L L       6 6 6 6p( ( ( ( ( (r'   c                  (    e Zd ZdZ	 	 dddZ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#list[str] | Callable[[], list[str]]	meta_dictdict[str, str] | Noner   r   r   r    c                    || _         |pi | _        || _        t          | j         | j        | j                  | _        t          | j        | j                  | _        d S )N)ru   r   rw   )r   )ru   rw   r   r   word_completerr   fuzzy_completer)r$   ru   rw   r   s       r%   r&   zFuzzyWordCompleter.__init__   sc     
"b	+*49
 
 
  .d.A	RRRr'   r(   r   r)   r   r*   c                8    | j                             ||          S r,   )r{   r.   r/   s      r%   r.   z"FuzzyWordCompleter.get_completions   s     #33HnMMMr'   )NF)ru   rv   rw   rx   r   r   r   r    ro   )rp   rq   rr   rs   r&   r.   rn   r'   r%   r   r      s^          ,0	S S S S S N N N N N Nr'   c                  .    e Zd ZU ded<   ded<   ded<   dS )r8   intrG   rF   r   rY   N)rp   rq   rr   __annotations__rn   r'   r%   r8   r8      s3         NNNr'   r8   )
__future__r   rN   typingr   r   r   r   r   r	   r
   r   prompt_toolkit.documentr   prompt_toolkit.filtersr   r   prompt_toolkit.formatted_textr   r   baser   r   r   rz   r   __all__r   r   r8   rn   r'   r%   <module>r      sw   " " " " " " 				 U U U U U U U U U U U U U U U U U U U U , , , , , , : : : : : : : : N N N N N N N N 6 6 6 6 6 6 6 6 6 6 ) ) ) ) ) ) [ [ [ [ [Y [ [ [|N N N N N N N NB    *     r'   