
    !<iN                     T   d 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	m
Z
mZmZmZmZ ddlmZ erddlmZ dd	lmZmZ d
ddedefdZdddededee         fdZded         dedee         fdZdddededee         fdZ	 	 	 	 d0deeedf         e
f         deedf         deded eedf         d!eedf         dee         fd"Zd#edefd$Z d%edefd&Z!dddedee         fd'Z"dddedefd(Z#d)eedf         d*ddedee         fd+Z$d,e	defd-Z%dddedefd.Z&d*ddedefd/Z'dS )1zZsh completion script generator.

Generates static zsh completion scripts using the compsys framework.
No runtime Python dependency.
    N)dedent)indent)TYPE_CHECKING)CompletionActionCompletionDataclean_choice_textextract_completion_dataget_completion_actionstrip_markup)docstring_parse)App)ArgumentArgumentCollectionappr   	prog_namereturnc                    |rt          j        d|          st          d|d          t          |           }d| dd| dddg}|                    t          |d	|| j        rt          | j                  nd	| j        rt          | j                  nd	
                     |                    ddg           d	                    |          dz   S )a  Generate zsh completion script.

    Parameters
    ----------
    app : App
        The Cyclopts application to generate completion for.
    prog_name : str
        Program name (alphanumeric with hyphens/underscores).

    Returns
    -------
    str
        Complete zsh completion script.

    Raises
    ------
    ValueError
        If prog_name contains invalid characters.
    z^[a-zA-Z0-9_-]+$zInvalid prog_name: z0. Must be alphanumeric with hyphens/underscores.z	#compdef  _z() {z  local line state )r   
help_flagsversion_flags}
)
rematch
ValueErrorr	   extend_generate_completion_for_pathr   tupler   join)r   r   completion_dataliness       /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/cyclopts/completion/zsh.pygenerate_completion_scriptr%      s   (  nBH%8)DD nlylllmmm-c22O 	 I
I
E 
LL%03FuS^,,,B696GO% 1222R	
 	
 	
   
LL	
   99Ud""    	argumentsr   
indent_strc                     t          d| d| d          }t          ||          }d |                    d          D             S )aA  Generate dynamic completion for the 'run' command.

    Parameters
    ----------
    arguments : ArgumentCollection
        Arguments for run command.
    indent_str : str
        Indentation string.
    prog_name : str
        Program name.

    Returns
    -------
    list[str]
        Zsh completion code lines.
    a          local script_path
        local -a completions
        local -a remaining_words

        # If completing first argument (the script path), suggest files
        if [[ $CURRENT -eq 2 ]]; then
          _files
          return
        fi

        # Get absolute path to the script file
        script_path=${words[2]}
        script_path=${script_path:a}
        if [[ -f $script_path ]]; then
          remaining_words=(${words[3,-1]})
          local result
          local cmd

          if command -v z$ &>/dev/null; then
            cmd="a  "
          else
            return
          fi
          # Call back into cyclopts to get dynamic completions from the script
          result=$($cmd _complete run "$script_path" "${remaining_words[@]}" 2>/dev/null)
          if [[ -n $result ]]; then
            # Parse and display completion results
            completions=()
            while IFS= read -r line; do
              completions+=($line)
            done <<< $result
            _describe 'command' completions
          fi
        fic                 6    g | ]}|                                 S r   )rstrip).0lines     r$   
