B
    Mô`0  ã               @   s  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 ddlmZ ddl	m
Z
 ddlmZ yd d	lmZ W n ek
rŒ   d dlZY nX d
ZdZdZdZeeedœZe 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d d!„ Zd"d#„ Z d$d%„ Z!d&d'„ Z"dS )(é    Né   )ÚArgument)ÚMultiCommand)ÚOption)Úsplit_arg_string)ÚChoice)Úecho)Úabcú=a†  
%(complete_func)s() {
    local IFS=$'
'
    COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   %(autocomplete_var)s=complete $1 ) )
    return 0
}

%(complete_func)setup() {
    local COMPLETION_OPTIONS=""
    local BASH_VERSION_ARR=(${BASH_VERSION//./ })
    # Only BASH version 4.4 and later have the nosort option.
    if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then
        COMPLETION_OPTIONS="-o nosort"
    fi

    complete $COMPLETION_OPTIONS -F %(complete_func)s %(script_names)s
}

%(complete_func)setup
a„  
#compdef %(script_names)s

%(complete_func)s() {
    local -a completions
    local -a completions_with_descriptions
    local -a response
    (( ! $+commands[%(script_names)s] )) && return 1

    response=("${(@f)$( env COMP_WORDS="${words[*]}" \
                        COMP_CWORD=$((CURRENT-1)) \
                        %(autocomplete_var)s="complete_zsh" \
                        %(script_names)s )}")

    for key descr in ${(kv)response}; do
      if [[ "$descr" == "_" ]]; then
          completions+=("$key")
      else
          completions_with_descriptions+=("$key":"$descr")
      fi
    done

    if [ -n "$completions_with_descriptions" ]; then
        _describe -V unsorted completions_with_descriptions -U
    fi

    if [ -n "$completions" ]; then
        compadd -U -V unsorted -a completions
    fi
    compstate[insert]="automenu"
}

compdef %(complete_func)s %(script_names)s
z¯complete --no-files --command %(script_names)s --arguments "(env %(autocomplete_var)s=complete_fish COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) %(script_names)s)")ÚbashÚzshÚfishz[^a-zA-Z0-9_]c             C   s>   t  d|  dd¡¡}t |t¡}|d |¡| |dœ  ¡ d S )NÚ ú-Ú_z_{}_completion)Zcomplete_funcZscript_namesZautocomplete_varú;)Ú_invalid_ident_char_reÚsubÚreplaceÚ_completion_scriptsÚgetÚCOMPLETION_SCRIPT_BASHÚformatÚstrip)Ú	prog_nameÚcomplete_varÚshellZcf_nameÚscript© r   ú7/tmp/pip-unpacked-wheel-8jvulpl1/click/_bashcomplete.pyÚget_completion_script_   s    r    c             C   sØ   | j ||dd�}|j|j }x¶|rÒt|jtƒrÎ|jjsv|j ||¡\}}}|dkrV|S |j |||dd�}|j|j }qÐxD|rº|j ||¡\}}}|dkrœ|S |j |||dddd�}|j}qxW |}|j|j }qP qW |S )a  Parse into a hierarchy of contexts. Contexts are connected
    through the parent variable.

    :param cli: command definition
    :param prog_name: the program that is running
    :param args: full list of args
    :return: the final context/command parsed
    T)Úresilient_parsingN)Úparentr!   F)r"   Zallow_extra_argsÚallow_interspersed_argsr!   )Zmake_contextÚprotected_argsÚargsÚ
isinstanceÚcommandr   ÚchainZresolve_command)Úclir   r%   ÚctxÚcmd_nameÚcmdZsub_ctxr   r   r   Úresolve_ctxl   s6    	
r-   c             C   s   | o| dd… dkS )z—
    :param param_str: param_str to check
    :return: whether or not this is the start of an option declaration
        (i.e. starts "-" or "--")
    Nr   r   r   )Z	param_strr   r   r   Ústart_of_option—   s    r.   c             C   sr   t |tƒsdS |jrdS d}x>ttdd„ | D ƒƒƒD ]$\}}|d |jkrLP t|ƒr4|}q4W |rn||jkrndS dS )aU  
    :param all_args: the full original list of args supplied
    :param cmd_param: the current command paramter
    :return: whether or not the last option declaration (i.e. starts
        "-" or "--") is incomplete and corresponds to this cmd_param. In
        other words whether this cmd_param option can still accept
        values
    FNc             S   s   g | ]}|t kr|‘qS r   )Ú	WORDBREAK)Ú.0Úargr   r   r   ú
<listcomp>¯   s    z(is_incomplete_option.<locals>.<listcomp>r   T)r&   r   Zis_flagÚ	enumerateÚreversedÚnargsr.   Úopts)Úall_argsÚ	cmd_paramZlast_optionÚindexZarg_strr   r   r   Úis_incomplete_option    s    	
r:   c             C   s^   t |tƒsdS | |j }|dkr$dS |jdkr2dS t |tjƒrZ|jdkrZt|ƒ|jk rZdS dS )a`  
    :param current_params: the current params and values for this
        argument as already entered
    :param cmd_param: the current command parameter
    :return: whether or not the last argument is incomplete and
        corresponds to this cmd_param. In other words whether or not the
        this cmd_param argument can still accept values
    FNTéÿÿÿÿr   )r&   r   Únamer5   r	   ÚIterableÚlen)Zcurrent_paramsr8   Zcurrent_param_valuesr   r   r   Úis_incomplete_argument¹   s    	



