From e14d51600f6380e21e720ea5d56c455656ad4088 Mon Sep 17 00:00:00 2001 From: rvcas Date: Mon, 14 Aug 2023 22:01:20 -0400 Subject: [PATCH] feat(format): logical op chain --- crates/aiken-lang/src/format.rs | 45 +++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/crates/aiken-lang/src/format.rs b/crates/aiken-lang/src/format.rs index f69d5353..98ad1b6d 100644 --- a/crates/aiken-lang/src/format.rs +++ b/crates/aiken-lang/src/format.rs @@ -1,11 +1,11 @@ use crate::{ ast::{ Annotation, Arg, ArgName, AssignmentKind, BinOp, ByteArrayFormatPreference, CallArg, - ClauseGuard, Constant, DataType, Definition, Function, IfBranch, ModuleConstant, Pattern, - RecordConstructor, RecordConstructorArg, RecordUpdateSpread, Span, TraceKind, TypeAlias, - TypedArg, UnOp, UnqualifiedImport, UntypedArg, UntypedClause, UntypedClauseGuard, - UntypedDefinition, UntypedFunction, UntypedModule, UntypedPattern, UntypedRecordUpdateArg, - Use, Validator, CAPTURE_VARIABLE, + ClauseGuard, Constant, DataType, Definition, Function, IfBranch, LogicalOpChainKind, + ModuleConstant, Pattern, RecordConstructor, RecordConstructorArg, RecordUpdateSpread, Span, + TraceKind, TypeAlias, TypedArg, UnOp, UnqualifiedImport, UntypedArg, UntypedClause, + UntypedClauseGuard, UntypedDefinition, UntypedFunction, UntypedModule, UntypedPattern, + UntypedRecordUpdateArg, Use, Validator, CAPTURE_VARIABLE, }, docvec, expr::{FnStyle, UntypedExpr, DEFAULT_ERROR_STR, DEFAULT_TODO_STR}, @@ -773,6 +773,10 @@ impl<'comments> Formatter<'comments> { .. } => self.if_expr(branches, final_else), + UntypedExpr::LogicalOpChain { + kind, expressions, .. + } => self.logical_op_chain(kind, expressions), + UntypedExpr::PipeLine { expressions, one_liner, @@ -1110,6 +1114,27 @@ impl<'comments> Formatter<'comments> { } } + fn logical_op_chain<'a>( + &mut self, + kind: &'a LogicalOpChainKind, + expressions: &'a [UntypedExpr], + ) -> Document<'a> { + kind.to_doc() + .append(" {") + .append( + line() + .append(join( + expressions.iter().map(|expression| self.expr(expression)), + ",".to_doc().append(line()), + )) + .nest(INDENT) + .group(), + ) + .append(",") + .append(line()) + .append("}") + } + fn pipeline<'a>( &mut self, expressions: &'a Vec1, @@ -1746,6 +1771,16 @@ impl<'a> Documentable<'a> for &'a UnqualifiedImport { } } +impl<'a> Documentable<'a> for &'a LogicalOpChainKind { + fn to_doc(self) -> Document<'a> { + match self { + LogicalOpChainKind::And => "and", + LogicalOpChainKind::Or => "or", + } + .to_doc() + } +} + impl<'a> Documentable<'a> for &'a BinOp { fn to_doc(self) -> Document<'a> { match self {