ocamlformat
ocamlformat copied to clipboard
Feature request: Stronger notion of formatting stability
A desirable but not currently achieved form of stability is for ocamlformat -m 80 -i file.ml to be equivalent to ocamlformat -m 999 -i file.ml; ocamlformat -m 80 -i file.ml (or some number even larger than 999).
This is not currently achieved for a number of reasons, that seem to have varying degrees of seriousness / difficulty:
- Single line detection gets tripped up and some phrases that are stable either as a single line or as multiple lines can change from the multiple-line form to the single line form when first formatting with a huge margin.
- Comments, particularly those that end a line, can be attached differently, and cause formatting changes. Perhaps detecting if a comment ends a line and adjusting the formatting to ensure a break is added after it would resolve this.
- Open lines around comments at the top of structures (and signatures?) are not always preserved. Perhaps this is another incarnation of the end-of-line comment behavior.
- The indentation of some comments affects their attachment, and this is not preserved.
- The detection of banner comments seems to be sensitive to the margin and they are not preserved by formatting with a huge margin.
Comment attachment is already complicated, but I wonder if there is something that can reasonably be done regarding the changes based on single-line detection. For example, with a margin of 76, the code
let marshal program file =
Out_channel.with_file file ~f:(fun oc ->
Marshal.to_channel oc program [] )
is stable and so is the single-line form:
let marshal program file =
Out_channel.with_file file ~f:(fun oc -> Marshal.to_channel oc program [])