
    f:                        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y)    )annotationsN)CallableIterable
NamedTupleSequence)Document)FilterOrBool	to_filter)AnyFormattedTextStyleAndTextTuples   )CompleteEvent	Completer
Completion)WordCompleterFuzzyCompleterFuzzyWordCompleterc                  v    e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 	 	 d	dZ	 	 	 	 	 	 d
dZddZ	 	 	 	 	 	 d
dZ	 	 	 	 	 	 ddZy)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.
    Nc                    ||j                  d      sJ || _        || _        || _        || _        t	        |      | _        y )N^)
startswith	completerpatternWORDr
   enable_fuzzy)selfr   r   r   r   s        d/var/www/cvtools/html/venv/lib/python3.12/site-packages/prompt_toolkit/completion/fuzzy_completer.py__init__zFuzzyCompleter.__init__0   sE     '"4"4S"999"	%l3    c                ~    | j                         r| j                  ||      S | j                  j                  ||      S N)r   _get_fuzzy_completionsr   get_completionsr   documentcomplete_events      r   r#   zFuzzyCompleter.get_completions?   s:     ..xHH>>11(NKKr   c                N    | j                   r| j                   S | j                  ryy)Nz[^\s]+z^[a-zA-Z0-9_]*)r   r   )r   s    r   _get_patternzFuzzyCompleter._get_patternG   s     <<<<99r   c           
   #  d  K   |j                  t        j                  | j                                     }t	        |j
                  d |j                  t        |      z
   |j                  t        |      z
        }t        | j                  j                  ||            }g }|dk(  r|D cg c]  }t        dd|       }}ndj                  t        t        j                  |            }d| d}t        j                  |t        j                        }	|D ]y  }t        |	j!                  |j
                              }
|
s*t#        |
d 	      }|j%                  t        t        |j'                  d
            |j)                         |             { dd}t+        ||	      }|D ]  }t-        |j.                  j
                  |j.                  j0                  t        |      z
  |j.                  j2                  | j5                  ||      |j.                  j6                          y c c}w w)N)r   )textcursor_position r   z.*?z(?=(z))c                V    | j                         t        | j                  d            fS )Nr   )startlengroup)ms    r   <lambda>z7FuzzyCompleter._get_fuzzy_completions.<locals>.<lambda>n   s    qwwy#aggaj/6R r   )keyr   c                2    | j                   | j                  fS )z8Sort by start position, then by the length of the match.)	start_posmatch_length)fuzzy_matchs    r   sort_keyz7FuzzyCompleter._get_fuzzy_completions.<locals>.sort_keys   s    ",,k.F.FFFr   )r*   start_positiondisplay_metadisplaystyle)r7   _FuzzyMatchreturnztuple[int, int])get_word_before_cursorrecompiler(   r   r*   r+   r/   listr   r#   r=   joinmapescape
IGNORECASEfinditerminappendr0   r.   sortedr   
completionr9   _display_meta_get_displayr<   )r   r%   r&   word_before_cursor	document2inner_completionsfuzzy_matchescomplpatregexmatchesbestr8   matchs                 r   r"   z%FuzzyCompleter._get_fuzzy_completionsN   s     &<<JJt0023 = 

 S!9!9C@R<S!ST$44s;M7NN
	
 !NN**9nE
 ,.# DUU%[Au5UMU**S,>?@CR.CJJsBMM2E* u~~ejj9:w,RSD!((#C

1$6

eLG #=h?M" 	E %%**$//>>() * #--;;))%1CD&&,, 	) Vs   B*H0,H+ BH0C.H0c                "    dfd} |       S )z@
        Generate formatted text for the display label.
        c                    } | j                   j                  }| j                  dk(  r| j                   j                  S g }|j	                  d|d | j
                   f       t              }|| j
                  | j
                  | j                  z    D ]E  }d}|r,|j                         |d   j                         k(  r|dz  }|d= |j	                  ||f       G |j	                  d|| j
                  | j                  z   d  f       |S )Nr   zclass:fuzzymatch.outsidezclass:fuzzymatch.insidez
.character)rK   r*   r6   r;   rI   r5   rB   lower)r1   wordresult
charactersc	classnamer7   rN   s         r   get_displayz0FuzzyCompleter._get_display.<locals>.get_display   s   A<<$$D~~" ||+++)+F MM5tMakk7JKL 01J!++ann(DE .5	!'')z!}/B/B/D"D-I"1y!n-. MM+T!++2N2P-QR Mr   )r>   r    )r   r7   rN   r`   s    `` r   rM   zFuzzyCompleter._get_display   s    	B }r   )FNT)
r   r   r   boolr   z
str | Noner   r	   r>   Noner%   r   r&   r   r>   zIterable[Completion])r>   str)r7   r=   rN   re   r>   r   )	__name__
__module____qualname____doc__r   r#   r(   r"   rM   ra   r   r   r   r      s    > "%)44 4 	4
 #4 
4L L2?L	L 6 62?6	6p(&(<?(	(r   c                  @    e Zd ZdZ	 	 d	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZy)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.
    Nc                    || _         |xs i | _        || _        t        | j                   | j                  | j                        | _        t        | j                  | j                        | _        y )N)wordsr   	meta_dict)r   )rl   rm   r   r   word_completerr   fuzzy_completer)r   rl   rm   r   s       r   r   zFuzzyWordCompleter.__init__   sX     
"b	+**499
  .d.A.A		Rr   c                :    | j                   j                  ||      S r!   )ro   r#   r$   s      r   r#   z"FuzzyWordCompleter.get_completions   s     ##33HnMMr   )NF)rl   z+Sequence[str] | Callable[[], Sequence[str]]rm   zdict[str, str] | Noner   rb   r>   rc   rd   )rf   rg   rh   ri   r   r#   ra   r   r   r   r      s`     ,0	S:S )S 	S
 
S N N2?N	Nr   c                  ,    e Zd ZU ded<   ded<   ded<   y)r=   intr6   r5   r   rK   N)rf   rg   rh   __annotations__ra   r   r   r=   r=      s    Nr   r=   )
__future__r   r@   typingr   r   r   r   prompt_toolkit.documentr   prompt_toolkit.filtersr	   r
   prompt_toolkit.formatted_textr   r   baser   r   r   rn   r   __all__r   r   r=   ra   r   r   <module>r{      s\    " 	 ; ; , : N 6 6 ) [Y [|N NB* r   