
    f                    l    d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZ dgZ G d	 de      Zy
)z"
Completer for a regular grammar.
    )annotations)Iterable)CompleteEvent	Completer
Completion)Document   )Match_CompiledGrammarGrammarCompleterc                  T    e Zd ZdZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 d	dZd
dZy)r   af  
    Completer which can be used for autocompletion according to variables in
    the grammar. Each variable can have a different autocompleter.

    :param compiled_grammar: `GrammarCompleter` instance.
    :param completers: `dict` mapping variable names of the grammar to the
                       `Completer` instances to be used for each variable.
    c                     || _         || _        y N)compiled_grammar
completers)selfr   r   s      n/var/www/cvtools/html/venv/lib/python3.12/site-packages/prompt_toolkit/contrib/regular_languages/completion.py__init__zGrammarCompleter.__init__   s     !1$    c              #     K   | j                   j                  |j                        }|r*| j                  | j	                  ||            E d {    y y 7 wr   )r   match_prefixtext_before_cursor_remove_duplicates_get_completions_for_match)r   documentcomplete_eventms       r   get_completionsz GrammarCompleter.get_completions#   sY      !!..x/J/JK..//>B   s   AAAAc           	   #  H  K   |j                         D ]
  }|j                  }|j                  }| j                  j	                  |      }|s:|j
                  }| j                  j                  ||      }t        |t        |            }	|j                  |	|      D ]  }
|dt        |      |
j                  z    |
j                  z   }t        | j                  j                  ||      |t        |j                        z
  |
j                   |
j"                           yw)z
        Yield all the possible completions for this input string.
        (The completer assumes that the cursor position was at the end of the
        input string.)
        N)textstart_positiondisplaydisplay_meta)	end_nodesvarnamestartr   getvaluer   unescaper   lenr   r!   r    r   escapestringr"   r#   )r   matchr   match_variabler%   r&   	completerr    unwrapped_textr   
completionnew_texts               r   r   z+GrammarCompleter._get_completions_for_match-   s     $oo/ 	N$,,G"((E++G4I%++ "&!6!6!?!?!N $NC4GH #,";";Hn"U J&'NTZ5N5N)NO$//*  %!2299'8L',s5<</@'@ * 2 2%/%<%<	 !	s   A
D"CD"c              #  z   K   dd}t               }|D ]$  } ||      }||vs|j                  |       | & yw)z
        Remove duplicates, while keeping the order.
        (Sometimes we have duplicates, because the there several matches of the
        same grammar, each yielding similar completions.)
        c                2    | j                   | j                  fS r   )r    r!   )r1   s    r   hash_completionz<GrammarCompleter._remove_duplicates.<locals>.hash_completionZ   s    ??J$=$===r   N)r1   r   returnztuple[str, int])setadd)r   itemsr5   yielded_so_farr1   
hash_values         r   r   z#GrammarCompleter._remove_duplicatesS   sI     	> 03u 	!J(4J/"":.  	!s    ;;N)r   r   r   zdict[str, Completer]r6   None)r   r   r   r   r6   Iterable[Completion])r-   r
   r   r   r6   r=   )r9   r=   r6   r=   )__name__
__module____qualname____doc__r   r   r   r    r   r   r   r      sd    % 0%>R%	% 2?	$$,9$	$L!r   N)rA   
__future__r   typingr   prompt_toolkit.completionr   r   r   prompt_toolkit.documentr   compilerr
   r   __all__r   rB   r   r   <module>rI      s8    #  J J , - 
Q!y Q!r   