
    T                        S r SSKJr  SSKJrJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  SSKJr  SSKJrJr  SSKrSr " S S\" \\5      5      r " S S\5      r  " S S\5      r! " S S\5      r" " S S\5      r# " S S\5      r$ " S S\5      r% " S S \5      r& " S! S"\5      r' " S# S$\5      r( " S% S&\5      r) " S' S(\5      r* " S) S*\5      r+ " S+ S,\5      r,g)-z
Processors are little transformation blocks that transform the token list from
a buffer before the BufferControl will render it to the screen.

They can insert tokens before or after, or highlight fragments by replacing the
token types.
    )unicode_literals)ABCMetaabstractmethod)with_metaclass)range)SimpleCache)Document)SEARCH_BUFFER)to_cli_filterViInsertMultipleMode)token_list_to_text)Integer)Token   )token_list_lenexplode_tokensN)	ProcessorTransformationHighlightSearchProcessorHighlightSelectionProcessorPasswordProcessor!HighlightMatchingBracketProcessorDisplayMultipleCursorsBeforeInput
AfterInputAppendAutoSuggestionConditionalProcessorShowLeadingWhiteSpaceProcessorShowTrailingWhiteSpaceProcessorTabsProcessorc                   .    \ rS rSrSr\S 5       rS rSrg)r   ,   ze
Manipulate the tokens for a given line in a
:class:`~prompt_toolkit.layout.controls.BufferControl`.
c                     [        U5      $ )a  
Apply transformation.  Returns a :class:`.Transformation` instance.

:param cli: :class:`.CommandLineInterface` instance.
:param lineno: The number of the line to which we apply the processor.
:param source_to_display: A function that returns the position in the
    `tokens` for any position in the source string. (This takes
    previous processors into account.)
:param tokens: List of tokens that we can transform. (Received from the
    previous processor.)
)r   selfclidocumentlinenosource_to_displaytokenss         3lib/third_party/prompt_toolkit/layout/processors.pyapply_transformationProcessor.apply_transformation1   s     f%%    c                     g)z]
Processors can override the focus.
(Used for the reverse-i-search prefix in DefaultPrompt.)
F r%   r&   s     r+   	has_focusProcessor.has_focus@   s    
 r.   r0   N)	__name__
__module____qualname____firstlineno____doc__r   r,   r2   __static_attributes__r0   r.   r+   r   r   ,   s      & &r.   r   c                   "    \ rS rSrSrSS jrSrg)r   H   a  
Transformation result, as returned by :meth:`.Processor.apply_transformation`.

Important: Always make sure that the length of `document.text` is equal to
           the length of all the text in `tokens`!

:param tokens: The transformed tokens. To be displayed, or to pass to the
    next processor.
:param source_to_display: Cursor position transformation from original string to
    transformed string.
:param display_to_source: Cursor position transformed from source string to
    original string.
Nc                 T    Xl         U=(       d    S U l        U=(       d    S U l        g )Nc                     U $ Nr0   is    r+   <lambda>)Transformation.__init__.<locals>.<lambda>X       r.   c                     U $ r>   r0   r?   s    r+   rA   rB   Y   rC   r.   )r*   r)   display_to_source)r%   r*   r)   rE   s       r+   __init__Transformation.__init__V   s!    !2!C{!2!C{r.   )rE   r)   r*   )NN)r4   r5   r6   r7   r8   rF   r9   r0   r.   r+   r   r   H   s    Dr.   r   c                   4    \ rS rSrSrS\S4S jrS rS rSr	g)	r   \   a(  
Processor that highlights search matches in the document.
Note that this doesn't support multiline search matches yet.

:param preview_search: A Filter; when active it indicates that we take
    the search text in real time while the user is typing, instead of the
    last active search state.
FNc                 R    [        U5      U l        X l        U=(       d    S U l        g )Nc                     U R                   $ r>   )search_stater&   s    r+   rA   3HighlightSearchProcessor.__init__.<locals>.<lambda>i   s
    AQAQr.   )r   preview_searchsearch_buffer_nameget_search_state)r%   rO   rP   rQ   s       r+   rF   !HighlightSearchProcessor.__init__e   s#    +N;"4 0 R5Qr.   c                     U R                  U5      (       aK  UR                  U R                     R                  (       a#  UR                  U R                     R                  $ U R	                  U5      R                  $ )z 
