B
    ¤dÝSÎ!  ã               @   s8   d dl mZmZmZ G dd„ deƒZG dd„ deƒZdS )é    )Ú
exceptionsÚchangeÚ
taskhandlec               @   s  e Zd ZdZd3dd„Zdd„ Ze ¡ fdd„Zd	d
„ Z	dd„ Z
dde ¡ fdd„Zde ¡ f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ed$d%„ ƒZed&d%„ ƒZed'd(„ ƒZed)d*„ ƒZed+d,„ ƒZed-d.„ ƒZed/d0„ ƒZd1d2„ ZdS )4ÚHistoryz"A class that holds project historyNc             C   s:   || _ g | _g | _|| _|  ¡  | j j | j¡ d | _d S )N)	ÚprojectÚ
_undo_listÚ
_redo_listÚ	_maxundosÚ_load_historyÚ
data_filesZadd_write_hookÚwriteÚcurrent_change)Úselfr   Zmaxundos© r   ú0lib/python3.7/site-packages/rope/base/history.pyÚ__init__   s    zHistory.__init__c             C   sx   | j rt| jjjd| jdd}|d k	rtt | j¡}x |d D ]}| j ||ƒ¡ q:W x |d D ]}| j	 ||ƒ¡ q\W d S )NÚhistoryT)ÚcompressÚimport_r   é   )
Úsaver   r   Z	read_datar   r   ZDataToChanger   Úappendr   )r   ÚresultZ	to_changeÚdatar   r   r   r
      s    zHistory._load_historyc             C   sT   z|| _ | t ||¡¡ W dd| _ X |  |¡rD| j |¡ |  ¡  | jdd…= dS )zµPerform the change and add it to the `self.undo_list`

        Note that uninteresting changes (changes to ignored files)
        will not be appended to `self.undo_list`.

        N)	r   Údor   Úcreate_job_setÚ_is_change_interestingÚ	undo_listr   Ú_remove_extra_itemsÚ	redo_list)r   ÚchangesÚtask_handler   r   r   r      s    
z
History.doc             C   s,   t | jƒ| jkr(| jdt | jƒ| j …= d S )Nr   )Úlenr   Ú	max_undos)r   r   r   r   r   ,   s    zHistory._remove_extra_itemsc             C   s&   x |  ¡ D ]}| j |¡s
dS q
W dS )NTF)Úget_changed_resourcesr   Z
is_ignored)r   r    Úresourcer   r   r   r   0   s    zHistory._is_change_interestingFc             C   s|   | j st d¡‚|dkr"| jd }|  | j|¡}|  | j|¡ |  t|ƒ|¡ | jt|ƒ d… }|rx| jt|ƒ d…= |S )a°  Redo done changes from the history

        When `change` is `None`, the last done change will be undone.
        If change is not `None` it should be an item from
        `self.undo_list`; this change and all changes that depend on
        it will be undone.  In both cases the list of undone changes
        will be returned.

        If `drop` is `True`, the undone change will not be appended to
        the redo list.

        zUndo list is emptyNéÿÿÿÿ)	r   r   ÚHistoryErrorr   Ú_find_dependenciesÚ_move_frontÚ_perform_undosr"   r   )r   r   Zdropr!   Údependenciesr   r   r   r   Úundo6   s    

zHistory.undoc             C   sb   | j st d¡‚|dkr"| j d }|  | j |¡}|  | j |¡ |  t|ƒ|¡ | jt|ƒ d… S )aV  Redo undone changes from the history

        When `change` is `None`, the last undone change will be
        redone.  If change is not `None` it should be an item from
        `self.redo_list`; this change and all changes that depend on
        it will be redone.  In both cases the list of redone changes
        will be returned.

        zRedo list is emptyNr&   )r   r   r'   r(   r)   Ú_perform_redosr"   r   )r   r   r!   r+   r   r   r   ÚredoP   s    


zHistory.redoc             C   s&   x |D ]}|  |¡ | |¡ qW d S )N)Úremover   )r   Úchange_listr    r   r   r   r   r)   c   s    

