
    ړia                       d dl mZ d dlZd dlmZ d dlZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZmZmZmZ d dlmZmZmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dl m!Z!m"Z" d dl#m$Z$ er
d dl%Z&d dlm'Z' g dZ( G d de
      Z) G d de      Z* G d de      Z+ G d de      Z, G d de      Z- G d de.      Z/	 d dl0Z0 e1e0      Z2	 	 	 	 ddZ4y# e3$ r g Z2Y w xY w)    )annotationsN)Enum)islice)TYPE_CHECKINGAnyCallableIterable)CompleteEvent	Completer
CompletionPathCompleter)SystemCompleter)compile)GrammarCompleter)Document)fragment_list_to_textto_formatted_text)get_jedi_script_from_document)_CompiledGrammar)PythonCompleterCompletePrivateAttributesHidePrivateCompleterc                      e Zd ZdZdZdZdZy)r   zH
    Should we display private attributes in the completion pop-up?
    NEVERIF_NO_PUBLICALWAYSN)__name__
__module____qualname____doc__r   r   r        9/tmp/pip-target-20744g9h/lib/python/ptpython/completer.pyr   r   !   s     E!LFr"   r   c                       e Zd ZdZ	 	 	 	 	 	 	 	 d	 fdZed
d       Zedd       ZddZddZ	ddZ
	 	 	 	 	 	 ddZ xZS )r   z$
    Completer for Python code.
    c                    t         |           || _        || _        || _        t               | _        t        ||      | _        t        ||      | _
        d | _        d | _        y N)super__init__get_globals
get_localsenable_dictionary_completionr   _system_completerJediCompleter_jedi_completerDictionaryCompleter_dictionary_completer_path_completer_cache_path_completer_grammar_cache)selfr)   r*   r+   	__class__s       r#   r(   zPythonCompleter.__init__0   s`     	&$,H)!0!2,[*E%8j%Q">B"FJ*r"   c                    | j                   2t        | j                  t        d      t        d      d      | _         | j                   S )NT)
expanduservar1var2)r1   r   _path_completer_grammarr   r3   s    r#   _path_completerzPythonCompleter._path_completerC   sG    %%-)9,,)T:)T:*D& )))r"   c                \    | j                   | j                         | _         | j                   S )zb
        Return the grammar for matching paths inside strings inside Python
        code.
        )r2   _create_path_completer_grammarr;   s    r#   r:   z'PythonCompleter._path_completer_grammarO   s-     --5151T1T1VD.111r"   c                F    dd}dd}dd}d}t        |||d||d      S )Nc                0    t        j                  dd|       S )Nz\\(.)z\1)resubtexts    r#   	unwrapperzAPythonCompleter._create_path_completer_grammar.<locals>.unwrapper\   s    66(E400r"   c                F    | j                  dd      j                  dd      S )N\\\'z\'replacerC   s    r#   single_quoted_wrapperzMPythonCompleter._create_path_completer_grammar.<locals>.single_quoted_wrapper_        <<f-55c5AAr"   c                F    | j                  dd      j                  dd      S )NrG   rH   "z\"rJ   rC   s    r#   double_quoted_wrapperzMPythonCompleter._create_path_completer_grammar.<locals>.double_quoted_wrapperb   rM   r"   a  
                # Text before the current string.
                (
                    [^'"#]                                  |  # Not quoted characters.
                    '''  ([^'\\]|'(?!')|''(?!')|\\.])*  ''' |  # Inside single quoted triple strings
                    "" " ([^"\\]|"(?!")|""(?!^)|\\.])* "" " |  # Inside double quoted triple strings

                    \#[^\n]*(\n|$)           |  # Comment.
                    "(?!"") ([^"\\]|\\.)*"   |  # Inside double quoted strings.
                    '(?!'') ([^'\\]|\\.)*'      # Inside single quoted strings.

                        # Warning: The negative lookahead in the above two
                        #          statements is important. If we drop that,
                        #          then the regex will try to interpret every
                        #          triple quoted string also as a single quoted
                        #          string, making this exponentially expensive to
                        #          execute!
                )*
                # The current string that we're completing.
                (
                    ' (?P<var1>([^\n'\\]|\\.)*) |  # Inside a single quoted string.
                    " (?P<var2>([^\n"\\]|\\.)*)    # Inside a double quoted string.
                )
        r7   )escape_funcsunescape_funcs)rD   strreturnrS   )compile_grammar)r3   rE   rL   rP   grammars        r#   r>   z.PythonCompleter._create_path_completer_grammar[   s=    	1	B	B2 "7AVW$-yA
 	
