ó
8…6ic           @   s¶   d  d l  Z  d  d l Z d  d l Z i  a d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d a d „  Z d „  Z d S(   iÿÿÿÿNt   Linec           B   s    e  Z d  Z d „  Z d „  Z RS(   s.   
    Represents a logical line in a file.
    c         C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ d |  _ d  S(   Nt    (   t   filenamet   numbert   startt   endt	   end_delimt   text(   t   selfR   R   R   (    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   __init__(   s    					c         C   s   d j  |  j |  j |  j ƒ S(   Ns   <Line {}:{} {!r}>(   t   formatR   R   R   (   R   (    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   __repr__<   s    (   t   __name__t
   __module__t   __doc__R	   R   (    (    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyR    #   s   	c         C   s+   |  | f t  k r# t  |  | f j Sd Sd S(   sq   
    Gets the text of the line with `filename` and `linenumber`, or the None if
    the line does not exist.
    N(   t   linesR   t   None(   R   t
   linenumber(    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   get_line_text@   s    c   	      C   sª   i  } x— t  j ƒ  D]‰ \ } } | \ } } | |  k rŒ | | k rŒ | | 7} | j | 7_ | j | 7_ | j | 7_ | j | 7_ n  | | | | f <q W| a  d S(   s8  
    Adjusts the locations in the line data structure.

    `filename`, `linenumber`
        The filename and first line number to adjust.

    `char_offset`
        The number of characters in the file to offset the code by,.

    `line_offset`
        The number of line in the file to offset the code by.
    N(   R   t	   iteritemsR   R   R   R   (	   R   R   t   char_offsett   line_offsett	   new_linest   keyt   linet   fnt   ln(    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   adjust_line_locationsL   s    
c   
      C   sÆ  t  j j r t d ƒ ‚ n  t  j j j s9 t d ƒ ‚ n  t | | f } t j	 d | j
 ƒ } | j d ƒ } |  s| d } n  | |  } | |  d }  t | j | j | j ƒ } | | _
 |  | _ | j t | ƒ | _ | j t |  ƒ | _ t j | j d d ƒ  } | j ƒ  }	 Wd	 QX|	 | j  |  |	 | j }	 t | | t |  ƒ |  j d
 ƒ ƒ t  j j L t j | j d d ƒ  } | j |	 ƒ Wd	 QXt  j j | j d t ƒWd	 QX| t | | f <d	 S(   s¹   
    Adds `code` immediately before `filename` and `linenumber`. Those must
    correspond to an existing line, and the code is inserted with the same
    indentation as that line.
    s<   config.clear_lines must be False for script editing to work.sA   The compile flag must have been given for script editing to work.s    *i    R   s   
t   rs   utf-8Ns   
t   wt   force(   t   renpyt   configt   clear_linest	   Exceptiont   gamet   argst   compileR   t   ret   matchR   t   groupR    R   R   R   t	   full_textt   lenR   R   t   codecst   opent   readR   t   countt   loadert	   auto_lockt   writet   add_autot   True(
   t   codeR   R   t   old_linet   mt   indentt   raw_codet   new_linet   ft   data(    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   insert_line_beforen   s2    	
		"c         C   s0  t  j j r t d ƒ ‚ n  t  j j j s9 t d ƒ ‚ n  t |  | f } t j	 | j
 d d ƒ  } | j ƒ  } Wd QX| | j | j !} | | j  | | j } t |  | f =t |  | t | ƒ | j d ƒ ƒ t  j j L t j	 | j
 d d ƒ  } | j | ƒ Wd QXt  j j | j
 d t ƒWd QXd S(	   si   
    Removes `linenumber` from `filename`. The line must exist and correspond
    to a logical line.
    s<   config.clear_lines must be False for script editing to work.sA   The compile flag must have been given for script editing to work.R   s   utf-8Ns   
R   R   (   R   R    R!   R"   R#   R$   R%   R   R+   R,   R   R-   R   R   R   R*   R.   R/   R0   R1   R2   R3   (   R   R   R   R:   R;   R4   (    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   remove_line   s    $c         C   s'   |  | f t  k r d St  |  | f j S(   s~   
    Returns the full text of `linenumber` from `filename`, including
    any comment or delimiter characters that exist.
    N(   R   R   R)   (   R   R   (    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   get_full_text¼   s    c         C   sR   g  } xE t  j j j D]4 } | j |  k r | j | k r | j | ƒ q q W| S(   sC   
    Returns a list of nodes that are found on the given line.
    (   R   R#   t   scriptt	   all_stmtsR   R   t   append(   R   R   t   rvt   i(    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   nodes_on_lineÈ   s
    c         C   sÓ   g  } g  } xp |  D]h } x. |  D] } | j  | k r  Pq  q  W| j | ƒ x. |  D] } | j  | k rQ PqQ qQ W| j | ƒ q Wt | ƒ d k r  t d ƒ ‚ n  t | ƒ d k rÁ t d ƒ ‚ n  | d | d f S(   sÄ   
    Finds the first and last nodes in `nodes`, a list of nodes. This assumes
    that all the nodes are "simple", with no control flow, and that all of
    the relevant nodes are in `nodes`.
    i   s%   Could not find unique first AST node.s$   Could not find unique last AST node.i    (   t   nextRA   R*   R"   (   t   nodest   firstst   lastsRC   t   j(    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   first_and_last_nodesÖ   s     c         C   sN   xG t  j j j D]6 } | j |  k r | j | k r | j | 7_ q q Wd S(   sí   
    This adjusts the line numbers in the the ast.

    `filename`
        The filename to adjust.

    `linenumber`
        The first line to adjust.

    `offset`
        The amount to adjust by. Positive numbers increase the line
    N(   R   R#   R?   R@   R   R   (   R   R   t   offsetRC   (    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   adjust_ast_linenumbers÷   s    c   
      C   sÌ   t  | | ƒ } t | ƒ \ } } t | j | d ƒ t j j j | j |  d | ƒ\ } } | j ƒ  } t j j j	 j
 | ƒ | s‡ d Sx+ t j j j	 D] }	 |	 j | | d ƒ q— Wt j j | | ƒ d S(   s   
    Adds `code`, which must be a textual line of Ren'Py code, to the AST
    immediately before `statement`, which should be an AST node.
    i   R   Ni    (   RD   RJ   RL   R   R   R#   R?   t   load_stringt   popR@   t   removet   replace_nextt   astt   chain_block(
   R4   R   R   RF   t   oldt   _t   blockt   _initt   ret_stmtRC   (    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   add_to_ast_before
  s    'c   	      C   sÜ   t  |  | ƒ } t | ƒ \ } } g  } xI t j j j D]8 } | | k rO q7 n  | j | | j ƒ | j | ƒ q7 W| t j j _ t j j j	 } x4 t
 | ƒ D]& } | | | k rž | j | | <qž qž Wt |  | d ƒ d S(   sû   
    Removes from the AST all statements that happen to be at `filename`
    and `linenumber`, then adjusts the line numbers appropriately.

    There's an assumption that the statement(s) on the line are "simple",
    not involving control flow.
    iÿÿÿÿN(   RD   RJ   R   R#   R?   R@   RP   RE   RA   t   namemapt   listRL   (	   R   R   RF   t   firstt   lastt	   new_stmtsRC   RY   t   k(    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   remove_from_ast$  s    	i   c          C   sx   d t  }  t  d 7a  t j j j t j j ƒ  j ƒ } | j } | j } t	 |  | | ƒ t
 |  | | ƒ t j j ƒ  d  S(   Ns   'Hello world %f'i   (   t   serialR   R#   R?   t   lookupt   contextt   currentR   R   RX   R<   t   exportst   restart_interaction(   t   st   nodeR   R   (    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   test_addL  s    

!		c          C   sp   t  j j j t  j j ƒ  j ƒ }  |  j } |  j } t | | ƒ t	 | | ƒ t  j
 j d d d t d t ƒ d  S(   Nt   checkpointsi    R   t   greedy(   R   R#   R?   Ra   Rb   Rc   R   R   R_   R=   Rd   t   rollbackR3   (   Rg   R   R   (    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   test_remove[  s    !		(   R   R&   R+   R   t   objectR    R   R   R<   R=   R>   RD   RJ   RL   RX   R_   R`   Rh   Rl   (    (    (    s_   Z:\home\souce\.local\share\Steam\steamapps\common\Doki Doki Literature Club\renpy\scriptedit.pyt   <module>   s"   		"	/				!			%	