Skip to content

Commit

Permalink
Take file kind from --name when formatting stdin (#1119)
Browse files Browse the repository at this point in the history
* Add tests, stdin_and_name failing
* With stdin, take file kind from --name

Closes #1112
  • Loading branch information
Julow authored Nov 4, 2019
1 parent a3521a2 commit 8f7423c
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### (master)

+ Internal: Take file kind from --name when formatting stdin (#1119) (Jules Aguillon)
+ Fix unstabilizing comments on assignments (#1093) (Guillaume Petiot)
+ Internal: Make Fmt.t abstract (#1109) (Jules Aguillon)
+ Improve: give a hint when warning 50 is raised (#1111) (Guillaume Petiot)
Expand Down
29 changes: 19 additions & 10 deletions src/Conf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,12 @@ let (_profile : t option C.t) =

let is_stdin = function Stdin -> true | File _ -> false

let kind_of_ext fname =
match Filename.extension fname with
| ".ml" | ".mlt" -> Some `Impl
| ".mli" -> Some `Intf
| _ -> None

let validate () =
let inputs_len = List.length !inputs in
let has_stdin = List.exists ~f:is_stdin !inputs in
Expand All @@ -1721,11 +1727,14 @@ let validate () =
`Error (false, "Must specify at least one input file, or `-` for stdin")
else if has_stdin && inputs_len > 1 then
`Error (false, "Cannot specify stdin together with other inputs")
else if has_stdin && Option.is_none !kind then
else if
has_stdin && Option.is_none !kind
&& Option.is_none (Option.bind ~f:kind_of_ext !name)
then
`Error
( false
, "Must specify at least one of --impl, --intf or --use-file when \
reading from stdin" )
, "Must specify at least one of --name, --impl or --intf when reading \
from stdin" )
else if has_stdin && !inplace then
`Error (false, "Cannot specify stdin together with --inplace")
else if !inplace && Option.is_some !output then
Expand Down Expand Up @@ -1973,13 +1982,13 @@ let kind_of file =
match !kind with
| Some kind -> kind
| None -> (
match file with
| Stdin -> impossible "checked by validate"
| File fname -> (
match Filename.extension fname with
| ".ml" -> `Impl
| ".mli" -> `Intf
| _ -> `Impl ) )
match Option.bind ~f:kind_of_ext !name with
| Some kind -> kind
| None -> (
match file with
| Stdin -> impossible "checked by validate"
| File fname -> (
match kind_of_ext fname with Some kind -> kind | None -> `Impl ) ) )

let name_of file =
match !name with
Expand Down
30 changes: 30 additions & 0 deletions test/cli/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@
(name runtest)
(action (diff err_stdin_and_inplace.ref err_stdin_and_inplace.output)))

(rule
(targets err_stdin_name_unknown_ext.output)
(action
(with-outputs-to %{targets}
(system "! %{bin:ocamlformat} %{read-lines:err_stdin_name_unknown_ext.opts} < err_stdin_name_unknown_ext.stdin"))))

(alias
(name runtest)
(action (diff err_stdin_name_unknown_ext.ref err_stdin_name_unknown_ext.output)))

(rule
(targets err_stdin_no_kind.output)
(action
Expand All @@ -89,6 +99,16 @@
(name runtest)
(action (diff err_stdin_no_kind.ref err_stdin_no_kind.output)))

(rule
(targets name_unknown_ext.output)
(action
(with-outputs-to %{targets}
(system "%{bin:ocamlformat} %{read-lines:name_unknown_ext.opts}"))))

(alias
(name runtest)
(action (diff name_unknown_ext.ref name_unknown_ext.output)))

(rule
(targets stdin_and_impl.output)
(action
Expand All @@ -108,3 +128,13 @@
(alias
(name runtest)
(action (diff stdin_and_intf.ref stdin_and_intf.output)))

(rule
(targets stdin_and_name.output)
(action
(with-outputs-to %{targets}
(system "%{bin:ocamlformat} %{read-lines:stdin_and_name.opts} < stdin_and_name.stdin"))))

(alias
(name runtest)
(action (diff stdin_and_name.ref stdin_and_name.output)))
2 changes: 1 addition & 1 deletion test/cli/err_stdin_and_inplace.ref
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ocamlformat: Must specify at least one of --impl, --intf or --use-file when reading from stdin
ocamlformat: Must specify at least one of --name, --impl or --intf when reading from stdin
1 change: 1 addition & 0 deletions test/cli/err_stdin_name_unknown_ext.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--name b.cpp -
1 change: 1 addition & 0 deletions test/cli/err_stdin_name_unknown_ext.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocamlformat: Must specify at least one of --name, --impl or --intf when reading from stdin
Empty file.
1 change: 1 addition & 0 deletions test/cli/err_stdin_name_unknown_ext.stdin
2 changes: 1 addition & 1 deletion test/cli/err_stdin_no_kind.ref
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ocamlformat: Must specify at least one of --impl, --intf or --use-file when reading from stdin
ocamlformat: Must specify at least one of --name, --impl or --intf when reading from stdin
1 change: 1 addition & 0 deletions test/cli/name_unknown_ext.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--name b.cpp sample/b.ml
1 change: 1 addition & 0 deletions test/cli/name_unknown_ext.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let () = print_endline A.x
1 change: 1 addition & 0 deletions test/cli/stdin_and_name.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--name a.ml -
1 change: 1 addition & 0 deletions test/cli/stdin_and_name.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x = "Hello World"
1 change: 1 addition & 0 deletions test/cli/stdin_and_name.stdin

0 comments on commit 8f7423c

Please sign in to comment.