
    W                     l    d dl mZ d dlmZ d dlmZ dZd Zd Zd Z	d Z
d	 Z G d
 de      Zd Zd Zy)    )unicode_literals
get_cwidth)Token)token_list_lentoken_list_widthtoken_list_to_textexplode_tokenssplit_linesfind_window_for_buffer_namec                 L    t         j                  t        fd| D              S )z
    Return the amount of characters in this token list.

    :param tokenlist: List of (token, text) or (token, text, mouse_handler)
                      tuples.
    c              3   L   K   | ]  }|d    k7  st        |d           ywr      N)len.0itemZeroWidthEscapes     .lib/third_party/prompt_toolkit/layout/utils.py	<genexpr>z!token_list_len.<locals>.<genexpr>   s%     P	T!W5Os47|	s   $$r   r   sum	tokenlistr   s    @r   r   r      s!     ++OP	PPP    c                 L    t         j                  t        fd| D              S )z
    Return the character width of this token list.
    (Take double width characters into account.)

    :param tokenlist: List of (token, text) or (token, text, mouse_handler)
                      tuples.
    c              3   Z   K   | ]"  }|d    D ]  }|d   k7  st        |        $ yw)r   r   Nr   )r   r   cr   s      r   r   z#token_list_width.<locals>.<genexpr>$   s.     bDGqtAwRaGaz!}G}s   ++r   r   s    @r   r   r      s!     ++Obbbbr   c                 X    t         j                  dj                  fd| D              S )z/
    Concatenate all the text parts again.
     c              3   :   K   | ]  }|d    k7  s|d     ywr    r   s     r   r   z%token_list_to_text.<locals>.<genexpr>,   s!     OytDG4N47ys   
)r   r   joinr   s    @r   r	   r	   '   s%     ++O77OyOOOr   c              #   |   K   g }t        |       D ]$  \  }}|j                  ||f       |dk(  s| g }& | yw)z8
    Iterator that yields tokenlists for each line.
    
N)r
   append)r   linetokenr   s       r   iter_token_linesr*   /   sH      D"9-qUAJ9JD . Js   ,<<c              #     K   g }| D ]  }t        |      dk(  rR|\  }}|j                  d      }|dd D ]  }|r|j                  ||f       | g } |j                  ||d   f       c|\  }}}|j                  d      }|dd D ]  }|r|j                  |||f       | g }  |j                  ||d   |f        | yw)a  
    Take a single list of (Token, text) tuples and yield one such list for each
    line. Just like str.split, this will yield at least one item.

    :param tokenlist: List of (token, text) or (token, text, mouse_handler)
                      tuples.
       r&   N)r   splitr'   )r   r(   r   r)   stringpartspartmouse_handlers           r   r   r   >   s      Dt9> ME6LL&Ecr
KK.
	 # KKb	*+ ,0(E6=LL&Ecr
KKm <=
	 # KKb	=9:; F Js   CCc                   B     e Zd ZdZ fdZd Z fdZd Z fdZ xZ	S )_ExplodedListz
    Wrapper around a list, that marks it as 'exploded'.

    As soon as items are added or the list is extended, the new items are
    automatically exploded as well.
    c                 :    t        t        | 
  |i | d| _        y )NT)superr4   __init__exploded)selfakw	__class__s      r   r7   z_ExplodedList.__init__u   s    mT+Q5"5r   c                 (    | j                  |g       y N)extend)r9   r   s     r   r'   z_ExplodedList.appendy   s    TFr   c                 >    t         t        |   t        |             y r>   )r6   r4   r?   r
   )r9   lstr<   s     r   r?   z_ExplodedList.extend|   s    mT).*=>r   c                     t         r>   )NotImplementedError)r9   indexr   s      r   insertz_ExplodedList.insert   s    !!r   c                     t        |t              st        ||dz         }t        |g      }t        t        |   ||       y)zh
        Ensure that when `(Token, 'long string')` is set, the string will be
        exploded.
        r   N)
isinstanceslicer
   r6   r4   __setitem__)r9   rD   valuer<   s      r   rI   z_ExplodedList.__setitem__   s<    
 %'%+Ew'mT.ue<r   )
__name__
__module____qualname____doc__r7   r'   r?   rE   rI   __classcell__)r<   s   @r   r4   r4   n   s&    ?"
= =r   r4   c                     t        | dd      r| S g }| D ]  \  }}|D ]  }|j                  ||f        ! t        |      S )a*  
    Turn a list of (token, text) tuples into another list where each string is
    exactly one character.

    It should be fine to call this function several times. Calling this on a
    list that is already exploded, is a null operation.

    :param tokenlist: List of (token, text) tuples.
    r8   F)getattrr'   r4   )r   resultr)   r/   r   s        r   r
   r
      sP     y*e,F"vAMM5!*%  #   r   c                    ddl m} t        | |      sJ ddlm} ddlm} | j                  j                  |       D ]C  }t        ||      st        |j                  |      s'|j                  j                  |k(  sA|c S  y)z
    Look for a :class:`~prompt_toolkit.layout.containers.Window` in the Layout
    that contains the :class:`~prompt_toolkit.layout.controls.BufferControl`
    for the given buffer and return it. If no such Window is found, return None.
    r   )CommandLineInterfacer   )Window)BufferControlN)prompt_toolkit.interfacerT   rG   
containersrU   controlsrV   layoutwalkcontentbuffer_name)clir]   rT   rU   rV   ls         r   r   r      sc     >c/000"'ZZ__S!a Z		=%Iyy$$3 "r   N)
__future__r   prompt_toolkit.utilsr   prompt_toolkit.tokenr   __all__r   r   r	   r*   r   listr4   r
   r   r#   r   r   <module>re      sI    ' + &Q	cP-`=D =B!.r   