
    T                        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dl
mZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZ ddlmZmZ ddlZdZ G d d eee            Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z% G d d e      Z& G d! d"e      Z' G d# d$e      Z( G d% d&e      Z) G d' d(e      Z* G d) d*e      Z+ G d+ d,e      Z,y)-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                   &    e Zd ZdZed        Zd Zy)r   zq
    Manipulate the tokens for a given line in a
    :class:`~prompt_toolkit.layout.controls.BufferControl`.
    c                     t        |      S )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_transformationzProcessor.apply_transformation1   s     f%%    c                      y)zu
        Processors can override the focus.
        (Used for the reverse-i-search prefix in DefaultPrompt.)
        F r$   r%   s     r*   	has_focuszProcessor.has_focus@   s    
 r,   N)__name__
__module____qualname____doc__r   r+   r0   r.   r,   r*   r   r   ,   s      & &r,   r   c                       e Zd ZdZddZy)r   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                 B    || _         |xs d | _        |xs d | _        y )Nc                     | S Nr.   is    r*   <lambda>z)Transformation.__init__.<locals>.<lambda>X       r,   c                     | S r8   r.   r9   s    r*   r;   z)Transformation.__init__.<locals>.<lambda>Y   r<   r,   )r)   r(   display_to_source)r$   r)   r(   r>   s       r*   __init__zTransformation.__init__V   s#    !2!C{!2!C{r,   )NN)r1   r2   r3   r4   r?   r.   r,   r*   r   r   H   s    Dr,   r   c                   *    e Zd ZdZdedfdZd Zd Zy)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                 J    t        |      | _        || _        |xs d | _        y )Nc                     | j                   S r8   )search_stater%   s    r*   r;   z3HighlightSearchProcessor.__init__.<locals>.<lambda>i   s
    AQAQr,   )r   preview_searchsearch_buffer_nameget_search_state)r$   rE   rF   rG   s       r*   r?   z!HighlightSearchProcessor.__init__e   s%    +N;"4 0 R5Qr,   c                     | j                  |      rF|j                  | j                     j                  r#|j                  | j                     j                  S | j	                  |      j                  S )z0
        The text we are searching for.
        )rE   buffersrF   textrG   r/   s     r*   _get_search_textz)HighlightSearchProcessor._get_search_textk   s]    
 s#D4K4K(L(Q(Q;;t667<<< ((-222r,   c                    | j                  |      }dt        j                  j                  z   }dt        j                  z   }|r|j                  st        |      }	t        |      }|j                  rt        j                  nd}
|j                  |k(  r ||j                        }nd }t        j                  t        j                  |      |	|
      D ]  }|-|j                         |cxk  xr |j                         k  nc }nd}t!        |j                         |j                               D ]-  }||   \  }}|r||z   ||   d   f||<   ||z   ||   d   f||<   /  t#        |      S )N:r   )flagsFr   )rK   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_textrO   cursor_columnmatch	on_cursorr:   	old_tokenrJ   s                    r*   r+   z-HighlightSearchProcessor.apply_transformationv   sL   ++C0$+e.?.?.G.G$G!#e&7&77s//*62I#F+F%(%9%9BMMqE ++v5 1(2N2N O $RYY{%;YeT , % L LI %Iu{{}eiik:A&,QiOIt %.1J%JFSTIVWL$Yq	%.1B%BF1IaL$Qq	 ; U f%%r,   )r1   r2   r3   r4   r
   r?   rK   r+   r.   r,   r*   r   r   \   s!     ',"&S	3&r,   r   c                       e Zd ZdZd Zy)r   zB
    Processor that highlights the selection in the document.
    c                 v   dt         j                  z   }|j                  |      }|r|\  }}	 ||      } ||	      }	t        |      }|dk(  r/|	dk(  r*t	        |      dk(  rt        t         j                  dfg      S t        ||	dz         D ]#  }
|
t	        |      k  s||
   \  }}||z   |f||
<   % t        |      S )NrM   r    r   )r   SelectedTextselection_range_at_liner   lenr   r   )r$   r%   r&   r'   r(   r)   selected_tokenselection_at_linefrom_tor:   rc   old_texts                r*   r+   z0HighlightSelectionProcessor.apply_transformation   s     5#5#55 %<<VD)IE2%e,E"2&B#F+FzbAg#f+*: &(:(:C'@&ABBub1f-A3v;.4Qi+	8%.%?$Jq	 .
 f%%r,   N)r1   r2   r3   r4   r+   r.   r,   r*   r   r      s    &r,   r   c                       e Zd ZdZddZd Zy)r   z
    Processor that turns masks the input. (For passwords.)

    :param char: (string) Character to be used. "*" by default.
    c                     || _         y r8   )char)r$   rq   s     r*   r?   zPasswordProcessor.__init__   s	    	r,   c           	      |    |D cg c]  \  }}|| j                   t        |      z  f! }}}t        |      S c c}}w r8   )rq   ri   r   )r$   r%   r&   r'   r(   r)   tokenrJ   s           r*   r+   z&PasswordProcessor.apply_transformation   s=    DJKF[UD5$))c$i/0FKf%% Ls   $8N)*)r1   r2   r3   r4   r?   r+   r.   r,   r*   r   r      s    
