
    Je                         d Z ddlZddlZddlZddlmZ dZd Zd Zd Z	d Z
dd	Zd
 Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Zy)z
Source code text utilities
    N)get_eol_chars))
nt)
posix)macc                     t        |       }|sSt        j                  dk(  rd}|S t        j                  j                  d      rd}|S t        j                  dk(  rd}|S d}|S )a   
    Get text end-of-line (eol) characters.

    If no eol chars are found, return ones based on the operating
    system.

    Parameters
    ----------
    text: str
        Text to get its eol chars from

    Returns
    -------
    eol: str or None
        Eol found in ``text``.
    r   r   linuxr   darwinr   )_get_eol_charsosnamesysplatform
startswith)text	eol_charss     9/usr/lib/python3/dist-packages/spyder/utils/sourcecode.pyr   r      sr    " t$I77d?I  \\$$W-I  \\X%I  I    c                 2    t         D ]  \  }}| |k(  s|c S  y)z"Return OS name from EOL charactersN	EOL_CHARS)r   charsos_names      r   get_os_name_from_eol_charsr   8   s"    # wNr   c                 2    t         D ]  \  }}|| k(  s|c S  y)z"Return EOL characters from OS nameNr   )r   r   r   s      r   get_eol_chars_from_os_namer   ?   s"    $ 	47?r   c                     t        |       }|y|j                  | |z   j                               }t        |      t        |       k7  S )z'Detect if text has mixed EOL charactersF)r   join
splitlinesrepr)r   r   correct_texts      r   has_mixed_eol_charsr$   F   sD    d#I>>4	>"="="?@Ld++r   c                 R    t         D ]  \  }}||k7  s| j                  ||      }  | S )zUse the same eol's in text)r   replace)r   eoleol_char_s       r   normalize_eolsr*   O   s3      /!s?<<#.D/ Kr   c                 &    | j                  d|      S )zReplace tabs by spaces	)r&   )r   indent_charss     r   fix_indentationr.   W   s    <<l++r   c                 ~    ddl }| t        |      D cg c]  }|j                  d      st        |        c}v S c c}w )z<Test if passed string is the name of a Python builtin objectr   Nr)   )builtinsdirr   str)r   r0   r   s      r   
is_builtinr3   \   s?    #h- 1$??3/ I 1 1 1 1s   #:c                 &    ddl }| |j                  v S )z5Test if passed string is the name of a Python keywordr   N)keywordkwlist)r   r5   s     r   
is_keywordr7   c   s    7>>!!r   c                    d}t        j                  d| d|       }|r
|d   r|d   }t        j                  d| |d       }|r|d   r||d   z  }|r|d   j                         rd}|s|r|r| |dz
     dv rt        | |dz
  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
     [^0-9a-zA-Z_.]Nz\Wr      z([.F)retry)resplitisdigitget_primary_at)source_codeoffsetr=   objleftrights         r   rA   rA   i   s     C88%{7F';<DR2hHHUK01EquQx
s1v~~5VFQJ(?5(Hk6A:UCCJr   c                 D    t        |       }|r| j                  |      S | gS )z!Split source code into lines
    )r   r?   )rB   r   s     r   split_sourcerH   ~   s*     k*I  ++}r   c                     t        t        j                  d|             }t        j                  d      }|D cg c]  }t        j                  ||      s| c}S c c}w )z4Split source code into python identifier-like tokensr:   z	[a-zA-Z_])setr>   r?   compilematch)rB   tokensvalidtokens       r   get_identifiersrP      sG    +[9:FJJ|$E%@e%)?E@@@s   AAc                     g }	 t         j                  j                  |       \  }}|j                  |       || k(  rn|} <|j                  |       |j	                          |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
    )r   pathr?   appendreverse)rR   
componentsnew_pathtails       r   path_componentsrX      sj     J 77==.4$t  hr   c                    g }d}d}t        t        | |            D ])  \  }\  }}||k7  r	|dk(  rd} n|}|j                  |       + t        | t        |       dz
           }t	        j
                  j                  |  d| dz
   }	t        |      dkD  rt	        j
                  j                  | }
t        |
      dz   }|	|d dk7  rh|sft        |	|d       }|d   dk(  rI|d   dk(  rAt        |	|d       dkD  r0|j                  d|       t	        j
                  j                  | }	|	S |	|d }	|	S |s|}	|	S t        j                  j                  d	      r|	dk(  rd
}	|	S )z
    Return the differentiated prefix of the given two iterables.

    Taken from https://stackoverflow.com/q/21498939/438386
    FN   Tr<   r   r9      r   /)	enumerateziprS   lenr   rR   r    rX   insertr   r   r   )path_components0path_components1longest_prefixroot_comparisoncommon_elmtindexelmt0elmt1file_name_lengthpath_0longest_path_prefixlongest_prefix_lengthpath_0_componentss                r   differentiate_prefixrn      s    NOK!*3/?AQ+R!S %~uE>z"&Ke$% +C0@,AA,EFGWW\\+,-C/?.?!.CDF
>Q ggllN; #$7 81 <'()R/ /7L7M0N O #r)->q-AR-G3(./D/E(FLHJLLM!((K8'89 M   5 67
 M	 ! F M \\$$W-&B,FMr   c                    t         j                  j                  |      }t        | |      }t	        |      dkD  rt        |      }||k(  r%|j                  t        |             t        |      }t        t        |      t        |            }t	        |      }t        |      }|dkD  rQt	        |      dkD  rC|d   dk7  r|d   dk7  r|d   d|d   g}n|d   d|d   g}t        j                  j                  | }|d	z   |z   }|S )
z"Get tab title without ambiguation.r<   r[   rZ   r   r\   r9   z...r;   z - )
r   rR   basenameget_same_name_filesr_   shortest_pathremoverX   rn   r    )files_path_listfilenamefnamesame_name_filescompare_path	diff_pathdiff_path_lengthpath_components           r   disambiguate_fnamer|      s   GGX&E)/5AO
?a$_58#""?8#<=(9L()B-<\-JL	y>(3r!c.&9A&=a C'N1,=,C"0"3U*8*<"> #1"3U*8*<">n5I	)Lr   c                     g }| D ]?  }|t         j                  j                  |      k(  s&|j                  t	        |             A |S )zBGet a list of the path components of the files with the same name.)r   rR   rp   rS   rX   )rt   ru   rw   rv   s       r   rq   rq      sH    O  ;rww''..""?5#9:; r   c                     t        |       dkD  rR| d   }t        | d         }| D ]  }t        |      |k  st        |      }|}  t        j                  j                  | S y)z(Shortest path between files in the list.r   N)r_   r   rR   r    )rt   rr   shortest_path_length
path_elmtss       r   rr   rr      sn    
?a'*"?1#56) 	+J:!55'*:$ *	+ ww||]++  r   )r   )T)__doc__r>   r   r   pylsp._utilsr   r   r   r   r   r$   r*   r.   r3   r7   rA   rH   rP   rX   rn   r|   rq   rr    r   r   <module>r      st   
 
 	 
 9 =	@,,
1"*A*"H0	,r   