<listcomp>z4_generate_run_command_completion.<locals>.<listcomp>   s     ;;;dDKKMM;;;r&   r   )r   textwrap_indentsplit)r'   r(   r   templateindenteds        r$    _generate_run_command_completionr3   P   sm    *  "& #'" "( )" " " " "HH x44H;;hnnT&:&:;;;;r&   positional_argsr   help_formatc                    g }d | D             }d | D             }|D ]}d|j         pdz   }t          ||          }|                    d          }|r(d |D             }	d                    |	          }
d	|
 d
}n!t	          t          |j                            }|rd| d| d| dnd| d| d}|                    |           |D ]}t          ||          }|                    d          }|r(d |D             }	d                    |	          }
d	|
 d
}n!t	          t          |j                            }|r	d| d| dnd| d}|                    |           |S )a  Generate positional argument specs for nested command context.

    In nested contexts (after *::arg:->args), word indexing is shifted:
    - words[1] = subcommand name
    - words[2] = first positional argument
    - words[3] = second positional argument, etc.

    Parameters
    ----------
    positional_args : list[Argument]
        Positional arguments to generate specs for.
    help_format : str
        Help text format.

    Returns
    -------
    list[str]
        List of zsh positional argument specs.
    c                 :    g | ]}|                                 |S r   is_var_positionalr,   args     r$   r.   z5_generate_nested_positional_specs.<locals>.<listcomp>   s)    OOOSs7L7L7N7NOSOOOr&   c                 :    g | ]}|                                 |S r   r8   r:   s     r$   r.   z5_generate_nested_positional_specs.<locals>.<listcomp>   s)    WWWs?T?T?V?VWWWWr&      r   Tforcec                 F    g | ]}t          t          |                    S r   _escape_completion_choicer   r,   cs     r$   r.   z5_generate_nested_positional_specs.<locals>.<listcomp>   +    ```ST89J19M9MNN```r&    ()':c                 F    g | ]}t          t          |                    S r   rA   rC   s     r$   r.   z5_generate_nested_positional_specs.<locals>.<listcomp>   rE   r&   '*:)index_get_description_from_argumentget_choicesr!   _map_completion_action_to_zshr
   hintappend)r4   r5   specsvariadic_argsnon_variadic_argsr;   posdescchoicesescaped_choiceschoices_stractionspecs                r$   !_generate_nested_positional_specsr]      s   . E POOOOOMWWWWW !   39>"-c;?? ///-- 	T``X_```O((?33K''''FF23H3R3RSSF-3J)3))))))))9JS9J9J49J9J9JT   -c;??///-- 	T``X_```O((?33K''''FF23H3R3RSSF)/B%T%%F%%%%]4]]]TLr&   argumentc           	      D   g }t          | |          }|                     d          }|rd |D             }|                    | d           |                    | d           |D ] }|                    | d| d| d           !|                    | d	           |                    | d
           net          | j                  }|t
          j        k    r|                    | d           n(|t
          j        k    r|                    | d           |S )aY  Generate _describe-based completion for a single positional argument.

    Parameters
    ----------
    argument : Argument
        Argument to generate completion for.
    help_format : str
        Help text format.
    indent_str : str
        Indentation string.

    Returns
    -------
    list[str]
        Zsh completion code lines.
    Tr>   c                 F    g | ]}t          t          |                    S r   rA   rC   s     r$   r.   z1_generate_describe_completion.<locals>.<listcomp>   *    \\\q45Fq5I5IJJ\\\r&   zlocal -a choicesz	choices=(z  'rJ   rI   rH   z_describe 'argument' choices_files_directories)rN   rO   rR   r
   rQ   r   FILESDIRECTORIES)	r^   r5   r(   r#   rW   rX   rY   choicer[   s	            r$   _generate_describe_completionrg      sg   * E)(K@@D """..G 6\\T[\\\
444555
---...% 	= 	=FLLJ;;6;;D;;;<<<<
%%%&&&
@@@AAAA 'x}55%+++LLJ...////'333LLJ444555 Lr&      cycloptsr   r"   .command_pathr   r   r   c           
      	   | |         }|j         }|j        }d|z  }	g }
|dk    r,|dk    r&|
                    t          ||	|                     |