r"   c                t    |j                   }t        |j                  xr |j                         xs |dv       S )Nz/.~)char_before_cursorboolrD   isalnum)r3   documentrX   s      r#   _complete_path_while_typingz+PythonCompleter._complete_path_while_typing   s?    %88MM N#++-L1Cu1L
 	
r"   c                j    |j                   }|dd }t        |xr |j                         xs |dv       S )zp
        When `complete_while_typing` is set, only return completions when this
        returns `True`.
        Nz_.([,)text_before_cursorrY   rZ   )r3   r[   rD   rX   s       r#   _complete_python_while_typingz-PythonCompleter._complete_python_while_typing   sE    
 **!"#YT(002S6HG6S
 	
r"   c              #     K   |j                   j                         j                  d      rJ| j                  j	                  t        |j                   dd |j                  dz
        |      E d{    y|j                  s| j                  |      rJ| j                         r:d}| j                  j	                  ||      D ]  }|j                   dvrd}|  |ry|j                  s| j                  |      r$| j                  j	                  ||      E d{    |j                  s| j                  |      rK| j                  j                  |j                        s%| j                   j	                  ||      E d{    yyy7 7 p7 w)z)
        Get Python completions.
        !   N)rD   cursor_positionFz[.T)rD   lstrip
startswithr,   get_completionsr   rd   completion_requestedr`   r+   r0   r\   r<   r:   matchr_   r.   )r3   r[   complete_eventhas_dict_completionscs        r#   rg   zPythonCompleter.get_completions   s     ==!,,S1--==!qr*H<T<TWX<X 	    ..$2T2T3
 002',$33CCn A vvT) 04,G ( ..$2R2R3
 ++;;HnUUU ..$2T2T3
 //55h6Q6QR//??n   S	3
A: Vs8   A.F0F1B(FF
A%F?F F
FF)r)   Callable[[], dict[str, Any]]r*   rm   r+   zCallable[[], bool]rT   None)rT   r   )rT   r   )r[   r   rT   rY   r[   r   rj   r
   rT   Iterable[Completion])r   r   r   r    r(   propertyr<   r:   r>   r\   r`   rg   __classcell__r4   s   @r#   r   r   +   s    K1K 1K '9	K
 
K& 	* 	* 	2 	2'
R
	
0 02?0	0r"   r   c                  D     e Zd ZdZ	 	 	 	 	 	 d fdZ	 	 	 	 	 	 ddZ xZS )r-   z3
    Autocompleter that uses the Jedi library.
    c                >    t         |           || _        || _        y r&   )r'   r(   r)   r*   )r3   r)   r*   r4   s      r#   r(   zJediCompleter.__init__   s    
 	&$r"   c           
   #    K   t        || j                         | j                               }|r	 |j                  |j                  |j
                  dz         }t        |d       }|D ]  }|j                  dk(  rd}nd}|j                  dk(  rd	}t        |j                  t        |j                        t        |j                        z
  |j                  |z   |j                  t        |      
        y y # t        $ r Y y t        $ r Y y t        $ r Y y t        $ r Y y t         $ r Y y t"        $ r Y y t$        $ r Y y t&        $ r Y y t(        $ r Y y t*        $ r Y y w xY ww)Nrc   )columnlinec                    | j                   dk7  | j                  j                  d      | j                  j	                         fS )Nparam_)typenamerf   name_with_symbolslower)jcs    r#   <lambda>z/JediCompleter.get_completions.<locals>.<lambda>  s9    7***3/,,224$ r"   keyfunction() rz   ...)displaydisplay_metastyle)r   r*   r)   completecursor_position_colcursor_position_rowsortedr|   r   r~   len_get_style_for_jedi_completion	TypeErrorUnicodeDecodeErrorAttributeError
