Help for configuration of ocamlformat

Hey, I just installed ocamlformat because I’m studying in class with ocaml and I’m trying to configure the formatter such as the code looks like how I was used to format it.
I have currently two point:
- Is it possible to have 4 spaces of indentation after a for/while statement?
- Is it possible to force a if statement to wrap?

Thank you and have a good day!!

4 spaces indentation should be automatic as long as you have a .ocamlformat file at the root of your dune project

I’m guessing by wrap you mean having something like this, which you can get by setting if_then_else to KR

let () =
  if true then
    print_endline "ok"
  else
    print_endline "onoes"

Shameless plug, you can try the different options on OCamlFormat configurator

2 Likes

Thanks for your replies (:
It seem like my .ocamlformat file isn’t loaded entirely:
(the file is in the same directory as my my test files, btw when I put the .ocamlformat file in my $XDG_CONFIG_HOME, the file isn’t loaded at all which is really weird)

break-cases=toplevel
break-collection-expressions=wrap
if-then-else=k-r
function-indent=4
extension-indent=4
indent-after-in=4
let-binding-indent=4
match-indent=4
match-indent-nested=always
cases-exp-indent=4
margin=60

output of ocamlformat --print-config

profile=default
range=<whole input>
quiet=false
ocaml-version=4.04.0
max-iters=10
margin-check=false
disable=false
debug=false
comment-check=true
wrap-fun-args=true
wrap-comments=false
type-decl-indent=2
type-decl=compact
stritem-extension-indent=0
space-around-variants=true
space-around-records=true
space-around-lists=true
space-around-arrays=true
single-case=compact
sequence-style=terminator
sequence-blank-line=preserve-one
parse-toplevel-phrases=false
parse-docstrings=false
parens-tuple-patterns=multi-line-only
parens-tuple=always
parens-ite=false
ocp-indent-compat=false
nested-match=wrap
module-item-spacing=compact
max-indent=68
match-indent-nested=never
match-indent=0
margin=80
line-endings=lf
let-module=compact
let-binding-spacing=compact
let-binding-indent=2
let-and=compact
leading-nested-match-parens=false
infix-precedence=indent
indicate-nested-or-patterns=unsafe-no
indicate-multiline-delimiters=no
indent-after-in=0
if-then-else=compact
function-indent-nested=never
function-indent=2
field-space=loose
extension-indent=2
exp-grouping=parens
dock-collection-brackets=true
doc-comments-tag-only=default
doc-comments-padding=2
doc-comments=after-when-possible
disambiguate-non-breaking-match=false
cases-matching-exp-indent=normal
cases-exp-indent=4
break-struct=force
break-string-literals=auto
break-sequences=true
break-separators=after
break-infix-before-func=false
break-infix=wrap
break-fun-sig=wrap
break-fun-decl=wrap
break-collection-expressions=fit-or-vertical
break-cases=fit
break-before-in=fit-or-vertical
assignment-operator=end-line

Maybe I did not understood how to write the configuration file but I didn’t find any example on the documentation, thanks

By default ocamlformat does not read configuration outside the project root. If you want $XDG_CONFIG_HOME/.ocamlformat to be used, make sure that there is no .ocamlformat in the project tree, and pass ocamlformat the --enable-outside-detected-project flag. For debugging, it may also help to explicitly pass the --root flag to specify the root of your project, to separate issues of detecting the expected root versus the configuration not having your expected effect.