How to indent existing OCaml code
I have ~30.000 lines of badly indented OCaml code (including mly and mll files) and would like to indent them. I tried googling for variations of 'ocaml indent' the closest I can get is to use Omlet.vim and indent the code one line at a time (via C-f in insert mode). Is there a way to do the indentation for all t开发者_高级运维he 30000 lines?
I use Emacs as my editor with this package installed:
http://caml.inria.fr/pub/docs/u3-ocaml/emacs/index.html
To indent existing code you need to highlight it and then use the key combination C-M-\
You can script this per file pretty easily and I find the indentation to be pretty good.
When I want to re-indent a whole file in vim, I use the following key sequence:
g g V G =
Breaking this down for you, g g moves the cursor to the beginning of the file. V enters visual mode. G selects to the end of the file. = indents the selected lines.
This should be much faster than your method of applying indentation line by line, but yet will use the same rules (warning: using Omlet, indenting a whole file may take a while).
Now, one can use ocp-indent (opam install ocp-indent) and invoke it like this :
ocp-indent bad-indented-ocaml.ml > well-indented-file.ml
Use ocp-indent
tool for this. Here is an example Makefile
rule, that will automatically indent you ml
files.
.PHONY: auto-ocp-indent
auto-ocp-indent: *.ml
for mlfile in $^; do ocp-indent -i $$mlfile; done
精彩评论