ValueErrorKeyErrorOSErrorAssertionErrorSystemErrorNotImplementedError	Exception)r3   r[   rj   scriptjedi_completionsr   suffixs          r#   rg   zJediCompleter.get_completions   s     /doo')9)9);
 F#)??#77!559 $3 $ T $*$
$  + Bww*,!%!#ww')!&$,,BKK(3r/C/C+DD " 4 4v =%'WW<R@ q    %  !      !    &  s   ,E#*C: B!E#:	E E#E E#E E#E !E##E +E#-E 5E#7E ?E#E 	E#E E#E E#E  E#r)   rm   r*   rm   rT   rn   ro   )r   r   r   r    r(   rg   rr   rs   s   @r#   r-   r-      sJ    %1% 1% 
	%N N2?N	Nr"   r-   c                       e Zd ZdZ	 	 	 	 	 	 d fdZddZ	 	 	 	 	 	 ddZddZddZ	 	 	 	 	 	 	 	 ddZ		 	 	 	 	 	 	 	 ddZ
	 	 	 	 	 	 	 	 dd	Zdd
Z xZS )r/   a  
    Experimental completer for Python dictionary keys.

    Warning: This does an `eval` and `repr` on some Python expressions before
             the cursor, which is potentially dangerous. It doesn't match on
             function calls, so it only triggers attribute access.
    c                   t         |           || _        || _        d}d| d| d}t	        j
                  d| dt        j                        | _        t	        j
                  d| dt        j                        | _        t	        j
                  d| d	t        j                        | _	        t	        j
                  d| d
t        j                        | _
        y )Nz[a-zA-Z_][a-zA-Z0-9_]*a@  
            # Any expression safe enough to eval while typing.
            # No operators, except dot, and only other dict lookups.
            # Technically, this can be unsafe of course, if bad code runs
            # in `__getattr__` or ``__getitem__``.
            (
                # Variable name
                zr

                \s*

                (?:
                    # Attribute access.
                    \s* \. \s* ap   \s*

                    |

                    # Item lookup.
                    # (We match the square brackets. The key can be anything.
                    # We don't care about matching quotes here in the regex.
                    # Nested square brackets are not supported.)
                    \s* \[ [^\[\]]+ \] \s*
                )*
            )
        z4
                for \s+ ([a-zA-Z0-9_]+) \s+ in \s+ z \s* :
            z
                z
                $
            z

                # Dict lookup to complete (square bracket open + start of
                # string).
                \[
                \s* ([^\[\]]*)$
            z

                # Attribute lookup to complete (dot + varname).
                \.
                \s* ([a-zA-Z0-9_]*)$
            )r'   r(   r)   r*   rA   r   VERBOSEfor_loop_patternexpression_patternitem_lookup_patternattribute_lookup_pattern)r3   r)   r*   varname
expressionr4   s        r#   r(   zDictionaryCompleter.__init__2  s   
 	&$ , 	   !(y )	
: !#

44>< @ JJ	!
 #%**  JJ#
 $&::  JJ
$
  )+

  JJ	)
%r"   c                t    	 t        |j                         | j                         |      S # t        $ r Y yw xY w)zw
        Do lookup of `object_var` in the context.
        `temp_locals` is a dictionary, used for the locals.
        N)evalstripr)   BaseException)r3   r   temp_localss      r#   _lookupzDictionaryCompleter._lookup  s:    
	
((*D,<,<,>LL 		s   (+ 	77c              #    K   | j                         j                         }| j                  j                  |j                        D ]I  }|j                         \  }}| j                  ||      }t        |t        t        f      s?|sB|d   ||<   K | j                  |||      E d {    | j                  |||      E d {    | j                  |||      E d {    y 7 ;7 "7 	w)Nr   )r*   copyr   finditerr_   groupsr   
isinstancelisttuple_get_expression_completions_get_item_lookup_completions_get_attribute_completions)r3   r[   rj   r   ri   r   r   expression_vals           r#   rg   z#DictionaryCompleter.get_completions  s      oo',,.**33H4O4OP 	9E"',,.GZ!\\*kBN .4-8^'5a'8G$	9 33nk
 	
 	
 44nk
 	
 	
 22nk
 	
 	
	
	
	
sB   BC*C* C*(C$)C*C&C*C(C*&C*(C*c                R    	 t        t        |            S # t        $ r t        w xY wr&   )rS   reprr   ReprFailedError)r3   objs     r#   _do_reprzDictionaryCompleter._do_repr  s*    	"tCy>! 	"!!	"s    &c                    | j                   j                  |j                        }|%|j                         d   }| j	                  ||      S y)z
        Evaluate
        Nr   )r   searchr_   r   r   )r3   r[   localsri   
object_vars        r#   eval_expressionz#DictionaryCompleter.eval_expression  sJ     ''..x/J/JK*J<<
F33r"   c              #     K   | j                  ||      }|Wt        |t        t        t        t
        j                  t
        j                  f      rt        dd       yt        dd       yyw)z?
        Complete the [ or . operator after an object.
        N[r   .)	r   r   r   r   dictcollections_abcMappingSequencer   )r3   r[   rj   r   results        r#   r   z/DictionaryCompleter._get_expression_completions  sh      %%h<udO$;$;_=U=UV !a((
 !a(( s   A-A/c           
   #  
   K   d	 fd} j                   j                  |j                        }||j                         \  }} j	                  ||      }t        |t        t        j                  f      rt        |      }	||dz   |dz   fD ]"  }
	 t        t        j                  |
            }	 n |D ]X  }
