pretty: provide a `softbreak` parameter to help break long lines

If a leaf subexpression to be computed gets over the soft break,
we allow it to be broken down (or unflattened).
2 files changed, 27 insertions(+), 4 deletions(-)

M psyl/lisp.py
M test/test_expressions.py
M psyl/lisp.py +5 -4
@@ 89,7 89,7 @@ def serialize(tree):
     return f'({" ".join(expr)})'
 
 
-def pretty(tree, indent=4):
+def pretty(tree, indent=4, softbreak=0):
 
     def leafnode(tree):
         kw = False

          
@@ 137,9 137,10 @@ def pretty(tree, indent=4):
                         ' ' + serialize(node)
                     )
                 elif leafnode(node):
-                    expr.append(
-                        '\n' + leftpad + serialize(node)
-                    )
+                    chunk = '\n' + leftpad + serialize(node)
+                    if softbreak and len(chunk) > softbreak:
+                        chunk = '\n' + leftpad + _pretty(node, depth + 1)
+                    expr.append(chunk)
                 else:
                     expr.append(
                         '\n' + leftpad + _pretty(node, depth + 1)

          
M test/test_expressions.py +22 -0
@@ 132,3 132,25 @@ def test_pretty_format():
         (foo "world"))
     1000)
 """.strip()
+
+    # force breakup of very wide expression
+    expr = parse(
+        '(/'
+        '    (add'
+        '         (gigle "a-very-very-long-identifier" '
+        '                #:from (date "2020-1-1") '
+        '                #:to (date "2020-12-31"))'
+        '         (foo "world"))'
+        '    1000)'
+    )
+    out = pretty(expr, softbreak=80)
+    assert out == """
+(/
+    (add
+        (gigle
+            "a-very-very-long-identifier"
+            #:from (date "2020-1-1")
+            #:to (date "2020-12-31"))
+        (foo "world"))
+    1000)
+""".strip()