Skip to content

Commit

Permalink
[move-format] Added support for function definitions (#16699)
Browse files Browse the repository at this point in the history
## Description 

This PR adds support for formatting functions definitions, including
native ones, but excluding macros (to make these PRs more incremental -
macros are a bit more subtle).

It also fixes an omission from the support for formatting structs
(phantom type parameters).

## Test Plan 

A new test has been added.
  • Loading branch information
awelc authored Mar 22, 2024
1 parent c26ab56 commit 561c142
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ export function print(path: AstPath, options: ParserOptions, print: printFn) {
path.call(print, 'namedChildren', 2),
';',
]
case 'function_definition':
let is_entry = false;
for (let i = 0; i < node.childCount; i++) {
if (node.child(i).type === 'entry') {
is_entry = true;
}
}
// first named child may be a visibility modifier
return [
node.namedChild(0).type === 'visibility_modifier' ? [ path.call(print, 'namedChildren', 0), ' '] : '',
is_entry ? 'entry ' : '',
'fun ',
node.namedChild(0).type !== 'visibility_modifier' ? path.call(print, 'namedChildren', 0) : '',
path.call(print, 'namedChildren', 1),
path.call(print, 'namedChildren', 2),
path.call(print, 'namedChildren', 3),
path.call(print, 'namedChildren', 4),
path.call(print, 'namedChildren', 5),
];
case 'native_function_definition':
// first named child may be a visibility modifier
return [
node.namedChild(0).type === 'visibility_modifier' ? [ path.call(print, 'namedChildren', 0), ' '] : '',
'native ',
'fun ',
node.namedChild(0).type !== 'visibility_modifier' ? path.call(print, 'namedChildren', 0) : '',
path.call(print, 'namedChildren', 1),
path.call(print, 'namedChildren', 2),
path.call(print, 'namedChildren', 3),
path.call(print, 'namedChildren', 4),
';',
];
// TODO: do macros
case 'ability_decls':
return [
' has ',
Expand All @@ -104,27 +137,20 @@ export function print(path: AstPath, options: ParserOptions, print: printFn) {
return breakable_comma_separated_list(path, node, '<', '>', print);
case 'type_parameter':
let abilities = [];
for (let i = 1; i < node.namedChildren.length; i++) {
for (let i = 1; i < node.namedChildCount; i++) {
abilities.push(path.call(print, 'namedChildren', i));
}
return [
// '$' and 'phantom' are mutually exclusive (one for macros and the other for structs)
node.child(0).type === '$' ? '$' : (node.child(0).type === 'phantom' ? 'phantom ' : ''),
path.call(print, 'firstNamedChild'),
node.namedChildren.length > 1 ? ': ' : '' ,
join(' + ', abilities),
];
case 'datatype_fields':
return path.call(print, 'firstNamedChild');
case 'named_fields':
return node.namedChildren.length == 0
? ' {}'
: [
' {',
indent(hardline),
indent(join([',', hardline], path.map(print, 'namedChildren'))),
',',
hardline,
'}',
];
return block(path, node, print, ',');
case 'field_annotation':
return [
path.call(print, 'namedChildren', 0),
Expand All @@ -133,6 +159,24 @@ export function print(path: AstPath, options: ParserOptions, print: printFn) {
];
case 'positional_fields':
return breakable_comma_separated_list(path, node, '(', ')', print);
case 'block':
return block(path, node, print, '');
case 'function_parameters':
return breakable_comma_separated_list(path, node, '(', ')', print);
case 'function_parameter':
return [
path.call(print, 'namedChildren', 0),
': ',
path.call(print, 'namedChildren', 1),
];
case 'ret_type':
return [ ': ', path.call(print, 'namedChildren', 0) ];
case 'struct_identifier':
case 'ability':
case 'type_parameter_identifier':
case 'field_identifier':
case 'function_identifier':
case 'variable_identifier':
default:
return node.text;
}
Expand All @@ -155,3 +199,16 @@ function breakable_comma_separated_list(path: AstPath,
end,
];
}

function block(path: AstPath, node: SyntaxNode, print: printFn, line_ending: string) {
return node.namedChildren.length == 0
? ' {}'
: [
' {',
indent(hardline),
indent(join([line_ending, hardline], path.map(print, 'namedChildren'))),
line_ending,
hardline,
'}',
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(__dirname);
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module test::functions {

fun empty() {}

public fun pub() {}

public entry fun pub_entry() {}

public entry fun entry_pub() {}

fun simple(p: u64): u64 {}

fun simple_generic<T1: key, T2: store + drop + key>() {}

fun long_type_list(
p1: SomeStructWithALongName,
p2: SomeStructWithALongName,
p3: SomeStructWithALongName,
p1: SomeStructWithALongName,
): u64 {}

fun long_type_list_and_generics<
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT1: key,
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT2: store,
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT3: drop,
>(
p1: SomeStructWithALongName,
p2: SomeStructWithALongName,
p3: SomeStructWithALongName,
p1: SomeStructWithALongName,
): u64 {}

fun long_type_list_generics_and_body<
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT1: key,
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT2: store,
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT3: drop,
>(
p1: SomeStructWithALongName,
p2: SomeStructWithALongName,
p3: SomeStructWithALongName,
p1: SomeStructWithALongName,
): u64 {
some_long_function_name();
some_long_function_name();
some_long_function_name();
some_long_function_name();
some_long_function_name();
some_long_function_name();
some_long_function_name();
}

native fun simple_native(p: u64): u64;

public native fun public_native(p: u64): u64;

native fun simple_native_generic<T1: key, T2: store + drop + key>(): u64;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module test::functions {

fun empty() {
}

public fun pub() {
}

public entry fun pub_entry() {
}

entry public fun entry_pub() {
}


fun simple(p: u64): u64 {
}

fun simple_generic<T1 : key,
T2
:
store
+ drop + key
,
>(
)
{
}

fun long_type_list(p1: SomeStructWithALongName,
p2: SomeStructWithALongName, p3:


SomeStructWithALongName, p1: SomeStructWithALongName): u64 {
}

fun long_type_list_and_generics<TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT1 : key, TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT2 : store, TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT3 : drop>

(p1: SomeStructWithALongName,
p2: SomeStructWithALongName, p3:


SomeStructWithALongName, p1: SomeStructWithALongName): u64 {
}

fun long_type_list_generics_and_body<TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT1 : key, TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT2 : store, TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT3 : drop>

(p1: SomeStructWithALongName,
p2: SomeStructWithALongName, p3:


SomeStructWithALongName, p1: SomeStructWithALongName): u64 {
some_long_function_name();
some_long_function_name();
some_long_function_name(); some_long_function_name();
some_long_function_name();
some_long_function_name();
some_long_function_name();
}

native fun simple_native(p: u64): u64;

public native fun public_native(p: u64): u64;

native fun simple_native_generic<T1 : key,
T2
:
store
+ drop + key
,
>(
): u64;

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module test::structs {
> {}

struct ThreeLongGenerics<
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT1: key,
phantom TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT1: key,
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT2: store,
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT3: drop,
> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module test::structs {
{
}

struct ThreeLongGenerics<TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT1 : key, TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT2 : store, TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT3 : drop>
struct ThreeLongGenerics<phantom TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT1 : key, TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT2 : store, TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT3 : drop>
{
}

Expand Down
Binary file not shown.

0 comments on commit 561c142

Please sign in to comment.