
    ^h:                        S SK Jr  S SKrS SKJrJrJrJr  S SKJ	r	  S SK
JrJr  S SKJrJr  SSKJrJrJr  SS	KJr  S
S/r " S S
\5      r " S S\5      r " S S\5      rg)    )annotationsN)CallableIterable
NamedTupleSequence)Document)FilterOrBool	to_filter)AnyFormattedTextStyleAndTextTuples   )CompleteEvent	Completer
Completion)WordCompleterFuzzyCompleterFuzzyWordCompleterc                      \ rS rSrSr   S
         SS jjr      SS jrSS jr      SS jr      SS jr	S	r
g)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                    Ub  UR                  S5      (       d   eXl        X0l        X l        X0l        [	        U5      U l        g )N^)
startswith	completerpatternWORDr
   enable_fuzzy)selfr   r   r   r   s        c/home/james-whalen/.local/lib/python3.13/site-packages/prompt_toolkit/completion/fuzzy_completer.py__init__FuzzyCompleter.__init__0   s@     '"4"4S"9"999"	%l3    c                    U R                  5       (       a  U R                  X5      $ U R                  R                  X5      $ N)r   _get_fuzzy_completionsr   get_completionsr   documentcomplete_events      r   r%   FuzzyCompleter.get_completions?   s9     ..xHH>>11(KKr!   c                b    U R                   (       a  U R                   $ U R                  (       a  gg)Nz[^\s]+z^[a-zA-Z0-9_]*)r   r   )r   s    r   _get_patternFuzzyCompleter._get_patternG   s     <<<<99r!   c           
   #  b  #    UR                  [        R                  " U R                  5       5      S9n[	        UR
                  S UR                  [        U5      -
   UR                  [        U5      -
  S9n[        U R                  R                  XB5      5      n/ nUS:X  a  U Vs/ s H  n[        SSU5      PM     nnOSR                  [        [        R                  U5      5      nSU S3n[        R                  " U[        R                  5      n	U H}  n[        U	R!                  UR
                  5      5      n
U
(       d  M0  [#        U
S S	9nUR%                  [        [        UR'                  S
5      5      UR)                  5       U5      5        M     SS jn[+        XlS	9nU H}  n[-        UR.                  R
                  UR.                  R0                  [        U5      -
  UR.                  R2                  U R5                  X5      UR.                  R6                  S9v   M     g s  snf 7f)N)r   )textcursor_position r   z.*?z(?=(z))c                V    U R                  5       [        U R                  S5      5      4$ )Nr   )startlengroup)ms    r   <lambda>7FuzzyCompleter._get_fuzzy_completions.<locals>.<lambda>n   s    qwwy#aggaj/6Rr!   )keyr   c                2    U R                   U R                  4$ )z8Sort by start position, then by the length of the match.)	start_posmatch_length)fuzzy_matchs    r   sort_key7FuzzyCompleter._get_fuzzy_completions.<locals>.sort_keys   s    ",,k.F.FFFr!   )r.   start_positiondisplay_metadisplaystyle)r<   _FuzzyMatchreturnztuple[int, int])get_word_before_cursorrecompiler+   r   r.   r/   r3   listr   r%   rC   joinmapescape
IGNORECASEfinditerminappendr4   r2   sortedr   
completionr?   _display_meta_get_displayrB   )r   r'   r(   word_before_cursor	document2inner_completionsfuzzy_matchescomplpatregexmatchesbestr=   matchs                 r   r$   %FuzzyCompleter._get_fuzzy_completionsN   s     &<<JJt0023 = 

 S!9!9C@R<S!ST$44s;M7NN
	
 !NN**9E
 ,.# DUUCT%[Au5CTMUM**S,>?@CR.CJJsBMM2E*u~~ejj9:7w,RSD!((#C

1$6

eL +G #=?M"E %%**$//>>() * #--;;))%D&&,,  #) Vs   B%H/'H*>BH/C(H/c                &   ^^ SUU4S jjnU" 5       $ )z0
Generate formatted text for the display label.
c                   > Tn U R                   R                  nU R                  S:X  a  U R                   R                  $ / nUR	                  SUS U R
                   45        [        T5      nXR
                  U R
                  U R                  -     HK  nSnU(       a-  UR                  5       US   R                  5       :X  a  US-  nUS	 UR	                  XT45        MM     UR	                  SXR
                  U R                  -   S  45        U$ )Nr   zclass:fuzzymatch.outsidezclass:fuzzymatch.insidez
.character)rQ   r.   r;   rA   rO   r:   rH   lower)r5   wordresult
charactersc	classnamer<   rT   s         r   get_display0FuzzyCompleter._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yn- F MM+T++2N2P-QR Mr!   )rD   r    )r   r<   rT   rg   s    `` r   rS   FuzzyCompleter._get_display   s    	 	B }r!   )r   r   r   r   )FNT)
r   r   r   boolr   z
str | Noner   r	   rD   Noner'   r   r(   r   rD   zIterable[Completion])rD   str)r<   rC   rT   rn   rD   r   )__name__
__module____qualname____firstlineno____doc__r   r%   r+   r$   rS   __static_attributes__ri   r!   r   r   r      s    > "%)44 4 	4
 #4 
4L L2?L	L 6 62?6	6p(&(<?(	(r!   c                  N    \ rS rSrSr  S       SS jjr      S	S jrSrg)
r      a)  
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l         U=(       d    0 U l        X0l        [        U R                   U R                  U R                  S9U l        [        U R                  U R                  S9U l        g )N)wordsr   	meta_dict)r   )rx   ry   r   r   word_completerr   fuzzy_completer)r   rx   ry   r   s       r   r   FuzzyWordCompleter.__init__   sT     
"b	+**499
  .d.A.A		Rr!   c                8    U R                   R                  X5      $ r#   )r{   r%   r&   s      r   r%   "FuzzyWordCompleter.get_completions   s     ##33HMMr!   )r   r{   ry   rz   rx   )NF)rx   z+Sequence[str] | Callable[[], Sequence[str]]ry   zdict[str, str] | Noner   rk   rD   rl   rm   )ro   rp   rq   rr   rs   r   r%   rt   ri   r!   r   r   r      s`     ,0	S:S )S 	S
 
S N N2?N	Nr!   c                  4    \ rS rSr% S\S'   S\S'   S\S'   Srg)	rC      intr;   r:   r   rQ   ri   N)ro   rp   rq   rr   __annotations__rt   ri   r!   r   rC   rC      s    Nr!   rC   )
__future__r   rF   typingr   r   r   r   prompt_toolkit.documentr   prompt_toolkit.filtersr	   r
   prompt_toolkit.formatted_textr   r   baser   r   r   rz   r   __all__r   r   rC   ri   r!   r   <module>r      s\    " 	 ; ; , : N 6 6 ) [Y [|N NB* r!   