
                         V    d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 dZ
 G d d	e      Zy
)z"
Completer for a regular grammar.
    )unicode_literals)	Completer
Completion)Document   )_CompiledGrammar)GrammarCompleterc                   (    e Zd ZdZd Zd Zd Z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                 h    t        |t              sJ t        |t              sJ || _        || _        y N)
isinstancer   dictcompiled_grammar
completers)selfr   r   s      Flib/third_party/prompt_toolkit/contrib/regular_languages/completion.py__init__zGrammarCompleter.__init__   s3    *,<===*d+++ 0$    c              #      K   | j                   j                  |j                        }|r-| j                  | j	                  ||            }|D ]  }|  y y wr   )r   match_prefixtext_before_cursor_remove_duplicates_get_completions_for_match)r   documentcomplete_eventmcompletionscs         r   get_completionsz GrammarCompleter.get_completions    s\     !!..x/J/JK11//>BDK ! !	 s   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"UJ-.Ts4y:;T;T/TUXbXgXggH %!2299'8L',s5<</@'@ * 2 2%/%<%<	> >	 #V! 0s   A
D"CD"c                 D    g }|D ]  }||vs|j                  |        |S )z
        Remove duplicates, while keeping the order.
        (Sometimes we have duplicates, because the there several matches of the
        same grammar, each yielding similar completions.)
        )append)r   itemsresultis       r   r   z#GrammarCompleter._remove_duplicatesJ   s-     Aa   r   N)__name__
__module____qualname____doc__r   r   r   r    r   r   r	   r	      s    %>@
r   r	   N)r<   
__future__r   prompt_toolkit.completionr   r   prompt_toolkit.documentr   compilerr   __all__r	   r=   r   r   <module>rC      s-    ( ; , &
Dy Dr   