)L i:ddlmZddlZddlmZmZmZmZddlm Z ddl m Z m Z ddl mZmZddlmZmZmZdd lmZd d gZGd d eZGd d eZGddeZy)) annotationsN)CallableIterable NamedTupleSequence)Document) FilterOrBool to_filter)AnyFormattedTextStyleAndTextTuples) CompleteEvent Completer Completion) WordCompleterFuzzyCompleterFuzzyWordCompletercveZdZdZ d d dZ d dZd dZ d dZ d dZy) ra 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||jdsJ||_||_||_||_t ||_y)N^) startswith completerpatternWORDr enable_fuzzy)selfrrrrs o/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/prompt_toolkit/completion/fuzzy_completer.py__init__zFuzzyCompleter.__init__0sE'"4"4S"999"   %l3c~|jr|j||S|jj||SN)r_get_fuzzy_completionsrget_completionsrdocumentcomplete_events rr#zFuzzyCompleter.get_completions?s:    ..xH H>>11(NK KrcN|jr |jS|jryy)Nz[^\s]+z^[a-zA-Z0-9_]*)rr)rs r _get_patternzFuzzyCompleter._get_patternGs <<<<  99rc #dK|jtj|j}t |j d|j t|z |j t|z }t|jj||}g}|dk(r|Dcgc]}tdd|}}ndjttj|}d|d}tj|tj} |D]y}t| j!|j } | s*t#| d } |j%tt| j'd | j)|{d d } t+|| }|D]} t-| j.j | j.j0t|z | j.j2|j5| || j.j6 ycc}ww)N)r)textcursor_positionrz.*?z(?=(z))cV|jt|jdfS)Nr )startlengroup)ms rz7FuzzyCompleter._get_fuzzy_completions..nsqwwy#aggaj/6Rr)keyr c2|j|jfS)z8Sort by start position, then by the length of the match.) start_pos match_length) fuzzy_matchs rsort_keyz7FuzzyCompleter._get_fuzzy_completions..sort_keyss",,k.F.FFFr)r*start_position display_metadisplaystyle)r7 _FuzzyMatchreturnztuple[int, int])get_word_before_cursorrecompiler(rr*r+r/listrr#r=joinmapescape IGNORECASEfinditerminappendr0r.sortedr completionr9 _display_meta _get_displayr<)rr%r&word_before_cursor document2inner_completions fuzzy_matchescomplpatregexmatchesbestr8matchs rr"z%FuzzyCompleter._get_fuzzy_completionsNs&<<JJt0023=  S!9!9C@R?@CR.CJJsBMM2E* u~~ejj9:w,RSD!((#C 1 $6 eL   G#=h?M" E%%**$//>>() *#--;;))%1CD&&,,  )VsB*H0,H+BH0C.H0c"dfd }|S)z@ Generate formatted text for the display label. c}|jj}|jdk(r|jjSg}|j d|d|j ft }||j |j |jzD]E}d}|r,|j|djk(r|dz }|d=|j ||fG|j d||j |jzdf|S)Nrzclass:fuzzymatch.outsidezclass:fuzzymatch.insidez .character)rKr*r6r;rIr5rBlower)r1wordresult charactersc classnamer7rNs r get_displayz0FuzzyCompleter._get_display..get_displaysA<<$$D~~"||+++)+F MM5tMakk7JK L01J!++ ann(DE .5 !'')z!}/B/B/D"D-I"1  y!n-  . MM+T!++2N2P-QR Mr)r>r )rr7rNr`s `` rrMzFuzzyCompleter._get_displays B}r)FNT) rrrboolrz str | Nonerr r>Noner%rr&rr>zIterable[Completion])r>str)r7r=rNrer>r ) __name__ __module__ __qualname____doc__rr#r(r"rMrarrrrs>"%) 4 4 4 4 # 4  4L L2?L L 6 62?6 6p(&(rcrd)rfrgrhrirr#rarrrrs`,0 S:S)S S  S N N2?N Nrc,eZdZUded<ded<ded<y)r=intr6r5rrKN)rfrgrh__annotations__rarrr=r=sNrr=) __future__rr@typingrrrrprompt_toolkit.documentrprompt_toolkit.filtersr r prompt_toolkit.formatted_textr r baserrrrnr__all__rrr=rarrr{s\" ;;,:N66)  [Y[|NNB*r