t        |
      j                  |	      s	  j                  |
      }t!        |dz   t#        |       d| d |||
             Z yt        |t&        t(        t        j*                  f      r|r|j-                         rzt/        t1        t#        |      d            D ]X  }
t        |
      j                  |      s	  j                  |
      }t!        |dz   t#        |       d| d |||
             Z yyyy# t        t        f$ r Y Iw xY w# t$        $ r Y 2w xY w# t2        $ r Y t$        $ r Y w xY ww)
z+
        Complete dictionary keys.
        c                $     dd fd}|S )z4Abbreviate meta text, make sure it fits on one line.Nc                     S 	    } j                  |       }d|v r|j                  dd      d   dz   }||S # t        $ r Y yw xY w)N-
rc   r   r   )r   r   split)valuerD   cached_resultr   r   r3   s     r#   get_value_reprz[DictionaryCompleter._get_item_lookup_completions.<locals>.meta_repr.<locals>.get_value_repr  sl     ,((HE==/D
 4<::dA.q1E9D $ % s   = 	A	A	)rT   rS   r!   )r   r   r   r   r3   s   `` @r#   	meta_reprzCDictionaryCompleter._get_item_lookup_completions.<locals>.meta_repr  s    (,M & "!r"   NrO   rI   ]r   )r   r   i  )r   objectr   r   rT   zCallable[[], str])r   r   r_   r   r   r   r   r   r   rS   astliteral_evalSyntaxErrorr   rf   r   r   r   r   r   r   r   isdigitrangeminr   )r3   r[   rj   r   r   ri   r   r   r   key_obj_strkk_reprs   `           r#   r   z0DictionaryCompleter._get_item_lookup_completions  s    	"@ ((//0K0KL#llnOJ \\*k:F &4)@)@"AB!#hsSy#)4 A&)#*:*:1*=&>    !A1v((5	!%)]]1%5F", &!$S	*+F81-6vq-A	# 	! FT5/2J2J$KLckkm"3s6{D#9: %q6,,S1%)-q)9&0$*SL%(XI./xqM1:611E	'" !"	% , MA  (4 ! !  / ! !  $, % !%#2 % $%s   BHG/"H:GA5H:G*>HGHGH	G'#H&G''H*	H 3H5H =H?H  Hc              #  d  K   | j                   j                  |j                        }||j                         \  }}| j	                  ||      | j                  t                    }dfd}|D ]9  }	|	j                  |      s ||	      }
t        |	t        |       |	|
z          ; yyw)z+
        Complete attribute names.
        Nc                    	 t        | d       }t        j                  |      st        j                  |      ryt	        |t
        j                        ryt	        |t
        j                        ry	 y#  Y yxY w)Nr   z{}z[]r   )getattrinspect
