mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
Improve pretty formatting for arrays
This commit is contained in:
parent
f2c40a4bd5
commit
0850f41617
1 changed files with 71 additions and 30 deletions
|
@ -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));
|
||||||
)
|
|
||||||
|
let formatted_values = if is_array {
|
||||||
|
format_array(values, config, visited, depth)?
|
||||||
} else {
|
} else {
|
||||||
format!(
|
format_table(values, config, visited, depth)?
|
||||||
"{}{}{}{} {} {}{}",
|
|
||||||
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(","),
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
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()
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue