Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- run: opam exec -- dune build @install
- run: opam install . --deps-only -t
- run: opam exec -- dune runtest
- run: opam exec -- dune build @fmt
- run: opam config report
- run: opam install .
- run: opam exec ocp-indent --help
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ ocp-build.root*
_obuild
config.log
config.status
ocp-indent
src/indentVersion.ml
*~
version.ocp
Expand All @@ -18,4 +17,4 @@ src/buildable_targets.list
**/.fe.sexp
_build
.merlin
*.install
*.install
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
+ Fix "Missing ‘lexical-binding’ cookie" warning in Emacs (#329, @nojb)
+ Fix a bug where ocp-indent translate LF to CRLF when run on Windows instead
of preserving the input's newlines. (#334, @nojb)
+ Add `ocp-indent-gen-rules` to support `dune fmt` like workflow with ocp-indent
(#333, @NathanReb)

## 1.8.1
* tiny API change to help with the detection of top-level phrase boundaries
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,44 @@ line like:
will enable you to have the indentation after `in` setup to 2 locally on this
file.

## Autoformat files with ocp-indent in dune

`dune fmt` or `dune build @fmt` can be used to format dune and OCaml files
with `ocamlformat`. This can prove a convenient workflow for new projects so we
made it available to `ocp-indent` users as well.

First you need to disable the default formatting rules for OCaml source files
by adding the following to your `dune-project`:
```dune
(formatting (enabled_for dune))
```

`dune fmt` won't try to format your OCaml files with `ocamlformat` from there.

The `ocp-indent` formatting rules need to be enabled on a per-directory basis by
adding the following dune rules to the `dune` file:

```dune
;; Auto indent files with `dune build @fmt`

(subdir
run
(dynamic_include ../rules/dune.ocp-indent))

(subdir
rules
(rule
(deps
(glob_files ../*.{ml,mli}))
(target dune.ocp-indent)
(action
(run ocp-indent-gen-rules -o %{target}))))
```

`ocp-indent-gen-rules` will generate promotion based formatting rules for each
`.ml` and `.mli` files in this folder. You can simply use the same
`dune build @fmt`/`dune promote` workflow as with ocamlformat.


## How does it compare to tuareg ?

Expand Down
10 changes: 5 additions & 5 deletions doc/dune
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(rule
(targets ocp-indent.1)
(action
(with-stdout-to %{targets}
(run %{bin:ocp-indent} --help=groff)))
)
(with-stdout-to
%{targets}
(run %{bin:ocp-indent} --help=groff))))

(install
(section man)
(files ocp-indent.1)
)
(files ocp-indent.1))
2 changes: 2 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

(name ocp-indent)
(version 1.8.1)

(formatting (enabled_for dune))
18 changes: 16 additions & 2 deletions src/ocp-indent-dynlink/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,19 @@
(name ocp_indent_dynlink)
(public_name ocp-indent.dynlink)
(wrapped false)
(libraries findlib dynlink ocp-indent.lexer ocp-indent.utils)
)
(libraries findlib dynlink ocp-indent.lexer ocp-indent.utils))

;; Auto indent files with `dune build @fmt`

(subdir
run
(dynamic_include ../rules/dune.ocp-indent))

