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): 2013, 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_shell,
   36          [ rcommit/0,
   37            rdiff/0,
   38            rdiff/1,
   39            rdiff/2,
   40            rreset/0,
   41            rsave/1,
   42            rshow/0
   43          ]).   44
   45:- use_module(library(lists)).   46:- use_module(library(readutil)).   47:- use_module(library(trim_utils)).   48:- use_module(library(file_changes)).   49:- use_module(library(ref_changes)).   50:- use_module(library(ref_command)).

Pending changes management

This library provides tools to manage the stack of pending changes of the refactoring tool, as well as to apply those changes to the files. */

   58ref_commit :-
   59    once(pending_change(Index)),
   60    rdiff(save, 0, Index),
   61    reset_changes.
   62
   63rshow :-
   64    once(pending_change(Index)),
   65    rdiff(show, 0, Index).
   66
   67rsave(Diff) :-
   68    tell(Diff),
   69    rshow,
   70    told.
   71
   72rdiff :-
   73    once(rdiff(_)).
   74
   75rdiff(Index) :-
   76    pending_change(Index),
   77    succ(Index1, Index),
   78    rdiff(show, Index1, Index).
   79
   80rdiff(Index1, Index) :-
   81    rdiff(show, Index1, Index).
   82
   83rdiff(Action, Index1, Index) :-
   84    findall(File, (pending_change(IdxI, File, _), IdxI=<Index), FileU),
   85    sort(FileU, FileL),
   86    forall(member(File, FileL),
   87           apply_diff(Action, Index1, File)).
   88
   89trim_content(RawContent, Content) :-
   90    atomics_to_string(RawList, "\n", RawContent),
   91    maplist(string_right_trim, RawList, List),
   92    atomics_to_string(List, "\n", Content).
   93
   94apply_diff(Action, Index1, File) :-
   95    once(pending_change(_, File, RawContent)), % Take the last one
   96    trim_content(RawContent, Content), % Remove right spaces
   97    ( pending_change(Idx1, File, Content1),
   98      Idx1 =< Index1
   99    ->setup_call_cleanup(tmp_file_stream(text, File1, Stream),
  100                         format(Stream, '~s', [Content1]),
  101                         close(Stream)),
  102      TmpFile = true
  103    ; File1 = File,
  104      TmpFile = fail,
  105      ( access_file(File, read)
  106      ->read_file_to_string(File, Content1, [])
  107      ; Content1 = []
  108      )
  109    ),
  110    ( Content1 \= Content
  111    ->do_file_change(Action, File1, File, Content)
  112    ; true
  113    ),
  114    ( TmpFile = true
  115    ->delete_file(File1)
  116    ; true
  117    ).
  118
  119rcommit :-
  120    ref_commit,
  121    reset_commands.
  122
  123rreset :-
  124    reset_changes,
  125    reset_commands