feat(format): logical op chain

This commit is contained in:
rvcas 2023-08-14 22:01:20 -04:00 committed by Lucas
parent 2c2f3c90fb
commit e14d51600f
1 changed files with 40 additions and 5 deletions

View File

@ -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<UntypedExpr>,
@ -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 {