Did you know ... | Search Documentation: |
Pack refactor -- prolog/ref_replace.pl |
This library provides the predicate replace/5, which is the basic entry point for all the refactoring scenarios.
Note for implementors/hackers:
TODO
:
document them.format("~a", [Atom])
does not behaves as write_term(Atom, Options)
, since a
space is not added to separate operators from the next term, for instance
after rewriting :- dynamic a/1, you would get :- dynamica/1.write('')
is used to reset the effect of the partial(true)
optionThe predicate is efficient enough to be used also as a walker to capture all matches of Term, by printing a message and failing. For example:
replace( sent, (:-use_module(X)), _, (refactor_message(information, format("~w", [X])), fail), [file(F)])
will display all the occurrences of use_module/1 declarations in the file F. This would be useful for some complex refactoring scenarios.
The levels of operations stablishes where to look for matching terms, and could take one of the following values:
If level is sent, some special cases of Term are used to control its behavior:
The term Into could contain certain hacks to control its behavior, as follows:
append(L1, L2, L)
, but preserving the formats of L1 and L2
Note that if you use append/3 directly, the format of L1 will be lost'$TEXT'(T,'$OUTPOS')
is equivalent to:
'$POS'(my_outpos, '$TEXT'(T, my_outpos))
Specific options for this predicate are:
fixpoint(+Value)
States that the replacement should be applied recursively, until no more
modifications are caused by the replacement.
Value=decreasing is the default, meaning that the recursion stops if the transformed term contains more terms that could potentially match. If the level is a non recursive one (see level_rec/2), such value is equivalent to none.
Value=file means that the recursion is performed over the hole file.
Value=term means that the recursion is performed over the transformed term.
Value=true means that the recursion is applied up to reach the fixpoint without decreasing control. If Level is a non recursive one, the recursion is performed over the hole file, otherwise the recursion is only applied over the transformed term.
Value=none don't apply the fixpoint algorithm.
predicate(+Term, +Pattern, -Size)
to define the metric used to perform the
decreasing control (by default pattern_size/3).line(-Line)
Unifies Line with the line number of the sentence being refactorized.clause(+Ref)
Apply the refactoring to the clause refered by Ref.max_tries(MaxTries)
Apply no more than MaxTries changesconj_width(+ConjWidth)
Print several conjunctions in the same line, provided that they don't
surpasses ConjWidth columns.
Default is 160term_width(+TermWidth)
Split long terms so that when printed, they don't surpasses TermWidth
columns.
Default is 160list_width(+ListWidth)
Split long lists so that when printed, they don't surpasses ListWidth
columns.
Default is 160linearize(+Linearize)
Linearize is a subset of [vars, atms], which will linearize the term to
avoid bounded variables or atoms. In some refactoring scenarios this is
important if we want to avoid ambiguities. For instance, supose that you
want to replayce f(A, B)
, by f(B, A)
, but if one of the matching terms is
f(X, X)
, the change will not be performed, even if the two arguments have
different layouts. To avoid this we should use the option
linearize([vars])
. Default is [].sentence(-SentPattern)
Unifies SentPattern with the sentence being processed. This is useful in
some refactoring scenarios.expand(Expand)
Apply the program transformation to let the goal_expansion hook in
ref_replace.pl be called. It only have sense if the expansion level is
goal, in such level the default value is yes, otherwise is no.expanded(Expanded)
Unifies Expanded with the current sentence after the expansion has been
applied (if applicable)cleanup_attributes(CleanupAttributes)
Remove attributes that could potentially be present in the sentence being
refactorized, in particular, if level is goal the term could contain the
attribute '$var_info'. Default value is yes.max_changes(Max)
Maximum number of changes performed by the refactoring.vars_prefix(Prefix)
Prefix added to new variables. Default 'V'file(AFile)
Unifies AFile with the file being reinstantiated. If AFile is instantiated
on call of the predicate, limits the refactoring to such file.loaded(loaded)
if Loaded is false (default), refactor non loaded files too.subterm_boundary(+Boundary)
Processed by fix_termpos/2 to stablish the boundaries of the subterms.
Options processed by read_term/2:
variable_names(-VNL)
Variable namescomments(-Comments)
Commentssyntax_errors(SE)
Default errorsubterm_positions(-SentPos)
Subterm positionsterm_position(-Pos)
Term position
Other options are processed by the predicate option_module_files/2 and allows to select the files or modules that are going to be modified.