
    	D,j)                         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 d dlmZ d d	lmZmZ d
ee         dee	         fdZ G d d          Zd Zedk    r e             dS dS )    NPath)Optional)TreeContext)PathSpec)GitWildMatchPattern)watch)dump)watch_ask_promptwatch_code_promptgitignore_pathsreturnc                    | sdS g d}| D ]d}|                                 rNt          |          5 }|                    |                                           ddd           n# 1 swxY w Y   e|rt	          j        t          |          ndS )z?Load and parse multiple .gitignore files into a single PathSpecN)z.aider*z.gitz*~z*.bakz*.swpz*.swoz\#*\#z.#*z*.tmpz*.tempz*.origz*.pycz__pycache__/z	.DS_Storez	Thumbs.dbz*.svgz*.pdfz.idea/z.vscode/z*.sublime-*z.projectz
.settings/z*.code-workspacez.envz.venv/znode_modules/zvendor/z*.logz.cache/z.pytest_cache/z	coverage/)existsopenextend	readlinesr   
from_linesr   )r   patternspathfs       Z/home/rurouni/.local/share/uv/tools/aider-chat/lib/python3.11/site-packages/aider/watch.pyload_gitignoresr      s     t$ $ $HJ   / /;;== 	/d /q.../ / / / / / / / / / / / / / / BJS82H===tSs   (A$$A(	+A(	c                   v    e Zd ZdZ ej        dej                  ZddZd Z	d Z
d Zd	 Zd
 Zd Zd Zd ZdS )FileWatcherz0Watches source files for changes and AI commentsz.(?:#|//|--|;+) *(ai\b.*|ai\b.*|.*\bai[?!]?) *$NFc                 \   || _         |j        | _        |rt          |          nt          |j                  | _        || _        || _        d | _        d | _        t                      | _	        || _
        t          | j
        rd | j
        D             ng           | _        | |j        _        d S )Nc                 ,    g | ]}t          |          S  r   ).0gs     r   
<listcomp>z(FileWatcher.__init__.<locals>.<listcomp>U   s    ...T!WW...    )coderior   rootverbose	analytics
stop_eventwatcher_threadsetchanged_files
gitignoresr   gitignore_specfile_watcher)selfr#   r,   r&   r'   r%   s         r   __init__zFileWatcher.__init__I   s    
("&<DJJJD,<,<	"" UU$-26/I..do....r
 
 !%r"   c                    t          |          }|                                }|                    | j                                                  sdS |                    | j                  }| j        rt          d|           | j        rG| j                            |	                                |
                                rdndz             rdS |                                r|                                j        dk    rdS | j        rt          d|           	 |                     t          |                    \  }}}t!          |          S # t"          $ r Y dS w xY w)z$Filter function for the file watcherFChanged/ i   CheckingN)r   absoluteis_relative_tor%   relative_tor&   printr-   
match_fileas_posixis_diris_filestatst_sizeget_ai_commentsstrbool	Exception)r/   change_typer   path_objpath_absrel_pathcomments_s           r   filter_funczFileWatcher.filter_funcZ   se   ::$$&&&&ty'9'9';';<< 	5''	22< 	')X&&& 	4#6#A#A(//*;*;#C33D$
 $
 	 5  	(--//"9O"K"K5< 	(*h'''	!11#h--@@NHa>>! 	 	 	FF	s   4E 
EEc                       j         r> fd j                                        D             }|r|nt           j                  gS t           j                  gS )z<Determine which root paths to watch based on gitignore rulesc                     g | ]n}j                             |                    j                                                  |                                rd ndz             _t          |          oS )r3   r4   )r-   r:   r8   r%   r;   r<   rA   )r   r   r/   s     r   r!   z2FileWatcher.get_roots_to_watch.<locals>.<listcomp>|   s~       *55$$TY//88::T[[]]>ZccXZ[ D		  r"   )r-   r%   iterdirrA   )r/   rootss   ` r   get_roots_to_watchzFileWatcher.get_roots_to_watchy   sq     		8    I--//  E "755DI'77DIr"   c                     |sdS d |D             }| j                             |           | j                                         dS )z-Process the detected changes and update stateFc                 R    h | ]$}t          t          |d                              %S )   )rA   r   )r   changes     r   	<setcomp>z-FileWatcher.handle_changes.<locals>.<setcomp>   s*    DDD&T&)__--DDDr"   T)r+   updater$   interrupt_input)r/   changesr+   s      r   handle_changeszFileWatcher.handle_changes   sR     	5DDGDDD!!-000!!!tr"   c                     	 |                                  }t          || j        | j        ddD ]}|                     |          r dS dS # t
          $ r }| j        rt          d|            |d}~ww xY w)z'Watch for file changes and process themT)watch_filterr(   ignore_permission_deniedNzFile watcher error: )rO   r	   rJ   r(   rX   rC   r&   r
   )r/   roots_to_watchrW   es       r   watch_fileszFileWatcher.watch_files   s    	!4466N !-?)-	     &&w// FF   	 	 	| 1/A//000G	s   AA A 
