easy-format icon indicating copy to clipboard operation
easy-format copied to clipboard

Question: How to accomplish common function call formatting

Open jordwalke opened this issue 10 years ago • 3 comments

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'}

jordwalke avatar Dec 31 '14 05:12 jordwalke

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'
   }

mjambon avatar Nov 28 '15 10:11 mjambon

Yes, and I believe Easy_format would format the final example exactly like that if the keys had grown too long.

jordwalke avatar Nov 28 '15 11:11 jordwalke

I would try to format the list of arguments as a Label(List(arguments 1..N-1), argument N). No guarantee.

mjambon avatar Nov 28 '15 11:11 mjambon