-
Notifications
You must be signed in to change notification settings - Fork 455
Integration with auto formatters #1252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| .. _formatting-main: | ||
|
|
||
| ******************** | ||
| Automatic formatting | ||
| ******************** | ||
|
|
||
| Dune can be set up to run automatic formatters for source code. | ||
|
|
||
| It can use ocamlformat_ to format OCaml source code (``*.ml`` and ``*.mli`` | ||
| files) and refmt_ to format Reason source code (``*.re`` and ``*.rei`` files). | ||
|
|
||
| .. _ocamlformat: https://github.com/ocaml-ppx/ocamlformat | ||
| .. _refmt: https://github.com/facebook/reason/tree/master/src/refmt | ||
|
|
||
| Enabling automatic formatting | ||
| ============================= | ||
|
|
||
| This feature is enabled by adding the following to the ``dune-project`` file: | ||
|
|
||
| .. code:: scheme | ||
|
|
||
| (using fmt 1.0) | ||
|
|
||
| Formatting a project | ||
| ==================== | ||
|
|
||
| When this feature is active, an alias named ``fmt`` is defined. When built, it | ||
| will format the source files in the corresponding project and display the | ||
| differences: | ||
|
|
||
| .. code:: | ||
|
|
||
| $ dune build @fmt | ||
| --- hello.ml | ||
| +++ hello.ml.formatted | ||
| @@ -1,3 +1 @@ | ||
| -let () = | ||
| - print_endline | ||
| - "hello, world" | ||
| +let () = print_endline "hello, world" | ||
|
|
||
| It is then possible to accept the correction by calling ``dune promote`` to | ||
| replace the source files by the corrected versions. | ||
|
|
||
| .. code:: | ||
|
|
||
| $ dune promote | ||
| Promoting _build/default/hello.ml.formatted to hello.ml. | ||
|
|
||
| As usual with promotion, it is possible to combine these two steps by running | ||
| ``dune build @fmt --auto-promote``. | ||
|
|
||
| Only enabling it for certain languages | ||
| ====================================== | ||
|
|
||
| By default, formatting will be enabled for all languages present in the project | ||
| that dune knows about. This is not always desirable, for example if in a mixed | ||
| Reason/OCaml project, one only wants to format the Reason files to avoid pulling | ||
| ``ocamlformat`` as a dependency. | ||
|
|
||
| In these cases, it is possible to use the ``enabled_for`` argument to restrict | ||
| the languages that are considered for formatting. | ||
|
|
||
| .. code:: scheme | ||
|
|
||
| (using fmt 1.0 (enabled_for reason)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ Welcome to dune's documentation! | |
| configurator | ||
| menhir | ||
| jsoo | ||
| formatting | ||
| faq | ||
| known-issues | ||
| migration | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| open Import | ||
|
|
||
| let flag_of_kind : Ml_kind.t -> _ = | ||
| function | ||
| | Impl -> "--impl" | ||
| | Intf -> "--intf" | ||
|
|
||
| let config_includes (config : Dune_file.Auto_format.t) s = | ||
| match config.enabled_for with | ||
| | Default -> true | ||
| | Only set -> List.mem s ~set | ||
|
|
||
| let add_diff sctx loc alias ~dir input output = | ||
| let module SC = Super_context in | ||
| let open Build.O in | ||
| let action = Action.diff input output in | ||
| SC.add_alias_action sctx alias ~loc:(Some loc) ~locks:[] ~stamp:input | ||
| (Build.paths [input; output] | ||
| >>> | ||
| Build.action | ||
| ~dir | ||
| ~targets:[] | ||
| action) | ||
|
|
||
| let gen_rules sctx (config : Dune_file.Auto_format.t) ~dir = | ||
| let loc = config.loc in | ||
| let files = | ||
| File_tree.files_of | ||
| (Super_context.file_tree sctx) | ||
| (Path.drop_build_context_exn dir) | ||
| in | ||
| let subdir = ".formatted" in | ||
| let output_dir = Path.relative dir subdir in | ||
| let alias = Build_system.Alias.make "fmt" ~dir in | ||
| let alias_formatted = Build_system.Alias.make "fmt" ~dir:output_dir in | ||
| let resolve_program = Super_context.resolve_program sctx ~loc:(Some loc) in | ||
| let setup_formatting file (arrows_acc, extra_deps_acc) = | ||
| let input_basename = Path.basename file in | ||
| let input = Path.relative dir input_basename in | ||
| let output = Path.relative output_dir input_basename in | ||
|
|
||
| let ocaml kind = | ||
| if config_includes config Ocaml then | ||
| let exe = resolve_program "ocamlformat" in | ||
emillon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let args = | ||
| let open Arg_spec in | ||
| [ A (flag_of_kind kind) | ||
| ; Dep input | ||
| ; A "--name" | ||
| ; Path file | ||
| ; A "-o" | ||
| ; Target output | ||
| ] | ||
| in | ||
| Some (Build.run ~dir exe args) | ||
| else | ||
| None | ||
| in | ||
|
|
||
| let formatter = | ||
| match Path.extension file with | ||
| | ".ml" -> ocaml Impl | ||
| | ".mli" -> ocaml Intf | ||
| | ".re" | ||
| | ".rei" when config_includes config Reason -> | ||
| let exe = resolve_program "refmt" in | ||
| let args = [Arg_spec.Dep input] in | ||
| Some (Build.run ~dir ~stdout_to:output exe args) | ||
| | _ -> None | ||
| in | ||
|
|
||
| let new_extra_deps_acc = | ||
| if String.equal input_basename ".ocamlformat" then | ||
| input::extra_deps_acc | ||
| else | ||
| extra_deps_acc | ||
| in | ||
|
|
||
| let new_arrows_acc = | ||
| match formatter with | ||
| | None -> arrows_acc | ||
| | Some arr -> (arr, input, output)::arrows_acc | ||
| in | ||
|
|
||
| (new_arrows_acc, new_extra_deps_acc) | ||
| in | ||
| Super_context.on_load_dir sctx ~dir:output_dir ~f:(fun () -> | ||
| let arrows, extra_deps = | ||
| Path.Set.fold files ~init:([], []) ~f:setup_formatting | ||
| in | ||
| List.iter | ||
| arrows | ||
| ~f:(fun (format_arr, input, output) -> | ||
| let open Build.O in | ||
| let arr = Build.paths extra_deps >>> format_arr in | ||
| Super_context.add_rule sctx ~mode:Standard ~loc arr; | ||
| add_diff sctx loc alias_formatted ~dir input output)); | ||
| Super_context.add_alias_deps sctx alias | ||
| (Path.Set.singleton (Build_system.Alias.stamp_file alias_formatted)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| open Import | ||
|
|
||
| (** Setup automatic format rules for the given dir. | ||
| If tools like ocamlformat are not available in $PATH, just display an error | ||
| message when the alias is built. *) | ||
| val gen_rules: | ||
| Super_context.t | ||
| -> Dune_file.Auto_format.t | ||
| -> dir:Path.t | ||
| -> unit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (library | ||
| (name lib) | ||
| ) |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/disabled/dune-project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (lang dune 1.2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let x = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| val x : int |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (library | ||
| (name lib_reason) | ||
| ) |
2 changes: 2 additions & 0 deletions
2
test/blackbox-tests/test-cases/formatting/enabled/dune-project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| (lang dune 1.2) | ||
| (using fmt 1.0) |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/enabled/ocaml_file.ml.orig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let y=() |
2 changes: 2 additions & 0 deletions
2
test/blackbox-tests/test-cases/formatting/enabled/ocaml_file.mli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| val y : | ||
| unit |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/enabled/other-project/a.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let x=2 |
3 changes: 3 additions & 0 deletions
3
test/blackbox-tests/test-cases/formatting/enabled/other-project/dune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (library | ||
| (name lib_other_project) | ||
| ) |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/enabled/other-project/dune-project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (lang dune 1.2) |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/enabled/reason_file.re.orig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let y = (); |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/enabled/reason_file.rei
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let y : unit; |
3 changes: 3 additions & 0 deletions
3
test/blackbox-tests/test-cases/formatting/enabled/subdir/dune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (library | ||
| (name lib) | ||
| ) |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/enabled/subdir/lib.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let x = 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (executables | ||
| (public_names ocamlformat refmt) | ||
| ) |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/fake-tools/dune-project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (lang dune 1.2) |
Empty file.
10 changes: 10 additions & 0 deletions
10
test/blackbox-tests/test-cases/formatting/fake-tools/ocamlformat.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| let process args ~output = | ||
| let oc = open_out output in | ||
| Printf.fprintf oc "Sys.argv: %s\n" (String.concat " " (Array.to_list args)); | ||
| Printf.fprintf oc "ocamlformat output\n"; | ||
| close_out oc | ||
|
|
||
| let () = | ||
| match Sys.argv with | ||
| | [| _ ; _; _; "--name"; _; "-o"; output|] -> process Sys.argv ~output | ||
| | _ -> assert false |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.