B
    6Li[ÇR  ã               @   sD   d dl Z d dlZd dlZdd„ ZG dd„ deƒZG dd„ deƒZdS )é    Nc             C   s   |   ¡ }t|ƒ}| |¡S )N)ÚreadÚWorderÚget_word_at)ÚresourceÚoffsetZsource_codeZword_finder© r   ú/lib/python3.7/site-packages/rope/base/worder.pyÚget_name_at   s    r	   c               @   s
  e Zd ZdZdBdd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Z d=d>„ Z!d?d@„ Z"dAS )Cr   z¶A class for finding boundaries of words and expressions

    Note that in these methods, offset should be the index of the
    character not the index of the character after it.
    Fc             C   s*   t jj |¡}t||ƒ| _|| _|| _d S )N)ÚropeÚbaseÚsimplifyZ	real_codeÚ_RealFinderÚcode_finderÚhandle_ignoresÚcode)Úselfr   r   Z
simplifiedr   r   r   Ú__init__   s    zWorder.__init__c             C   sD   t jj | j¡}t| j| jƒ| _dd„ |D ƒ| _dd„ |D ƒ| _d S )Nc             S   s   g | ]}|d  ‘qS )r   r   )Ú.0Úignoredr   r   r   ú
<listcomp>   s    z(Worder._init_ignores.<locals>.<listcomp>c             S   s   g | ]}|d  ‘qS )é   r   )r   r   r   r   r   r      s    )	r
   r   r   Zignored_regionsr   r   Údumb_finderÚstartsÚends)r   Zignoresr   r   r   Ú_init_ignores   s    zWorder._init_ignoresc             C   s`   | j rPt| dƒs|  ¡  t | j|¡}|dkrP|| j|d  k rPt| j|ƒ|ƒS t| j|ƒ|ƒS )Nr   r   r   )	r   Úhasattrr   Úbisectr   r   Úgetattrr   r   )r   Únamer   Ústartr   r   r   Ú_context_call    s    
zWorder._context_callc             C   s   |   d|¡S )NÚget_primary_at)r    )r   r   r   r   r   r!   )   s    zWorder.get_primary_atc             C   s   |   d|¡S )Nr   )r    )r   r   r   r   r   r   ,   s    zWorder.get_word_atc             C   s   |   d|¡S )NÚget_primary_range)r    )r   r   r   r   r   r"   /   s    zWorder.get_primary_rangec             C   s   |   d|¡S )NÚget_splitted_primary_before)r    )r   r   r   r   r   r#   2   s    z"Worder.get_splitted_primary_beforec             C   s   |   d|¡S )NÚget_word_range)r    )r   r   r   r   r   r$   5   s    zWorder.get_word_rangec             C   s   | j  |¡S )N)r   Úis_function_keyword_parameter)r   r   r   r   r   r%   8   s    z$Worder.is_function_keyword_parameterc             C   s   | j  |¡S )N)r   Ú%is_a_class_or_function_name_in_header)r   r   r   r   r   r&   ;   s    z,Worder.is_a_class_or_function_name_in_headerc             C   s   | j  |¡S )N)r   Úis_from_statement_module)r   r   r   r   r   r'   >   s    zWorder.is_from_statement_modulec             C   s   | j  |¡S )N)r   Úis_from_aliased)r   r   r   r   r   r(   A   s    zWorder.is_from_aliasedc             C   s   | j  |¡S )N)r   Ú"is_import_statement_aliased_module)r   r   r   r   r   r)   D   s    z)Worder.is_import_statement_aliased_modulec             C   s   | j  |¡S )N)r   Úfind_parens_start_from_inside)r   r   r   r   r   r*   G   s    z$Worder.find_parens_start_from_insidec             C   s   | j  |¡S )N)r   Úis_a_name_after_from_import)r   r   r   r   r   r+   J   s    z"Worder.is_a_name_after_from_importc             C   s   | j  |¡S )N)r   Úis_from_statement)r   r   r   r   r   r,   M   s    zWorder.is_from_statementc             C   s   | j  |¡S )N)r   Úget_from_aliased)r   r   r   r   r   r-   P   s    zWorder.get_from_aliasedc             C   s   | j  |¡S )N)r   Úis_import_statement)r   r   r   r   r   r.   S   s    zWorder.is_import_statementc             C   s   | j  |¡S )N)r   Úis_assigned_here)r   r   r   r   r   r/   V   s    zWorder.is_assigned_herec             C   s   | j  |¡S )N)r   Úis_a_function_being_called)r   r   r   r   r   r0   Y   s    z!Worder.is_a_function_being_calledc             C   s   | j  |¡S )N)r   Úget_word_parens_range)r   r   r   r   r   r1   \   s    zWorder.get_word_parens_rangec             C   s   | j  |¡S )N)r   Úis_name_assigned_in_class_body)r   r   r   r   r   r2   _   s    z%Worder.is_name_assigned_in_class_bodyc             C   s   | j  |¡S )N)r   Úis_on_function_call_keyword)r   r   r   r   r   r3   b   s    z"Worder.is_on_function_call_keywordc             C   s   | j  |¡S )N)r   Ú_find_parens_start)r   r   r   r   r   r4   e   s    zWorder._find_parens_startc             C   s   | j  ||¡S )N)r   Úget_parameters)r   ÚfirstÚlastr   r   r   r5   h   s    zWorder.get_parametersc             C   s   | j  |¡S )N)r   Úget_from_module)r   r   r   r   r   r8   k   s    zWorder.get_from_modulec             C   s   | j  |¡S )N)r   Ú!is_assigned_in_a_tuple_assignment)r   r   r   r   r   r9   n   s    z(Worder.is_assigned_in_a_tuple_assignmentc             C   s   | j  |¡S )N)r   Úget_assignment_type)r   r   r   r   r   r:   q   s    zWorder.get_assignment_typec             C   s   | j  |¡S )N)r   Úget_function_and_args_in_header)r   r   r   r   r   r;   t   s    z&Worder.get_function_and_args_in_headerc             C   s   | j  |¡S )N)r   Úget_lambda_and_args)r   r   r   r   r   r<   w   s    zWorder.get_lambda_and_argsc             C   s   | j  |¡S )N)r   Úfind_function_offset)r   r   r   r   r   r=   z   s    zWorder.find_function_offsetN)F)#Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r!   r   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r8   r9   r:   r;   r<   r=   r   r   r   r   r      s@   
	r   c               @   s`  e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Z d=d>„ Z!d?d@„ Z"dAdB„ Z#dCdD„ Z$dEdF„ Z%dGdH„ Z&dYdKdL„Z'dMdN„ Z(dOdP„ Z)dQdR„ Z*dZdTdU„Z+dVdW„ Z,dXS )[r   c             C   s   || _ || _d S )N)r   Úraw)r   r   rB   r   r   r   r   €   s    z_RealFinder.__init__c             C   s,   |}x|dkr"|   |¡r"|d8 }qW |d S )Nr   r   )Ú_is_id_char)r   r   Zcurrent_offsetr   r   r   Ú_find_word_start„   s    z_RealFinder._find_word_startc             C   s2   x,|d t | jƒk r,|  |d ¡r,|d7 }qW |S )Nr   )Úlenr   rC   )r   r   r   r   r   Ú_find_word_endŠ   s    "z_RealFinder._find_word_endc             C   s@   x4|dkr4| j |  ¡ r4| j | dkr*|S |d8 }qW td|ƒS )Nr   Ú