S g }g }d |D             }d |D             }|                    d            |D ],}t          ||j                  }|                    |           -t                      }|D ]o}t          d |j	        D                       rOt          |j	        |j        |j                  }|                    |           |                    |j	                   p|D ]6}|                    d	          r||vrd
| d}|                    |           7|D ]6}|                    d	          r||vrd
| d}|                    |           7t          d |D                       }|rN|sL|rt          ||j                  }n/|D ],}t!          ||j                  }|                    |           -||z   }|r*|                    d           |                    d           |r~|rdnd}|
                    |	 d| d           |dd         D ]}|
                    |	 d| d           |
                    |	 d|d                     |
                    d           |r)|
                    |	 d           |
                    |	 d           g }|D ]W}|j	        D ]M}|                    d	          s6t#          |j        |j                  }|                    d
| d| d
           NX|
                    |	 d           |
                    |	 d           |D ]}|
                    |	 d|            |
                    |	 d           |
                    |	 d           |
                    |	 d            |
                    |	 d!           |
                    |	 d"           |D ]}|j	        D ]}|                    d	          r||fz   }|| v r_|
                    |	 d| d#           t%          | ||d$z   |||          }|
                    |           |
                    |	 d%           |
                    |	 d&           |
                    |	 d            |
                    |	 d'           |
S )(a  Generate completion code for a specific command path.

    Parameters
    ----------
    completion_data : dict
        Extracted completion data.
    command_path : tuple[str, ...]
        Command path.
    indent : int
        Indentation level.
    prog_name : str
        Program name.
    help_flags : tuple[str, ...]
        Help flags.
    version_flags : tuple[str, ...]
        Version flags.

    Returns
    -------
    list[str]
        Zsh code lines.
    rF   )runri   c                 .    g | ]}|j         	|j        |S )N)rM   showr:   s     r$   r.   z1_generate_completion_for_path.<locals>.<listcomp>,  s&    VVVs393HSX3Hs3H3H3Hr&   c                 H    g | ]}|                                 |j        | S r   )is_positional_onlyrn   r:   s     r$   r.   z1_generate_completion_for_path.<locals>.<listcomp>-  s2    ZZZCC4J4J4L4LZQTQYZCZZZr&   c                     | j         pdS )Nr   )rM   )as    r$   <lambda>z/_generate_completion_for_path.<locals>.<lambda>0  s    qw|! r&   )keyc              3   @   K   | ]}|                     d           V  dS -N)
startswith)r,   names     r$   	<genexpr>z0_generate_completion_for_path.<locals>.<genexpr>:  s.      IIts##IIIIIIr&   rw   rI   z![Display this message and exit.]'z[Display application version.]'c              3   V   K   | ]$}|j         D ]}|                    d            V  %dS rv   )namesrx   )r,   registered_commandcmd_names      r$   rz   z0_generate_completion_for_path.<locals>.<genexpr>L  s\          );XjXp   LTH$$$             r&   z'1: :->cmds'z'*::arg:->args'z-C r   z_arguments \Nz  z \zcase $state inz  cmds)rJ   z    local -a commandsz    commands=(z      z    )z,    _describe -t commands 'command' commandsz    ;;z  args)z    case $words[1] inrH      z
        ;;z    esacesac)commandsr'   r   r3   sort_generate_keyword_specsr5   setanyr|   #_generate_keyword_specs_for_commandr   updaterx   rR   r]   _generate_positional_spec_safe_get_description_from_appr   )r"   rj   r   r   r   r   datar   r'   r(   r#   
args_specspositional_specsr4   keyword_argsr^   rS   flag_command_namesr}   flagr\   has_non_flag_commandsc_flagcmd_listr~   rW   cmdsub_path	sub_liness                                r$   r   r      sy   < <(D}HIvJExI$;$;5iYWWXXXJ WViVVVOZZ9ZZZL 33444 ! ! !'$2BCC%     & @ @II0B0HIIIII 	@7"(*<*@$BR E e$$$%%&8&>???  $ $??3 	$D0B$B$B>t>>>Dd### $ $??3 	$D0B$B$B<t<<<Dd###    ?G         34 3 	.@RVRbcc , . .04;KLL ''---- &
2
 -.)))+,,, /7R