&r,   r   c                   (    e Zd ZdZdZddZd Zd Zy)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                 B    || _         || _        t        d      | _        y )N   )maxsize)charsmax_cursor_distancer   _positions_cache)r$   ry   rz   s      r*   r?   z*HighlightMatchingBracketProcessor.__init__   s    
#6  +A 6r,   c                    |j                   rZ|j                   | j                  v rB|j                  |j                  | j                  z
  |j                  | j                  z         }n|j
                  r|j
                  | j                  v r}|j
                  | j                  v ret        |j                  |j                  dz
        }|j                  |j                  | j                  z
  |j                  | j                  z         }nd}|r?||j                  z  }|j                  |      \  }}||f|j                  |j                  fgS g S )zQ
        Return a list of (row, col) tuples that need to be highlighted.
        )	start_posend_posr   N)current_charry   find_matching_bracket_positioncursor_positionrz   char_before_cursor_closing_bracesr	   rJ   translate_index_to_positionrV   rW   )r$   r&   posrowcols        r*   _get_positions_to_highlightz=HighlightMatchingBracketProcessor._get_positions_to_highlight   s=   
   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 8+++C;;C@HC#J!=!=x?[?[ \]]Ir,   c                     |j                   j                  j                  f} j                  j	                  | fd      }|r|D ]}  \  }}	||k(  s ||	      }	t        |      }||	   \  }
}|	j                  k(  r!|
dt        j                  j                  z   z  }
n |
dt        j                  j                  z   z  }
|
|f||	<    t        |      S )Nc                  &    j                         S r8   )r   )r&   r$   s   r*   r;   zHHighlightMatchingBracketProcessor.apply_transformation.<locals>.<lambda>   s    99(Cr,   rM   )render_counterrJ   r   r{   getr   rW   r   MatchingBracketCursorOtherr   )r$   r%   r&   r'   r(   r)   key	positionsr   r   rs   rJ   s   ` `         r*   r+   z6HighlightMatchingBracketProcessor.apply_transformation   s    !!8==(2J2JK))--CE	 %S&=+C0C+F3F"(+KE4h:::5+@+@+G+G!GG5+@+@+F+F!FF#($-F3K & f%%r,   N)z[](){}<>i  )r1   r2   r3   r4   r   r?   r   r+   r.   r,   r*   r   r      s    	 O7:&r,   r   c                   *    e Zd ZdZ e       Zd Zd Zy)r   zF
    When we're in Vi block insert mode, display all the cursors.
    c                     || _         y r8   )buffer_name)r$   r   s     r*   r?   zDisplayMultipleCursors.__init__  s
    &r,   c                    |j                   | j                     }| j                  |      r|j                  }t	        |      }|j                  |d      }|t        |j                  |         z   }	dt        j                  j                  z   }
|D ]I  }||cxk  r|	k  r#n n  |||z
        }||   \  }}||
z  }||f||<   1||	k(  s7|j                  |
df       K t        |      S t        |      S )Nr   rM   rf   )rI   r   _insert_multiplemultiple_cursor_positionsr   translate_row_col_to_indexri   linesr   MultipleCursorsr   appendr   )r$   r%   r&   r'   r(   r)   buffr   r}   r~   token_suffixpcolumnrs   rJ   s                  r*   r+   z+DisplayMultipleCursors.apply_transformation  s    {{4++,  %66I#F+F !;;FAFI#hnnV&<"==G"U%:%:%A%AAL+G+.q9}=F #).KE4\)E&+T]F6N'\MM<"56  "&))!&))r,   N)r1   r2   r3   r4   r   r   r?   r+   r.   r,   r*   r   r     s     -.'*r,   r   c                   6    e Zd ZdZd Zd Zeefd       Zd Z	y)r   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                 ,    t        |      sJ || _        y r8   callable
get_tokensr$   r   s     r*   r?   zBeforeInput.__init__:      
###$r,   c                     |dk(  r,| j                  |      }||z   }t        |      fd}fd}nd }d }t        |||      S )Nr   c                     | z   S r8   r.   r:   shift_positions    r*   r;   z2BeforeInput.apply_transformation.<locals>.<lambda>D  
    !n*<r,   c                     | z
  S r8   r.   r   s    r*   r;   z2BeforeInput.apply_transformation.<locals>.<lambda>E  r   r,   r(   r>   )r   r   r   )	r$   r%   r&   r'   r(   r)   tokens_beforer>   r   s	           @r*   r+   z BeforeInput.apply_transformation>  s[    Q; OOC0M"V+F+M:N < < $ $f8I0AC 	Cr,   c                 "    fd} | |      S )zd
        Create a :class:`.BeforeInput` instance that always inserts the same
        text.
        c                     fgS r8   r.   r%   rJ   rs   s    r*   get_static_tokensz-BeforeInput.static.<locals>.get_static_tokensS      DM?"r,   r.   clsrJ   rs   r   s    `` r*   staticzBeforeInput.staticM      	#$%%r,   c                 N    | j                   j                  d| j                  dS Nz(get_tokens=)	__class__r1   r   r$   s    r*   __repr__zBeforeInput.__repr__W      NN##T__6 	6r,   N
