Skip to content

Commit

Permalink
If a dune file uses OCaml syntax, use ocamlformat
Browse files Browse the repository at this point in the history
Closes #2012
  • Loading branch information
emillon committed Apr 3, 2019
1 parent 9f6daf1 commit 1be4341
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
16 changes: 12 additions & 4 deletions src/format_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,18 @@ let gen_rules_output sctx (config : Dune_file.Auto_format.t) ~output_dir =
let exe = resolve_program "refmt" in
let args = [Arg_spec.Dep input] in
Some (Build.run ~dir ~stdout_to:output exe args)
| "dune", _ when Dune_file.Auto_format.includes config Dune ->
let exe = resolve_program "dune" in
let args = [Arg_spec.A "format-dune-file"; Dep input] in
Some (Build.run ~dir ~stdout_to:output exe args)
| "dune", _ ->
begin
match Format_dune_lang.parse_file (Some file) with
| OCaml_syntax _ ->
ocaml Impl
| Sexps _ when Dune_file.Auto_format.includes config Dune ->
let exe = resolve_program "dune" in
let args = [Arg_spec.A "format-dune-file"; Dep input] in
Some (Build.run ~dir ~stdout_to:output exe args)
| Sexps _ ->
None
end
| _ -> None
in

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(* -*- tuareg -*- *)

let () = Jbuild_plugin.V1.send {|
(alias
(name runtest)
(action (echo "ocaml syntax")))
|}
46 changes: 28 additions & 18 deletions test/blackbox-tests/test-cases/formatting/run.t
Original file line number Diff line number Diff line change
@@ -1,47 +1,54 @@
Formatting can be checked using the @fmt target:

$ cp enabled/dune-ocaml-syntax/dune.orig enabled/dune-ocaml-syntax/dune
$ cp enabled/ocaml_file.ml.orig enabled/ocaml_file.ml
$ cp enabled/reason_file.re.orig enabled/reason_file.re
$ cp enabled/dune.orig enabled/dune
$ dune build --display short @fmt
ocamldep fake-tools/.ocamlformat.eobjs/ocamlformat.ml.d
ocamldep fake-tools/.ocamlformat.eobjs/refmt.ml.d
ocamlc fake-tools/.ocamlformat.eobjs/byte/ocamlformat.{cmi,cmo,cmt}
ocamlopt fake-tools/.ocamlformat.eobjs/native/ocamlformat.{cmx,o}
ocamlopt fake-tools/ocamlformat.exe
ocamlformat enabled/.formatted/ocaml_file.ml
File "enabled/ocaml_file.ml", line 1, characters 0-0:
Files _build/default/enabled/ocaml_file.ml and _build/default/enabled/.formatted/ocaml_file.ml differ.
dune enabled/.formatted/dune
File "enabled/dune", line 1, characters 0-0:
Files _build/default/enabled/dune and _build/default/enabled/.formatted/dune differ.
dune enabled/subdir/.formatted/dune
File "enabled/subdir/dune", line 1, characters 0-0:
Files _build/default/enabled/subdir/dune and _build/default/enabled/subdir/.formatted/dune differ.
ocamlc fake-tools/.ocamlformat.eobjs/byte/refmt.{cmi,cmo,cmt}
ocamlopt fake-tools/.ocamlformat.eobjs/native/refmt.{cmx,o}
ocamlopt fake-tools/refmt.exe
refmt enabled/.formatted/reason_file.rei
File "enabled/reason_file.rei", line 1, characters 0-0:
Files _build/default/enabled/reason_file.rei and _build/default/enabled/.formatted/reason_file.rei differ.
ocamlformat enabled/subdir/.formatted/lib.ml
File "enabled/subdir/lib.ml", line 1, characters 0-0:
Files _build/default/enabled/subdir/lib.ml and _build/default/enabled/subdir/.formatted/lib.ml differ.
ocamlformat partial/.formatted/a.ml
File "partial/a.ml", line 1, characters 0-0:
Files _build/default/partial/a.ml and _build/default/partial/.formatted/a.ml differ.
dune enabled/.formatted/dune
File "enabled/dune", line 1, characters 0-0:
Files _build/default/enabled/dune and _build/default/enabled/.formatted/dune differ.
dune enabled/subdir/.formatted/dune
File "enabled/subdir/dune", line 1, characters 0-0:
Files _build/default/enabled/subdir/dune and _build/default/enabled/subdir/.formatted/dune differ.
ocamlc fake-tools/.ocamlformat.eobjs/byte/ocamlformat.{cmi,cmo,cmt}
ocamlopt fake-tools/.ocamlformat.eobjs/native/ocamlformat.{cmx,o}
ocamlopt fake-tools/ocamlformat.exe
ocamlformat enabled/.formatted/ocaml_file.mli
File "enabled/ocaml_file.mli", line 1, characters 0-0:
Files _build/default/enabled/ocaml_file.mli and _build/default/enabled/.formatted/ocaml_file.mli differ.
refmt enabled/.formatted/reason_file.re
File "enabled/reason_file.re", line 1, characters 0-0:
Files _build/default/enabled/reason_file.re and _build/default/enabled/.formatted/reason_file.re differ.
ocamlformat enabled/.formatted/ocaml_file.ml
File "enabled/ocaml_file.ml", line 1, characters 0-0:
Files _build/default/enabled/ocaml_file.ml and _build/default/enabled/.formatted/ocaml_file.ml differ.
ocamlformat enabled/subdir/.formatted/lib.ml
File "enabled/subdir/lib.ml", line 1, characters 0-0:
Files _build/default/enabled/subdir/lib.ml and _build/default/enabled/subdir/.formatted/lib.ml differ.
ocamlformat partial/.formatted/a.ml
File "partial/a.ml", line 1, characters 0-0:
Files _build/default/partial/a.ml and _build/default/partial/.formatted/a.ml differ.
ocamlformat enabled/dune-ocaml-syntax/.formatted/dune
File "enabled/dune-ocaml-syntax/dune", line 1, characters 0-0:
Files _build/default/enabled/dune-ocaml-syntax/dune and _build/default/enabled/dune-ocaml-syntax/.formatted/dune differ.
[1]

Configuration files are taken into account for this action:

$ touch enabled/.ocamlformat
$ dune build --display short @fmt
ocamlformat enabled/dune-ocaml-syntax/.formatted/dune
File "enabled/dune-ocaml-syntax/dune", line 1, characters 0-0:
Files _build/default/enabled/dune-ocaml-syntax/dune and _build/default/enabled/dune-ocaml-syntax/.formatted/dune differ.
refmt enabled/.formatted/reason_file.re
File "enabled/reason_file.re", line 1, characters 0-0:
Files _build/default/enabled/reason_file.re and _build/default/enabled/.formatted/reason_file.re differ.
Expand Down Expand Up @@ -88,6 +95,9 @@ All .ocamlformat files are considered dependencies:

$ echo 'margin = 70' > .ocamlformat
$ dune build --display short @fmt
ocamlformat enabled/dune-ocaml-syntax/.formatted/dune
File "enabled/dune-ocaml-syntax/dune", line 1, characters 0-0:
Files _build/default/enabled/dune-ocaml-syntax/dune and _build/default/enabled/dune-ocaml-syntax/.formatted/dune differ.
refmt enabled/.formatted/reason_file.re
refmt enabled/.formatted/reason_file.rei
File "enabled/reason_file.rei", line 1, characters 0-0:
Expand Down

0 comments on commit 1be4341

Please sign in to comment.