B
    nb\!                 @   s   d Z ddlZddlZddlZddlmZ er8ddlmZ dZ	dddd	d
ddddddddddZ
dZddiZdd Zdd Zdd Zdd Zd:d!d"Zd#d$ Zd%d& Zd'd( Zd;d*d+Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 ZdS )<z
Source code text utilities
    N)PY2)izip))z
nt)
posix)Zmac)pyZpywZpythonZipy)ZpyxZpxiZpxd)Zenaml)fforZf77)Zf90Zf95Zf2k)Zpro)ZdiffZpatchZrej)ZpoZpot)ZnsiZnsh)ZhtmZhtml)cZccZcppZcxxhZhhZhppZhxx)Zcl)ZyamlZyml)ZmdZmdw)PythonCythonEnamlZ	Fortran77ZFortranZIdlZDiffZGetTextZNsisZHtmlZCppZOpenCLZYamlZMarkdown)r   r   r   r   )z#%%z# %%z# <codecell>z# In[c             C   s(   x"t D ]\}}| |dkr|S qW dS )zGet text EOL charactersN)	EOL_CHARSfind)text	eol_charsZ_os_name r   6lib/python3.7/site-packages/spyder/utils/sourcecode.pyget_eol_chars,   s    r   c             C   s"   xt D ]\}}| |kr|S qW dS )z"Return OS name from EOL charactersN)r   )r   charsos_namer   r   r   get_os_name_from_eol_chars3   s    r   c             C   s"   xt D ]\}}|| kr|S qW dS )z"Return EOL characters from OS nameN)r   )r   r   namer   r   r   get_eol_chars_from_os_name:   s    r   c             C   s6   t | }|dkrdS || |  }t|t| kS )z'Detect if text has mixed EOL charactersNF)r   join
splitlinesrepr)r   r   Zcorrect_textr   r   r   has_mixed_eol_charsA   s
    r    r   c             C   s*   x$t D ]\}}||kr| ||} qW | S )zUse the same eol's in text)r   replace)r   ZeolZeol_char_r   r   r   normalize_eolsJ   s    r#   c             C   s   |  ddS )zReplace tabs by spaces	z    )r!   )r   r   r   r   fix_indentationR   s    r%   c             C   s"   ddl m} | dd t|D kS )z<Test if passed string is the name of a Python builtin objectr   )builtinsc             S   s   g | ]}| d st|qS )r"   )
startswithstr).0r   r   r   r   
<listcomp>Z   s    zis_builtin.<locals>.<listcomp>)spyder.py3compatr&   dir)r   r&   r   r   r   
is_builtinW   s    r-   c             C   s   ddl }| |jkS )z5Test if passed string is the name of a Python keywordr   N)keywordkwlist)r   r.   r   r   r   
is_keyword^   s    r0   Tc             C   s   d}t d| d| }|r,|d r,|d }t d| |d }|rX|d rX||d 7 }|rl|d  rld}|s|r|r| |d  dkrt| |d d	d
S |S )zReturn Python object in *source_code* at *offset*
    Periods to the left of the cursor are carried forward 
      e.g. 'functools.par^tial' would yield 'functools.partial'
    Retry prevents infinite recursion: retry only once
     z[^0-9a-zA-Z_.]Nr   z\Wr      z([.F)retry)resplitisdigitget_primary_at)source_codeoffsetr3   objleftrightr   r   r   r7   d   s    r7   c             C   s    t | }|r| |S | gS dS )z!Split source code into lines
    N)r   r5   )r8   r   r   r   r   split_sourcey   s    
r=   c                s,   t td| }td  fdd|D S )z4Split source code into python identifier-like tokensz[^0-9a-zA-Z_.]z	[a-zA-Z_]c                s   g | ]}t  |r|qS r   )r4   match)r)   token)validr   r   r*      s    z#get_identifiers.<locals>.<listcomp>)setr4   r5   compile)r8   tokensr   )r@   r   get_identifiers   s    
rD   c             C   sH   g }x,t j| \}}|| || kr*P |} qW || |  |S )z
    Return the individual components of a given file path
    string (for the local operating system).

    Taken from https://stackoverflow.com/q/21498939/438386
    )ospathr5   appendreverse)rF   Z
componentsnew_pathtailr   r   r   path_components   s    

rK   c             C   sT  g }d}d}xDt t| |D ]2\}\}}||kr@|dkr<d}P n|}|| qW t| t| d  }tjj|  d| d  }	t|dkrPtjj| }
t|
d }|	|d dkr(|s(t|	|d }|d dkr|d dkrt|	|d dkr|d| tjj| }	n|	|d }	n(|s4|}	nt	j
d	rP|	dkrPd
}	|	S )z
    Return the differentiated prefix of the given two iterables. 
     
    Taken from https://stackoverflow.com/q/21498939/438386
    FN   Tr2   r   r1      Zlinux/)	enumerateziprG   lenrE   rF   r   rK   insertsysplatformr'   )Zpath_components0Zpath_components1Zlongest_prefixZroot_comparisonZcommon_elmtindexZelmt0Zelmt1Zfile_name_lengthZpath_0Zlongest_path_prefixZlongest_prefix_lengthZpath_0_componentsr   r   r   differentiate_prefix   s6    rV   c             C   s   t j|}t| |}t|dkrt|}||krH|t| t|}tt|t|}t|}t|}|dkrt|dkr|d dkr|d dkr|d d|d g}n|d d|d g}t jj	| }|d	 | }|S )
z"Get tab title without ambiguation.r2   rM   rL   r   rN   r1   z...r   z - )
rE   rF   basenameget_same_name_filesrQ   shortest_pathremoverK   rV   r   )files_path_listfilenamefnamesame_name_filesZcompare_pathZ	diff_pathZdiff_path_lengthZpath_componentr   r   r   disambiguate_fname   s(    


r_   c             C   s4   g }x*| D ]"}|t j|kr
|t| q
W |S )zBGet a list of the path components of the files with the same name.)rE   rF   rW   rG   rK   )r[   r\   r^   r]   r   r   r   rX      s
    
rX   c             C   sV   t | dkrR| d }t | d }x$| D ]}t ||k r&t |}|}q&W tjj| S dS )z(Shortest path between files in the list.r   N)rQ   rE   rF   r   )r[   rY   Zshortest_path_lengthZ
path_elmtsr   r   r   rY      s    
rY   )r   )T)__doc__r4   rE   rS   r+   r   	itertoolsr   rP   r   ZALL_LANGUAGESZPYTHON_LIKE_LANGUAGESZCELL_LANGUAGESr   r   r   r    r#   r%   r-   r0   r7   r=   rD   rK   rV   r_   rX   rY   r   r   r   r   <module>	   sN   	


$