1/*  Part of Refactoring Tools for SWI-Prolog
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/refactor
    6    Copyright (C): 2017, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(ref_replay,
   36          [ apply_command_q/1,
   37            rdelete/1,
   38            rlist/0,
   39            rlist/1,
   40            rnostats/0,
   41            rrewind/0,
   42            rrewind/1,
   43            rstats/0,
   44            rundo/0
   45          ]).   46
   47:- use_module(library(lists)).   48:- use_module(library(listing)).   49:- use_module(library(ref_changes),
   50              [ pending_change/1,
   51                undo_changes/1]).   52:- use_module(library(ref_msgtype)).   53:- use_module(library(ref_command)).

Individual pending changes management

This library provides tools to manage individual elements of the stack of pending changes of the refactoring tool. The pending changes are indexed by a sequential number, so that you can use such index in such predicates.

*/

   63:- meta_predicate apply_command_q(0).
 apply_command_q(:Call)
   67%   Execute Call and  associated such call with an index  for further management
   68%   of such Call.
   69
   70apply_command_q(Call) :-
   71    apply_command(Call),
   72    once(pending_change(Index)),
   73    message_type(Type),
   74    print_message(Type, format('Saved changes in index ~w', [Index])).
 rstats is det
Stablish that it must be printed an information message at at the end of a command.
   81rstats :-
   82    retractall(rstats_db),
   83    assertz(rstats_db).
 rnostats is det
Stablish that it must be printed an informational message at at the end of a command, this means that only when the verbosity is the highest you will see that message.
   91rnostats :-
   92    retractall(rstats_db).
 rlist is det
Display all commands that are waiting to be applied.
   98rlist :-
   99    \+ ( rlist(_),
  100         fail
  101       ).
 rlist(Index) is det
Display on backtracking the commands that are waiting to be applied, and unifies Index with such command's index.
  108rlist(Index) :-
  109    pending_command(Index, Command),
  110    with_output_to(string(SCommand), portray_clause(Command)),
  111    print_message(information, format('Index ~w, Command: ~s', [Index, SCommand])).
 rrewind is det
Reaply all the pending commands. This is useful if you change a file before to commit the changes, making the refactoring outdated with respect to the current files.
  119rrewind :-
  120    rrewind(0).
 rrewind(Index) is det
Reaply all the commands whose index is greater than Index.
  126rrewind(Index) :-
  127    findall(Command, ( pending_command(Index1, Command),
  128                       Index1 > Index,
  129                       rdrop(Index1, _)
  130                     ),
  131            CommandR),
  132    reverse(CommandR, CommandL),
  133    maplist(apply_command, CommandL).
 rundo is multi
Undo refactoring commands on backtracking.
  139rundo :-
  140    rundo(_).
 rdelete(Index)
Undo and rewind refactoring commands on backtracking.
  146rdelete(Index) :-
  147    rundo(Index),
  148    rrewind(Index).
 rundo(-Index) is multi
Undo refactoring commands on backtracking and unifies Index with the index of the removed command.
  155rundo(Index) :-
  156    rdrop(Index, Command),
  157    print_message(information, format('Undone ~w ---> ~w', [Index, Command])).
 rdrop(-Index) is multi
Undo refactoring commands on backtracking, unifies Index and Command with the index and the removed command respectively.
  164rdrop(Index, Command) :-
  165    undo_changes(Index),
  166    undo_command(Index, Command)