The text we are searching for.
)rO   buffersrP   textrQ   r1   s     r+   _get_search_text)HighlightSearchProcessor._get_search_textk   s`    
 s##D4K4K(L(Q(Q;;t667<<< ((-222r.   c                 
   U R                  U5      nS[        R                  R                  -   nS[        R                  -   nU(       Ga0  UR                  (       Gd  [        U5      n	[        U5      nUR                  (       a  [        R                  OSn
UR                  U:X  a  U" UR                  5      nOS n[        R                  " [        R                  " U5      XS9 H  nUb2  UR                  5       Us=:*  =(       a    UR                  5       :  Os  nOSn[!        UR                  5       UR                  5       5       H-  nX^   u  nnU(       a  X-   X^   S   4X^'   M   X-   X^   S   4X^'   M/     M     [#        U5      $ )N:r   )flagsFr   )rV   r   SearchMatchCurrentis_returningr   r   is_ignoring_casere
IGNORECASEcursor_position_rowcursor_position_colfinditerescapestartendr   r   )r%   r&   r'   r(   r)   r*   search_textsearchmatch_current_tokensearchmatch_token	line_textr[   cursor_columnmatch	on_cursorr@   	old_tokenrU   s                    r+   r,   -HighlightSearchProcessor.apply_transformationv   s?   ++C0$+e.?.?.G.G$G!#e&7&77s///*62I#F+F%(%9%9BMMqE ++v5 1(2N2N O $RYY{%;YT , % L L LI %Iu{{}eiik:A&,iOIt %.%JFIVWL$Y	%.%BFIaL$Q	 ; U f%%r.   )rQ   rO   rP   )
r4   r5   r6   r7   r8   r
   rF   rV   r,   r9   r0   r.   r+   r   r   \   s!     ',"&S	3&r.   r   c                       \ rS rSrSrS rSrg)r      z:
Processor that highlights the selection in the document.
c                    S[         R                  -   nUR                  U5      nU(       a  Uu  pU" U5      nU" U	5      n	[        U5      nUS:X  a1  U	S:X  a+  [	        U5      S:X  a  [        [         R                  S4/5      $ [        XS-   5       H"  n
U
[	        U5      :  d  M  XZ   u  pX-   U4XZ'   M$     [        U5      $ )NrY   r    r   )r   SelectedTextselection_range_at_liner   lenr   r   )r%   r&   r'   r(   r)   r*   selected_tokenselection_at_linefrom_tor@   ro   old_texts                r+   r,   0HighlightSelectionProcessor.apply_transformation   s     5#5#55 %<<VD)IE%e,E"2&B#F+FzbAg#f+*: &(:(:C'@&ABBu1f-A3v;.4i+	%.%?$J	 .
 f%%r.   r0   N)r4   r5   r6   r7   r8   r,   r9   r0   r.   r+   r   r      s    &r.   r   c                   (    \ rS rSrSrSS jrS rSrg)r      zu
Processor that turns masks the input. (For passwords.)

:param char: (string) Character to be used. "*" by default.
c                     Xl         g r>   char)r%   r   s     r+   rF   PasswordProcessor.__init__   s    	r.   c           	      ~    U VVs/ s H  u  pgX`R                   [        U5      -  4PM      nnn[        U5      $ s  snnf r>   )r   rw   r   )r%   r&   r'   r(   r)   r*   tokenrU   s           r+   r,   &PasswordProcessor.apply_transformation   s9    DJKF[U5))c$i/0FKf%% Ls   %9r   N)*)r4   r5   r6   r7   r8   rF   r,   r9   r0   r.   r+   r   r      s    
&r.   r   c                   2    \ rS rSrSrSrS	S jrS rS rSr	g)
r      a  
When the cursor is on or right after a bracket, it highlights the matching
bracket.

:param max_cursor_distance: Only highlight matching brackets when the
    cursor is within this distance. (From inside a `Processor`, we can't
    know which lines will be visible on the screen. But we also don't want
    to scan the whole document for matching brackets on each key press, so
    we limit to this value.)
