reasonml / reason

Simple, fast & type safe code that leverages the JavaScript & OCaml ecosystems
http://reasonml.github.io
MIT License
10.12k stars 428 forks source link

Prettier Reason -> OCaml printing #516

Open chenglou opened 8 years ago

chenglou commented 8 years ago

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).

jordwalke commented 8 years ago

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.

objmagic commented 8 years ago

maybe checkout Daniel's fmt?

yunxing commented 8 years ago

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

bordoley commented 7 years ago

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

hcarty commented 6 years ago

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

jberdine commented 6 years ago

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?

anmonteiro commented 1 month ago

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

Lupus commented 1 month ago

My list of pain points: