Supports commenting validator inner functions.

This commit is contained in:
KtorZ 2023-03-30 13:37:09 +02:00
parent 814157dd7b
commit 5d4c95d538
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 64 additions and 30 deletions

View File

@ -508,9 +508,10 @@ impl<'comments> Formatter<'comments> {
other_fun: &'a Option<UntypedFunction>, other_fun: &'a Option<UntypedFunction>,
end_position: usize, end_position: usize,
) -> Document<'a> { ) -> Document<'a> {
// Stick it all together let fun_comments = self.pop_comments(fun.location.start);
let inner_fn = line() let fun_doc_comments = self.doc_comments(fun.location.start);
.append(self.definition_fn( let first_fn = self
.definition_fn(
&false, &false,
"fn", "fn",
&fun.name, &fun.name,
@ -518,16 +519,18 @@ impl<'comments> Formatter<'comments> {
&fun.return_annotation, &fun.return_annotation,
&fun.body, &fun.body,
fun.end_position, fun.end_position,
)) )
.nest(INDENT) .group();
.group() let first_fn = commented(fun_doc_comments.append(first_fn).group(), fun_comments);
.append(if other_fun.is_some() { line() } else { nil() })
.append( let other_fn = match other_fun {
other_fun None => nil(),
.as_ref() Some(other) => {
.map(|other| { let other_comments = self.pop_comments(other.location.start);
line() let other_doc_comments = self.doc_comments(other.location.start);
.append(self.definition_fn(
let other_fn = self
.definition_fn(
&false, &false,
"fn", "fn",
&other.name, &other.name,
@ -535,16 +538,21 @@ impl<'comments> Formatter<'comments> {
&other.return_annotation, &other.return_annotation,
&other.body, &other.body,
other.end_position, other.end_position,
)) )
.nest(INDENT) .group();
.group()
})
.unwrap_or_else(nil),
);
let inner_fn = match printed_comments(self.pop_comments(end_position), false) { commented(other_doc_comments.append(other_fn).group(), other_comments)
Some(comments) => inner_fn.append(line()).append(comments), }
None => inner_fn, };
let v_body = line()
.append(first_fn)
.append(if other_fun.is_some() { lines(2) } else { nil() })
.append(other_fn);
let v_body = match printed_comments(self.pop_comments(end_position), false) {
Some(comments) => v_body.append(lines(2)).append(comments).nest(INDENT),
None => v_body.nest(INDENT),
}; };
// validator(params) // validator(params)
@ -556,7 +564,7 @@ impl<'comments> Formatter<'comments> {
v_head v_head
.append(" {") .append(" {")
.append(inner_fn) .append(v_body)
.append(line()) .append(line())
.append("}") .append("}")
} }

View File

@ -59,18 +59,42 @@ fn test_format_if() {
fn test_format_validator() { fn test_format_validator() {
let src = indoc! {r#" let src = indoc! {r#"
validator ( ) { validator ( ) {
// What is the purpose of life
fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool { fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
True True
} }
} }
// What?
validator {
/// Some documentation for foo
fn foo() {
Void
}
// I am lost
}
"#}; "#};
let expected = indoc! {r#" let expected = indoc! {r#"
validator { validator {
// What is the purpose of life
fn foo(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool { fn foo(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
True True
} }
} }
// What?
validator {
/// Some documentation for foo
fn foo() {
Void
}
// I am lost
}
"#}; "#};
assert_fmt(src, expected) assert_fmt(src, expected)
@ -83,6 +107,7 @@ fn test_format_double_validator() {
fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool { fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
True True
} }
/// This is bar
fn bar(r: Redeemer, ctx : ScriptContext ) -> Bool { True } fn bar(r: Redeemer, ctx : ScriptContext ) -> Bool { True }
} }
"#}; "#};
@ -93,6 +118,7 @@ fn test_format_double_validator() {
True True
} }
/// This is bar
fn bar(r: Redeemer, ctx: ScriptContext) -> Bool { fn bar(r: Redeemer, ctx: ScriptContext) -> Bool {
True True
} }