1
1
Fork 0
mirror of https://github.com/lune-org/lune.git synced 2025-04-13 06:50:54 +01:00

fix: handle formatting for cyclic tables

This commit is contained in:
Erica Marigold 2024-02-24 22:39:49 +05:30
parent 347da823b7
commit 9a3421dcf9
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1

View file

@ -103,9 +103,9 @@ pub fn style_from_style_str<S: AsRef<str>>(s: S) -> LuaResult<Option<&'static St
pub fn pretty_format_value( pub fn pretty_format_value(
buffer: &mut String, buffer: &mut String,
value: &LuaValue, value: &LuaValue,
parent_table_addr: Option<String>,
depth: usize, depth: usize,
) -> std::fmt::Result { ) -> std::fmt::Result {
// TODO: Handle tables with cyclic references
match &value { match &value {
LuaValue::Nil => write!(buffer, "nil")?, LuaValue::Nil => write!(buffer, "nil")?,
LuaValue::Boolean(true) => write!(buffer, "{}", COLOR_YELLOW.apply_to("true"))?, LuaValue::Boolean(true) => write!(buffer, "{}", COLOR_YELLOW.apply_to("true"))?,
@ -127,6 +127,8 @@ pub fn pretty_format_value(
write!(buffer, "{}", STYLE_DIM.apply_to("{ ... }"))?; write!(buffer, "{}", STYLE_DIM.apply_to("{ ... }"))?;
} else if let Some(s) = call_table_tostring_metamethod(tab) { } else if let Some(s) = call_table_tostring_metamethod(tab) {
write!(buffer, "{s}")?; write!(buffer, "{s}")?;
} else if depth >= 1 && parent_table_addr.eq(&Some(format!("{:p}", tab))) {
write!(buffer, "{}", STYLE_DIM.apply_to("<self>"))?
} else { } else {
let mut is_empty = false; let mut is_empty = false;
let depth_indent = INDENT.repeat(depth); let depth_indent = INDENT.repeat(depth);
@ -144,11 +146,11 @@ pub fn pretty_format_value(
)?, )?,
_ => { _ => {
write!(buffer, "\n{depth_indent}{INDENT}[")?; write!(buffer, "\n{depth_indent}{INDENT}[")?;
pretty_format_value(buffer, &key, depth)?; pretty_format_value(buffer, &key, parent_table_addr.clone(), depth)?;
write!(buffer, "] {} ", STYLE_DIM.apply_to("="))?; write!(buffer, "] {} ", STYLE_DIM.apply_to("="))?;
} }
} }
pretty_format_value(buffer, &value, depth + 1)?; pretty_format_value(buffer, &value, parent_table_addr.clone(), depth + 1)?;
write!(buffer, "{}", STYLE_DIM.apply_to(","))?; write!(buffer, "{}", STYLE_DIM.apply_to(","))?;
is_empty = false; is_empty = false;
} }
@ -192,7 +194,7 @@ pub fn pretty_format_multi_value(multi: &LuaMultiValue) -> LuaResult<String> {
if let LuaValue::String(s) = value { if let LuaValue::String(s) = value {
write!(buffer, "{}", s.to_string_lossy()).into_lua_err()?; write!(buffer, "{}", s.to_string_lossy()).into_lua_err()?;
} else { } else {
pretty_format_value(&mut buffer, value, 0).into_lua_err()?; pretty_format_value(&mut buffer, value, Some(format!("{:p}", value)), 0).into_lua_err()?;
} }
if counter < multi.len() { if counter < multi.len() {
write!(&mut buffer, " ").into_lua_err()?; write!(&mut buffer, " ").into_lua_err()?;