From f2c40a4bd5b85f19528e7e28e26f1726cd29f35d Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 1 Jun 2024 20:24:48 +0200 Subject: [PATCH] Improve formatting for empty tables --- crates/lune-utils/src/fmt/value/recursive.rs | 26 ++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/crates/lune-utils/src/fmt/value/recursive.rs b/crates/lune-utils/src/fmt/value/recursive.rs index 7f0e9c2..1014996 100644 --- a/crates/lune-utils/src/fmt/value/recursive.rs +++ b/crates/lune-utils/src/fmt/value/recursive.rs @@ -50,9 +50,10 @@ pub(crate) fn format_value_recursive( } else if !visited.insert(LuaValueId::from(t)) { write!(buffer, "{}", STYLE_DIM.apply_to("{ recursive }"))?; } else { - writeln!(buffer, "{}", STYLE_DIM.apply_to("{"))?; + write!(buffer, "{}", STYLE_DIM.apply_to("{"))?; let mut is_empty = true; + let mut table_lines = Vec::new(); for res in t.clone().pairs::() { let (key, value) = res.expect("conversion to LuaValue should never fail"); let formatted = if let Some(plain_key) = lua_value_as_plain_string_key(&key) { @@ -75,19 +76,24 @@ pub(crate) fn format_value_recursive( STYLE_DIM.apply_to(","), ) }; - buffer.push_str(&formatted); - buffer.push('\n'); + table_lines.push(formatted); is_empty = false; } visited.remove(&LuaValueId::from(t)); - write!( - buffer, - "{}{}{}", - INDENT.repeat(depth), - if is_empty { " " } else { "" }, - STYLE_DIM.apply_to("}") - )?; + + if is_empty { + write!(buffer, " {}", STYLE_DIM.apply_to("}"))?; + } else { + write!( + buffer, + "\n{}\n{}{}{}", + table_lines.join("\n"), + INDENT.repeat(depth), + if is_empty { " " } else { "" }, + STYLE_DIM.apply_to("}") + )?; + } } } else { let prefer_plain = depth == 0;