isfunctionismethodr   r   r   r   )r}   r   r   s     r#   
get_suffixzBDictionaryCompleter._get_attribute_completions.<locals>.get_suffixE  sr    	!&$5C))#.'2B2B32G#!#'>'>?#!#'?'?@# A s   7A3 A3 A3 3A7)r   )r}   rS   rT   rS   )
r   r   r_   r   r   _sort_attribute_namesdirrf   r   r   )r3   r[   rj   r   ri   r   	attr_namenamesr   r}   r   r   s              @r#   r   z.DictionaryCompleter._get_attribute_completions3  s      --44X5P5PQ$)LLN!J	 \\*k:F..s6{;E  S??9-'-F$TC	N?D6MRRS+ s   BB0)B0c                $    dd}t        ||      S )z~
        Sort attribute names alphabetically, but move the double underscore and
        underscore names to the end.
        c                ^    | j                  d      rd| fS | j                  d      rd| fS d| fS )N__   r{   rc   r   )rf   )r}   s    r#   sort_keyz;DictionaryCompleter._sort_attribute_names.<locals>.sort_key]  s8    t$4y s#4y t9r"   r   )r}   rS   rT   ztuple[int, str])r   )r3   r   r   s      r#   r   z)DictionaryCompleter._sort_attribute_namesW  s    	 e**r"   r   )r   rS   r   dict[str, Any]rT   r   ro   )r   r   rT   rS   )r[   r   r   r   rT   r   )r[   r   rj   r
   r   r   rT   rp   )r   	list[str]rT   r   )r   r   r   r    r(   r   rg   r   r   r   r   r   r   rr   rs   s   @r#   r/   r/   )  s    S
1S
 1S
 
	S
j
 
2?
	
8"	)) &) $	)
 
).\%\% &\% $	\%
 
\%|"S"S &"S $	"S
 
"SH+r"   r/   c                  8    e Zd ZdZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZy)r   z
    Wrapper around completer that hides private fields, depending on whether or
    not public fields are shown.

    (The reason this is implemented as a `Completer` wrapper is because this
    way it works also with `FuzzyCompleter`.)
    c                     || _         || _        y r&   )	completercomplete_private_attributes)r3   r   r   s      r#   r(   zHidePrivateCompleter.__init__p  s    
 #+F(r"   c                L   t        t        | j                  j                  ||      dd            }| j	                         }d}dd|t
        j                  k(  rd}n'|t
        j                  k(  rt        fd|D              }|r|D cg c]  } |      r| }}|S c c}w )Nr   i  Fc                `    t        t        | j                              }|j                  d      S )Nr{   )r   r   r   rf   )
completionrD   s     r#   
is_privatez8HidePrivateCompleter.get_completions.<locals>.is_private  s'    ()::;M;M)NOD??3''r"   Tc              3  0   K   | ]  } |         y wr&   r!   ).0r   r   s     r#   	<genexpr>z7HidePrivateCompleter.get_completions.<locals>.<genexpr>  s     Xj:j#99Xs   )r   r   rT   rY   )	r   r   r   rg   r   r   r   r   any)r3   r[   rj   completionsr   hide_privater   r   s          @r#   rg   z$HidePrivateCompleter.get_completionsx  s     4>>11(NKQPTU
 '+&F&F&H#	( '*C*I*IIL(,E,R,RRXKXXL-8)
:@V
K  	s   B!B!N)r   r   r   z'Callable[[], CompletePrivateAttributes]rT   rn   ro   )r   r   r   r    r(   rg   r!   r"   r#   r   r   g  sJ    GG &MG 
	G 2?	r"   r   c                      e Zd ZdZy)r   z;Raised when the repr() call in `DictionaryCompleter` fails.N)r   r   r   r    r!   r"   r#   r   r     s    Ar"   r   c                z    | j                   }| j                  dk(  ry|t        v ryt        j                  |      ryy)z7
    Return completion style to use for this name.
    rz   zclass:completion.paramzclass:completion.builtinzclass:completion.keywordr   )r~   r|   _builtin_nameskeyword	iskeyword)jedi_completionr}   s     r#   r   r     s?     ,,Dw&'~))r"   )r  zjedi.api.classes.CompletionrT   rS   )5
__future__r   r   collections.abcabcr   r   r  rA   enumr   	itertoolsr   typingr   r   r   r	   prompt_toolkit.completionr
   r   r   r   (prompt_toolkit.contrib.completers.systemr   1prompt_toolkit.contrib.regular_languages.compilerr   rU   3prompt_toolkit.contrib.regular_languages.completionr   prompt_toolkit.documentr   prompt_toolkit.formatted_textr   r   ptpython.utilsr   jedi.api.classesjedir   __all__r   r   r-   r/   r   r   r   builtinsr   r  ImportErrorr   r!   r"   r#   <module>r     s    " 
 )   	   9 9  E X P , R 8R
R [i [|]I ]@{+) {+|	*9 *ZBi B]N
0	  Ns   :C CC