ocamlformat icon indicating copy to clipboard operation
ocamlformat copied to clipboard

Feature request: Stronger notion of formatting stability

Open jberdine opened this issue 4 years ago • 0 comments

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:

  1. 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.
  2. 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.
  3. 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.
  4. The indentation of some comments affects their attachment, and this is not preserved.
  5. 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 [])

jberdine avatar Jun 04 '21 13:06 jberdine