
    '<ilU                     0   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	 ddl
mZ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l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 	 	 	 	 d2d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&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d0Z,d+d,dedefd1Z-y)3zZsh completion script generator.

Generates static zsh completion scripts using the compsys framework.
No runtime Python dependency.
    N)dedent)indent)TYPE_CHECKING)is_iterable_type)CompletionActionCompletionDataclean_choice_textescape_for_shell_patternextract_completion_dataget_completion_actionstrip_markup)docstring_parse)App)ArgumentArgumentCollection)CommandSpecappr   	prog_namereturnc                    |rt        j                  d|      st        d|d      t        |       }d| dd| dddg}|j	                  t        |d	|| j                  rt        | j                        nd	| j                  rt        | j                        nd	
             |j	                  ddg       dj                  |      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       }/home/jay/workspace/.worktrees/task-2057-dev2/scripts/.codegraph-venv/lib/python3.12/site-packages/cyclopts/completion/zsh.pygenerate_completion_scriptr(      s    ( BHH%8)D.ym;klmm-c2O I;

I;e
E 
LL%03uS^^,B696G6G% 1 12R	
 
LL	
 99Ud""    	argumentsr   
indent_strc                     t        d| d| d      }t        ||      }|j                  d      D cg c]  }|j                          c}S c c}w )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
        fir   )r   textwrap_indentsplitrstrip)r*   r+   r   templateindentedlines         r'    _generate_run_command_completionr3   S   s`    *  & # $ )" "HH x4H&.nnT&:;dDKKM;;;s   Apositional_argsr   help_formatc           	         g }| D cg c]*  }|j                         st        |j                        s)|, }}| D cg c]+  }|j                         rt        |j                        r*|- }}|D ]  }d|j                  xs dz   }t	        ||      }|j                  d      }|r9|D 	cg c]  }	t        t        |	             }
}	dj                  |
      }d| d}nt        t        |j                              }|rd| d	| d	| dnd| d	| d}|j                  |        |D ]  }t	        ||      }|j                  d      }|r9|D 	cg c]  }	t        t        |	             }
}	dj                  |
      }d| d}nt        t        |j                              }|r	d
| d	| dnd
| d}|j                  |        |S c c}w c c}w c c}	w c c}	w )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.
       r   Tforce ()':'*:)is_var_positionalr   hintindex_get_description_from_argumentget_choices_escape_completion_choicer	   r$   _map_completion_action_to_zshr   append)r4   r5   specsargvariadic_argsnon_variadic_argsposdescchoicescescaped_choiceschoices_stractionspecs                 r'   !_generate_nested_positional_specsrT      s   . E %4mSs7L7L7NRbcfckckRlSmMm&c.C.C.EN^_b_g_gNh 
 !  399>"-c;? ///-X_`ST89J19MN`O`((?3KQ'F23H3RSF-33%qaxq)1SE4&9JT#(  -c;?///-X_`ST89J19MN`O`((?3KQ'F23H3RSF)/TF!F81%s4&]T LQ n a as'   *G	G	GG(G*GGargumentc           	      F   g }t        | |      }| j                  d      }|r|D cg c]  }t        t        |             }}|j	                  | d       |j	                  | d       |D ]  }|j	                  | d| d| d        |j	                  | d       |j	                  | d	       |S t        | j                        }	|	t        j                  k(  r|j	                  | d
       |S |	t        j                  k(  r|j	                  | d       |S c c}w )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.
    Tr8   zlocal -a choicesz	choices=(z  'r>   r=   r<   z_describe 'argument' choices_files_directories)
rC   rD   rE   r	   rG   r   rA   r   FILESDIRECTORIES)
rU   r5   r+   r&   rM   rN   rO   rP   choicerR   s
             r'   _generate_describe_completionr\      s9   * E)(K@D """.GT[\q45Fq5IJ\\
|#345
|9-.% 	=FLLJ<s6(!D6;<	=
|1%&
|#?@A L 'x}}5%+++LLJ<v./
 L	 '333LLJ<|45 L! ]s   Dr%   .command_pathr   r   r   c           
      	   | |   }|j                   }|j                  }d|z  }	g }