r?   c                sT   g }t |jtƒr(‡ fdd„|jjD ƒ}n(|jdk	rP|j| |ˆ d�}dd„ |D ƒ}|S )a  
    :param ctx: context associated with the parsed command
    :param args: full list of args
    :param incomplete: the incomplete text to autocomplete
    :param cmd_param: command definition
    :return: all the possible user-specified completions for the param
    c                s"   g | ]}t |ƒ ˆ ¡r|d f‘qS )N)ÚstrÚ
startswith)r0   Úc)Ú
incompleter   r   r2   Þ   s    z,get_user_autocompletions.<locals>.<listcomp>N)r*   r%   rC   c             S   s"   g | ]}t |tƒr|n|d f‘qS )N)r&   Útuple)r0   rB   r   r   r   r2   å   s    )r&   Útyper   ÚchoicesÚautocompletion)r*   r%   rC   r8   ÚresultsZdynamic_completionsr   )rC   r   Úget_user_autocompletionsÒ   s    
rI   c             c   s>   x8| j  | ¡D ](}| |¡r| j  | |¡}|js|V  qW dS )zÌ
    :param ctx: context associated with the parsed command
    :starts_with: string that visible commands must start with.
    :return: all visible (not hidden) commands that start with starts_with.
    N)r'   Zlist_commandsrA   Zget_commandÚhidden)r*   Zstarts_withrB   r'   r   r   r   Ú"get_visible_commands_starting_withê   s
    
rK   c                s€   t ˆ jtƒr&| dd„ tˆ |ƒD ƒ¡ xTˆ jd k	rzˆ j‰ t ˆ jtƒr(ˆ jjr(‡ fdd„tˆ |ƒD ƒ}| dd„ |D ƒ¡ q(W d S )Nc             S   s   g | ]}|j | ¡ f‘qS r   )r<   Úget_short_help_str)r0   rB   r   r   r   r2   ü   s   z.add_subcommand_completions.<locals>.<listcomp>c                s   g | ]}|j ˆ jkr|‘qS r   )r<   r$   )r0   rB   )r*   r   r   r2     s   c             S   s   g | ]}|j | ¡ f‘qS r   )r<   rL   )r0   rB   r   r   r   r2     s    )r&   r'   r   ÚextendrK   r"   r(   )r*   rC   Zcompletions_outZremaining_commandsr   )r*   r   Úadd_subcommand_completions÷   s    
rN   c       	         sB  t  |¡‰ t| ||ƒ}|dkr"g S dˆ k}tˆƒr\tˆkr\ˆ t¡}ˆ  |d ¡ |d ‰nˆtkrhd‰g }|sÔtˆƒrÔxV|jjD ]J‰t	ˆt
ƒr‚ˆjs‚‡ ‡fdd„ˆjˆj D ƒ}| ‡‡fdd„|D ƒ¡ q‚W |S x(|jjD ]‰tˆ ˆƒrÞt|ˆ ˆˆƒS qÞW x.|jjD ]"‰t|jˆƒ�rt|ˆ ˆˆƒS �qW t|ˆ|ƒ t|ƒS )	zõ
    :param cli: command definition
    :param prog_name: the program that is running
    :param args: full list of args
    :param incomplete: the incomplete text to autocomplete
    :return: all the possible completions for the incomplete
    Nz--r   é   r   c                s   g | ]}|ˆ ksˆj r|‘qS r   )Úmultiple)r0   Z	param_opt)r7   Úparamr   r   r2   /  s   zget_choices.<locals>.<listcomp>c                s    g | ]}|  ˆ ¡r|ˆjf‘qS r   )rA   Úhelp)r0   Úo)rC   rQ   r   r   r2   4  s    )ÚcopyÚdeepcopyr-   r.   r/   Ú	partitionÚappendr'   Úparamsr&   r   rJ   r6   Zsecondary_optsrM   r:   rI   r?   rN   Úsorted)	r)   r   r%   rC   r*   Zhas_double_dashZpartition_incompleteZcompletionsZ
param_optsr   )r7   rC   rQ   r   Úget_choices  s8    



rZ   c             C   s’   t tjd ƒ}ttjd ƒ}|d|… }y|| }W n tk
rL   d}Y nX x>t| |||ƒD ],}t|d ƒ |r^t|d r„|d ndƒ q^W dS )NÚ
COMP_WORDSÚ
COMP_CWORDr   r   r   r   T)r   ÚosÚenvironÚintÚ
IndexErrorrZ   r   )r)   r   Zinclude_descriptionsÚcwordsÚcwordr%   rC   Úitemr   r   r   Údo_completeE  s    
rd   c             C   sp   t tjd ƒ}tjd }|dd … }xFt| |||ƒD ]4}|d r\tdj|d |d d�ƒ q4t|d ƒ q4W dS )Nr[   r\   r   z{arg}	{desc}r   )r1   ÚdescT)r   r]   r^   rZ   r   r   )r)   r   ra   rC   r%   rc   r   r   r   Údo_complete_fishY  s    
rf   c             C   st   d|kr|  dd¡\}}n|}d}|dkr>tt|||ƒƒ dS |dkrp|dkrXt| |ƒS |dkrpt| ||d	kƒS d
S )Nr   r   r   ÚsourceTZcompleter   >   r   r   r   F)Úsplitr   r    rf   rd   )r)   r   r   Zcomplete_instrr'   r   r   r   r   Úbashcompleteg  s    
ri   )#rT   r]   ÚreÚcorer   r   r   Úparserr   Útypesr   Úutilsr   Úcollectionsr	   ÚImportErrorr/   r   ZCOMPLETION_SCRIPT_ZSHZCOMPLETION_SCRIPT_FISHr   Úcompiler   r    r-   r.   r:   r?   rI   rK   rN   rZ   rd   rf   ri   r   r   r   r   Ú<module>   s@   #
+	5