
    ue&              	          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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j#                  e       d      Z ej&                  e      Zd!d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jL                  dk(  r#d dl'Z'e'jP                  jR                  Z)dZ*d Z+d  Z-yd dl,Z,d Z+d  Z-y)#    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                  ^    t        j                        } |j                   i }
r|j                  
   nd  fd}5  j	                        }|r|j                          t        j                  	|      }|<   |j                          d d d        y # 1 sw Y   y xY w)Nc                  H    5  = d d d          i S # 1 sw Y   xY wN )argsfunckeykwargslocktimerss   ./usr/lib/python3/dist-packages/pylsp/_utils.pyrunz9debounce.<locals>.wrapper.<locals>.debounced.<locals>.run(   s2     $s$T,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"   s    ##D)C $1&1I3;)%%h/C- -
  "JJsO	$$&!
C8#s  s   AB##B,)r   Lock	functoolswraps)r   r(   r   r   r&   r'   s   ` @@r   wrapperzdebounce.<locals>.wrapper   s7    ~~			 
	&     r   )r&   r'   r,   s   `` r   debouncer.      s    4 Nr-   c                       fd}|S )z5Throttles calls to a function evey `seconds` seconds.c                 J     t        j                          fd       S )Nc                      t        d      sd_        t        j                         j                  z
  k\  r!t        j                         _         | i |S y )N	last_callr   )hasattrr2   time)r   r   r   secondsr,   s     r   r,   z,throttle.<locals>.decorator.<locals>.wrapper?   sQ    7K0$%!yy{W...'9$(IIK!T,V,, :r-   )r*   r+   )r   r,   r5   s   `@r   	decoratorzthrottle.<locals>.decorator>   s%    			- 
	- r-   r   )r5   r6   s   ` r   throttler7   ;   s    	 r-   c                 x   | sg S t         j                  j                  | |f      st        j	                  d||        g S | gt         j                  j                  t         j                  j                  |      |       j                  t         j                  j                        z   }|rt        j                  j                  | }t        t        t         j                  j                  |D cg c]"  }t         j                  j                  ||      $ c}            }|r|S |j                          |rg S c c}w )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 %s)ospathcommonprefixlogwarningrelpathdirnamesplitsepjoinlistfilterexistspop)rootr:   namesdirs
search_dirnexistings          r   find_parentsrM   L   s     	77t-'t4	
 6BGGOOBGGOOD$94@FFrww{{SSD WW\\4(
277>>#OABGGLLQ$?#OP
 O
  I $Ps   ,'D7c                 d   t         j                  j                  |       }t         j                  j                  t         j                  j	                  |             \  }}|g}t         j                  j                  t         j                  j                  |d            rt         j                  j	                  |      }t         j                  j                  |      }|g|z   }t         j                  j                  t         j                  j                  |d            rdj                  |      S )z=Given a path to a module, derive its dot-separated full name.z__init__.py.)r9   r:   r?   splitextbasenamerE   rB   )r:   	directorymodule_name_	full_namethis_directorys         r   path_to_dot_namerW   q   s    %IWW%%bgg&6&6t&<=NKI
''..i?
@)))4GGOOI.	#$y0	 ''..i?
@ 88Ir-   c                 2   | y d\  }}t        j                  |       j                  }|D ]j  }t        j                  |      j                  }t        |      t        |      kD  r:d}t	        ||      D ]  \  }}	||	k(  s|dz  } |dkD  sa||kD  sg|}|}l |S )N)Nr      )pathlibPathpartslenzip)
uri
workspacesmax_lenchosen_workspacer:   	workspaceworkspace_parts	match_lenworkspace_part	path_parts
             r   match_uri_to_workspaceri   }   s    
{ (G<<""D -	!,,y177#d)+	),_d)C 	%NI*Q		 q=7"##, - r-   c                 H    t        | t              rdj                  |       S | S )N,)
isinstancerC   rB   )values    r   list_to_stringrn      s    (5388E?@5@r-   c                 2    fdt         | |            S )z[Recursively merge dictionary b into dictionary a.

    If override_nones is True, then
    c           	   3     K   t        | j                               j                  |j                               D ]  }|| v r||v rt        | |   t              r0t        ||   t              r|t	         | |   ||               f Nt        | |   t
              r5t        ||   t
              r"|t        t        | |   ||   z               f ||   
|||   f || |   f || v r
|| |   f ||   |||   f  y wr   )setkeysunionrl   dictrC   )abr   _merge_dicts_s      r   rw   z"merge_dicts.<locals>._merge_dicts_   s    qvvx=&&qvvx0 	$CaxC1Hafd+
1S640H]1S61S6%B CDD#-*QsVT2JS3!C&%9 :;;sV'#-'#-'AcFm#3#AcFm#	$s   C9D	=D	)rt   )dict_adict_brw   s     @r   merge_dictsrz      s    $  ff-..r-   contentsreturnc                 N    | j                  dd      } | j                  dd      } | S )z_
    Format plain text to display nicely in environments which do not respect whitespaces.
    	u       z  u     )replacer{   s    r   escape_plain_textr      s-     l3Hl3HOr-   c                 J    t        j                  dd|       } t        |       } | S )zF
    Format plain text to display nicely in Markdown environment.
    z([\\*_#[\]])z\\\1)resubr   r   s    r   escape_markdownr      s%    
 vvow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 s|c S  y)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      s&    
 . 00K r-   markup_kind
signaturesc                 D   t        | t              sd} |dk(  r=	 t        j                  |       }|r t        dj                  |            dz   |z   }d|dS | }|rdj                  |      dz   |z   }dt        |      dS # t        j                  $ r t        |       }Y pw xY w)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   z

)r   rm   r   )	rl   strdocstring_to_markdownconvertUnknownFormatErrorr   r   rB   r   )r{   r   r   rm   s       r   format_docstringr      s     h$j 	.)11(;E
 "499Z#89FBUJE"U33E		*%.6*;E*BCC %77 	.#H-E	.s   A> >BBc                 t    t        |      |kD  rt        ||   j                  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   )r^   rstripmin)columnlinesline_number
max_columns       r   clip_columnr      s>     36e*{2JE+%%f-.PQ  vz""r-   c                 Z    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
    linerZ   	character)r   r   )r   r   )documentpositioncode_positions      r   position_to_jedi_linecolumnr      sE     MV$q(!%x~~x7G
 r-   nti   c                 p    t         j                  t        d|       }|dk7  rt         j                  |       yy)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     s4     &&'@!SIa<  )r-   c                     | dk  ry	 t        j                  | d       y# t        $ r'}|j                  t        j                  k(  cY d}~S d}~ww xY w)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)r9   killOSErrorerrnoEPERM)r   es     r   r   r   .  sJ     7	*GGCO   	*77ekk))	*s    	AA
A
Ac                 T    t         j                  |       }|r|j                  d      S y)zGet EOL chars used in text.r   N)	EOL_REGEXsearchgroup)textmatchs     r   get_eol_charsr   @  s&    T"E{{1~r-   r   )rZ   ).r*   r   loggingr9   r[   r   r   r4   typingr   r   r   jedi__version__JEDI_VERSION	EOL_CHARScompilerB   r   	getLogger__name__r<   r.   r7   rM   rW   ri   rn   rz   r   r   r   r   r   r   r   r   r   namectypeswindllr   r   r   r   r   r   r-   r   <module>r      sX      	  	   !   !	BJJ388I./q12	g!@""J	(A/2  c c 1 ",[ 9 d3i  HLDD #D19$s)1DD@	#" 77d?}}%%H &R) $r-   