99v999:::ssO 	5 	5DLLJ33$3334444
66jn66777R &*
222333
+++,,,"* 	< 	<.4 < <**3// <9:L:PRVRbccDOO$:$:$:4$:$:$:;;;<
 	
999:::
222333 	5 	5CLLJ33c334444
)))***
PPPQQQ
***+++
+++,,,
999:::"* 	< 	<.4 < <&&s++ '8+5..LLJ!A!Ah!A!A!ABBB ='6A:y*Vc! !I LL+++LLJ!:!:!:;;;< 	
,,,---
***+++
((()))Lr&   rf   c                 n   |                      dd          } |                      dd          } |                      dd          } |                      dd          } |                      d	d
          } |                      dd          } |                      dd          } |                      dd          } |                      dd          } |                      dd          } |                      dd          } |                      dd          } |                      dd          } |                      dd          } | S )a{  Escape special characters in a completion choice value for zsh.

    Choice should already be cleaned via clean_choice_text() before calling this function.
    This function only applies zsh-specific escaping.

    Parameters
    ----------
    choice : str
        Cleaned choice value.

    Returns
    -------
    str
        Escaped choice value safe for zsh completion.
    r   \\rI   '\''`\`$\$"\"rF   z\ rG   z\(rH   z\)[\[]\];z\;|z\|&z\&rJ   \:replace)rf   s    r$   rB   rB     s     ^^D&))F^^C))F^^C''F^^C''F^^C''F^^C''F^^C''F^^C''F^^C''F^^C''F^^C''F^^C''F^^C''F^^C''FMr&   textc                 f   |                      dd          } |                      dd          } |                      dd          } |                      dd          } |                      d	d
          } |                      dd          } |                      dd          } |                      dd          } | S )zEscape special characters in description text for zsh.

    Parameters
    ----------
    text : str
        Cleaned description text.

    Returns
    -------
    str
        Escaped description safe for zsh completion.
    r   r   r   r   r   r   r   r   rI   r   rJ   r   r   r   r   r   r   )r   s    r$   _escape_zsh_descriptionr     s     <<f%%D<<U##D<<U##D<<U##D<<W%%D<<U##D<<U##D<<U##DKr&   c           
         g }t          | |          }|                                 }d}|                     d          }|r*d |D             }d                    |          }d| d}d}n!t	          t          | j                            }| j        j        D ]}	|		                    d	          s|r|s
d
|	 d| d}
nD|r#d
|	 d| d|	
                    d	           d| d
	}
nd
|	 d| d|	
                    d	           d
}
|                    |
           | j        D ]6}	|		                    d	          sd
|	 d| d}
|                    |
           7|S )a  Generate zsh _arguments specs for a keyword argument.

    Parameters
    ----------
    argument : Argument
        Argument object from ArgumentCollection.
    help_format : str
        Help text format.

    Returns
    -------
    list[str]
        List of zsh argument specs.
    r   Tr>   c                 F    g | ]}t          t          |                    S r   rA   rC   s     r$   r.   z+_generate_keyword_specs.<locals>.<listcomp>  ra   r&   rF   rG   rH   Frw   rI   r   ]'z]:rJ   )rN   is_flagrO   r!   rP   r
   rQ   	parameterry   rx   lstriprR   	negatives)r^   r5   rS   rW   r   r[   rX   rY   rZ   ry   r\   s              r$   r   r     s    E)(K@@DD F"""..G U\\T[\\\hh//#[###./DX]/S/STT "' 	 	s## 	 	: 	:&t&&d&&&DD 	:BtBBdBBdkk#&6&6BBBBBDD9t99d99dkk#&6&6999DT "  s## 	"4""$"""TLr&   c                    t          | |          }|                     d          }|r(d |D             }d                    |          }d| d}n!t          t	          | j                            }|                                 r|r	d| d| d	nd| d	S | j        t          d| j	         d          | j        dz   }|rd	| d| d| d	nd	| d| d	S )a  Generate zsh _arguments spec for a positional argument.

    Parameters
    ----------
    argument : Argument
        Positional argument object.
    help_format : str
        Help text format.

    Returns
    -------
    str
        Zsh positional argument spec.
    Tr>   c                 F    g | ]}t          t          |                    S r   rA   rC   s     r$   r.   z-_generate_positional_spec.<locals>.<listcomp>  ra   r&   rF   rG   rH   rL   rJ   rI   NzPositional-only argument z missing indexr=   )
rN   rO   r!   rP   r
   rQ   r9   rM   r   r|   )r^   r5   rW   rX   rY   rZ   r[   rV   s           r$   r   r     s6    *(K@@D """..G U\\T[\\\hh//#[###./DX]/S/STT!!## C)/B%T%%F%%%%]4]]]B ~SX^SSSTTT
.1
C)/F%s%%T%%F%%%%5F5F5Ft5F5F5FFr&   r|   cmd_appc                     g }t          ||          }| D ]5}|                    d          rd| d| d}|                    |           6|S )ad  Generate zsh _arguments specs for a command that looks like a flag.

    Parameters
    ----------
    names : tuple[str, ...]
        Registered names for the command.
    cmd_app : App
        Command app with flag-like names.
    help_format : str
        Help text format.

    Returns
    -------
    list[str]
        List of zsh argument specs.
    rw   rI   r   r   )r   rx   rR   )r|   r   r5   rS   rW   ry   r\   s          r$   r   r   *  sk    " E)';??D  ??3 	&t&&d&&&DLLLr&   r[   c                 N    | t           j        k    rdS | t           j        k    rdS dS )a  Map shell-agnostic completion action to zsh-specific completion command.

    Parameters
    ----------
    action : CompletionAction
        Shell-agnostic completion action.

    Returns
    -------
    str
        Zsh completion command (e.g., "_files", "_directories", or "").
    rb   rc   r   )r   rd   re   )r[   s    r$   rP   rP   F  s1     !'''x	#/	/	/~2r&   c                 Z    t          | j        j        pd|          }t          |          S )aH  Extract plain text description from Argument, escaping zsh special chars.

    Parameters
    ----------
    argument : Argument
        Argument object with parameter help text.
    help_format : str
        Help text format.

    Returns
    -------
    str
        Escaped plain text description (truncated to 80 chars).
    r   format)r   r   helpr   )r^   r5   r   s      r$   rN   rN   Z  s/     */52kJJJD"4(((r&   c                     | j         sdS 	 t          | j         d          }|j        pd}n$# t          $ r t	          | j                   }Y nw xY wt          ||          }t          |          S )a/  Extract plain text description from App, escaping zsh special chars.

    Parameters
    ----------
    cmd_app : App
        Command app with help text.
    help_format : str
        Help text format.

    Returns
    -------
    str
        Escaped plain text description (truncated to 80 chars).
    r   	plaintextr   )r   r   short_description	Exceptionstrr   r   )r   r5   parsedr   s       r$   r   r   m  s     < r! {;;'-2 ! ! !7<  ! [111D"4(((s   * A
A)rh   ri   r   r   )(__doc__r   textwrapr   r   r/   typingr   cyclopts.completion._baser   r   r   r	   r
   r   cyclopts.help.helpr   ri   r   cyclopts.argumentr   r   r   r%   listr3   r]   rg   dictr    intr   rB   r   r   r   r   rP   rN   r   r   r&   r$   <module>r      s    
			       . . . . . .                            / . . . . . ?>>>>>>>>2#E 2#c 2#c 2# 2# 2# 2#j:<#:<:< :< 
#Y	:< :< :< :<z@*%@@ 
#Y@ @ @ @F,,, , 
#Y	, , , ,d "$%'T T%S/>9:TS/T T 	T
 c3hT c?T 
#YT T T Tnc c    B# #    03j 3s 3tCy 3 3 3 3l"G
 "G "G "G "G "G "GJuS#X  ]` eijmen    8*: s    ()Z )c )c ) ) ) )&)E ) ) ) ) ) ) ) )r&   