Skip to content

Commit

Permalink
add support of dune
Browse files Browse the repository at this point in the history
  • Loading branch information
Khady committed Feb 10, 2023
1 parent 85e7373 commit e2f3c47
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ code formatters.
`merge-fmt` currently only knows about the following formatters:
- [ocamlformat](https://github.com/ocaml-ppx/ocamlformat) for OCaml.
- [refmt](https://github.com/facebook/reason) for reason.
- [dune](https://github.com/ocaml/dune) for dune.

Note that supporting new code formatters is trivial.

Expand Down Expand Up @@ -47,4 +48,4 @@ Install
-------
```sh
$ opam pin add merge-fmt git@github.com:hhugo/merge-fmt.git
```
```
3 changes: 3 additions & 0 deletions merge-fmt-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ COMMANDS
Register the [merge-fmt] mergetool in git

OPTIONS
--dune=VAL
dune path

--echo
Echo all commands.

Expand Down
3 changes: 3 additions & 0 deletions merge-fmt-mergetool-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ OPTIONS

--current=<current-file>

--dune=VAL
dune path

--echo
Echo all commands.

Expand Down
24 changes: 18 additions & 6 deletions src/fmters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open Common
type config =
{ ocamlformat_path : string option
; refmt_path : string option
; dune_path : string option
}

type t = string
Expand All @@ -15,12 +16,19 @@ let ocamlformat ~bin ~name =

let refmt ~bin = sprintf "%s --inplace" (Option.value ~default:"refmt" bin)

let dune ~bin ~filename =
sprintf "%s format-dune-file %s > %s"
(Option.value ~default:"dune" bin)
filename filename

let find ~config ~filename ~name =
let filename = Option.value ~default:filename name in
match (Caml.Filename.extension filename, config) with
| (".ml" | ".mli"), { ocamlformat_path; _ } ->
match (filename, Caml.Filename.extension filename, config) with
| _, (".ml" | ".mli"), { ocamlformat_path; _ } ->
Some (ocamlformat ~bin:ocamlformat_path ~name)
| (".re" | ".rei"), { refmt_path; _ } -> Some (refmt ~bin:refmt_path)
| _, (".re" | ".rei"), { refmt_path; _ } -> Some (refmt ~bin:refmt_path)
| ("dune" | "dune-project" | "dune-workspace"), "", { dune_path; _ } ->
Some (dune ~bin:dune_path ~filename)
| _ -> None

let run t ~echo ~filename = system ~echo "%s %s" t filename
Expand All @@ -36,9 +44,13 @@ module Flags = struct
let doc = "refmt path" in
Arg.(value & opt (some string) None & info [ "refmt" ] ~doc)

let dune_path =
let doc = "dune path" in
Arg.(value & opt (some string) None & info [ "dune" ] ~doc)

let t =
Term.(
const (fun ocamlformat_path refmt_path ->
{ ocamlformat_path; refmt_path })
$ ocamlformat_path $ refmt_path)
const (fun ocamlformat_path refmt_path dune_path ->
{ ocamlformat_path; refmt_path; dune_path })
$ ocamlformat_path $ refmt_path $ dune_path)
end

0 comments on commit e2f3c47

Please sign in to comment.