
    G@d                         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dS )z
Source code text utilities
    N)get_eol_chars))
nt)
posix)macc                     t          |           }|sJt          j        dk    rd}n7t          j                            d          rd}nt          j        dk    rd}n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     7lib/python3.11/site-packages/spyder/utils/sourcecode.pyr   r      si    " t$$I 7d??II\$$W-- 	II\X%%III    c                 4    t           D ]\  }}| |k    r|c S dS )z"Return OS name from EOL charactersN	EOL_CHARS)r   charsos_names      r   get_os_name_from_eol_charsr   8   s8    #  wNNN  r   c                 4    t           D ]\  }}|| k    r|c S dS )z"Return EOL characters from OS nameNr   )r   r   r   s      r   get_eol_chars_from_os_namer   ?   s9    $  	47??  r   c                     t          |           }|dS |                    | |z                                             }t          |          t          |           k    S )z'Detect if text has mixed EOL charactersNF)r   join
splitlinesrepr)r   r   correct_texts      r   has_mixed_eol_charsr$   F   sT    d##Iu>>4	>"="="?"?@@Ld++r   r   c                 X    t           D ]!\  }}||k    r|                     ||          } "| S )zUse the same eol's in text)r   replace)r   eoleol_char_s       r   normalize_eolsr*   O   s7      / /!s??<<#..DKr   c                 .    |                      d|          S )zReplace tabs by spaces	)r&   )r   indent_charss     r   fix_indentationr.   W   s    <<l+++r   c                 @    ddl }| d t          |          D             v S )z<Test if passed string is the name of a Python builtin objectr   Nc                 V    g | ]&}|                     d           t          |          'S )r)   )r   str).0r   s     r   
<listcomp>zis_builtin.<locals>.<listcomp>_   s>     1 1 1$??3//1CII 1 1 1r   )builtinsdir)r   r4   s     r   
is_builtinr6   \   s9    OOO 1 1#h-- 1 1 1 1 1r   c                     ddl }| |j        v S )z5Test if passed string is the name of a Python keywordr   N)keywordkwlist)r   r8   s     r   
is_keywordr:   c   s    NNN7>!!r   Tc                 X   d}t          j        d| d|                   }|r|d         r|d         }t          j        d| |d                   }|r|d         r||d         z  }|r|d                                         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   rD   rD   i   s     C8%{7F7';<<D R 2hHUK011E q uQx
 s1v~~  D5 DV DFQJ(?5(H(Hk6A:UCCCCJr   c                 T    t          |           }|r|                     |          S | gS )z!Split source code into lines
    )r   rB   )rE   r   s     r   split_sourcerK   ~   s5     k**I   +++}r   c                     t          t          j        d|                     }t          j        d          fd|D             S )z4Split source code into python identifier-like tokensr=   z	[a-zA-Z_]c                 >    g | ]}t          j        |          |S  )rA   match)r2   tokenvalids     r   r3   z#get_identifiers.<locals>.<listcomp>   s*    @@@e%)?)?@E@@@r   )setrA   rB   compile)rE   tokensrQ   s     @r   get_identifiersrU      sF    +[99::FJ|$$E@@@@v@@@@r   c                     g }	 t           j                            |           \  }}|                    |           || k    rn|} A|                    |           |                                 |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   pathrB   appendreverse)rW   
componentsnew_pathtails       r   path_componentsr]      s     J7==..4$t hr   c                    g }d}d}t          t          | |                    D ]/\  }\  }}||k    r
|dk    rd} n|}|                    |           0t          | t          |           dz
                     }t	          j        j        |  d| dz
           }	t          |          dk    rt	          j        j        | }
t          |
          dz   }|	|d         dk    r|st          |	|d                   }|d         dk    rQ|d         dk    rEt          |	|d                   dk    r*|                    d|           t	          j        j        | }	n7|	|d         }	n,|s|}	n't          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   r<      r   /)	enumerateziprX   lenr   rW   r    r]   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_prefixrs      s    NOK!*3/?AQ+R+R!S!S % %~uE>>zz"&EKe$$$$+C0@,A,AA,EFGGW\+,-C/?.?!.C-CDF
>Q glN; #$7 8 81 <'(()R/// /7L7M7M0N O O #r))->q-AR-G-G3(./D/E/E(FLH LHJLLM LM!((K888'89 5 6 67  	 FF\$$W-- 	&B,,FMr   c                    t           j                            |          }t          | |          }t	          |          dk    rt          |          }||k    r1|                    t          |                     t          |          }t          t          |          t          |                    }t	          |          }t          |          }|dk    rat	          |          dk    rN|d         dk    r|d         dk    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`   r_   r   ra   r<   z...r>   z - )
r   rW   basenameget_same_name_filesrd   shortest_pathremover]   rs   r    )files_path_listfilenamefnamesame_name_filescompare_path	diff_pathdiff_path_lengthpath_components           r   disambiguate_fnamer      sL   GX&&E)/5AAO
?a$_558##""?8#<#<===(99L()B)B-<\-J-JL L	y>>(33r!!c.&9&9A&=&=a C''N1,=,C,C"0"3U*8*<"> #1"3U*8*<">n5I	)Lr   c                     g }| D ]G}|t           j                            |          k    r"|                    t	          |                     H|S )zBGet a list of the path components of the files with the same name.)r   rW   ru   rX   r]   )ry   rz   r|   r{   s       r   rv   rv      sT    O  ; ;rw''....""?5#9#9:::r   c                     t          |           dk    rY| d         }t          | d                   }| D ]&}t          |          |k     rt          |          }|}'t          j        j        | S dS )z(Shortest path between files in the list.r   N)rd   r   rW   r    )ry   rw   shortest_path_length
path_elmtss       r   rw   rw      s|    
?a'*"?1#566) 	+ 	+J:!555'*:$ *w|]++  r   )r   )T)__doc__rA   r   r   pylsp._utilsr   r   r   r   r   r$   r*   r.   r6   r:   rD   rK   rU   r]   rs   r   rv   rw   rN   r   r   <module>r      sP   
 
			 				 



 9 8 8 8 8 8 =	  @    , , ,   , , ,
1 1 1" " "   *  A A A  *" " "H  0  	, 	, 	, 	, 	,r   