diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2018-04-05 06:53:24 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2018-04-05 06:53:24 -0700 |
commit | e69a99cc4c183a98dd380fdaf47a5a1dcb5d68a0 (patch) | |
tree | 8dceea3e5455d6c8e711cbdfc78525bc87257fc6 /lib.c | |
parent | 34efdb8b2e45eb6543a8fbaadf8867ee58870677 (diff) | |
download | txr-e69a99cc4c183a98dd380fdaf47a5a1dcb5d68a0.tar.gz txr-e69a99cc4c183a98dd380fdaf47a5a1dcb5d68a0.tar.bz2 txr-e69a99cc4c183a98dd380fdaf47a5a1dcb5d68a0.zip |
printer: improve object formatting.
There is an issue with the printer in that it produces
output whereby objects continue on the same line after
a multi-line object, e.g:
(foo (foobly bar
xyzzy quux) (oops same
line))
rather than:
(foo (foobly bar
xyzzy quux)
(oops same line))
There is a simple fix for this: set a flag to force
a line break on the next width-check operation whenever
an object has been broken into multiple lines.
width-check can return a Boolean indication whether
it generated a line break, and so aggregate object
printing routines can tell whether their object
has been broken into lines, and set the flag.
* stream.h (struct strm_base): New member, force_break.
(force_break): Declared.
* stream.c (strm_base_init): Extent initializer to cover
force_break flag.
(put_string, put_char): Clear the force_break flag whenever
we hit column zero.
(width_check): If indent mode is on, and force_break is
true, generate a break. Clear force_break.
(force_break): New function.
(stream_init): Register force-break intrinsic.
* buf.c (buf_print): Set the force break flag if the buffer
was broken into multiple lines.
* hash.c (hash_print_op): Set the force break flag if the
hash was broken into multiple lines.
* lib.c (obj_print_impl): Same logic for lists.
* struct.c (struct_inst_print): Same logic for structs.
* tests/009/json.expected, tests/011/macros-2.expected,
tests/012/struct.tl, tests/017/glob-zarray.expected:
Update expected textual output to reflect new formatting.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 14 |
1 files changed, 12 insertions, 2 deletions
@@ -11109,6 +11109,7 @@ val obj_print_impl(val obj, val out, val pretty, struct strm_ctx *ctx) val iter; val closepar = chr(')'); val indent = zero; + int force_br = 0; if (sym == dwim_s && consp(cdr(obj))) { put_char(chr('['), out); @@ -11181,7 +11182,8 @@ finish: iter = nil; goto dot; } else if (consp(d)) { - width_check(out, chr(' ')); + if (width_check(out, chr(' '))) + force_br = 1; } else { dot: put_string(lit(" . "), out); @@ -11189,6 +11191,9 @@ dot: put_char(closepar, out); } } + + if (force_br) + force_break(out); } if (save_indent) @@ -11299,6 +11304,7 @@ dot: val save_mode = test_set_indent_mode(out, num_fast(indent_off), num_fast(indent_data)); val save_indent; + int force_br = 0; put_string(lit("#("), out); @@ -11308,11 +11314,15 @@ dot: val elem = obj->v.vec[i]; obj_print_impl(elem, out, pretty, ctx); if (i < length - 1) - width_check(out, chr(' ')); + if (width_check(out, chr(' '))) + force_br = 1; } put_char(chr(')'), out); + if (force_br) + force_break(out); + set_indent(out, save_indent); set_indent_mode(out, save_mode); } |