r1   r2   r3   r4   r?   r+   classmethodr   r   r   r.   r,   r*   r   r   2  s.    %C  % & &6r,   r   c                   6    e Zd ZdZd Zd Zeefd       Zd Z	y)r   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                 ,    t        |      sJ || _        y r8   r   r   s     r*   r?   zAfterInput.__init__d  r   r,   c                 z    ||j                   dz
  k(  rt        || j                  |      z         S t        |      S )Nr   r)   )
line_countr   r   r#   s         r*   r+   zAfterInput.apply_transformationh  s9    X((1,,!$//#2F)FGG!00r,   c                 "    fd} | |      S )zc
        Create a :class:`.AfterInput` instance that always inserts the same
        text.
        c                     fgS r8   r.   r   s    r*   r   z,AfterInput.static.<locals>.get_static_tokensu  r   r,   r.   r   s    `` r*   r   zAfterInput.statico  r   r,   c                 N    | j                   j                  d| j                  dS r   r   r   s    r*   r   zAfterInput.__repr__y  r   r,   Nr   r.   r,   r*   r   r   \  s-    %1  % & &6r,   r   c                   <    e Zd ZdZdej
                  fdZd Zd Zy)r   a  
    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                      || _         || _        y r8   )r   rs   )r$   r   rs   s      r*   r?   zAppendAutoSuggestion.__init__  s    &
r,   c                 d    | j                   r|j                  | j                      S |j                  S r8   )r   rI   current_bufferr/   s     r*   _get_bufferz AppendAutoSuggestion._get_buffer  s,    ;;t//00%%%r,   c                    ||j                   dz
  k(  rh| j                  |      }|j                  r-|j                  j                  r|j                  j
                  }nd}t        || j                  |fgz         S t        |      S )Nr    r   )r   r   
suggestionr&   is_cursor_at_the_endrJ   r   rs   )r$   r%   r&   r'   r(   r)   bufferr   s           r*   r+   z)AppendAutoSuggestion.apply_transformation  sw    X((1,,%%c*F  V__%I%I#..33

!DJJ
3K2L)LMM!00r,   )	r1   r2   r3   r4   r   AutoSuggestionr?   r   r+   r.   r,   r*   r   r   ~  s#     $(u/C/C &1r,   r   c                   6    e Zd ZdZdej
                  fdZd Zy)r   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                 H    |t        |      sJ |d }|| _        || _        y )Nc                 `    dj                  | j                  j                         d      dk(  ryyN   ·replace   ?.encodeoutputencodingrD   s    r*   get_charz9ShowLeadingWhiteSpaceProcessor.__init__.<locals>.get_char  (    ==!4!4!6	BdJ!r,   r   rs   r   r$   r   rs   s      r*   r?   z'ShowLeadingWhiteSpaceProcessor.__init__  0    8H#555" 
 r,   c                 
   |rwt        |      j                  d      r]| j                  | j                  |      f}t	        |      }t        t        |            D ]  }||   d   dk(  r|||<    t        |      S  t        |      S )Nrf   r   )r   
startswithrs   r   r   r   ri   r   )r$   r%   r&   r'   r(   r)   tr:   s           r*   r+   z3ShowLeadingWhiteSpaceProcessor.apply_transformation  s    (0;;C@T]]3/0A#F+F3v;'!9Q<3& !F1If%% ( f%%r,   )r1   r2   r3   r4   r   LeadingWhiteSpacer?   r+   r.   r,   r*   r   r     s     !%E,C,C !&r,   r   c                   6    e Zd ZdZdej
                  fdZd Zy)r   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                 H    |t        |      sJ |d }|| _        || _        y )Nc                 `    dj                  | j                  j                         d      dk(  ryyr   r   rD   s    r*   r   z:ShowTrailingWhiteSpaceProcessor.__init__.<locals>.get_char  r   r,   r   r   s      r*   r?   z(ShowTrailingWhiteSpaceProcessor.__init__  r   r,   c                    |r{|d   d   j                  d      rd| j                  | j                  |      f}t        |      }t	        t        |      dz
  dd      D ]   }||   d   }|dk(  r|||<    t        |      S  t        |      S )Nr   rf   )endswithrs   r   r   r   ri   r   )	r$   r%   r&   r'   r(   r)   r   r:   rq   s	            r*   r+   z4ShowTrailingWhiteSpaceProcessor.apply_transformation  s    fRjm,,S1T]]3/0A#F+F 3v;?B3ay|3; !F1If%% 4 f%%r,   )r1   r2   r3   r4   r   TrailingWhiteSpacer?   r+   r.   r,   r*   r   r     s     !%E,D,D !&r,   r   c                   :    e Zd ZdZdddej
                  fdZd Zy)r    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                     t        |t              sJ |t        |      sJ |t        |      sJ |xs |xs d | _        |xs |xs d | _        || _        || _        y )Nc                      y)N|r.   rD   s    r*   r;   z(TabsProcessor.__init__.<locals>.<lambda>  s    r,   c                      y)Nu   ┈r.   rD   s    r*   r;   z(TabsProcessor.__init__.<locals>.<lambda>  s    r,   )
