
    k7i=                     ~   S r SSKrSSKJr  SSKJrJrJrJrJ	r	J
r
  \(       a  SSKJr  SSS\S	\4S
 jrS\S	\4S jrS\S	\4S jrS\S\\\S4   \4   S	\\   4S jrS\S	\4S jrS\\\S4   \4   S\S\\S4   S\\S4   S	\\   4
S jrS\\\S4   \4   S\\S4   S\S\\S4   S\\S4   S	\\   4S jrS\S\\S4   S\S\S	\\   4
S jrS\\S4   S	\\   4S jrS\S\S\\S4   S\\S4   S	\\   4
S jrS\S\S\S\S	\\   4
S jrS \S\S\S\S	\\   4
S! jrS\\S4   S\S	\4S" jrS#SS\S	\4S$ jrg)%zFish completion script generator.

Generates static fish completion scripts using `complete -c COMMAND` statements.
Completions auto-load from ~/.config/fish/completions/PROGNAME.fish.
    N)TYPE_CHECKING)CompletionActionCompletionDataclean_choice_textextract_completion_dataget_completion_actionstrip_markup)Appappr
   	prog_namereturnc                 0   U(       a  [         R                  " SU5      (       d  [        SU< S35      e[        U 5      nSU 3SS/n[	        S UR                  5        5       5      nU(       a+  UR                  [        X5      5        UR                  S5        U R                  (       a  [        U R                  5      OSnU R                  (       a  [        U R                  5      OSnUR                  [        X!XV5      5        S	R                  U5      S	-   $ )
aV  Generate fish completion script.

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

Returns
-------
str
    Complete fish 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# Fish completion for z# Generated by Cyclopts c              3   >   #    U  H  n[        U5      S :  v   M     g7f)r   N)len).0paths     R/home/james-whalen/.local/lib/python3.13/site-packages/cyclopts/completion/fish.py	<genexpr>-generate_completion_script.<locals>.<genexpr>6   s     O8Nc$i!m8Ns    
)rematch
ValueErrorr   anykeysextend_generate_helper_functionsappend
help_flagstupleversion_flags_generate_completionsjoin)r   r   completion_datalineshas_nested_commandsr!   r#   s          r   generate_completion_scriptr)      s    ( BHH%8)DD.ym;klmm-c2O !,!
E O8L8L8NOO/	KLR*-..s~~&bJ030A0AE#++,rM	LL&:]^99Ud""    textc                 &    U R                  SS5      $ )z&Escape single quotes for fish strings.'z'\'')replacer+   s    r   _escape_fish_stringr0   C   s    <<W%%r*   c                 `    U R                  SS5      n U R                  SS5      n [        U 5      $ )z!Escape description text for fish.r    )r.   r0   r/   s    r   _escape_fish_descriptionr4   H   s-    <<c"D<<c"Dt$$r*   r&   .c                 D   [        5       nUR                  5        H  nUR                   H}  nUR                  5       (       a  M  UR                  R
                  (       d  M7  UR                  R
                   H,  nUR                  S5      (       d  M  UR                  U5        M.     M     M     SU  S3nSSU 3SS/nU(       a7  SR                  S	 [        U5       5       5      nUR                  S
U S35        OUR                  S5        UR                  / SQ5        U$ )a  Generate helper function for command path detection.

Parameters
----------
prog_name : str
    Program name.
completion_data : dict
    Completion data used to identify options that take values.

Returns
-------
list[str]
    Lines defining the helper function.
-__fish__using_commandz6# Helper function to check exact command path sequencez	function z!    set -l cmd (commandline -opc)z    set -l subcommandsr2   c              3   8   #    U  H  n[        U5      v   M     g 7fNr0   )r   opts     r   r   -_generate_helper_functions.<locals>.<genexpr>r   s     `D_S 3C 8 8D_   z     set -l options_with_values 'r-   z!    set -l options_with_values '')z    set -l skip_next 0z;    # Extract non-option words (commands) from command linez!    for i in (seq 2 (count $cmd))z        set -l word $cmd[$i]z         if test $skip_next -eq 1z            set skip_next 0z            continue        endz)        if string match -qr -- '^-' $wordz>            # Check if this option takes a value (exact match)zF            if string match -q -- "* $word *" " $options_with_values "z                set skip_next 1z            endz        elsez*            # Non-option word is a commandz$            set -a subcommands $wordr?       endz8    # Check if subcommand sequence matches expected pathz2    if test (count $subcommands) -ne (count $argv)z        return 1r@   z"    for i in (seq 1 (count $argv))z-        if test $subcommands[$i] != $argv[$i]z            return 1r?   r@   z    return 0end)setvalues	argumentsis_flag	parametername
startswithaddr%   sortedr    r   )	r   r&   options_with_valuesdataargumentrG   	func_namer'   escaped_optss	            r   r   r   O   s   $ %&&(H##%%(*<*<*A*A*A$..33Ds+++//5 4 ' ) )N3I@
I;+ 	E xx`FK^D_``7~QGH89	LL	
 B Lr*   actionc                 X    U [         R                  :X  a  gU [         R                  :X  a  gg)zMap completion action to fish flags.

Parameters
----------
action : CompletionAction
    Completion action type.

Returns
-------
str
    Fish completion flags ("-r -F" for files, "-r -a '(...)'" for directories, "" otherwise).
z-r -Fz%-r -a '(__fish_complete_directories)'r   )r   FILESDIRECTORIES)rP   s    r   _map_completion_action_to_fishrT      s+     !'''!---6r*   r!   r#   c           
          / n[        U R                  5       5       HV  u  pVUR                  [        U UUUU5      5        U[	        U R                  5       [        S9:w  d  ME  UR                  S5        MX     U$ )a/  Generate all fish completion commands.

Parameters
----------
completion_data : dict
    Extracted completion data.
prog_name : str
    Program name.
help_flags : tuple[str, ...]
    Help flags.
version_flags : tuple[str, ...]
    Version flags.

Returns
-------
list[str]
    Completion command lines.
)keyr   )rJ   itemsr   _generate_completions_for_pathmaxr   r   r    )r&   r   r!   r#   r'   command_path_datas          r   r$   r$      st    0 E%o&;&;&=>*	
 33353??LL  ? Lr*   rZ   c                 >   X;  a  / $ X   n/ n[        X5      nUR                  [        XQX'5      5        UR                   Vs/ s H.  oR	                  5       (       a  M  UR
                  (       d  M,  UPM0     n	nU	(       d  U(       d  U(       a  UR                  [        U5      5        UR                  [        X'X45      5        UR                  [        XXuR                  5      5        UR                  [        UR                  X'UR                  5      5        U$ s  snf )ak  Generate completions for a specific command path.

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

Returns
-------
list[str]
    Completion command lines.
)_get_condition_for_pathr    _generate_subcommand_completionsrD   is_positional_onlyshow_generate_option_section_header"_generate_help_version_completions!_generate_keyword_arg_completionshelp_format$_generate_command_option_completionscommands)
r&   rZ   r   r!   r#   rL   r'   	conditionargkeyword_argss
             r   rX   rX      s    6 *	(DE'@I	LL1$i[\#'>>_>C9O9O9QCVYV^V^C>L_z]4\BC7	jhi6|PY[k[klm9$--_c_o_opqL `s   DD1DrL   rg   c                 T   U R                    VVs/ s H/  oDR                    H  oUR                  S5      (       a  M  UPM     M1     nnnU(       d  / $ / nU(       a$  UR                  SSR	                  U5       35        OUR                  S5        U R                    H  nUR                   Ho  nUR                  S5      (       a  M  [        UR                  U R                  5      n	[        U	5      n
[        U5      nUR                  SU SU SU SU
 S3	5        Mq     M     U$ s  snnf )	a  Generate completions for subcommands.

Parameters
----------
data : CompletionData
    Completion data.
command_path : tuple[str, ...]
    Command path.
prog_name : str
    Program name.
condition : str
    Fish condition.

Returns
-------
list[str]
    Completion command lines.
r6   z# Subcommands for: r2   z# Root-level commandscomplete -c z -a '' -d 'r-   )
rf   namesrH   r    r%   _get_description_from_appr   rd   r4   r0   )rL   rZ   r   rg   registered_commandrG   rf   r'   cmd_namedescescaped_descescaped_cmds               r   r^   r^     s   2 (,}}'4#AYAYapapqtauAY}   	E*388L+A*BCD,-"mm*00H""3'',-?-C-CTEUEUVD3D9L-h7KLL<	{!I;eK=PVWcVddefg 1 , L/s
   (D$
D$c                 @    U (       a  SSR                  U 5       3/$ S/$ )zGenerate section header comment for options.

Parameters
----------
command_path : tuple[str, ...]
    Command path.

Returns
-------
list[str]
    Comment line.
z# Options for: r2   z# Root-level optionsr%   )rZ   s    r   ra   ra   9  s*     !#((<"8!9:;;"##r*   c           
      2   / nU H  nUR                  S5      (       a"  USS nUR                  SU  SU SU S35        M;  UR                  S5      (       d  MS  [        U5      S:X  d  Md  US	   nUR                  SU  SU S
U S35        M     U H  nUR                  S5      (       a"  USS nUR                  SU  SU SU S35        M;  UR                  S5      (       d  MS  [        U5      S:X  d  Md  US	   nUR                  SU  SU S
U S35        M     U$ )a'  Generate completions for help and version flags.

Parameters
----------
prog_name : str
    Program name.
condition : str
    Fish condition.
help_flags : tuple[str, ...]
    Help flags.
version_flags : tuple[str, ...]
    Version flags.

Returns
-------
list[str]
    Completion command lines.
--   Nrk   r2    -l z$ -d 'Display this message and exit.'r6       -s z" -d 'Display application version.')rH   r    r   )r   rg   r!   r#   r'   flag	long_name
short_names           r   rb   rb   K  s&   0 E??4  QRILL<	{!I;d9+Mqrs__S!!c$i1naJLL<	{!I;d:,Nrst  ??4  QRILL<	{!I;d9+Mopq__S!!c$i1naJLL<	{!I;d:,Npqr  Lr*   ri   rd   c                    / nU  GH`  n[        UR                  R                  =(       d    SUS9n[        U5      nUR	                  5       nUR                  SS9n	[        UR                  5      n
UR                  R                  =(       d    /  GH8  nUR                  S5      (       d  M  UR                  S5      (       a  USS nS	U S
U SU 3/nO#[        U5      S:X  a  US   nS	U S
U SU 3/nOMg  U(       a  UR                  SU S35        OU	(       aN  U	 Vs/ s H  n[        [        U5      5      PM     nnS
R                  U5      nUR                  SU SU S35        O?[        U
5      nU(       a  UR                  U SU S35        OUR                  SU S35        UR                  S
R                  U5      5        GM;     UR                    H  nUR                  S5      (       d  M  UR                  S5      (       a%  USS nUR                  S	U S
U SU SU S3	5        MV  [        U5      S:X  d  Mg  US   nUR                  S	U S
U SU SU S3	5        M     GMc     U$ s  snf )a  Generate completions for keyword arguments.

Parameters
----------
keyword_args : list
    Keyword arguments.
prog_name : str
    Program name.
condition : str
    Fish condition.
help_format : str
    Help text format.

Returns
-------
list[str]
    Completion command lines.
r   formatT)forcer6   rw   rx   Nrk   r2   ry   rz   r{   z-d 'r-   z-x -a 'rl    -d 'z-r -d ')r	   rF   helpr4   rE   get_choicesr   hintrG   rH   r   r    r0   r   r%   rT   	negatives)ri   r   rg   rd   r'   rM   rq   rr   rE   choicesrP   rG   r}   
line_partsr~   cescaped_choiceschoices_straction_flagss                      r   rc   rc   x  sv   0 E H..339r+N/5""$&&T&2&x}}5&&++1r1D??3''t$$ H	 ,YKq4	{ST
Ta!!W
 ,YKq4
|TU
!!Da"89V]"^V]QR#67H7K#LV]"^!hh7!!GK=|nA"NO=fE%%eL>&KL%%~Q&?@LL*-.5 28 &&D??3''t$$ H	|I;a	{$ykQVWcVddefgTa!!W
|I;a	{$zlRWXdWeefgh 'I !^ L/ #_s   I,rf   c                    / nU  H  nUR                    H  nUR                  S5      (       d  M  [        UR                  U5      n[	        U5      nUR                  S5      (       a%  USS n	UR                  SU SU SU	 SU S	3	5        Mw  [        U5      S:X  d  M  US
   n
UR                  SU SU SU
 SU S	3	5        M     M     U$ )a.  Generate completions for commands that look like options.

Parameters
----------
commands : list
    List of RegisteredCommand tuples.
prog_name : str
    Program name.
condition : str
    Fish condition.
help_format : str
    Help text format.

Returns
-------
list[str]
    Completion command lines.
r6   rw   rx   Nrk   r2   ry   r   r-   rz   r{   )rm   rH   rn   r   r4   r    r   )rf   r   rg   rd   r'   ro   rp   rq   rr   r}   r~   s              r   re   re     s    0 E&*00H&&s++,-?-C-C[QD3D9L""4(($QRL	|I;a	{$ykQVWcVddefgX!#%a[
|I;a	{$zlRWXdWeefgh 1 ' Lr*   c                 `    U (       d  gSU S3nSR                  S U  5       5      nSU SU S3$ )zGenerate fish condition string for a command path.

Parameters
----------
command_path : tuple[str, ...]
    Command path (empty for root).
prog_name : str
    Program name.

Returns
-------
str
    Fish condition flag.
z-n __fish_use_subcommandr7   r8   r2   c              3   8   #    U  H  n[        U5      v   M     g 7fr:   r;   )r   cmds     r   r   *_get_condition_for_path.<locals>.<genexpr>  s     QLS 3C 8 8Lr>   z-n 'r-   ru   )rZ   r   rN   escaped_commandss       r   r]   r]     sD     ))N3IxxQLQQ)A./q11r*   cmd_appc                     SSK Jn  U R                  (       d  g U" U R                  S5      nUR                  =(       d    Sn[        XAS9$ ! [         a    [        U R                  5      n N*f = f)zExtract description from App.

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

Returns
-------
str
    Description text.
r   )docstring_parser   	plaintextr   )cyclopts.help.helpr   r   short_description	Exceptionstrr	   )r   rd   r   parsedr+   s        r   rn   rn     sb     3<<! {;''-2 11  !7<< !s   (A A-,A-)__doc__r   typingr   cyclopts.completion._baser   r   r   r   r   r	   cycloptsr
   r   r)   r0   r4   dictr"   listr   rT   r$   rX   r^   ra   rb   rc   re   r]   rn   r   r*   r   <module>r      s   
    )#E )#c )#c )#X&c &c &
%3 %3 %II%S/>9:I 
#YIX+;  ('%S/>9:'' c3h' c?	'
 
#Y'T+%S/>9:+S/+ + c3h	+
 c?+ 
#Y+\/
/S// / 	/
 
#Y/d$%S/ $d3i $$*** c3h* c?	*
 
#Y*ZIII I 	I
 
#YIX))) ) 	)
 
#Y)X2%S/ 2c 2c 2.2u 23 23 2r*   