easy-format
easy-format copied to clipboard
Question: How to accomplish common function call formatting
It is very common to format source code as follows:
(* In ML languages with space separated "arguments" *)
let result = myFunction x y {
key1= 'long';
key2='long';
}
// In JS style languages with parenthesis-wrapped comma-delimited arguments
var result = myFunction (x, y, {
key1: 'long',
key2: 'long'
});
It's even difficult for me to articulate exactly when this is the preferred formatting but I'll try:
- The entire line will not fit within the allotted space and some line breaking is required.
- Having the last argument to a function break into several lines eliminates any need for the //previous// items on the line to have to break onto new lines.
For example, suppose the following samples cannot fit onto a single line. Here are some correct (and incorrect) formatting examples:
(* _Incorrect_ because even after last argument is broken,
the original line is still too long *)
let result = myFunction reallyLongArgument reallyReallyLongArgument {
key1= 'long';
key2='long';
}
(* _Correct_ Every argument would need to be placed on their own lines *)
let result = myFunction
reallyLongArgument
reallyReallyLongArgument
{key1= 'long'; key2='long'}
(* _Incorrect_ because in order to fit the first line, the final
argument had to be place on its own line.
*)
let result = myFunction supposeThisIsReallyLong reallyReallyLongArgument {
key1= 'long';
key2='long';
}
anotherArgument
(* _Correct_ *)
let result = myFunction
supposeThisIsReallyLong
reallyReallyLongArgument
{key1= 'long'; key2='long'}
anotherArgument
(* _Incorrect_ because the last two arguments had to have
their bodies broken up.
*)
let result = myFunction reallyLongArgument reallyReallyLongArgument {
key1= 'long';
key2='long';
} {
key1='long';
key2='long';
}
(* _Correct_*)
let result = myFunction
reallyLongArgument
reallyReallyLongArgument
{key1= 'long'; key2='long'}
{key1='long'; key2='long'}
For the last example, is the following correct, assuming each field is really long?
let result = myFunction
reallyLongArgument
reallyReallyLongArgument
{
key1= 'long';
key2='long'
}
{
key1='long';
key2='long'
}
Yes, and I believe Easy_format
would format the final example exactly like that if the keys had grown too long.
I would try to format the list of arguments as a Label(List(arguments 1..N-1), argument N). No guarantee.