|dk(  r#|dk(  r|
j                  t        ||	|             |
S g }g }|D cg c]  }|j                  |j
                  s|  }}|D cg c]"  }|j                         r|j
                  s!|$ }}|j                  d        |D ])  }t        ||j                        }|j                  |       + t               }|D ]v  }t        d |j                  D              s t        |j                  |j                  |j                        }|j                  |       |j                  |j                         x |D ]0  }|j!                  d      s||vsd	| d
}|j#                  |       2 |D ]0  }|j!                  d      s||vsd	| d}|j#                  |       2 t        d |D              }|rN|sL|rt%        ||j                        }n.|D ])  }t'        ||j                        }|j#                  |       + ||z   }|r"|j#                  d       |j#                  d       |rh|rdnd}|
j#                  |	 d| d       |dd D ]  }|
j#                  |	 d| d        |
j#                  |	 d|d           |
j#                  d       |r|
j#                  |	 d       |
j#                  |	 d       g }|D ]h  }|j                  D ]W  }|j!                  d      rt)        |j                  |j                        }t+        |      }|j#                  d	| d| d	       Y j |
j#                  |	 d       |
j#                  |	 d       |D ]  }|
j#                  |	 d|         |
j#                  |	 d       |
j#                  |	 d       |
j#                  |	 d       |
j#                  |	 d       |
j#                  |	 d        |D ]  }|j                  D ]y  }|j!                  d      r||fz   }|| v s t-        |      }|
j#                  |	 d| d!       t/        | ||d"z   |||      }|
j                  |       |
j#                  |	 d#       {  |
j#                  |	 d$       |
j#                  |	 d       |
j#                  |	 d%       |
S c c}w c c}w )&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.
    r:   )runcycloptsNc                 "    | j                   xs dS )Nr   )rB   )as    r'   <lambda>z/_generate_completion_for_path.<locals>.<lambda>5  s    qww|! r)   )keyc              3   >   K   | ]  }|j                  d         yw-N)
startswith).0names     r'   	<genexpr>z0_generate_completion_for_path.<locals>.<genexpr>?  s     Its#Is   rg   r=   z![Display this message and exit.]'z[Display application version.]'c              3   b   K   | ]'  }|j                   D ]  }|j                  d          ) ywrf   )namesrh   )ri   registered_commandcmd_names      r'   rk   z0_generate_completion_for_path.<locals>.<genexpr>Q  s<       );XjXpXp LTH$$ $ s   -/z'1: :->cmds'z'*::arg:->args'z-C r   z_arguments \z  z \zcase $state inz  cmds)r>   z    local -a commandsz    commands=(z      z    )z,    _describe -t commands 'command' commandsz    ;;z  args)z    case $words[1] inr<      z
        ;;z    esacesac)commandsr*   r!   r3   rB   showis_positional_onlysort_generate_keyword_specsr5   setanyrm   #_generate_keyword_specs_for_commandr   updaterh   rG   rT   _generate_positional_spec_safe_get_description_from_apprE   _escape_command_name_for_caser"   ) r%   r]   r   r   r   r   datart   r*   r+   r&   
args_specspositional_specsrI   r4   keyword_argsrU   rH   flag_command_namesrn   flagrS   has_non_flag_commandsc_flagcmd_listro   rM   escaped_cmd_namecmdsub_pathescaped_case_name	sub_liness                                    r'   r"   r"     sP   < <(D}}HIvJExI$;5iYWXJ '0Vs3993HSXXsVOV#,ZCC4J4J4LQTQYQYCZLZ 34 ! !'$2B2BC% !
 & @I0B0H0HII7"((*<*@*@$BRBRE e$%%&8&>&>?@  $??3D0B$Btf=>Dd#$
  $??3D0B$Btf;<Dd#$
    ?G   4@RVRbRbc , .04;K;KL ''-.
 &