A5A00A5c                     t          j                    | _        t                      | _        t          j        | j        d          | _        | j                                         dS )zStart watching for file changesT)targetdaemonN)		threadingEventr(   r*   r+   Threadr^   r)   startr/   s    r   re   zFileWatcher.start   sS    #/++ UU'.d6FtTTT!!#####r"   c                     | j         r| j                                          | j        r)| j                                         d| _        d| _         dS dS )zStop watching for file changesN)r(   r*   r)   joinrf   s    r   stopzFileWatcher.stop   s]    ? 	"O!!! 	#$$&&&"&D"DOOO	# 	#r"   c                    d}d}| j         D ]}|                     |          \  }}}|dv r|}|| j        j        v r0| j        r| j                            d           | j        j                            |           | j                            |          }|s| j        	                                 d}| j        	                    d| d           |s|r| j        	                    d           d	S | j        r| j                            d
           | j        	                    d           |dk    rt          }n|dk    rt          }| j        j        D ]}|                     |          \  }}	}
|s| j                            |          }|s<| j                            |          }|d| dz  }d t          ||	          D             }	 t          ||dddddddd
  
        }t                      |_        |                    |           |                                 ||                                z  }# t(          $ r& t          ||	          D ]\  }}|d| d| dz  }Y w xY w|S )zGet any detected file changesNF)!?zai-comments file-addTzAdded z to the chatzDEnd your comment with AI! to request changes or AI? to ask questionsr4   zai-comments executezProcessing your request...rk   rl   
z:
c                 *    g | ]\  }}|d k    |dz
  S )r   rR   r   )r   lnrI   s      r   r!   z/FileWatcher.process_changes.<locals>.<listcomp>   s%    KKKur1BFFBFFFFr"   r      )colorline_numberchild_context	last_linemargin	mark_loisloi_padshow_top_of_file_parent_scopez  Line z: )r+   r@   r#   
abs_fnamesr'   eventaddget_rel_fnamer$   tool_outputr   r   	read_textzipr   r*   lines_of_interestadd_lines_of_interestadd_contextformat
ValueError)r/   
has_actionaddedfnamerI   action	rel_fnameres	line_numsrH   _actioncodeloiscontextro   comments                   r   process_changeszFileWatcher.process_changes   s    
' 	B 	BE//66LAq&###

---~ =$$%;<<<J!%%e,,,
0077I ##%%%G @ @ @ @AAAA 	 ##Z   2> 	8N  !67778999#CC3"C Z* "	7 "	7E+/+?+?+F+F(Ix 7$$U++D 
0077I&	&&&&C LKIx(@(@KKKD7% %"'#"27   -0EE)--d333##%%%w~~''' 7 7 7#&y(#;#; 7 7KB6R6676666CC7 77 
s   A,H;;,I+*I+c                    g }g }d}| j                             |d          }|sdS t          |                                d          D ]\  }}| j                            |          x}r|                    d                                          }	|	r|                    |           |                    |	           |		                                }	|	
                    d          }	|	                                }	|	                    d          s|	                    d          rd	}|	                    d
          s|	                    d
          rd}|sdS |||fS )zGExtract AI comment line numbers, comments and action status from a fileNT)silent)NNNrR   r   z/#-;zai!rk   zai?rl   )r$   r~   	enumerate
splitlinesai_comment_patternsearchgroupstripappendlowerlstrip
startswithendswith)
r/   filepathr   rH   r   contentilinematchr   s
             r   r@   zFileWatcher.get_ai_comments  sr   	
'##HT#:: 	$## !3!3!5!5q99 	) 	)GAt/66t<<<u )++a....00 	)$$Q'''OOG,,,%mmooG%nnV44G%mmooG))%00 )G4D4DU4K4K )%(

 ++E22 )g6F6Fu6M6M )%(
 	$##(J..r"   )NFNN)__name__
__module____qualname____doc__recompile
IGNORECASEr   r0   rJ   rO   rX   r^   re   ri   r   r@   r   r"   r   r   r   A   s        :: $92= % % % %"  >         &$ $ $# # #J J JX/ / / / /r"   r   c                  X   ddl } |                     d          }|                    dd           |                    dd	d
           |                                }|j        }t          d| d           d }t          ||j                  }	 |                                 	 |	                                x}r:t          |                                          D ]}t          |           d|_        Q# t          $ r' t          d           |                                 Y dS w xY w)z!Example usage of the file watcherr   NzWatch source files for changes)description	directoryzDirectory to watch)helpz--gitignorer   z9Path to .gitignore file (can be specified multiple times))r   r   zWatching source files in z...c                 8    d| j                                         v S )Ntest)namer   )r   s    r   ignore_test_fileszmain.<locals>.ignore_test_files-  s    ****r"   )r,   Tz
Stopped watching files)argparseArgumentParseradd_argument
parse_argsr   r9   r   	gitignorere   get_changessortedkeysr+   KeyboardInterruptri   )r   parserargsr   r   watcherrW   files           r   mainr     sc   OOO$$1Q$RRF
*>???
H    
 DI	
4i
4
4
4555+ + + )???G		-!--///w -"7<<>>22    D$KKKK(,%		-
    ()))s   A&C8 8-D)(D)__main__)r   rb   pathlibr   typingr   grep_astr   pathspecr   pathspec.patternsr   
watchfilesr	   
aider.dumpr
   aider.watch_promptsr   r   listr   r   r   r   r   r"   r   <module>r      sG   				                                   1 1 1 1 1 1             C C C C C C C C/TT$Z /THX4F /T /T /T /TdX/ X/ X/ X/ X/ X/ X/ X/v  B zDFFFFF r"   