Skip to content

Commit fca20da

Browse files
authored
Add format rules for dune files (#1821)
* Rename unstable-fmt to format-dune-file Signed-off-by: Etienne Millon <[email protected]> * Format rules: in 1.1, format dune files Signed-off-by: Etienne Millon <[email protected]> * Remove --inplace option for format-dune-file This command is to be only used with formatting rules, where fixing files is done using promotion. Signed-off-by: Etienne Millon <[email protected]> * Rename Dune_fmt to Format_dune_lang Signed-off-by: Etienne Millon <[email protected]>
1 parent 8607059 commit fca20da

File tree

22 files changed

+161
-152
lines changed

22 files changed

+161
-152
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ unreleased
134134
necessary until virtual libraries were supported (#1822, fix #1816,
135135
@diml)
136136

137+
- Rename `unstable-fmt` to `format-dune-file` and remove its `--inplace` option.
138+
(#1821, @emillon).
139+
140+
- Autoformatting: `(using fmt 1.1)` will also format dune files (#1821, @emillon).
141+
137142
1.6.2 (05/12/2018)
138143
------------------
139144

bin/fmt_cmd.ml

Lines changed: 0 additions & 40 deletions
This file was deleted.

bin/format_dune_file.ml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
open Import
2+
open Stdune
3+
4+
module Format_dune_lang = Dune.Format_dune_lang
5+
6+
let doc = "Format dune files"
7+
8+
let man =
9+
[ `S "DESCRIPTION"
10+
; `P {|$(b,dune format-dune-file) reads a dune file and outputs a formatted
11+
version. |}
12+
]
13+
14+
let info = Term.info "format-dune-file" ~doc ~man
15+
16+
let term =
17+
let%map path_opt =
18+
let docv = "FILE" in
19+
let doc = "Path to the dune file to parse." in
20+
Arg.(value & pos 0 (some path) None & info [] ~docv ~doc)
21+
in
22+
let input = Option.map ~f:Arg.Path.path path_opt in
23+
Format_dune_lang.format_file ~input
24+
25+
let command = term, info
File renamed without changes.

bin/main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ let all =
141141
; promote
142142
; Printenv.command
143143
; Help.command
144-
; Fmt_cmd.command
144+
; Format_dune_file.command
145145
; Compute.command
146146
; Upgrade.command
147147
]

doc/dune.inc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@
4444
(package dune)
4545
(files dune-external-lib-deps.1))
4646

47+
(rule
48+
(with-stdout-to dune-format-dune-file.1
49+
(run dune format-dune-file --help=groff)))
50+
51+
(install
52+
(section man)
53+
(package dune)
54+
(files dune-format-dune-file.1))
55+
4756
(rule
4857
(with-stdout-to dune-help.1
4958
(run dune help --help=groff)))
@@ -125,15 +134,6 @@
125134
(package dune)
126135
(files dune-uninstall.1))
127136

128-
(rule
129-
(with-stdout-to dune-unstable-fmt.1
130-
(run dune unstable-fmt --help=groff)))
131-
132-
(install
133-
(section man)
134-
(package dune)
135-
(files dune-unstable-fmt.1))
136-
137137
(rule
138138
(with-stdout-to dune-upgrade.1
139139
(run dune upgrade --help=groff)))

doc/formatting.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This feature is enabled by adding the following to the ``dune-project`` file:
1919

2020
.. code:: scheme
2121
22-
(using fmt 1.0)
22+
(using fmt 1.1)
2323
2424
Formatting a project
2525
====================
@@ -63,4 +63,17 @@ the languages that are considered for formatting.
6363

6464
.. code:: scheme
6565
66-
(using fmt 1.0 (enabled_for reason))
66+
(using fmt 1.1 (enabled_for reason))
67+
68+
Version history
69+
===============
70+
71+
1.1
72+
---
73+
74+
* Format Dune files.
75+
76+
1.0
77+
---
78+
79+
* Format OCaml (using ocamlformat_) and Reason (using refmt_) source code.

src/dune_file.ml

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,35 +538,42 @@ module Auto_format = struct
538538
let syntax =
539539
Syntax.create ~name:"fmt"
540540
~desc:"integration with automatic formatters"
541-
[ (1, 0) ]
541+
[ (1, 1) ]
542542

543543
type language =
544544
| Ocaml
545545
| Reason
546+
| Dune
546547

547548
let language_to_sexp = function
548549
| Ocaml -> Sexp.Atom "ocaml"
549550
| Reason -> Sexp.Atom "reason"
551+
| Dune -> Sexp.Atom "dune"
550552

551553
let language =
552554
sum
553555
[ ("ocaml", return Ocaml)
554556
; ("reason", return Reason)
557+
; ("dune",
558+
let%map () = Syntax.since syntax (1, 1) in
559+
Dune)
555560
]
556561

557562
type enabled_for =
558-
| Default
563+
| Default of Syntax.Version.t
559564
| Only of language list
560565

561566
let enabled_for_field =
562-
let%map r = field_o "enabled_for" (repeat language) in
567+
let%map r = field_o "enabled_for" (repeat language)
568+
and version = Syntax.get_exn syntax
569+
in
563570
match r with
564571
| Some l -> Only l
565-
| None -> Default
572+
| None -> Default version
566573

567574
let enabled_for_to_sexp =
568575
function
569-
| Default -> Sexp.Atom "default"
576+
| Default v -> Sexp.List [Atom "default"; Syntax.Version.to_sexp v]
570577
| Only l -> List [Atom "only"; List (List.map ~f:language_to_sexp l)]
571578

572579
type t =
@@ -587,6 +594,25 @@ module Auto_format = struct
587594

588595
let key =
589596
Dune_project.Extension.register syntax dparse_args to_sexp
597+
598+
let enabled_languages config =
599+
match config.enabled_for with
600+
| Default ver ->
601+
let in_1_0 =
602+
[Ocaml; Reason]
603+
in
604+
let extra =
605+
match Syntax.Version.compare ver (1, 1) with
606+
| Lt -> []
607+
| Eq | Gt -> [Dune]
608+
in
609+
in_1_0 @ extra
610+
| Only l -> l
611+
612+
let includes config language =
613+
List.mem language ~set:(enabled_languages config)
614+
615+
let loc t = t.loc
590616
end
591617

592618
module Buildable = struct

src/dune_file.mli

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,15 @@ module Auto_format : sig
9797
type language =
9898
| Ocaml
9999
| Reason
100+
| Dune
100101

101-
type enabled_for =
102-
| Default
103-
| Only of language list
102+
type t
104103

105-
type t =
106-
{ loc : Loc.t
107-
; enabled_for : enabled_for
108-
}
104+
val key : t Dune_project.Extension.t
109105

110-
val syntax : Syntax.t
106+
val loc : t -> Loc.t
111107

112-
val key : t Dune_project.Extension.t
108+
val includes : t -> language -> bool
113109
end
114110

115111
module Buildable : sig

src/dune_fmt.mli

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)