vbigiani
vbigiani

Reputation: 135

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 the 30000 lines?

Upvotes: 13

Views: 2163

Answers (4)

ivg
ivg

Reputation: 35210

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

Upvotes: 0

Pierre G.
Pierre G.

Reputation: 4431

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

Upvotes: 3

a_m0d
a_m0d

Reputation: 12195

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

Upvotes: 3

chollida
chollida

Reputation: 7894

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.

Upvotes: 8

Related Questions