z])}>c                 8    Xl         X l        [        SS9U l        g )N   )maxsize)charsmax_cursor_distancer   _positions_cache)r%   r   r   s      r+   rF   *HighlightMatchingBracketProcessor.__init__   s    
#6  +A 6r.   c                    UR                   (       aY  UR                   U R                  ;   a?  UR                  UR                  U R                  -
  UR                  U R                  -   S9nOUR
                  (       a  UR
                  U R                  ;   a|  UR
                  U R                  ;   ab  [        UR                  UR                  S-
  5      nUR                  UR                  U R                  -
  UR                  U R                  -   S9nOSnU(       a<  X!R                  -  nUR                  U5      u  p4X44UR                  UR                  4/$ / $ )zA
Return a list of (row, col) tuples that need to be highlighted.
)	start_posend_posr   N)current_charr   find_matching_bracket_positioncursor_positionr   char_before_cursor_closing_bracesr	   rU   translate_index_to_positionrb   rc   )r%   r'   posrowcols        r+   _get_positions_to_highlight=HighlightMatchingBracketProcessor._get_positions_to_highlight   s7   
   X%:%:djj%H99&669Q9QQ$44t7O7OO : QC
 ))h.I.I""/#'/'B'Bdjj'Px/G/G!/KLH99&669Q9QQ$44t7O7OO : QC C +++C;;C@HCJ!=!=x?[?[ \]]Ir.   c                   ^ ^ UR                   TR                  TR                  4nT R                  R	                  UUU 4S j5      nU(       a  U H{  u  pX:X  d  M  U" U	5      n	[        U5      nXY   u  pU	TR                  :X  a!  U
S[        R                  R                  -   -  n
O U
S[        R                  R                  -   -  n
X4XY'   M}     [        U5      $ )Nc                  &   > TR                  T 5      $ r>   )r   )r'   r%   s   r+   rA   HHighlightMatchingBracketProcessor.apply_transformation.<locals>.<lambda>   s    99(Cr.   rY   )render_counterrU   r   r   getr   rc   r   MatchingBracketCursorOtherr   )r%   r&   r'   r(   r)   r*   key	positionsr   r   r   rU   s   ` `         r+   r,   6HighlightMatchingBracketProcessor.apply_transformation   s    !!8==(2J2JK))--CE	 %=+C0C+F3F"(+KEh:::5+@+@+G+G!GG5+@+@+F+F!FF#(-FK & f%%r.   )r   r   r   N)z[](){}<>i  )
r4   r5   r6   r7   r8   r   rF   r   r,   r9   r0   r.   r+   r   r      s    	 O7:&r.   r   c                   2    \ rS rSrSr\" 5       rS rS rSr	g)r   i  z>
When we're in Vi block insert mode, display all the cursors.
c                     Xl         g r>   buffer_name)r%   r   s     r+   rF   DisplayMultipleCursors.__init__  s    &r.   c                    UR                   U R                     nU R                  U5      (       a  UR                  n[	        U5      nUR                  US5      nU[        UR                  U   5      -   n	S[        R                  R                  -   n
U HG  nXs=::  a  U	:  a  O  OU" X-
  5      nX\   u  pX-  nX4X\'   M-  X:X  d  M4  UR                  U
S45        MI     [        U5      $ [        U5      $ )Nr   rY   rt   )rT   r   _insert_multiplemultiple_cursor_positionsr   translate_row_col_to_indexrw   linesr   MultipleCursorsr   appendr   )r%   r&   r'   r(   r)   r*   buffr   r   r   token_suffixpcolumnr   rU   s                  r+   r,   +DisplayMultipleCursors.apply_transformation  s    {{4++,  %%66I#F+F !;;FAFI#hnnV&<"==G"U%:%:%A%AAL+G+.q}=F #).KE)E&+]FN\MM<"56  "&))!&))r.   r   N)
r4   r5   r6   r7   r8   r   r   rF   r,   r9   r0   r.   r+   r   r     s     -.'*r.   r   c                   @    \ rS rSrSrS rS r\\4S j5       r	S r
Srg)	r   i2  z
Insert tokens before the input.

:param get_tokens: Callable that takes a
    :class:`~prompt_toolkit.interface.CommandLineInterface` and returns the
    list of tokens to be inserted.
c                 4    [        U5      (       d   eXl        g r>   callable
get_tokensr%   r   s     r+   rF   BeforeInput.__init__:      
####$r.   c                    ^ US:X  a-  U R                  U5      nXe-   n[        U5      mU4S jnU4S jnOS nS n[        XTUS9$ )Nr   c                    > U T-   $ r>   r0   r@   shift_positions    r+   rA   2BeforeInput.apply_transformation.<locals>.<lambda>D  
    !n*<r.   c                    > U T-
  $ r>   r0   r   s    r+   rA   r   E  r   r.   r)   rE   )r   r   r   )	r%   r&   r'   r(   r)   r*   tokens_beforerE   r   s	           @r+   r,    BeforeInput.apply_transformation>  sV    Q; OOC0M"+F+M:N < < $ $f0AC 	Cr.   c                 $   ^^ UU4S jnU " U5      $ )zL
Create a :class:`.BeforeInput` instance that always inserts the same
text.
c                    > TT4/$ r>   r0   r&   rU   r   s    r+   get_static_tokens-BeforeInput.static.<locals>.get_static_tokensS      DM?"r.   r0   clsrU   r   r   s    `` r+   staticBeforeInput.staticM      	#$%%r.   c                 R    U R                   R                  < SU R                  < S3$ Nz(get_tokens=)	__class__r4   r   r%   s    r+   __repr__BeforeInput.__repr__W      NN##T__6 	6r.   r   Nr4   r5   r6   r7   r8   rF   r,   classmethodr   r   r   r9   r0   r.   r+   r   r   2  s.    %C  % & &6r.   r   c                   @    \ rS rSrSrS rS r\\4S j5       r	S r
Srg)	r   i\  z
Insert tokens after the input.