isinstancer   r   	get_char1	get_char2tabstoprs   )r$   r   r   r   rs   s        r*   r?   zTabsProcessor.__init__  si    '7+++ HY$777 HY$777"DiDO"IiI4H
r,   c                    t        | j                        }| j                  }| j                  |      }| j	                  |      }	t        |      }i g }
d}t        |      D ]i  \  }}||<   |d   dk(  rA|||z  z
  }|dk(  r|}|
j                  ||f       |
j                  ||	|dz
  z  f       ||z  }T|
j                  |       |dz  }k |t        |      <   fd}fd}t        |
||      S )Nr   r   	c                     |    S )z/ Maps original cursor position to the new one. r.   )from_positionposition_mappingss    r*   r(   z=TabsProcessor.apply_transformation.<locals>.source_to_display   s    $]33r,   c                     t        d j                         D              }| dk\  r	 ||    S y# t        $ r | dz  } Y nw xY w| dk\  r!)z3 Maps display cursor position to the original one. c              3   *   K   | ]  \  }}||f  y wr8   r.   ).0kvs      r*   	<genexpr>zPTabsProcessor.apply_transformation.<locals>.display_to_source.<locals>.<genexpr>&  s     -[AZAq!fAZs   r   r   )dictitemsKeyError)display_posposition_mappings_reversedr   s     r*   r>   z=TabsProcessor.apply_transformation.<locals>.display_to_source$  sa    )--[ARAXAXAZ-[)[&"%5kBB    %1$K% "s   . ??r   )
intr   rs   r   r   r   	enumerater   ri   r   )r$   r%   r&   r'   r(   r)   r   rs   
separator1
separator2result_tokensr   r:   token_and_textcountr>   r   s                   @r*   r+   z"TabsProcessor.apply_transformation  s   dll#

 ^^C(
^^C(
  '!*6!2A~#&a a D(3=1A:#E $$eZ%89$$eZ519-E%FGu$$^4q "3" *-#f+&	4		 //1 	1r,   )r1   r2   r3   r4   r   Tabr?   r+   r.   r,   r*   r    r      s     	  !DD		 41r,   r    c                   (    e Zd ZdZd Zd Zd Zd Zy)r   a  
    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                 V    t        |t              sJ || _        t        |      | _        y r8   )r   r   	processorr   filter)r$   r  r  s      r*   r?   zConditionalProcessor.__init__G  s%    )Y///"#F+r,   c                 x    | j                  |      r| j                  j                  |||||      S t        |      S r8   )r  r  r+   r   r#   s         r*   r+   z)ConditionalProcessor.apply_transformationM  s@    ;;s>>66Xv'8&B B "&))r,   c                 \    | j                  |      r| j                  j                  |      S y)NF)r  r  r0   r/   s     r*   r0   zConditionalProcessor.has_focusU  s&    ;;s>>++C00r,   c                 h    | j                   j                  d| j                  d| j                  dS )Nz(processor=z	, filter=r   )r   r1   r  r  r   s    r*   r   zConditionalProcessor.__repr__[  s'    NN##T^^T[[B 	Br,   N)r1   r2   r3   r4   r?   r+   r0   r   r.   r,   r*   r   r   5  s    ",*Br,   r   )-r4   
__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   rT   __all__objectr   r   r   r   r   r   r   r   r   r   r   r   r    r   r.   r,   r*   <module>r(     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,   