feat: new formatting for validators v3

This commit is contained in:
rvcas 2024-07-30 18:01:54 -04:00 committed by KtorZ
parent 9e866a5ec1
commit 4287fa3f4a
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
3 changed files with 53 additions and 42 deletions

View File

@ -239,8 +239,9 @@ impl<'comments> Formatter<'comments> {
handlers, handlers,
fallback, fallback,
params, params,
name,
.. ..
}) => self.definition_validator(params, handlers, fallback, *end_position), }) => self.definition_validator(name, params, handlers, fallback, *end_position),
Definition::Test(Function { Definition::Test(Function {
name, name,
@ -581,57 +582,67 @@ impl<'comments> Formatter<'comments> {
fn definition_validator<'a>( fn definition_validator<'a>(
&mut self, &mut self,
name: &'a str,
params: &'a [UntypedArg], params: &'a [UntypedArg],
handlers: &'a [UntypedFunction], handlers: &'a [UntypedFunction],
fallback: &'a UntypedFunction, fallback: &'a UntypedFunction,
end_position: usize, end_position: usize,
) -> Document<'a> { ) -> Document<'a> {
// validator(params) // validator name(params)
let v_head = "validator".to_doc().append(if !params.is_empty() { let v_head = "validator"
.to_doc()
.append(" ")
.append(name)
.append(if !params.is_empty() {
wrap_args(params.iter().map(|e| (self.fn_arg(e), false))) wrap_args(params.iter().map(|e| (self.fn_arg(e), false)))
} else { } else {
nil() nil()
}); });
let fun_comments = self.pop_comments(fun.location.start); let mut handler_docs = vec![];
let fun_doc_comments = self.doc_comments(fun.location.start);
for handler in handlers.iter() {
let fun_comments = self.pop_comments(handler.location.start);
let fun_doc_comments = self.doc_comments(handler.location.start);
let first_fn = self let first_fn = self
.definition_fn( .definition_fn(
&fun.public, &handler.public,
&fun.name, &handler.name,
&fun.arguments, &handler.arguments,
&fun.return_annotation, &handler.return_annotation,
&fun.body, &handler.body,
fun.end_position, handler.end_position,
) )
.group(); .group();
let first_fn = commented(fun_doc_comments.append(first_fn).group(), fun_comments); let first_fn = commented(fun_doc_comments.append(first_fn).group(), fun_comments);
let other_fn = match other_fun { handler_docs.push(first_fn);
None => nil(), }
Some(other) => {
let other_comments = self.pop_comments(other.location.start);
let other_doc_comments = self.doc_comments(other.location.start);
let other_fn = self let fallback_comments = self.pop_comments(fallback.location.start);
let fallback_doc_comments = self.doc_comments(fallback.location.start);
let fallback_fn = self
.definition_fn( .definition_fn(
&other.public, &fallback.public,
&other.name, &fallback.name,
&other.arguments, &fallback.arguments,
&other.return_annotation, &fallback.return_annotation,
&other.body, &fallback.body,
other.end_position, fallback.end_position,
) )
.group(); .group();
commented(other_doc_comments.append(other_fn).group(), other_comments) let fallback_fn = commented(
} fallback_doc_comments.append(fallback_fn).group(),
}; fallback_comments,
);
let v_body = line() handler_docs.push(fallback_fn);
.append(first_fn)
.append(if other_fun.is_some() { lines(2) } else { nil() }) let v_body = line().append(join(handler_docs, lines(2)));
.append(other_fn);
let v_body = match printed_comments(self.pop_comments(end_position), false) { let v_body = match printed_comments(self.pop_comments(end_position), false) {
Some(comments) => v_body.append(lines(2)).append(comments).nest(INDENT), Some(comments) => v_body.append(lines(2)).append(comments).nest(INDENT),

View File

@ -1100,7 +1100,7 @@ impl ExtraData for Error {
| Error::LastExpressionIsAssignment { .. } | Error::LastExpressionIsAssignment { .. }
| Error::LogicalOpChainMissingExpr { .. } | Error::LogicalOpChainMissingExpr { .. }
| Error::MissingVarInAlternativePattern { .. } | Error::MissingVarInAlternativePattern { .. }
| Error::MultiValidatorEqualArgs { .. } | Error::NonLocalClauseGuardVariable { .. }
| Error::NotIndexable { .. } | Error::NotIndexable { .. }
| Error::NotExhaustivePatternMatch { .. } | Error::NotExhaustivePatternMatch { .. }
| Error::NotFn { .. } | Error::NotFn { .. }

View File

@ -9,7 +9,7 @@ use crate::{
ast::{ ast::{
Annotation, ArgName, ArgVia, DataType, Definition, Function, ModuleConstant, ModuleKind, Annotation, ArgName, ArgVia, DataType, Definition, Function, ModuleConstant, ModuleKind,
RecordConstructor, RecordConstructorArg, Tracing, TypeAlias, TypedArg, TypedDefinition, RecordConstructor, RecordConstructorArg, Tracing, TypeAlias, TypedArg, TypedDefinition,
TypedFunction, TypedModule, UntypedArg, UntypedDefinition, UntypedModule, Use, Validator, TypedModule, UntypedArg, UntypedDefinition, UntypedModule, Use, Validator,
}, },
builtins::{self, fuzzer, generic_var}, builtins::{self, fuzzer, generic_var},
tipo::{expr::infer_function, Span, Type, TypeVar}, tipo::{expr::infer_function, Span, Type, TypeVar},