:param get_tokens: Callable that takes a
    :class:`~prompt_toolkit.interface.CommandLineInterface` and returns the
    list of tokens to be appended.
c                 4    [        U5      (       d   eXl        g r>   r   r   s     r+   rF   AfterInput.__init__d  r   r.   c                 l    X2R                   S-
  :X  a  [        XPR                  U5      -   S9$ [        US9$ )Nr   r*   )
line_countr   r   r$   s         r+   r,   AfterInput.apply_transformationh  s5    ((1,,!//#2F)FGG!00r.   c                 $   ^^ UU4S jnU " U5      $ )zK
Create a :class:`.AfterInput` instance that always inserts the same
text.
c                    > TT4/$ r>   r0   r   s    r+   r   ,AfterInput.static.<locals>.get_static_tokensu  r   r.   r0   r   s    `` r+   r   AfterInput.statico  r   r.   c                 R    U R                   R                  < SU R                  < S3$ r   r   r   s    r+   r   AfterInput.__repr__y  r   r.   r   Nr   r0   r.   r+   r   r   \  s-    %1  % & &6r.   r   c                   F    \ rS rSrSrS\R                  4S jrS rS r	Sr
g)r   i~  z
Append the auto suggestion to the input.
(The user can then press the right arrow the insert the suggestion.)

:param buffer_name: The name of the buffer from where we should take the
    auto suggestion. If not given, we take the current buffer.
Nc                     Xl         X l        g r>   r   r   )r%   r   r   s      r+   rF   AppendAutoSuggestion.__init__  s    &
r.   c                 n    U R                   (       a  UR                  U R                      $ UR                  $ r>   )r   rT   current_bufferr1   s     r+   _get_buffer AppendAutoSuggestion._get_buffer  s,    ;;t//00%%%r.   c                    X2R                   S-
  :X  an  U R                  U5      nUR                  (       a2  UR                  R                  (       a  UR                  R
                  nOSn[        XPR                  U4/-   S9$ [        US9$ )Nr    r   )r   r   
suggestionr'   is_cursor_at_the_endrU   r   r   )r%   r&   r'   r(   r)   r*   bufferr   s           r+   r,   )AppendAutoSuggestion.apply_transformation  ss    ((1,,%%c*F  V__%I%I#..33

!JJ
3K2L)LMM!00r.   r   )r4   r5   r6   r7   r8   r   AutoSuggestionrF   r   r,   r9   r0   r.   r+   r   r   ~  s#     $(u/C/C &1r.   r   c                   @    \ rS rSrSrS\R                  4S jrS rSr	g)r   i  z
Make leading whitespace visible.

:param get_char: Callable that takes a :class:`CommandLineInterface`
    instance and returns one character.
:param token: Token to be used.
Nc                 R    Ub  [        U5      (       d   eUc  S nX l        Xl        g )Nc                 b    SR                  U R                  R                  5       S5      S:X  a  ggN   ·replace   ?.encodeoutputencodingrM   s    r+   get_char9ShowLeadingWhiteSpaceProcessor.__init__.<locals>.get_char  (    ==!4!4!6	BdJ!r.   r   r   r  r%   r  r   s      r+   rF   'ShowLeadingWhiteSpaceProcessor.__init__  /    8H#5#555" 
 r.   c                    U(       as  [        U5      R                  S5      (       aT  U R                  U R                  U5      4n[	        U5      n[        [        U5      5       H  nXW   S   S:X  a  XeU'   M    O   [        U5      $ )Nrt   r   )r   
startswithr   r  r   r   rw   r   )r%   r&   r'   r(   r)   r*   tr@   s           r+   r,   3ShowLeadingWhiteSpaceProcessor.apply_transformation  st    (0;;C@@T]]3/0A#F+F3v;'9Q<3& !1I	 ( f%%r.   r  r   )
r4   r5   r6   r7   r8   r   LeadingWhiteSpacerF   r,   r9   r0   r.   r+   r   r     s     !%E,C,C !&r.   r   c                   @    \ rS rSrSrS\R                  4S jrS rSr	g)r   i  z
Make trailing whitespace visible.

:param get_char: Callable that takes a :class:`CommandLineInterface`
    instance and returns one character.
:param token: Token to be used.
Nc                 R    Ub  [        U5      (       d   eUc  S nX l        Xl        g )Nc                 b    SR                  U R                  R                  5       S5      S:X  a  ggr  r  rM   s    r+   r  :ShowTrailingWhiteSpaceProcessor.__init__.<locals>.get_char  r  r.   r  r  s      r+   rF   (ShowTrailingWhiteSpaceProcessor.__init__  r  r.   c                    U(       aw  US   S   R                  S5      (       a[  U R                  U R                  U5      4n[        U5      n[	        [        U5      S-
  SS5       H  nXW   S   nUS:X  a  XeU'   M    O   [        U5      $ )Nr   rt   )endswithr   r  r   r   rw   r   )	r%   r&   r'   r(   r)   r*   r  r@   r   s	            r+   r,   4ShowTrailingWhiteSpaceProcessor.apply_transformation  s    fRjm,,S11T]]3/0A#F+F 3v;?B3y|3; !1I 4 f%%r.   r  )
r4   r5   r6   r7   r8   r   TrailingWhiteSpacerF   r,   r9   r0   r.   r+   r   r     s     !%E,D,D !&r.   r   c                   D    \ rS rSrSrSSS\R                  4S jrS rSr	g)r    i  a  
Render tabs as spaces (instead of ^I) or make them visible (for instance,
by replacing them with dots.)

:param tabstop: (Integer) Horizontal space taken by a tab.
:param get_char1: Callable that takes a `CommandLineInterface` and return a
    character (text of length one). This one is used for the first space
    taken by the tab.
:param get_char2: Like `get_char1`, but for the rest of the space.
   Nc                    [        U[        5      (       d   eUb  [        U5      (       d   eUb  [        U5      (       d   eU=(       d    U=(       d    S U l        U=(       d    U=(       d    S U l        Xl        X@l        g )Nc                     g)N|r0   rM   s    r+   rA   (TabsProcessor.__init__.<locals>.<lambda>  s    r.   c                     g)Nu   ┈r0   rM   s    r+   rA   r(    s    r.   )
isinstancer   r   	get_char1	get_char2tabstopr   )r%   r-  r+  r,  r   s        r+   rF   TabsProcessor.__init__  sn    '7++++ HY$7$777 HY$7$777"DiDO"IiI4H
r.   c                   ^ [        U R                  5      nU R                  nU R                  U5      nU R	                  U5      n	[        U5      n0 m/ n
Sn[        U5       Hh  u  pUTU'   US   S:X  a?  XkU-  -
  nUS:X  a  UnU
R                  Xx45        U
R                  XyUS-
  -  45        X-  nMR  U
R                  U5        US-  nMj     UT[        U5      '   U4S jnU4S jn[        U
UUS9$ )Nr   r   	c                    > TU    $ )z.Maps original cursor position to the new one. r0   )from_positionposition_mappingss    r+   r)   =TabsProcessor.apply_transformation.<locals>.source_to_display   s    $]33r.   c                    > [        S TR                  5        5       5      nU S:  a   X   $ g! [         a    U S-  n  Of = fU S:  a  M#  N)z2Maps display cursor position to the original one. c              3   ,   #    U  H
  u  pX!4v   M     g 7fr>   r0   ).0kvs      r+   	<genexpr>PTabsProcessor.apply_transformation.<locals>.display_to_source.<locals>.<genexpr>&  s     -[AZqfAZs   r   r   )dictitemsKeyError)display_posposition_mappings_reversedr3  s     r+   rE   =TabsProcessor.apply_transformation.<locals>.display_to_source$  s^    )--[ARAXAXAZ-[)[&"%5BB    %1$K% "s   . A A r   )
intr-  r   r+  r,  r   	enumerater   rw   r   )r%   r&   r'   r(   r)   r*   r-  r   
separator1
separator2result_tokensr   r@   token_and_textcountrE   r3  s                   @r+   r,   "TabsProcessor.apply_transformation  s   dll#

 ^^C(
^^C(
  '!*6!2A#&a a D(=1A:#E $$e%89$$e519-E%FG$$^4q "3" *-#f+&	4		 //1 	1r.   )r+  r,  r-  r   )
r4   r5   r6   r7   r8   r   TabrF   r,   r9   r0   r.   r+   r    r      s     	  !DD		 41r.   r    c                   0    \ rS rSrSrS rS rS rS rSr	g)	r   i5  aV  
Processor that applies another processor, according to a certain condition.
Example::

    # Create a function that returns whether or not the processor should
    # currently be applied.
    def highlight_enabled(cli):
        return true_or_false

    # Wrapt it in a `ConditionalProcessor` for usage in a `BufferControl`.
    BufferControl(input_processors=[
        ConditionalProcessor(HighlightSearchProcessor(),
                             Condition(highlight_enabled))])

:param processor: :class:`.Processor` instance.
:param filter: :class:`~prompt_toolkit.filters.CLIFilter` instance.
c                 ^    [        U[        5      (       d   eXl        [        U5      U l        g r>   )r*  r   	processorr   filter)r%   rM  rN  s      r+   rF   ConditionalProcessor.__init__G  s&    )Y////"#F+r.   c                 ~    U R                  U5      (       a  U R                  R                  XX4U5      $ [        U5      $ r>   )rN  rM  r,   r   r$   s         r+   r,   )ConditionalProcessor.apply_transformationM  s>    ;;s>>66v&B B "&))r.   c                 f    U R                  U5      (       a  U R                  R                  U5      $ g)NF)rN  rM  r2   r1   s     r+   r2   ConditionalProcessor.has_focusU  s)    ;;s>>++C00r.   c                 n    U R                   R                  < SU R                  < SU R                  < S3$ )Nz(processor=z	, filter=r   )r   r4   rM  rN  r   s    r+   r   ConditionalProcessor.__repr__[  s'    NN##T^^T[[B 	Br.   )rN  rM  N)
r4   r5   r6   r7   r8   rF   r,   r2   r   r9   r0   r.   r+   r   r   5  s    ",*Br.   r   )-r8   
__future__r   abcr   r   sixr   	six.movesr   prompt_toolkit.cacher   prompt_toolkit.documentr	   prompt_toolkit.enumsr
   prompt_toolkit.filtersr   r   prompt_toolkit.layout.utilsr   prompt_toolkit.reactiver   prompt_toolkit.tokenr   utilsr   r   r`   __all__objectr   r   r   r   r   r   r   r   r   r   r   r   r    r   r0   r.   r+   <module>rd     s    ( '   , , . F : + & 1 	&w/ 8DV D(9&y 9&x&) &<&	 &E&	 E&P#*Y #*L'6) '6T6 6D19 1B!&Y !&H#&i #&LI1I I1X(B9 (Br.   