reason icon indicating copy to clipboard operation
reason copied to clipboard

Prettier Reason -> OCaml printing

Open chenglou opened this issue 9 years ago • 8 comments

OCaml -> Reason is fully under our control because it uses the Reason pretty-printer. However, the other direction uses the built-in OCaml pretty-printer, and in addition to not preserving comments, it doesn't print that nicely. I've tried passing the output through ocp-indent, which fixes a few indentations but not the general look (haven't tried adjusting the ocp-indent configurations though).

chenglou avatar May 22 '16 19:05 chenglou

I think we'll either make significant improvements to Easy_format or build our own similar utilities on top of OCaml's lower level Format library. When we do that, we should consider how to reuse as much as possible for the sake of printing OCaml output. If we take the current printer as is, which is definitely in-flux right now, and fork it to output OCaml, I'm pretty sure that effort would get dropped. If we first clean up the printer and make parts of it reusable easily, then I think the OCaml printer could be sustained/supported successfully.

jordwalke avatar May 23 '16 08:05 jordwalke

maybe checkout Daniel's fmt?

objmagic avatar May 23 '16 16:05 objmagic

@jordwalke I agree. We should really spend sometime refactoring our existing stuff. It will also help us get correct comments printing in the future .

yunxing avatar May 23 '16 17:05 yunxing

Another valuable use case for this is supporting documentation generation using ocamldoc.

bordoley avatar Mar 09 '17 07:03 bordoley

ocamlformat provides tooling for this. It may be worth suggesting/relying on it when Reason -> OCaml formatting is necessary.

hcarty avatar Jan 09 '18 22:01 hcarty

AFAIU comments are the one main point* that stands between really slick interop between refmt and ocamlformat that would allow seamless translation between concrete syntaxes. The heuristics for associating comments with abstract syntax phrases in ocamlformat is best at consuming its own output (surprise!) and in particular does a markedly worse job on refmt'ed Reason code.

If refmt were to report the results of its own heuristics for associating comments to parsetree nodes as attributes in the parse tree, for example, then this annotated parsetree could be given to ocamlformat to produce OCaml concrete syntax. Likewise, with such interop attributes, ocamlformat could report its comment association decisions as attributes and pass the resulting annotated parsetree to refmt to produce Reason concrete syntax.

Doing this would not be much work on the ocamlformat side, but last I checked, refmt used an intermediate structure, not the parsetree, when attaching comments, which would make it more (maybe too much) work. Perhaps there is another alternative?

  • And the current limitations of ocamlformat that are independent of Reason, like not supporting the object language.

jberdine avatar Mar 04 '18 22:03 jberdine

we should consider using ocamlformat and checking whether we can pass it AST + comments. would likely require changing our comments data structure, too.

anmonteiro avatar Jul 20 '24 04:07 anmonteiro

My list of pain points:

  • Need to run this sed sed -z -e 's/[[]@reason[^]]\+\]//g' -e 's/\[@explicit_arity *\]//g' -i Your_file.ml
  • let%bind's get messed up as [%bind let foo = bar in ...]
  • Local opens are printed as let open X in y + z instead of X.(y + z)

Lupus avatar Jul 20 '24 15:07 Lupus