Improve pretty formatting for arrays

This commit is contained in:
Filip Tibell 2024-06-01 21:01:51 +02:00
parent f2c40a4bd5
commit 0850f41617
No known key found for this signature in database

View file

@ -52,33 +52,23 @@ pub(crate) fn format_value_recursive(
} else { } else {
write!(buffer, "{}", STYLE_DIM.apply_to("{"))?; write!(buffer, "{}", STYLE_DIM.apply_to("{"))?;
let mut is_empty = true; let values = t
let mut table_lines = Vec::new(); .clone()
for res in t.clone().pairs::<LuaValue, LuaValue>() { .pairs::<LuaValue, LuaValue>()
let (key, value) = res.expect("conversion to LuaValue should never fail"); .map(|res| res.expect("conversion to LuaValue should never fail"))
let formatted = if let Some(plain_key) = lua_value_as_plain_string_key(&key) { .collect::<Vec<_>>();
format!(
"{}{plain_key} {} {}{}", let is_empty = values.is_empty();
INDENT.repeat(1 + depth), let is_array = values
STYLE_DIM.apply_to("="), .iter()
format_value_recursive(&value, config, visited, depth + 1)?, .enumerate()
STYLE_DIM.apply_to(","), .all(|(i, (key, _))| key.as_integer().is_some_and(|x| x == (i as i32) + 1));
)
} else { let formatted_values = if is_array {
format!( format_array(values, config, visited, depth)?
"{}{}{}{} {} {}{}", } else {
INDENT.repeat(1 + depth), format_table(values, config, visited, depth)?
STYLE_DIM.apply_to("["), };
format_value_recursive(&key, config, visited, depth + 1)?,
STYLE_DIM.apply_to("]"),
STYLE_DIM.apply_to("="),
format_value_recursive(&value, config, visited, depth + 1)?,
STYLE_DIM.apply_to(","),
)
};
table_lines.push(formatted);
is_empty = false;
}
visited.remove(&LuaValueId::from(t)); visited.remove(&LuaValueId::from(t));
@ -87,10 +77,9 @@ pub(crate) fn format_value_recursive(
} else { } else {
write!( write!(
buffer, buffer,
"\n{}\n{}{}{}", "\n{}\n{}{}",
table_lines.join("\n"), formatted_values.join("\n"),
INDENT.repeat(depth), INDENT.repeat(depth),
if is_empty { " " } else { "" },
STYLE_DIM.apply_to("}") STYLE_DIM.apply_to("}")
)?; )?;
} }
@ -102,3 +91,55 @@ pub(crate) fn format_value_recursive(
Ok(buffer) Ok(buffer)
} }
fn format_array(
values: Vec<(LuaValue, LuaValue)>,
config: &ValueFormatConfig,
visited: &mut HashSet<LuaValueId>,
depth: usize,
) -> Result<Vec<String>, fmt::Error> {
values
.into_iter()
.map(|(_, value)| {
Ok(format!(
"{}{}{}",
INDENT.repeat(1 + depth),
format_value_recursive(&value, config, visited, depth + 1)?,
STYLE_DIM.apply_to(","),
))
})
.collect()
}
fn format_table(
values: Vec<(LuaValue, LuaValue)>,
config: &ValueFormatConfig,
visited: &mut HashSet<LuaValueId>,
depth: usize,
) -> Result<Vec<String>, fmt::Error> {
values
.into_iter()
.map(|(key, value)| {
if let Some(plain_key) = lua_value_as_plain_string_key(&key) {
Ok(format!(
"{}{plain_key} {} {}{}",
INDENT.repeat(1 + depth),
STYLE_DIM.apply_to("="),
format_value_recursive(&value, config, visited, depth + 1)?,
STYLE_DIM.apply_to(","),
))
} else {
Ok(format!(
"{}{}{}{} {} {}{}",
INDENT.repeat(1 + depth),
STYLE_DIM.apply_to("["),
format_value_recursive(&key, config, visited, depth + 1)?,
STYLE_DIM.apply_to("]"),
STYLE_DIM.apply_to("="),
format_value_recursive(&value, config, visited, depth + 1)?,
STYLE_DIM.apply_to(","),
))
}
})
.collect()
}