
    Qf(                        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
e          Z G d de          Z G d de          ZdS )    )annotationsN)CallableIterable
NamedTuple)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        [/var/www/html/env/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_matchr3   r   tuple[int, int]c                    | j         | j        fS )z8Sort by start position, then by the length of the match.)	start_posmatch_length)r>   s    r    sort_keyz7FuzzyCompleter._get_fuzzy_completions.<locals>.sort_keys   s    ",k.FFFr"   )r/   start_positiondisplay_metadisplaystyle)r>   r3   r   r?   )get_word_before_cursorrecompiler-   r   r/   r0   r9   listr   r)   joinmapescape
IGNORECASEfinditerminappendr3   r:   r8   sortedr   
completionrD   _display_meta_get_displayrG   )r   r#   r$   word_before_cursor	document2inner_completionsfuzzy_matchespatregexr5   matchesbestrC   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"   r>   r3   rW   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)rT   r/   rB   rF   rR   rA   rK   lower)r;   wordresult
charactersc	classnamer>   rW   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   r>   rW   rh   s    `` r    rV   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   r+   )r>   r3   rW   r+   r   r
   )	__name__
__module____qualname____doc__r!   r)   r-   r(   rV   ri   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)rp   r   rr   )r   )rp   rr   r   r   word_completerr   fuzzy_completer)r   rp   rr   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'   )rv   r)   r*   s      r    r)   z"FuzzyWordCompleter.get_completions   s     #33HnMMMr"   )NF)rp   rq   rr   rs   r   r   r   r   rj   )rk   rl   rm   rn   r!   r)   ri   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 )r3   intrB   rA   r   rT   N)rk   rl   rm   __annotations__ri   r"   r    r3   r3      s3         NNNr"   r3   )
__future__r   rI   typingr   r   r   prompt_toolkit.documentr   prompt_toolkit.filtersr   r	   prompt_toolkit.formatted_textr
   r   baser   r   r   ru   r   __all__r   r   r3   ri   r"   r    <module>r      sY   " " " " " " 				 1 1 1 1 1 1 1 1 1 1 , , , , , , : : : : : : : : 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"   