Open chenglou opened 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.
@jordwalke I agree. We should really spend sometime refactoring our existing stuff. It will also help us get correct comments printing in the future .
Another valuable use case for this is supporting documentation generation using ocamldoc.
ocamlformat provides tooling for this. It may be worth suggesting/relying on it when Reason -> OCaml formatting is necessary.
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?
we should consider using ocamlformat and checking whether we can pass it AST + comments. would likely require changing our comments data structure, too.
My list of pain points:
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 ...]
let open X in y + z
instead of X.(y + z)
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).