r   éÿÿÿÿ)r   ÚisspaceÚmax)r   r   r   r   r   Ú_find_last_non_space_char   s
    z%_RealFinder._find_last_non_space_charc             C   s(   |   |¡}| j|  |¡|  |¡d … S )Nr   )Ú_get_fixed_offsetrB   rD   rF   )r   r   r   r   r   r   –   s    
z_RealFinder.get_word_atc             C   sj   |t | jƒkr|d S |  |¡sf|dkr>|  |d ¡r>|d S |t | jƒd k rf|  |d ¡rf|d S |S )Nr   r   )rE   r   rC   )r   r   r   r   r   rL   ›   s    
 z_RealFinder._get_fixed_offsetc             C   s   | j |  ¡ p| j | dkS )NÚ_)r   Úisalnum)r   r   r   r   r   rC   ¥   s    z_RealFinder._is_id_charc             C   s4   | j | }y| j  |d|¡S  tk
r.   dS X d S )Nr   )r   ÚrindexÚ
ValueError)r   r   Zkindr   r   r   Ú_find_string_start¨   s
    
z_RealFinder._find_string_startc             C   sT   |   |d ¡}x@|dkrN| j| dkrN| j| dkr>|  |¡}|   |d ¡}qW |S )Nr   r   z[({z:,)rK   r   Ú_find_primary_start)r   r   r   r   r   r4   ¯   s    
z_RealFinder._find_parens_startc             C   sz   |}| j | dkr|d S | j |  ¡ r2|  |¡}| j | dkrJ|  |¡S | j | dkrb|  |¡S |  |¡rv|  |¡S |S )NrG   r   z'"z)]})r   rI   rK   rQ   r4   rC   rD   )r   r   Z
old_offsetr   r   r   Ú_find_atom_start·   s    




z_RealFinder._find_atom_startc             C   sŒ   |}|   |¡}x2|dkr@| j| dkr@|  |¡}|   |d ¡}qW |dkrˆ| j| dksb|  |¡rˆ|  |¡}t | j||d … ¡sˆ|S |S )z±It tries to find the undotted primary start

        It is different from `self._get_atom_start()` in that it
        follows function calls, too; such as in ``f(x)``.

        r   z)]r   z"'})])rK   r   r4   rC   rS   ÚkeywordÚ	iskeyword)r   r   Z	last_atomZ
atom_startr   r   r   Ú_find_primary_without_dot_startÅ   s    



z+_RealFinder._find_primary_without_dot_startc             C   s   |t | jƒkrt | jƒd }| j| dkr6|  |¡}n|d }xL|dkrŠ|  |d ¡}|dksl| j| dkrnP |  |d ¡}|  |¡s@P q@W |S )Nr   Ú.r   )rE   r   rV   rK   rC   )r   r   Úprevr   r   r   rR   Ø   s    

z_RealFinder._find_primary_startc             C   s*   |   |¡}|  |¡\}}| j||…  ¡ S )N)rL   r"   rB   Ústrip)r   r   r   Úendr   r   r   r!   é   s    
z_RealFinder.get_primary_atc             C   s@  |dkrdS |d }|   |¡}|  |¡}| j||…  ¡ dkrB|}| j|  ¡ rT|}| j||…  ¡ dkrn|}||  kr‚|kršn n|  |¡sšdd|fS ||kr¶d| j||… |fS | j| dkrØ| j||… d|fS |}| j| dkrø|  |d ¡}|  |d ¡}| j|  ¡ r|}| j||d … | j||… |fS dS )z{returns expression, starting, starting_offset

        This function is used in `rope.codeassist.assist` function.
        r   )Ú r[   r   r   r[   rW   N)rS   rR   r   rY   rI   rC   rB   rK   )r   r   rZ   Ú
word_startZ
real_startZlast_dot_positionZlast_char_positionr   r   r   r#   î   s2    

