-
Notifications
You must be signed in to change notification settings - Fork 409
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
Integration with auto formatters #1252
Conversation
I'm having trouble with paths and directories: I'm using I see that in Line 474 in 5745c8a
Also, that's probably related, but I'd like to have Thanks! |
src/ocamlformat.ml
Outdated
let formatted = formatted_path path in | ||
let format_action = | ||
let args = [sv flag; dep path] in | ||
Action.Unexpanded.Redirect |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this better than having ocamlformat write down the file directly ?
src/dune_file.ml
Outdated
@@ -678,6 +678,7 @@ module Buildable = struct | |||
; ocamlopt_flags : Ordered_set_lang.Unexpanded.t | |||
; js_of_ocaml : Js_of_ocaml.t | |||
; allow_overlapping_dependencies : bool | |||
; ocamlformat : bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a property of libs and binary or just of the current directory ?
also, You might want to be able to add exceptions, useful for automatically generated source files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's now possible to use OSL to perform exclusions.
per directory would work, but we already have a mapping between files and modules in buildables, so that seems to be the correct abstraction (at least it's the simplest one to start)
src/lib_rules.ml
Outdated
@@ -374,6 +374,7 @@ module Gen (P : Install_rules.Params) = struct | |||
~lib_name:(Some lib.name) | |||
~dir_kind | |||
in | |||
Ocamlformat.gen_rules sctx lib.buildable ~dir ~scope modules; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used for ocaml files only or also for reasonML files ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now, this ignores reason files
@hhugo at the moment the interface is not defined yet, I just pushed the WIP branch for advice on how to deal with paths :) |
Well, you have some early review now |
7708457
to
7b01914
Compare
Well, nothing a good night of sleep couldn't fix. I just passed the basename and it now works. I'll ping the PR when it's ready for more feedback :) Sorry for the noise! (& thanks for the early review!) |
7501b9a
to
6940d4a
Compare
I believe that this is ready for a round of review. Some outstanding problems/questions:
Thanks! |
I think I prefer Also could this just be enabled by default on all the libraries/binaries (e.g. without the need for a specific stanza) so you could just write |
6940d4a
to
0770939
Compare
It seems to me that per-directory makes more sense. It's more a property of the source files themselves rather than individual libraries/executables. In fact, personally I would prefer to enable it once and for all for a given project. To get the list of files to format, we can simply look in the file tree. We don't need to bother with generated files since we clearly don't want to format them. |
Once this is in, would it be possible to have the same support for Reason syntax and Aa far as enabling formatting by default, ocamlformat can be disabled per-file with an attribute. So that provides an option for users who need a file or few to remain untouched by |
0770939
to
a886241
Compare
Thanks for your feedback! I changed this so that it works as a stanza, per directory. This uses I suggest the following plan to generalize this a bit:
This plan would obviously span several PRs, but it's great if we all agree on the direction to take. |
Why do we need a |
For the CLI, I suggest to have a single sub-command to format all kinds of files. I agree with @avsm that we shouldn't need one stanza per directory, except when there are exceptions. We might want to allow to opt-in or opt-out via the dune-project file though, so that in a mono-repo dune can skip projects that don't want to be auto-formatted. I suggest to make it opt-in via the dune-project file for now, to be conservative, and opt-out with |
OK! Let's make this less hidden and more global. I'll deal with the CLI part in a separate PR. I am not sure how to add new features through Thanks! |
Yh, the idea is to add
In this case, rather than or in addition to adding a new stanza in module Extension : sig
type 'a t
(** [register id parser] registers a new extension. Users will
enable this extension by writing:
{[ (using <name> <version> <args>) ]}
in their [dune-project] file. [parser] is used to describe
what [<args>] might be. *)
val register
: ?experimental:bool
-> Syntax.t
-> ('a * Stanza.Parser.t list) Dsexp.Of_sexp.t
-> 'a t
end
val get_extension : t -> 'a Extension.t -> 'a option |
a886241
to
c8fa4c8
Compare
I just did that. For now it just returns the loc but I'll extend that to refmt and add |
src/format_rules.ml
Outdated
let msg = "Cannot find ocamlformat, skipping.\n" in | ||
add_alias (Echo [sv msg]) | ||
| Some ocamlformat -> | ||
let files = Dir_contents.text_files dir_contents in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This includes generated files. We really need to look into the file tree for the actual files to reformat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I used Path.drop_build_context_exn
which works, is that the correct function to use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep
src/dune_project.ml
Outdated
@@ -215,6 +215,7 @@ module Extension = struct | |||
} | |||
|
|||
let extensions = Hashtbl.create 32 | |||
let instanciated_extensions = Hashtbl.create 32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be per-project, otherwise it won't work when there are several projects in the same workspace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's now stored in the project.
BTW, could you benchmark this change? The platform repository should be a good test case. Given that this more work that we will do systematically, we need to know what the cost is. |
Yes, I'm adding a cache so that we only call which on each formatter as well. But a benchmark will be useful! |
c8fa4c8
to
08e74f2
Compare
I:
Next I'll add the configuration syntax and make the instanciation table per project. |
08e74f2
to
f97e929
Compare
About testing: rather than installing ocamlformat & refmt, I was thinking that I could bring "fake" binaries in PATH (e.g. prepending each line with a prefix + displaying sys.argv in a header), what do you think about that? |
Ah ok, I get why we pass the file in the source tree via |
We can use |
|
Sure, sorry about the nitpicking. |
6f911d3
to
56f1e98
Compare
No problem! I filed two bugs so that I don't forget about this. |
This adds a "fmt" extension in dune-project files. When used, it will setup a `@fmt` alias that will call `ocamlformat` on ocaml source code, and `refmt` on reason source code. The tools are not configured by dune. Closes #1201 Signed-off-by: Etienne Millon <me@emillon.org>
Thanks for the thorough review! |
No problem, should we switch Dune to ocamlformat then? :) |
Yes! I've been running some tests about which configuration yields the smallest diff. I think our style corresponds to the "sparse" profile with some minor addition. I'll run the tests and post the results in a new issue. |
CHANGES: - Do not fail if the output of `ocamlc -config` doesn't include `standard_runtime` (ocaml/dune#1326, @diml) - Let `Configurator.V1.C_define.import` handle negative integers (ocaml/dune#1334, @Chris00) - Re-execute actions when a target is modified by the user inside `_build` (ocaml/dune#1343, fix ocaml/dune#1342, @diml) - Pass `--set-switch` to opam (ocaml/dune#1341, fix ocaml/dune#1337, @diml) - Fix bad interaction between multi-directory libraries the `menhir` stanza (ocaml/dune#1373, fix ocaml/dune#1372, @diml) - Integration with automatic formatters (ocaml/dune#1252, fix ocaml/dune#1201, @emillon) - Better error message when using `(self_build_stubs_archive ...)` and `(c_names ...)` or `(cxx_names ...)` simultaneously. (ocaml/dune#1375, fix ocaml/dune#1306, @nojb) - Improve name detection for packages when the prefix isn't an actual package (ocaml/dune#1361, fix ocaml/dune#1360, @rgrinberg) - Support for new menhir rules (ocaml/dune#863, fix ocaml/dune#305, @fpottier, @rgrinberg) - Do not remove flags when compiling compatibility modules for wrapped mode (ocaml/dune#1382, fix ocaml/dune#1364, @rgrinberg) - Fix reason support when using `staged_pps` (ocaml/dune#1384, @charlesetc) - Add support for `enabled_if` in `rule`, `menhir`, `ocamllex`, `ocamlyacc` (ocaml/dune#1387, @diml) - Exit gracefully when a signal is received (ocaml/dune#1366, @diml) - Load all defined libraries recursively into utop (ocaml/dune#1384, fix ocaml/dune#1344, @rgrinberg) - Allow to use libraries `bytes`, `result` and `uchar` without `findlib` installed (ocaml/dune#1391, @nojb)
CHANGES: - Do not fail if the output of `ocamlc -config` doesn't include `standard_runtime` (ocaml/dune#1326, @diml) - Let `Configurator.V1.C_define.import` handle negative integers (ocaml/dune#1334, @Chris00) - Re-execute actions when a target is modified by the user inside `_build` (ocaml/dune#1343, fix ocaml/dune#1342, @diml) - Pass `--set-switch` to opam (ocaml/dune#1341, fix ocaml/dune#1337, @diml) - Fix bad interaction between multi-directory libraries the `menhir` stanza (ocaml/dune#1373, fix ocaml/dune#1372, @diml) - Integration with automatic formatters (ocaml/dune#1252, fix ocaml/dune#1201, @emillon) - Better error message when using `(self_build_stubs_archive ...)` and `(c_names ...)` or `(cxx_names ...)` simultaneously. (ocaml/dune#1375, fix ocaml/dune#1306, @nojb) - Improve name detection for packages when the prefix isn't an actual package (ocaml/dune#1361, fix ocaml/dune#1360, @rgrinberg) - Support for new menhir rules (ocaml/dune#863, fix ocaml/dune#305, @fpottier, @rgrinberg) - Do not remove flags when compiling compatibility modules for wrapped mode (ocaml/dune#1382, fix ocaml/dune#1364, @rgrinberg) - Fix reason support when using `staged_pps` (ocaml/dune#1384, @charlesetc) - Add support for `enabled_if` in `rule`, `menhir`, `ocamllex`, `ocamlyacc` (ocaml/dune#1387, @diml) - Exit gracefully when a signal is received (ocaml/dune#1366, @diml) - Load all defined libraries recursively into utop (ocaml/dune#1384, fix ocaml/dune#1344, @rgrinberg) - Allow to use libraries `bytes`, `result` and `uchar` without `findlib` installed (ocaml/dune#1391, @nojb) - Take argument to self_build_stubs_archive into account. (ocaml/dune#1395, @nojb) - Fix bad interaction between `env` customization and vendored projects: when a vendored project didn't have its own `env` stanza, the `env` stanza from the enclosing project was in effect (ocaml/dune#1408, @diml) - Fix stop early bug when scanning for watermarks (ocaml/dune#1423, @diml)
This adds a "fmt" extension in dune-project files. When used, it will setup a `@fmt` alias that will call `ocamlformat` on ocaml source code, and `refmt` on reason source code. The tools are not configured by dune. Closes ocaml#1201 Signed-off-by: Etienne Millon <me@emillon.org>
I've mentioned that in a comment already (#1252 (comment)). |
For generated files that are not promoted, we should probably disable auto-formatting automatically. |
Actually, looking at the code that's already the case, so there should be no need to disable ocamlformat on files generated by ocamllex, yacc, ... |
My use case is with lexers and parsers files that are promoted. |
This adds a "fmt" extension in dune-project files. When used, it will setup a
@fmt
alias that will callocamlformat
on ocaml source code, andrefmt
on reason source code. The tools are not configured by dune.📝 TODO
pass(superseded by dependencies)--name