
    8@dI%              	          d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
Z
d dlZej        Zg dZ ej        dd                    e           d          Z ej        e          Zd dZd Zd	 Zd
 Zd Zd ZdedefdZdedefdZd ZddhZdee         fdZ d dedede	ee                  fdZ!d Z"d Z#ej$        dk    rd dl%Z%e%j&        j'        Z'dZ(d Z)nd dl*Z*d Z)d Z+dS )!    N)ListOptional)

(|)c                       fd}|S )zEDebounce calls to this function until interval_s seconds have passed.c                 ~     i t          j                    t          j                    fd            }|S )Nc                  n    t          j                  } |j         i }
r|j        
         nd  fd}5                                }|r|                                 t          j        	|          }|<   |                                 d d d            d S # 1 swxY w Y   d S )Nc                  N    5  = d d d            n# 1 swxY w Y     i S N )argsfunckeykwargslocktimerss   ,lib/python3.11/site-packages/pylsp/_utils.pyrunz9debounce.<locals>.wrapper.<locals>.debounced.<locals>.run&   sz     $ $s$ $ $ $ $ $ $ $ $ $ $ $ $ $ $tT,V,,,s   )	inspect	signaturebind	argumentsgetcancel	threadingTimerstart)r   r   sig	call_argsr   	old_timertimerr   r   
interval_skeyed_byr   r   s   ``     @r   	debouncedz,debounce.<locals>.wrapper.<locals>.debounced    s1   #D))C $1&11I3;E)%h//C- - - - - - - - - -
   "JJsOO	 '$$&&&!
C88#s                 s   AB**B.1B.)r   Lock	functoolswraps)r   r(   r   r   r&   r'   s   ` @@r   wrapperzdebounce.<locals>.wrapper   sb    ~				 	 	 	 	 	 	 	 
		$     r   )r&   r'   r,   s   `` r   debouncer.      s)         0 Nr-   c                 "   | sg S t           j                            | |f          st                              d||            g S | gt           j                            t           j                            |          |                               t           j        j                  z   }|ret          j        j	        | t          t          t           j        j        fd|D                                 }|r|S |                                 |eg S )aI  Find files matching the given names relative to the given path.

    Args:
        path (str): The file path to start searching up from.
        names (List[str]): The file/directory names to look for.
        root (str): The directory at which to stop recursing upwards.

    Note:
        The path MUST be within the root.
    zPath %s not in %sc                 P    g | ]"}t           j                            |          #S r   )ospathjoin).0n
search_dirs     r   
<listcomp>z find_parents.<locals>.<listcomp>Q   s*    /[/[/[PQZ0K0K/[/[/[r-   )r1   r2   commonprefixlogwarningrelpathdirnamesplitsepr3   listfilterexistspop)rootr2   namesdirsexistingr6   s        @r   find_parentsrG   7   s      	7t-- 't444	
 6BGOOBGOOD$9$94@@FFrw{SSSD  W\4(
rw~/[/[/[/[UZ/[/[/[\\]] 	O


   Ir-   c                 j   t           j                            |           }t           j                            t           j                            |                     \  }}|g}t           j                            t           j                            |d                    rt           j                            |          }t           j                            |          }|g|z   }t           j                            t           j                            |d                    d                    |          S )z=Given a path to a module, derive its dot-separated full name.z__init__.py.)r1   r2   r<   splitextbasenamerA   r3   )r2   	directorymodule_name_	full_namethis_directorys         r   path_to_dot_namerQ   Z   s    %%IW%%bg&6&6t&<&<==NKI
'..i??
@
@ 1)))44GOOI..	#$y0	 '..i??
@
@ 1 88Ir-   c                 .   | d S d\  }}t          j        |           j        }|D ]o}t          j        |          j        }t          |          t          |          k    r<d}t	          ||          D ]\  }}	||	k    r|dz  }|dk    r
||k    r|}|}p|S )N)Nr      )pathlibPathpartslenzip)
uri
workspacesmax_lenchosen_workspacer2   	workspaceworkspace_parts	match_lenworkspace_part	path_parts
             r   match_uri_to_workspacerc   f   s    
{t (G<"D - -	!,y117#d))++	),_d)C)C 	 	%NI**Q	q==7""##, r-   c                 Z    t          | t                    rd                    |           n| S )N,)
isinstancer?   r3   )values    r   list_to_stringrh   z   s%    (55@388E???5@r-   c                 @    fdt           | |                    S )z[Recursively merge dictionary b into dictionary a.

    If override_nones is True, then
    c           	   3     K   t          |                                                               |                                          D ]}|| v r||v rt          | |         t                    rEt          ||         t                    r*|t	           | |         ||                             fV  kt          | |         t
                    rKt          ||         t
                    r0|t          t          | |         ||         z                       fV  ||         |||         fV  || |         fV  || v r|| |         fV  ||         |||         fV  d S r   )setkeysunionrf   dictr?   )abr   _merge_dicts_s      r   rq   z"merge_dicts.<locals>._merge_dicts_   s     qvvxx==&&qvvxx00 	$ 	$CaxxC1HHafd++ (
