Don’t pretty print newlines in objects and arrays if empty

This commit is contained in:
Tobie Morgan Hitchcock 2023-04-15 16:59:55 +01:00
parent 40e75f3f35
commit 7413e93a48
2 changed files with 21 additions and 17 deletions

View file

@ -153,9 +153,11 @@ impl Display for Array {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut f = Pretty::from(f); let mut f = Pretty::from(f);
f.write_char('[')?; f.write_char('[')?;
let indent = pretty_indent(); if !self.is_empty() {
write!(f, "{}", Fmt::pretty_comma_separated(self.as_slice()))?; let indent = pretty_indent();
drop(indent); write!(f, "{}", Fmt::pretty_comma_separated(self.as_slice()))?;
drop(indent);
}
f.write_char(']') f.write_char(']')
} }
} }

View file

@ -144,20 +144,22 @@ impl Display for Object {
} else { } else {
f.write_str("{ ")?; f.write_str("{ ")?;
} }
let indent = pretty_indent(); if !self.is_empty() {
write!( let indent = pretty_indent();
f, write!(
"{}", f,
Fmt::pretty_comma_separated( "{}",
self.0.iter().map(|args| Fmt::new(args, |(k, v), f| write!( Fmt::pretty_comma_separated(
f, self.0.iter().map(|args| Fmt::new(args, |(k, v), f| write!(
"{}: {}", f,
escape_key(k), "{}: {}",
v escape_key(k),
))), v
) ))),
)?; )
drop(indent); )?;
drop(indent);
}
if is_pretty() { if is_pretty() {
f.write_char('}') f.write_char('}')
} else { } else {