2
.)+,/R
|;vhb9:sO 	5DLLJ<r$s34	5
|2jn%567R
|>23
|7+,"* 	D.44 D**3/9:L:P:PRVRbRbcD'@'J$OOa(8'94&$BC	D	D 	
|#89:
|>23 	5CLLJ<vcU34	5
|5)*
|#OPQ
|6*+
|7+,
|#89:"* 	<.44 <&&s+'8+5.(Eh(O%LLJ<v6G5H!JK ='6A:y*Vc!I LL+LLJ<z!:;<	< 	
|8,-
|6*+
|4()LS WZs$   S'S4S>SS!Sr[   c                    | j                  dd      } | j                  dd      } | j                  dd      } | j                  dd      } | j                  d	d
      } | j                  dd      } | j                  dd      } | j                  dd      } | j                  dd      } | j                  dd      } | j                  dd      } | j                  dd      } | j                  dd      } | j                  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.
    rp   \\r=   '\''`\`$\$"\"r:   z\ r;   z\(r<   z\)[\[]\];z\;|z\|&z\&r>   \:replace)r[   s    r'   rE   rE     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)   rj   c                     t        | d      S )a  Escape special characters in command name for zsh case patterns.

    In zsh case patterns, glob characters need to be escaped to match literally.
    Colons also need escaping because zsh's completion system may treat them
    specially when populating the $words array after _describe completion.

    Parameters
    ----------
    name : str
        Command name.

    Returns
    -------
    str
        Escaped command name safe for zsh case patterns.
    z*?[]()|:)chars)r
   )rj   s    r'   r   r     s    & $D
;;r)   textc                 &   | j                  dd      } | j                  dd      } | j                  dd      } | j                  dd      } | j                  d	d
      } | j                  dd      } | j                  dd      } | j                  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.
    rp   r   r   r   r   r   r   r   r=   r   r>   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        | |      }| j                         }d}| j                  d      }|r;|D cg c]  }t        t	        |             }}dj                  |      }	d|	 d}d}nt        t        | j                              }| j                  j                  D ]o  }
|
j                  d      s|r|s
d	|
 d
| d}n<|rd	|
 d
| d|
j                  d       d| d		}nd	|
 d
| d|
j                  d       d	}|j                  |       q | j                  D ].  }
|
j                  d      sd	|
 d
| d}|j                  |       0 |S c c}w )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   Tr8   r:   r;   r<   Frg   r=   r   ]'z]:r>   )rC   is_flagrD   rE   r	   r$   rF   r   rA   	parameterrj   rh   lstriprG   	negatives)rU   r5   rH   rM   r   rR   rN   rO   rP   rQ   rj   rS   s               r'   rx   rx     s    E)(K@DD F""".GT[\q45Fq5IJ\\hh/[M#./DX]]/ST ""'' 	s#tfAdV2&DtfAdV2dkk#&6%7qBDtfAdV2dkk#&6%7q9DT	 "" s#4&$r"T L7 ]s   Ec                    t        | |      }| j                  d      }|r9|D cg c]  }t        t        |             }}dj	                  |      }d| d}nt        t        | j                              }| j                         st        | j                        r|r	d| d| dS d| dS | j                  t        d	| j                   d
      | j                  dz   }|rd| d| d| dS d| d| dS c c}w )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.
    Tr8   r:   r;   r<   r?   r>   r=   zPositional-only argument z missing indexr7   )rC   rD   rE   r	   r$   rF   r   rA   r@   r   rB   r    rm   )	rU   r5   rM   rN   rO   rP   rQ   rR   rL   s	            r'   r}   r}   "  s    *(K@D """.GT[\q45Fq5IJ\\hh/[M#./DX]]/ST!!#'7'F)/TF!F81%Bs4&]B ~~4X^^4DNSTT
..1
C)/Qse1TF!F81%FqQtfA5FF ]s   C9rm   cmd_appzApp | CommandSpecc                     g }t        ||      }| D ].  }|j                  d      sd| d| d}|j                  |       0 |S )ae  Generate zsh _arguments specs for a command that looks like a flag.

    Parameters
    ----------
    names : tuple[str, ...]
        Registered names for the command.
    cmd_app : App | CommandSpec
        Command app or spec.
    help_format : str
        Help text format.

    Returns
    -------
    list[str]
        List of zsh argument specs.
    rg   r=   r   r   )r~   rh   rG   )rm   r   r5   rH   rM   rj   rS   s          r'   r{   r{   G  sW    & E)';?D ??3tfAdV2&DLL
 Lr)   rR   c                 T    | t         j                  k(  ry| t         j                  k(  ryy)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 "").
    rW   rX   r   )r   rY   rZ   )rR   s    r'   rF   rF   e  s+     !'''	#//	/r)   c                     t        | j                  j                  xs d|      }|s| j                  r| j                  d   nd}t	        |      S )a  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).
        Falls back to argument name if help text is empty, since zsh _arguments
        requires a non-empty description for positional specs to work correctly.
    r   formatr   rU   )r   r   helprm   r   )rU   r5   r   s      r'   rC   rC   y  sG    " **//52kJD %-NNx~~a 
"4((r)   c                     	 t        | j                  d      }|j                  xs d}t        ||      }t        |      S # t        $ r t	        | j                  xs d      }Y <w xY w)aE  Extract plain text description from App, escaping zsh special chars.

    Parameters
    ----------
    cmd_app : App | CommandSpec
        Command app or spec with help text.
    help_format : str
        Help text format.

    Returns
    -------
    str
        Escaped plain text description (truncated to 80 chars).
    	plaintextr   r   )r   r   short_description	Exceptionstrr   r   )r   r5   parsedr   s       r'   r~   r~     sd    ' {;''-2 [1D"4((	  '7<<%2&'s   &A   "A%$A%)   r`   r   r   ).__doc__r   textwrapr   r   r-   typingr   cyclopts.annotationsr   cyclopts.completion._baser   r   r	   r
   r   r   r   cyclopts.help.helpr   r`   r   cyclopts.argumentr   r   cyclopts.command_specr   r   r(   listr3   rT   r\   dictr#   intr"   rE   r   r   rx   r}   r{   rF   rC   r~   r   r)   r'   <module>r      s^   
  .   1   />12#E 2#c 2#c 2#j:<#:<:< :< 
#Y	:<zB*%BB 
#YBJ,,, , 
#Y	,d "$%'V%S/>9:VS/V V 	V
 c3hV c?V 
#YVrc c B< < <,# # 03j 3s 3tCy 3l"G
 "G "G "GJc?%8GJ	#Y<*: s ()Z )c )c )2),? )c )VY )r)   