"
z'_RealFinder.get_splitted_primary_beforec             C   s.   y| j  dd|d ¡S  tk
r(   dS X d S )NrG   r   r   )r   rO   rP   )r   r   r   r   r   Ú_get_line_start  s    z_RealFinder._get_line_startc             C   s.   y| j  d|¡S  tk
r(   t| j ƒS X d S )NrG   )r   ÚindexrP   rE   )r   r   r   r   r   Ú_get_line_end  s    z_RealFinder._get_line_endc             C   sb   |   |d ¡}|  |¡d }d| j||… kr2dS |  |¡}| j||…  ¡ }| o`|  |¡dkS )Nr   rW   Fú=)rD   rF   r   r]   rY   r:   )r   r   r\   Úword_endÚ
line_startÚliner   r   r   r2     s    
z*_RealFinder.is_name_assigned_in_class_bodyc             C   s2   |   |d ¡}|  |¡}| j||…  ¡ }|dkS )Nr   )ÚdefÚclass)rD   r]   r   rY   )r   r   r\   rb   Ú	prev_wordr   r   r   r&   %  s    
z1_RealFinder.is_a_class_or_function_name_in_headerc             C   sX   |t | jƒkrt | jƒS x:|t | jƒk rR| j|  ¡ rR| j| dkrH|S |d7 }qW |S )NrG   r   )rE   r   rI   )r   r   r   r   r   Ú_find_first_non_space_char+  s    
z&_RealFinder._find_first_non_space_charc             C   s@   |   |¡d }|  |¡}|t| jƒk o>| j| dko>|  |¡ S )Nr   ú()rF   rg   rE   r   r&   )r   r   ra   Ú	next_charr   r   r   r0   4  s
    
z&_RealFinder.is_a_function_being_calledc             C   s
   |   |¡S )N)r_   )r   r   r   r   r   Ú_find_import_end;  s    z_RealFinder._find_import_endc             C   sT   y| j  dd|¡}W n tk
r(   dS X |  |¡}|  |d ¡|koR|  |¡|kS )Nzimport r   Fé   )r   rO   rP   r]   rj   rD   )r   r   Zlast_importrb   r   r   r   r.   >  s    
z_RealFinder.is_import_statementc             C   sX   y*| j  dd|¡}| j  d|¡}|d }W n tk
r>   dS X |  |¡}|  |¡|kS )Nzfrom r   z import é   F)r   rO   r^   rP   rg   rj   )r   r   Ú	last_fromÚfrom_importÚ
from_namesr   r   r   r,   G  s    
z_RealFinder.is_from_statementc             C   sD   |t | jƒd krdS |  |¡}|  |¡}| j||…  ¡ }|dkS )Nr   FÚfrom)rE   r   rR   r]   rY   )r   r   Z
stmt_startrb   rf   r   r   r   r'   Q  s    

z$_RealFinder.is_from_statement_modulec             C   sÆ   |   |¡sdS y&|  |¡}| j d||¡}|d }W n tk
rH   dS X |d |ksd|  |¡|k rhdS yD|  |¡}t|  |d ¡t| jƒƒ}|  	|¡}| j||d … dkS  tk
rÀ   dS X d S )NFÚimportrk   r   Úas)
r.   r]   r   rO   rP   rj   rF   ÚminrE   rD   )r   r   rb   Z
import_idxZimported_namesrZ   Úas_endÚas_startr   r   r   r)   Y  s$    



z._RealFinder.is_import_statement_aliased_modulec             C   s”   y`t | jƒ|kr.| j| dkr.|  |d ¡}n
|  |¡}| j d||¡}| j d|¡}|d }W n tk
rt   dS X |d |kr†dS |  |¡|kS )NrG   r   zfrom z import rl   F)rE   r   r]   rO   r^   rP   rj   )r   r   rb   rm   rn   ro   r   r   r   r+   n  s    
z'_RealFinder.is_a_name_after_from_importc             C   sL   y2| j  dd|¡}| j  d|¡}|  |¡}|  |¡S  tk
rF   Y nX d S )Nzfrom r   z import )r   rO   r^   rK   r!   rP   )r   r   rm   Zimport_offsetrZ   r   r   r   r8   }  s    

z_RealFinder.get_from_modulec             C   sl   |   |¡sdS yD|  |¡}t|  |d ¡t| jƒƒ}|  |¡}| j||d … dkS  tk
rf   dS X d S )NFr   rr   )r+   rF   rs   rE   r   rD   rP   )r   r   rZ   rt   ru   r   r   r   r(   †  s    


z_RealFinder.is_from_aliasedc             C   s\   yB|   |¡}|   |d ¡}|   |d ¡}|  |¡}| j||d … S  tk
rV   Y nX d S )Nr   )rF   rD   rB   rP   )r   r   rZ   Zas_Úaliasr   r   r   r   r-   ‘  s    

z_RealFinder.get_from_aliasedc             C   sˆ   |   |¡}|d t| jƒkr dS |  |d ¡}| j||d … }|dksR| d¡sVdS |  |¡}|  |d ¡}|d dko†| j| dkS )Nr   Fé   z==r`   r   z,()rF   rE   r   rg   Ú
startswithrD   rK   )r   r   ra   ri   Zequalsr\   Ú	prev_charr   r   r   r%   ›  s    

z)_RealFinder.is_function_keyword_parameterc             C   sX   |   |¡}|  |¡r"|  |¡d }|  |¡}||ksB| j| dkrFdS |  |¡}||k S )Nr   z(,F)r]   rC   rD   rK   r   r*   )r   r   ÚstopÚparens_startr   r   r   r3   §  s    



z'_RealFinder.is_on_function_call_keywordc             C   sR   |   |¡}x<||krF| j| dkr$P | j| dkr<|  |¡}|d8 }qW t||ƒS )Nrh   ú,r   )r]   r   rR   rJ   )r   r   rz   r   r   r   r*   ±  s    


z)_RealFinder.find_parens_start_from_insidec             C   s   |   |¡d k	S )N)r:   )r   r   r   r   r   r/   »  s    z_RealFinder.is_assigned_herec             C   s|   |   |¡}|  |d ¡}| j||d … }| j||d … }| j||d … }|dkrxx |||gD ]}| d¡rb|S qbW d S )Nr   rw   é   )z==z<=z>=z!=r`   )rF   rg   r   Úendswith)r   r   ra   ri   ZsingleZdoubleZtripleÚopr   r   r   r:   ¾  s    

