diff --git a/CHANGES.md b/CHANGES.md index ad69eae2e5..57abdac4a1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,8 @@ profile. This started with version 0.26.0. ### Changed +- Added back the flag `--disable-outside-detected-project` (#2439, @gpetiot) + It was removed in version 0.22. - \* Consistent formatting of comments (#2371, @Julow) - Documentation comments are now formatted by default (#2390, @Julow) Use the option `parse-docstrings = false` to disable. diff --git a/doc/manpage_ocamlformat.mld b/doc/manpage_ocamlformat.mld index 2977fddff3..f0f4cdc0db 100644 --- a/doc/manpage_ocamlformat.mld +++ b/doc/manpage_ocamlformat.mld @@ -470,11 +470,6 @@ OPTIONS (REMOVED) --align-variants-decl=VAL This option has been removed in version 0.22. - --disable-outside-detected-project=VAL - This option has been removed in version 0.22. OCamlFormat is - disabled outside of a detected project by default, to enable the - opposite behavior use `enable-outside-detected-project`. - --doc-comments-val=VAL This option has been removed in version 0.16. If you are using `doc-comments-val=before` in combination with @@ -535,6 +530,12 @@ OPTIONS --disable-conf-files Disable .ocamlformat configuration files. + --disable-outside-detected-project + If no .ocamlformat config files have been detected, disable the + formatting. OCamlFormat is disabled outside of a detected project + by default, to enable the opposite behavior use + --enable-outside-detected-project. + --doc Parse input as an odoc documentation. diff --git a/lib/Conf.ml b/lib/Conf.ml index 5199751279..e08101e39c 100644 --- a/lib/Conf.ml +++ b/lib/Conf.ml @@ -277,14 +277,6 @@ module V = struct let v0_22 = Version.make ~major:0 ~minor:22 ~patch:None end -let disable_outside_detected_project = - let msg = - "OCamlFormat is disabled outside of a detected project by default, to \ - enable the opposite behavior use `enable-outside-detected-project`." - in - let names = ["disable-outside-detected-project"] in - Decl.removed_option ~names ~since:V.v0_22 ~msg - let profile = let doc = "Select a preset profile which sets $(i,all) options, overriding lower \ @@ -349,7 +341,7 @@ let profile = {conf with profile= elt; fmt_opts= p} ) (fun conf -> conf.profile) -let options = Store.[elt disable_outside_detected_project; elt profile] +let options = Store.[elt profile] (** Options affecting formatting *) module Formatting = struct diff --git a/lib/bin_conf/Bin_conf.ml b/lib/bin_conf/Bin_conf.ml index 05280bf891..26b15c6f7c 100644 --- a/lib/bin_conf/Bin_conf.ml +++ b/lib/bin_conf/Bin_conf.ml @@ -116,23 +116,35 @@ let enable_outside_detected_project = (List.map File_system.project_root_witness ~f:(fun name -> Format.sprintf "$(b,%s)" name ) ) in - let doc = - Format.sprintf - "Read $(b,.ocamlformat) config files outside the current project when \ - no project root has been detected for the input file. The project \ - root of an input file is taken to be the nearest ancestor directory \ - that contains a %s file. If $(b,.ocamlformat) config files are \ - located in the same directory or parents they are applied, if no \ - $(b,.ocamlformat) file is found then the global configuration \ - defined in $(b,\\$XDG_CONFIG_HOME/.ocamlformat) (or in \ - $(b,\\$HOME/.config/.ocamlformat) if $(b,\\$XDG_CONFIG_HOME) is \ - undefined) is applied." - witness + let enable = + let doc_enable = + Format.sprintf + "Read $(b,.ocamlformat) config files outside the current project \ + when no project root has been detected for the input file. The \ + project root of an input file is taken to be the nearest ancestor \ + directory that contains a %s file. If $(b,.ocamlformat) config \ + files are located in the same directory or parents they are \ + applied, if no $(b,.ocamlformat) file is found then the global \ + configuration defined in $(b,\\$XDG_CONFIG_HOME/.ocamlformat) (or \ + in $(b,\\$HOME/.config/.ocamlformat) if $(b,\\$XDG_CONFIG_HOME) is \ + undefined) is applied." + witness + in + Arg.info ["enable-outside-detected-project"] ~doc:doc_enable ~docs + in + let disable = + let doc_disable = + "If no $(b,.ocamlformat) config files have been detected, disable the \ + formatting. OCamlFormat is disabled outside of a detected project by \ + default, to enable the opposite behavior use \ + $(b,--enable-outside-detected-project)." + in + Arg.info ["disable-outside-detected-project"] ~doc:doc_disable ~docs in Term.( const (fun enable_outside_detected_project conf -> {conf with enable_outside_detected_project} ) - $ Arg.(value & flag & info ["enable-outside-detected-project"] ~doc ~docs) ) + $ Arg.(value & vflag false [(true, enable); (false, disable)]) ) let inplace = let doc = "Format in-place, overwriting input file(s)." in diff --git a/test/projects/disable_outside_detected_project.expected b/test/projects/disable_outside_detected_project.expected new file mode 100644 index 0000000000..84259279e7 --- /dev/null +++ b/test/projects/disable_outside_detected_project.expected @@ -0,0 +1,7 @@ +File "main.ml", line 1: +Warning: Ocamlformat disabled because [--enable-outside-detected-project] is not set and no [.ocamlformat] was found within the project (root: ../disable_outside_detected_project) +(* The following code should not be formatted *) +type t = { + foooooo : string; + baaaaar : Baaaaar.t; +} diff --git a/test/projects/disable_outside_detected_project/dune-project b/test/projects/disable_outside_detected_project/dune-project new file mode 100644 index 0000000000..0636ab6acf --- /dev/null +++ b/test/projects/disable_outside_detected_project/dune-project @@ -0,0 +1 @@ +(lang dune 1.11) diff --git a/test/projects/disable_outside_detected_project/main.ml b/test/projects/disable_outside_detected_project/main.ml new file mode 100644 index 0000000000..8f4b7ca2f4 --- /dev/null +++ b/test/projects/disable_outside_detected_project/main.ml @@ -0,0 +1,5 @@ +(* The following code should not be formatted *) +type t = { + foooooo : string; + baaaaar : Baaaaar.t; +} diff --git a/test/projects/dune b/test/projects/dune index 986f52b6c2..04b905377a 100644 --- a/test/projects/dune +++ b/test/projects/dune @@ -41,6 +41,28 @@ (action (diff outside_detected_project.expected outside_detected_project.output))) +(rule + (deps + disable_outside_detected_project/dune-project + disable_outside_detected_project/main.ml) + (package ocamlformat) + (enabled_if + (<> %{os_type} Win32)) + (action + (with-outputs-to + disable_outside_detected_project.output + (chdir + disable_outside_detected_project + (run ocamlformat --disable-outside-detected-project main.ml))))) + +(rule + (alias runtest) + (package ocamlformat) + (enabled_if + (<> %{os_type} Win32)) + (action + (diff disable_outside_detected_project.expected disable_outside_detected_project.output))) + (rule (deps (source_tree outside_detected_project_with_name))