ocamlformat icon indicating copy to clipboard operation
ocamlformat copied to clipboard

Feature request: wrap, fit-or-vertical, or vertical based on collections type

Open tachukao opened this issue 6 years ago • 1 comments

We recently encountered the following dilemma https://github.com/owlbarn/owl/pull/468:

On the one hand we would like to wrap long float list. That is, we would prefer

https://github.com/tachukao/owl/blob/6cd62ddcdab29f5f67da537be2d4e0e4df342bb1/test/unit_ndarray_core_generic.ml#L306

to

https://github.com/tachukao/owl/blob/34c0768572d73f24431297415c640faa1f4d6b96/test/unit_ndarray_core_generic.ml#L27://github.com/tachukao/owl/blob/34c0768572d73f24431297415c640faa1f4d6b96/test/unit_ndarray_core_generic.ml#L275

On the other hand, we would like to have fit-or-vertical for other types of lists. That is, we would like to have

https://github.com/tachukao/owl/blob/6cd62ddcdab29f5f67da537be2d4e0e4df342bb1/test/unit_algodiff_matrix_generic.ml#L275

as opposed to https://github.com/tachukao/owl/blob/34c0768572d73f24431297415c640faa1f4d6b96/test/unit_algodiff_matrix_generic.ml#L275

The only "solution" I can think of is to allow the users to choose break-collection-expressions based on the type of the expression. In this case, we might want to have wrap for float collections and fit-or-vertical for other collection types. Is this something could be implemented, or are there other solutions?

A somewhat related issue is the formatting matrices of type float array array or float list list. Ideally, we would like to define a matrix like this

let x = [| [| 3. ; 4. ;  5. |];
           [| 6. ; 7. ; 10. |] |]

such that the line break clearly delineates the rows of the matrix and the semicolons on each row delineates the columns. Is this something that could be done?

I realise this feature request might be quite out there. Thank you for your help!

tachukao avatar Dec 06 '19 09:12 tachukao

Note that ocamlformat operates on the untyped parsetree, rather than the type tree data structure from after type inference / checking has been done. It would be a major change, with significant downsides, to change to the typed tree.

Even if type information was available, this is getting into territory where it is not at all clear that there is a universally applicable rule that the tooling can implement. It seems that this is the sort of case where providing more info on intent to the tool is needed. How feasible is it to use attributes in your use case? For example, would adding e.g. [@ocamlformat "break-collection-expressions=wrap"] to the cases with floats you want to wrap be an acceptable workaround?

jberdine avatar May 04 '20 23:05 jberdine