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::{ use crate::{
ast::{ ast::{
Annotation, Arg, ArgName, AssignmentKind, BinOp, ByteArrayFormatPreference, CallArg, Annotation, Arg, ArgName, AssignmentKind, BinOp, ByteArrayFormatPreference, CallArg,
ClauseGuard, Constant, DataType, Definition, Function, IfBranch, ModuleConstant, Pattern, ClauseGuard, Constant, DataType, Definition, Function, IfBranch, LogicalOpChainKind,
RecordConstructor, RecordConstructorArg, RecordUpdateSpread, Span, TraceKind, TypeAlias, ModuleConstant, Pattern, RecordConstructor, RecordConstructorArg, RecordUpdateSpread, Span,
TypedArg, UnOp, UnqualifiedImport, UntypedArg, UntypedClause, UntypedClauseGuard, TraceKind, TypeAlias, TypedArg, UnOp, UnqualifiedImport, UntypedArg, UntypedClause,
UntypedDefinition, UntypedFunction, UntypedModule, UntypedPattern, UntypedRecordUpdateArg, UntypedClauseGuard, UntypedDefinition, UntypedFunction, UntypedModule, UntypedPattern,
Use, Validator, CAPTURE_VARIABLE, UntypedRecordUpdateArg, Use, Validator, CAPTURE_VARIABLE,
}, },
docvec, docvec,
expr::{FnStyle, UntypedExpr, DEFAULT_ERROR_STR, DEFAULT_TODO_STR}, expr::{FnStyle, UntypedExpr, DEFAULT_ERROR_STR, DEFAULT_TODO_STR},
@ -773,6 +773,10 @@ impl<'comments> Formatter<'comments> {
.. ..
} => self.if_expr(branches, final_else), } => self.if_expr(branches, final_else),
UntypedExpr::LogicalOpChain {
kind, expressions, ..
} => self.logical_op_chain(kind, expressions),
UntypedExpr::PipeLine { UntypedExpr::PipeLine {
expressions, expressions,
one_liner, 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>( fn pipeline<'a>(
&mut self, &mut self,
expressions: &'a Vec1<UntypedExpr>, 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 { impl<'a> Documentable<'a> for &'a BinOp {
fn to_doc(self) -> Document<'a> { fn to_doc(self) -> Document<'a> {
match self { match self {