
    nye(                        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y)    )annotationsN)CallableIterable
NamedTuple)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        K/usr/lib/python3/dist-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)r6   _FuzzyMatchreturnztuple[int, int])get_word_before_cursorrecompiler'   r   r)   r*   r.   listr   r"   r<   joinmapescape
IGNORECASEfinditerminappendr/   r-   sortedr   
completionr8   _display_meta_get_displayr;   )r   r$   r%   word_before_cursor	document2inner_completionsfuzzy_matchescomplpatregexmatchesbestr7   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)rJ   r)   r5   r:   rH   r4   rA   lower)r0   wordresult
charactersc	classnamer6   rM   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   r6   rM   r_   s    `` r   rL   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)r6   r<   rM   rd   r=   r
   )	__name__
__module____qualname____doc__r   r"   r'   r!   rL   r`   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   )rk   rl   r   r   word_completerr   fuzzy_completer)r   rk   rl   r   s       r   r   zFuzzyWordCompleter.__init__   sX     
"b	+**499
  .d.A.A		Rr   c                :    | j                   j                  ||      S r    )rn   r"   r#   s      r   r"   z"FuzzyWordCompleter.get_completions   s     ##33HnMMr   )NF)rk   z#list[str] | Callable[[], list[str]]rl   zdict[str, str] | Noner   ra   r=   rb   rc   )re   rf   rg   rh   r   r"   r`   r   r   r   r      s`     ,0	S2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<   intr5   r4   r   rJ   N)re   rf   rg   __annotations__r`   r   r   r<   r<      s    Nr   r<   )
__future__r   r?   typingr   r   r   prompt_toolkit.documentr   prompt_toolkit.filtersr   r	   prompt_toolkit.formatted_textr
   r   baser   r   r   rm   r   __all__r   r   r<   r`   r   r   <module>rz      s\    " 	 1 1 , : N 6 6 ) [Y [|N NB* r   