z_RealFinder.get_assignment_typec             C   s    |   |¡}|  |¡d }||fS )Nr   )rR   rF   )r   r   r   rZ   r   r   r   r"   Ê  s    
z_RealFinder.get_primary_rangec             C   s*   t d|ƒ}|  |¡}|  |¡d }||fS )Nr   r   )rJ   rD   rF   )r   r   r   rZ   r   r   r   r$   Ï  s    

z_RealFinder.get_word_rangerh   ú)c             C   s„   |   |¡}| j ||¡}|}d}xZ|t| jƒk rz| j| |krF|d7 }| j| |kr\|d8 }|dkrp||d fS |d7 }q"W ||fS )Nr   r   )rF   r   r^   rE   )r   r   ÚopeningÚclosingrZ   Zstart_parensr^   Z
open_countr   r   r   r1   Õ  s    
z!_RealFinder.get_word_parens_rangec       	      C   s,  g }g }|   |d ¡}xü||kr|}|  |¡}x:||krj| j| dksZ| j|d  dkrj|   |d ¡}q2W | j|d |d …  ¡ }| j| dkrø|d }|d8 }x(||krÌ| j| dkrÌ|   |d ¡}q¦W | j|d |d …  ¡ }| ||f¡ n
| |¡ |   |d ¡}qW | ¡  | ¡  ||fS )Nr   z=,z=!<>r`   r|   )rK   rR   r   rB   rY   ÚappendÚreverse)	r   r6   r7   ÚkeywordsÚargsZcurrentÚprimary_startZprimaryZ
param_namer   r   r   r5   ä  s,    

z_RealFinder.get_parametersc             C   sÖ   |   |¡}|  |¡}|  |¡}|  |¡}|  |d ¡}|  |d ¡}d }}	||kr^| j| }	||k rp| j| }y| j d||¡}
W n tk
r˜   dS X |	dkr®|dkr®dS |  	|¡}||
k oÔ| j||…  
¡ dkS )Nr   r[   r`   Fz(,z,))r]   r_   rR   rF   rK   rg   r   r^   rP   r*   rY   )r   r   r   rZ   r‡   Zprimary_endZprev_char_offsetZnext_char_offsetri   ry   Zequals_offsetr{   r   r   r   r9   ý  s(    






z-_RealFinder.is_assigned_in_a_tuple_assignmentc             C   s*   |   |¡}|  |¡\}}| j||d … S )Nr   )r=   r1   rB   )r   r   ÚlparensÚrparensr   r   r   r;     s    
z+_RealFinder.get_function_and_args_in_headerúdef c             C   sF   x2| j  ||¡}|dks&|  |d ¡s(P |d7 }qW |d }|  |¡S )Nr   r   é   )r   r^   rC   rg   )r   r   Ú
definitionZdef_r   r   r   r=     s    z _RealFinder.find_function_offsetc             C   s4   | j |dd}| j|ddd\}}| j||d … S )Nzlambda )rŒ   ú ú:)r   r‚   r   )r=   r1   rB   )r   r   rˆ   r‰   r   r   r   r<   #  s    z_RealFinder.get_lambda_and_argsN)rh   r€   )rŠ   )-r>   r?   r@   r   rD   rF   rK   r   rL   rC   rQ   r4   rS   rV   rR   r!   r#   r]   r_   r2   r&   rg   r0   rj   r.   r,   r'   r)   r+   r8   r(   r-   r%   r3   r*   r/   r:   r"   r$   r1   r5   r9   r;   r=   r<   r   r   r   r   r   ~   sT   
"			
	




	r   )r   rT   Zrope.base.simplifyr
   r	   Úobjectr   r   r   r   r   r   Ú<module>   s
   q