From e69a99cc4c183a98dd380fdaf47a5a1dcb5d68a0 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 5 Apr 2018 06:53:24 -0700 Subject: 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. --- struct.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'struct.c') diff --git a/struct.c b/struct.c index 5f614af2..943cc393 100644 --- a/struct.c +++ b/struct.c @@ -1410,6 +1410,7 @@ static void struct_inst_print(val obj, val out, val pretty, val save_mode = test_set_indent_mode(out, num_fast(indent_off), num_fast(indent_data)); val save_indent, iter, once; + int force_br = 0; int compat = opt_compat && opt_compat <= 154; if (!compat || pretty) { @@ -1433,7 +1434,8 @@ static void struct_inst_print(val obj, val out, val pretty, put_char(chr(' '), out); once = nil; } else { - width_check(out, chr(' ')); + if (width_check(out, chr(' '))) + force_br = 1; } obj_print_impl(sym, out, pretty, ctx); put_char(chr(' '), out); @@ -1441,6 +1443,8 @@ static void struct_inst_print(val obj, val out, val pretty, } } put_char(chr(')'), out); + if (force_br) + force_break(out); set_indent_mode(out, save_mode); set_indent(out, save_indent); } -- cgit v1.2.3