a
    lc                     @   s   d dl Z d dlmZmZmZmZ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	 d	eZG dd
 d
eZG dd deZdS )    N)CallableDictIterableList
NamedTupleOptionalTupleUnion)Document)FilterOrBool	to_filter)AnyFormattedTextStyleAndTextTuples   )CompleteEvent	Completer
Completion)WordCompleterFuzzyCompleterFuzzyWordCompleterc                   @   sv   e Zd ZdZdeeee edddZ	e
eee dd	d
ZedddZe
eee dddZdeed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)	completerWORDpatternenable_fuzzyc                 C   s<   |d u s| dsJ || _|| _|| _|| _t|| _d S )N^)
startswithr   r   r   r   r   )selfr   r   r   r    r   e/var/www/html/django/DPS/env/lib/python3.9/site-packages/prompt_toolkit/completion/fuzzy_completer.py__init__.   s    zFuzzyCompleter.__init__documentcomplete_eventreturnc                 C   s&   |   r| ||S | j||S d S N)r   _get_fuzzy_completionsr   get_completionsr   r!   r"   r   r   r   r&   >   s    zFuzzyCompleter.get_completions)r#   c                 C   s   | j r| j S | jrdS dS )Nz[^\s]+z^[a-zA-Z0-9_]*)r   r   )r   r   r   r   _get_patternF   s
    zFuzzyCompleter._get_patternc              	   c   s6  |j t|  d}t|jd |jt|  |jt| d}t| j	
||}g }dttj|}d| d}t|tj}|D ]H}	t||	j}
|
rt|
dd d}|tt|d	| |	 qd
tttf ddd}t||d}|D ]8}t|jj|jjt| |jj| |||jjdV  qd S )N)r   )textcursor_positionz.*?z(?=(z))c                 S   s   |   t| dfS )Nr   )startlengroup)mr   r   r   <lambda>f       z7FuzzyCompleter._get_fuzzy_completions.<locals>.<lambda>)keyr   _FuzzyMatch)fuzzy_matchr#   c                 S   s   | j | jfS )z8Sort by start position, then by the length of the match.)	start_posmatch_length)r3   r   r   r   sort_keyk   s    z7FuzzyCompleter._get_fuzzy_completions.<locals>.sort_key)r)   start_positiondisplay_metadisplaystyle)Zget_word_before_cursorrecompiler(   r
   r)   r*   r,   listr   r&   joinmapescape
IGNORECASEfinditerminappendr2   r-   r+   r   intsortedr   
completionr7   r8   _get_displayr:   )r   r!   r"   word_before_cursorZ	document2ZcompletionsZfuzzy_matchespatregexZcomplmatchesbestr6   matchr   r   r   r%   M   s>    
z%FuzzyCompleter._get_fuzzy_completionsr2   )r3   rI   r#   c           	      C   s   |}|j j}|jdkr|j jS g }|d|d|j f t|}||j|j|j  D ]<}d}|r| |d  kr|d7 }|d= |||f qX|d||j|j d f |S )z@
        Generate formatted text for the display label.
        r   zclass:fuzzymatch.outsideNzclass:fuzzymatch.insidez
.character)rG   r)   r5   r9   rD   r4   r=   lower)	r   r3   rI   r.   wordresult
charactersc	classnamer   r   r   rH   }   s"    
zFuzzyCompleter._get_display)FNT)__name__
__module____qualname____doc__r   boolr   strr   r   r
   r   r   r   r&   r(   r%   r   rH   r   r   r   r   r      s(      1c                   @   s`   e Zd ZdZd
eee eg ee f f ee	eef  e
ddddZeeee d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	meta_dictr   r#   c                 C   sB   || _ |pi | _|| _t| j | j| jd| _t| j| jd| _d S )N)r[   r   r\   )r   )r[   r\   r   r   word_completerr   fuzzy_completer)r   r[   r\   r   r   r   r   r      s    
zFuzzyWordCompleter.__init__r    c                 C   s   | j ||S r$   )r^   r&   r'   r   r   r   r&      s    z"FuzzyWordCompleter.get_completions)NF)rU   rV   rW   rX   r	   r   rZ   r   r   r   rY   r   r
   r   r   r   r&   r   r   r   r   r      s     c                   @   s&   e Zd ZU eed< eed< eed< dS )r2   r5   r4   rG   N)rU   rV   rW   rE   __annotations__r   r   r   r   r   r2      s   
r2   )r;   typingr   r   r   r   r   r   r   r	   Zprompt_toolkit.documentr
   Zprompt_toolkit.filtersr   r   Zprompt_toolkit.formatted_textr   r   baser   r   r   r]   r   __all__r   r   r2   r   r   r   r   <module>   s   ( "