]> Repositorios git - scryer-prolog.git/commitdiff
ENHANCED: more readable indentation of nested ( If -> Then ; Else ) constructs
authorMarkus Triska <[email protected]>
Wed, 3 Mar 2021 21:04:28 +0000 (22:04 +0100)
committerMarkus Triska <[email protected]>
Wed, 3 Mar 2021 21:34:09 +0000 (22:34 +0100)
Example:

    ?- portray_clause((h :- ( a -> b ; c -> d ; e, f))).
    h :-
       (  a ->
          b
       ;  c ->
          d
       ;  e,
          f
       ).
       true.

src/lib/format.pl

index fc4c49bb1d0ba235f985b29d40a7547ae4d9f2dc..fe9c7d27c4b5bb82c13c87b3ceb2565f6ea6325c 100644 (file)
@@ -494,8 +494,8 @@ body_(Var, C, I, VNs) --> { var(Var) }, !,
 body_((A,B), C, I, VNs) --> !,
         body_(A, C, I, VNs), ",\n",
         body_(B, 0, I, VNs).
-body_((A ; Else), C, I, VNs) --> % ( If -> Then ; Else )
-        { nonvar(A), A = (If -> Then) },
+body_(Body, C, I, VNs) -->
+        { body_if_then_else(Body, If, Then, Else) },
         !,
         indent_to(C, I),
         "(  ",
@@ -513,16 +513,28 @@ body_(Goal, C, I, VNs) -->
         indent_to(C, I), literal(Goal, VNs).
 
 
+% True iff Body has the shape ( If -> Then ; Else ).
+body_if_then_else(Body, If, Then, Else) :-
+        nonvar(Body),
+        Body = (A ; Else),
+        nonvar(A),
+        A = (If -> Then).
+
 else_branch(Else, C, I, VNs) -->
         indent_to(0, I),
         ";  ",
-        body_(Else, C, C, VNs), "\n",
-        indent_to(0, I),
-        ")".
+        (   { body_if_then_else(Else, If, Then, NextElse) } ->
+            { C1 is I + 3 },
+            body_(If, C1, C1, VNs), " ->\n",
+            body_(Then, 0, C1, VNs), "\n",
+            else_branch(NextElse, C1, I, VNs)
+        ;   body_(Else, C, C, VNs), "\n",
+            indent_to(0, I),
+            ")"
+        ).
 
 indent_to(CurrentColumn, Indent) -->
-        { Delta is Indent - CurrentColumn },
-        format_("~t~*|", [Delta]).
+        format_("~t~*|", [Indent-CurrentColumn]).
 
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ?- portray_clause(a).