zHistory._move_frontc             C   s   |  |¡}t||d … ƒƒ S )N)ÚindexÚ_FindChangeDependencies)r   r0   r   r1   r   r   r   r(   h   s    
zHistory._find_dependenciesc          	   C   s\   xVt |ƒD ]J}| jd | _zt || j¡}| j |¡ W d d | _X | j | j ¡ ¡ q
W d S )Nr&   )	Úranger   r   r   r   r,   r   r   Úpop)r   Úcountr!   ÚiÚjob_setr   r   r   r*   l   s    zHistory._perform_undosc          	   C   s\   xVt |ƒD ]J}| jd | _zt || j¡}| j |¡ W d d | _X | j | j ¡ ¡ q
W d S )Nr&   )	r3   r   r   r   r   r   r   r   r4   )r   r5   r!   r6   r7   r   r   r   r-   w   s    zHistory._perform_redosc             C   sJ   | j d krd S |  | j g|¡}|d k	r*|S | ¡ rB| ¡ sB| ¡ S d S d S )N)r   Ú_search_for_change_contentsÚexistsÚ	is_folderÚread)r   Úfiler   r   r   r   Úcontents_before_current_change‚   s    
z&History.contents_before_current_changec             C   sX   xRt |ƒD ]F}t|tjƒr4|  |j|¡}|d k	r4|S t|tjƒr
|j|kr
|jS q
W d S )N)	ÚreversedÚ
isinstancer   Z	ChangeSetr8   r    ZChangeContentsr%   Zold_contents)r   r0   r<   Úchange_r   r   r   r   r8      s    
z#History._search_for_change_contentsc                sh   | j rdg }t ¡ ‰ |  ¡  | ‡ fdd„| jD ƒ¡ | ‡ fdd„| jD ƒ¡ | jjj	d|| j
d d S )Nc                s   g | ]}ˆ |ƒ‘qS r   r   )Ú.0r@   )Úto_datar   r   ú
<listcomp>   s    z!History.write.<locals>.<listcomp>c                s   g | ]}ˆ |ƒ‘qS r   r   )rA   r@   )rB   r   r   rC   ž   s    r   )r   )r   r   ZChangeToDatar   r   r   r   r   r   Z
write_datar   )r   r   r   )rB   r   r   ˜   s    zHistory.writec             C   s.   g }x$| j D ]}|| ¡ kr| |¡ qW |S )N)r   r$   r   )r   r%   r   r   r   r   r   Úget_file_undo_list¢   s
    zHistory.get_file_undo_listc             C   s   dt | jƒt | jƒ  S )Nz"History holds %s changes in memory)r"   r   r   )r   r   r   r   Ú__str__©   s    zHistory.__str__c             C   s   | j S )N)r   )r   r   r   r   Ú<lambda>­   s    zHistory.<lambda>c             C   s   | j S )N)r   )r   r   r   r   rF   ®   s    c             C   s   | j r| j d S dS )z3The last done change if available, `None` otherwiser&   N)r   )r   r   r   r   Útobe_undone°   s    zHistory.tobe_undonec             C   s   | j r| j d S dS )z5The last undone change if available, `None` otherwiser&   N)r   )r   r   r   r   Útobe_redone¶   s    zHistory.tobe_redonec             C   s$   | j d kr| jj dd¡S | j S d S )NZmax_history_itemséd   )r	   r   ÚprefsÚget)r   r   r   r   r#   ¼   s    
zHistory.max_undosc             C   s   | j j dd¡S )NZsave_historyF)r   rJ   rK   )r   r   r   r   r   Ã   s    zHistory.savec             C   s   | j j dd¡S )NZcompress_historyF)r   rJ   rK   )r   r   r   r   r   Ç   s    zHistory.compressc             C   s   | j dd…= | jdd…= dS )z$Forget all undo and redo informationN)r   r   )r   r   r   r   ÚclearË   s    zHistory.clear)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   r   ZNullTaskHandler   r   r   r,   r.   r)   r(   r*   r-   r=   r8   r   rD   rE   Úpropertyr   r   rG   rH   r#   r   r   rL   r   r   r   r   r      s4   
	
r   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )r2   c             C   s$   |d | _ || _t| j  ¡ ƒ| _d S )Nr   )r   r0   Úsetr$   Úchanged_resources)r   r0   r   r   r   r   Ó   s    
z _FindChangeDependencies.__init__c             C   sJ   | j g}x<| jdd … D ]*}|  ||¡r| |¡ | j | ¡ ¡ qW |S )Nr   )r   r0   Ú_depends_onr   rS   Úupdater$   )r   r   r   r   r   r   Ú__call__Ø   s    
z _FindChangeDependencies.__call__c             C   sj   xd|  ¡ D ]X}|d krq
|| jkr&dS x:| jD ]0}| ¡ rH| |¡rHdS | ¡ r.| |¡r.dS q.W q
W dS )NTF)r$   rS   r:   Úcontains)r   r    r   r%   Zchangedr   r   r   rT   à   s    
z#_FindChangeDependencies._depends_onN)rM   rN   rO   r   rV   rT   r   r   r   r   r2   Ñ   s   r2   N)Z	rope.baser   r   r   Úobjectr   r2   r   r   r   r   Ú<module>   s    N