Improve formatting for empty tables

This commit is contained in:
Filip Tibell 2024-06-01 20:24:48 +02:00
parent bfb89dec01
commit f2c40a4bd5
No known key found for this signature in database

View file

@ -50,9 +50,10 @@ pub(crate) fn format_value_recursive(
} else if !visited.insert(LuaValueId::from(t)) { } else if !visited.insert(LuaValueId::from(t)) {
write!(buffer, "{}", STYLE_DIM.apply_to("{ recursive }"))?; write!(buffer, "{}", STYLE_DIM.apply_to("{ recursive }"))?;
} else { } else {
writeln!(buffer, "{}", STYLE_DIM.apply_to("{"))?; write!(buffer, "{}", STYLE_DIM.apply_to("{"))?;
let mut is_empty = true; let mut is_empty = true;
let mut table_lines = Vec::new();
for res in t.clone().pairs::<LuaValue, LuaValue>() { for res in t.clone().pairs::<LuaValue, LuaValue>() {
let (key, value) = res.expect("conversion to LuaValue should never fail"); 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) { let formatted = if let Some(plain_key) = lua_value_as_plain_string_key(&key) {
@ -75,20 +76,25 @@ pub(crate) fn format_value_recursive(
STYLE_DIM.apply_to(","), STYLE_DIM.apply_to(","),
) )
}; };
buffer.push_str(&formatted); table_lines.push(formatted);
buffer.push('\n');
is_empty = false; is_empty = false;
} }
visited.remove(&LuaValueId::from(t)); visited.remove(&LuaValueId::from(t));
if is_empty {
write!(buffer, " {}", STYLE_DIM.apply_to("}"))?;
} else {
write!( write!(
buffer, buffer,
"{}{}{}", "\n{}\n{}{}{}",
table_lines.join("\n"),
INDENT.repeat(depth), INDENT.repeat(depth),
if is_empty { " " } else { "" }, if is_empty { " " } else { "" },
STYLE_DIM.apply_to("}") STYLE_DIM.apply_to("}")
)?; )?;
} }
}
} else { } else {
let prefer_plain = depth == 0; let prefer_plain = depth == 0;
write!(buffer, "{}", format_value_styled(value, prefer_plain))?; write!(buffer, "{}", format_value_styled(value, prefer_plain))?;