(subdir
rules
(rule
(deps
(glob_files ../*.{ml,mli}))
(target dune.ocp-indent)
(action
(run ocp-indent-gen-rules -o %{target}))))
24 changes: 24 additions & 0 deletions src/ocp-indent-gen-rules/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(rule
(with-stdout-to
version.ml
(echo "let v = \"%{version:ocp-indent}\"")))

(executable
(name main)
(public_name ocp-indent-gen-rules)
(libraries cmdliner))

;; Auto indent files with `dune build @fmt`

(subdir
run
(dynamic_include ../rules/dune.ocp-indent))

(subdir
rules
(rule
(deps
(glob_files ../*.{ml,mli}))
(target dune.ocp-indent)
(action
(run ocp-indent-gen-rules -o %{target}))))
82 changes: 82 additions & 0 deletions src/ocp-indent-gen-rules/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
let exe_name = "ocp-indent-gen-rules"

let named f = Cmdliner.Term.(app (const f))

let ignore =
let open Cmdliner.Arg in
let doc = "Comma separated list of files that should not be indented" in
named
(fun x -> `Ignore x)
(value & opt (list string) [] & info ~doc ["ignore"])

let alias =
let open Cmdliner.Arg in
let doc = "The alias for the ocp-indent rules, default is fmt" in
named
(fun x -> `Alias x)
(value & opt string "fmt" & info ~doc ["alias"])

let static =
let open Cmdliner.Arg in
let doc =
"Generate rules for a regular include stanza rather than a dynamic_include"
in
named
(fun x -> `Static x)
(value & flag & info ~doc ["static"])

let output =
let open Cmdliner.Arg in
let docv = "OUTFILE" in
let doc = "Writes rules to $(docv). When absent, writes rules to stdout." in
named
(fun x -> `Output x)
(value & opt (some string) None & info ~doc ~docv ["o"])

let print_rules ~ignore ~alias ~static oc file =
let source_file = if static then file else "../" ^ file in
if List.mem file ignore then
()
else
match Filename.extension file with
| ".ml" | ".mli" ->
let formatted_ext = "fmtd" in
let pf fmt = Printf.fprintf oc (fmt ^^ "\n") in
pf "(rule";
pf " (target %s.%s)" file formatted_ext;
pf " (action (run ocp-indent %%{dep:%s} -o %%{target})))" source_file;
pf "";
pf "(rule";
pf " (alias %s)" alias;
pf " (action (diff %s %s.%s)))" source_file file formatted_ext;
pf ""
| _ -> ()

let with_output ~f output =
match output with
| None -> f stdout
| Some file ->
let oc = open_out file in
f oc;
close_out oc

let run (`Ignore ignore) (`Alias alias) (`Static static)
(`Output output) =
let src_dir = if static then Sys.getcwd () else ".." in
let files = Sys.readdir src_dir in
with_output output
~f:(fun oc ->
Array.sort String.compare files;
Array.iter (print_rules ~ignore ~alias ~static oc) files)

let term =
let open Cmdliner.Term in
const run $ ignore $ alias $ static $ output

let info =
let open Cmdliner in
Cmd.info exe_name ~version:Version.v ~exits:Cmd.Exit.defaults
~doc:"Generate ocp-indent dune rules"

let () =
exit (Cmdliner.Cmd.eval (Cmdliner.Cmd.v info term))
16 changes: 8 additions & 8 deletions src/ocp-indent-lexer/approx_tokens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ type token =
| COLONEQUAL
| COLONGREATER
| COMMA
(* Start of comment from code *)
(* Start of comment from code *)
| COMMENT
(* Start of inline code section within comment: "{[" *)
(* Start of inline code section within comment: "{[" *)
| OCAMLDOC_CODE
(* Start of verbatim section within comment: "{v" *)
(* Start of verbatim section within comment: "{v" *)
| OCAMLDOC_VERB
(* Continuation of comment after a closed ocamldoc code or verb section *)
(* Continuation of comment after a closed ocamldoc code or verb section *)
| COMMENTCONT
| CONSTRAINT
| DO
Expand Down Expand Up @@ -170,13 +170,13 @@ let to_string = function
| COLONEQUAL -> "COLONEQUAL"
| COLONGREATER -> "COLONGREATER"
| COMMA -> "COMMA"
(* Start of comment from code *)
(* Start of comment from code *)
| COMMENT -> "COMMENT"
(* Start of inline code section within comment: "{[" *)
(* Start of inline code section within comment: "{[" *)
| OCAMLDOC_CODE -> "OCAMLDOC_CODE"
(* Start of verbatim section within comment: "{v" *)
(* Start of verbatim section within comment: "{v" *)
| OCAMLDOC_VERB -> "OCAMLDOC_VERB"
(* Continuation of comment after a closed ocamldoc code or verb section *)
(* Continuation of comment after a closed ocamldoc code or verb section *)
| COMMENTCONT -> "COMMENTCONT"
| CONSTRAINT -> "CONSTRAINT"
| DO -> "DO"
Expand Down
18 changes: 16 additions & 2 deletions src/ocp-indent-lexer/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,19 @@
(library
(name ocp_indent_lexer)
(public_name ocp-indent.lexer)
(wrapped false)
)
(wrapped false))

;; Auto indent files with `dune build @fmt`

(subdir
run
(dynamic_include ../rules/dune.ocp-indent))

(subdir
rules
(rule
(deps
(glob_files ../*.{ml,mli}))
(target dune.ocp-indent)
(action
(run ocp-indent-gen-rules --ignore approx_lexer.ml -o %{target}))))
18 changes: 16 additions & 2 deletions src/ocp-indent-lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,19 @@
(wrapped false)
(public_name ocp-indent.lib)
(libraries ocp-indent.utils)
(flags :standard -w -9 -warn-error -57)
)
(flags :standard -w -9 -warn-error -57))

;; Auto indent files with `dune build @fmt`

(subdir
run
(dynamic_include ../rules/dune.ocp-indent))

(subdir
rules
(rule
(deps
(glob_files ../*.{ml,mli}))
(target dune.ocp-indent)
(action
(run ocp-indent-gen-rules -o %{target}))))
Loading
Loading