1S640H0H (]]1S61S6%B%B C CDDDDD#-- (*QsVT2J2J (S3!C&%9%9 : :;;;;;sV'#-''''#-''''AcFm####3#AcFm###	$ 	$r-   )rn   )dict_adict_brq   s     @r   merge_dictsrt   ~   s8    
$ $ $ $ $ ff--...r-   contentsreturnc                 ^    |                      dd          } |                      dd          } | S )z_
    Format plain text to display nicely in environments which do not respect whitespaces.
    	u       z  u     )replaceru   s    r   escape_plain_textr{      s3     l33Hl33HOr-   c                 P    t          j        dd|           } t          |           } | S )zF
    Format plain text to display nicely in Markdown environment.
    z([\\*_#[\]])z\\\1)resubr{   rz   s    r   escape_markdownr      s)    
 vow99H **HOr-   c                     d| z   dz   S )Nz
```python
z
```
r   )r   s    r   wrap_signaturer      s    9$y00r-   markdown	plaintextclient_supported_markup_kindsc                 *    | D ]}|t           v r|c S dS )zChoose a markup kind supported by both client and the server.

    This gives priority to the markup kinds provided earlier on the client preference list.
    r   )SERVER_SUPPORTED_MARKUP_KINDS)r   kinds     r   choose_markup_kindr      s2    
 .  000KKK 1:r-   markup_kind
signaturesc                 x   t          | t                    sd} |dk    rm	 t          j        |           }d|dS # t          j        $ r t          |           }Y nw xY w|r(t          d                    |                    dz   |z   }d|dS | }|rd                    |          dz   |z   }dt          |          dS )a  Transform the provided docstring into a MarkupContent object.

    If `markup_kind` is 'markdown' the docstring will get converted to
    markdown representation using `docstring-to-markdown`; if it is
    `plaintext`, it will be returned as plain text.
    Call signatures of functions (or equivalent code summaries)
    provided in optional `signatures` argument will be prepended
    to the provided contents of the docstring if given.
     r   )r   rg   r   z

r   )	rf   strdocstring_to_markdownconvertUnknownFormatErrorr   r   r3   r{   )ru   r   r   rg   s       r   format_docstringr      s    h$$ j  	.)1(;;E"   %7 	. 	. 	.#H--EEE	.  	K"499Z#8#899FBUJE 
 
 	
 E 7		*%%.6"5))  s   8 AAc                     t          |          |k    r(t          ||                             d                    nd}t          | |          S )z
    Normalise the position as per the LSP that accepts character positions > line length

    https://microsoft.github.io/language-server-protocol/specification#position
    r   r   )rX   rstripmin)columnlinesline_number
max_columns       r   clip_columnr      sJ     <?u::;S;SU;'..v66777YZJvz"""r-   c                 j    i }|r.|d         dz   t          |d         | j        |d                   d}|S )z
    Convert the LSP format 'line', 'character' to Jedi's 'line', 'column'

    https://microsoft.github.io/language-server-protocol/specification#position
    linerT   	character)r   r   )r   r   )documentpositioncode_positions      r   position_to_jedi_linecolumnr      sX     M B!)&!1A!5#.x/D/7~/7/?$A $AB B r-   nti   c                     t                               t          d|           }|dk    rt                               |           dS dS )a0  Check whether the process with the given pid is still alive.

        Running `os.kill()` on Windows always exits the process, so it can't be used to check for an alive process.
        see: https://docs.python.org/3/library/os.html?highlight=os%20kill#os.kill

        Hence ctypes is used to check for the process directly via windows API avoiding any other 3rd-party dependency.

        Args:
            pid (int): process ID

        Returns:
            bool: False if the process is not alive or don't have permission to check, True otherwise.
        r   TF)kernel32OpenProcessPROCESS_QUERY_INFROMATIONCloseHandle)pidprocesss     r   is_process_aliver     sC     &&'@!SIIa<<  )))4ur-   c                     | dk     rdS 	 t          j        | d           n,# t          $ r}|j        t          j        k    cY d}~S d}~ww xY wdS )zCheck whether the process with the given pid is still alive.

        Args:
            pid (int): process ID

        Returns:
            bool: False if the process is not alive or don't have permission to check, True otherwise.
        r   FNT)r1   killOSErrorerrnoEPERM)r   es     r   r   r     sj     775	*GCOOOO 	* 	* 	*7ek)))))))	*ts     
A	AA	A	c                 h    t                               |           }|r|                    d          S dS )zGet EOL chars used in text.r   N)	EOL_REGEXsearchgroup)textmatchs     r   get_eol_charsr   +  s2    T""E {{1~~4r-   r   ),r*   r   loggingr1   rU   r}   r   typingr   r   r   jedi__version__JEDI_VERSION	EOL_CHARScompiler3   r   	getLogger__name__r9   r.   rG   rQ   rc   rh   rt   r   r{   r   r   r   r   r   r   r   namectypeswindllr   r   r   r   r   r   r-   r   <module>r      so         				  				     ! ! ! ! ! ! ! !      !  	BJ1388I..11122	g!!   :     F	 	 	  (A A A/ / /.     c c    1 1 1 ",[ 9 d3i    % %s % %(4PS9BU % % % %P# # #   7d??MMM}%H &   * LLL  $    r-   