
    Mh:                        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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Sequence)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        i/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/prompt_toolkit/completion/fuzzy_completer.py__init__zFuzzyCompleter.__init__0   sQ     '"4"4S"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_matchr4   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#   )r0   start_positiondisplay_metadisplaystyle)r?   r4   r   r@   )get_word_before_cursorrecompiler.   r   r0   r1   r:   listr   r*   joinmapescape
IGNORECASEfinditerminappendr4   r;   r9   sortedr   
completionrE   _display_meta_get_displayrH   )r    r$   r%   word_before_cursor	document2inner_completionsfuzzy_matchespatregexr6   matchesbestrD   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?   r4   rX   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)rU   r0   rC   rG   rS   rB   rL   lower)r<   wordresult
charactersc	classnamer?   rX   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?   rX   ri   s    `` r!   rW   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?   r4   rX   r,   r   r   )	__name__
__module____qualname____doc__r"   r*   r.   r)   rW   rj   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+Sequence[str] | Callable[[], Sequence[str]]	meta_dictdict[str, str] | Noner   r   r   r   c                    || _         |pi | _        || _        t          | j         | j        | j                  | _        t          | j        | j                  | _        d S )N)rq   r   rs   )r   )rq   rs   r   r   word_completerr   fuzzy_completer)r    rq   rs   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(   )rw   r*   r+   s      r!   r*   z"FuzzyWordCompleter.get_completions   s     #33HnMMMr#   )NF)rq   rr   rs   rt   r   r   r   r   rk   )rl   rm   rn   ro   r"   r*   rj   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 )r4   intrC   rB   r   rU   N)rl   rm   rn   __annotations__rj   r#   r!   r4   r4      s3         NNNr#   r4   )
__future__r   rJ   typingr   r   r   r   prompt_toolkit.documentr   prompt_toolkit.filtersr	   r
   prompt_toolkit.formatted_textr   r   baser   r   r   rv   r   __all__r   r   r4   rj   r#   r!   <module>r      s_   " " " " " " 				 ; ; ; ; ; ; ; ; ; ; ; ; , , , , , , : : : : : : : : 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#   