From 5e7ba6de246fc4c359a38885a07ae23abdcee2c6 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 30 Jul 2024 20:40:22 -0500 Subject: [PATCH] cl-ast: Improve formatting of blocks and groups --- compiler/cl-ast/src/ast_impl.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/compiler/cl-ast/src/ast_impl.rs b/compiler/cl-ast/src/ast_impl.rs index 1ba6e56..8158519 100644 --- a/compiler/cl-ast/src/ast_impl.rs +++ b/compiler/cl-ast/src/ast_impl.rs @@ -628,7 +628,12 @@ mod display { impl Display for Block { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - separate(&self.stmts, "\n")(f.delimit(BRACES)) + let Self { stmts } = self; + + match stmts.as_slice() { + [] => "{}".fmt(f), + stmts => separate(stmts, "\n")(f.delimit(BRACES)), + } } } @@ -640,7 +645,13 @@ mod display { impl Display for Tuple { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - separate(&self.exprs, ", ")(f.delimit(INLINE_PARENS)) + let Self { exprs } = self; + + match exprs.as_slice() { + [] => write!(f, "()"), + [expr] => write!(f, "({expr},)"), + exprs => separate(exprs, ", ")(f.delimit(INLINE_PARENS)), + } } }