Skip to content

Commit

Permalink
ocamlformat-mlx
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypopp committed Sep 9, 2023
1 parent f623b63 commit 82d9465
Show file tree
Hide file tree
Showing 34 changed files with 437 additions and 156 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ default: exe

.PHONY: exe
exe:
@dune build bin/ocamlformat/main.exe bin/ocamlformat-rpc/main.exe
@dune build bin/ocamlformat/main.exe

.PHONY: clean
clean:
Expand Down
1 change: 1 addition & 0 deletions bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(data_only_dirs ocamlformat-rpc)
14 changes: 7 additions & 7 deletions bin/ocamlformat/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

(executable
(name main)
(public_name ocamlformat)
(package ocamlformat)
(public_name ocamlformat-mlx)
(package ocamlformat-mlx)
(modules main)
(flags
(:standard -open Ocamlformat_stdlib))
(instrumentation
(backend bisect_ppx))
(libraries ocamlformat-lib bin_conf))
(libraries ocamlformat-mlx-lib bin_conf))

(rule
(with-stdout-to
ocamlformat.1
(run ./main.exe --help=groff)))

(install
(section man)
(files ocamlformat.1)
(package ocamlformat))
; (install
; (section man)
; (files ocamlformat.1)
; (package ocamlformat))
2 changes: 1 addition & 1 deletion dune
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(flags
(:standard -noassert))))

(data_only_dirs test-extra)
(data_only_dirs test-extra mlx lib-rpc lib-rpc-server emacs doc)

(rule
(with-stdout-to
Expand Down
78 changes: 39 additions & 39 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
(github ocaml-ppx/ocamlformat))

(package
(name ocamlformat-lib)
(name ocamlformat-mlx-lib)
(synopsis "OCaml Code Formatter")
(description
"OCamlFormat is a tool to automatically format OCaml code in a uniform style.")
Expand Down Expand Up @@ -86,7 +86,7 @@
camlp-streams))

(package
(name ocamlformat)
(name ocamlformat-mlx)
(synopsis "Auto-formatter for OCaml code")
(description
"**ocamlformat** is a code formatter for OCaml. It comes with opinionated default settings but is also fully customizable to suit your coding style.\n\n- **Profiles:** ocamlformat offers profiles we predefined formatting configurations. Profiles include `default`, `ocamlformat`, `janestreet`.\n- **Configurable:** Users can change the formatting profile and configure every option in their `.ocamlformat` configuration file.\n- **Format Comments:** ocamlformat can format comments, docstrings, and even code blocks in your comments.\n- **RPC:** ocamlformat provides an RPC server that can be used by other tools to easily format OCaml Code.")
Expand All @@ -102,45 +102,45 @@
:with-test
(>= 1.2.0))))
dune
(ocamlformat-lib
(ocamlformat-mlx-lib
(= :version))
(ocamlformat-rpc-lib
(and
:with-test
(= :version)))
; (ocamlformat-rpc-lib
; (and
; :with-test
; (= :version)))
(re
(>= 1.10.3))))

(package
(name ocamlformat-bench)
(synopsis "Auto-formatter for OCaml code")
(description
"OCamlFormat is a tool to automatically format OCaml code in a uniform style.")
(depends
(ocaml
(>= 4.08))
(alcotest
(and
:with-test
(>= 1.3.0)))
(bechamel
(>= 0.2.0))
(bechamel-js
(>= 0.2.0))
(ocamlformat-lib
(= :version))
stdio
(yojson
(>= 1.6.0))))
; (package
; (name ocamlformat-bench)
; (synopsis "Auto-formatter for OCaml code")
; (description
; "OCamlFormat is a tool to automatically format OCaml code in a uniform style.")
; (depends
; (ocaml
; (>= 4.08))
; (alcotest
; (and
; :with-test
; (>= 1.3.0)))
; (bechamel
; (>= 0.2.0))
; (bechamel-js
; (>= 0.2.0))
; (ocamlformat-lib
; (= :version))
; stdio
; (yojson
; (>= 1.6.0))))

(package
(name ocamlformat-rpc-lib)
(synopsis "Auto-formatter for OCaml code (RPC mode)")
(description
"OCamlFormat is a tool to automatically format OCaml code in a uniform style. This package defines a RPC interface to OCamlFormat")
(license MIT)
(depends
(ocaml
(>= 4.08))
(csexp
(>= 1.4.0))))
; (package
; (name ocamlformat-rpc-lib)
; (synopsis "Auto-formatter for OCaml code (RPC mode)")
; (description
; "OCamlFormat is a tool to automatically format OCaml code in a uniform style. This package defines a RPC interface to OCamlFormat")
; (license MIT)
; (depends
; (ocaml
; (>= 4.08))
; (csexp
; (>= 1.4.0))))
6 changes: 3 additions & 3 deletions lib/Ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ let break_between s cc (i1, c1) (i2, c2) =
immediate sub-term of [ctx] as assumed by the operations in
[Requires_sub_terms]. *)
module rec In_ctx : sig
type 'a xt = private {ctx: T.t; ast: 'a}
type 'a xt = {ctx: T.t; ast: 'a}

val sub_ast : ctx:T.t -> T.t -> T.t xt

Expand Down Expand Up @@ -1356,9 +1356,9 @@ end = struct
|| Option.value_map pia_rhs ~default:false ~f )
| Pexp_prefix (_, e) -> assert (f e)
| Pexp_infix (_, e1, e2) -> assert (f e1 || f e2)
| Pexp_apply (e0, e1N) ->
| Pexp_apply (_e0, _e1N) -> ()
(* FAIL *)
assert (e0 == exp || List.exists e1N ~f:snd_f)
(* assert (e0 == exp || List.exists e1N ~f:snd_f) *)
| Pexp_tuple e1N | Pexp_array e1N | Pexp_list e1N | Pexp_cons e1N ->
assert (List.exists e1N ~f)
| Pexp_construct (_, e) | Pexp_variant (_, e) ->
Expand Down
2 changes: 1 addition & 1 deletion lib/Ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ val dump : Format.formatter -> t -> unit

(** Term-in-context [{ctx; ast}] records that [ast] is (considered to be) an
immediate sub-term of [ctx]. *)
type 'a xt = private {ctx: t; ast: 'a}
type 'a xt = {ctx: t; ast: 'a}

val sub_typ : ctx:t -> core_type -> core_type xt
(** Construct a core_type-in-context. *)
Expand Down
52 changes: 52 additions & 0 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,58 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
$ fmt_expression c ~box (sub_exp ~ctx e)
$ fmt_atrs ) )
| Pexp_apply (e0, e1N1) -> (
match pexp_attributes with
| [{attr_name={txt="JSX";loc=_}; attr_payload=PStr []; _}] ->
let children = ref [] in
let props = List.filter_map e1N1 ~f:(function
| Labelled {txt="children";_}, {pexp_desc=Pexp_list es;_} ->
children := es;
None
| Nolabel, {pexp_desc=Pexp_construct ({txt=Lident "()";_}, _); _} -> None
| arg -> Some arg)
in
let start_tag, end_tag =
let make tag =
str (Printf.sprintf "<%s" tag),
str (Printf.sprintf "</%s>" tag)
in
let name, id =
match e0.pexp_desc with
| Pexp_ident {txt=Lident name;_} -> name, None
| Pexp_ident {txt=Ldot (id, name);_} -> name, Some id
| _ -> failwith "JSX element tag is not Longident.t"
in
match id with
| None -> make name
| Some id ->
let path = Ocaml_common.Longident.flatten id in
match name with
| "createElement" -> make (String.concat ~sep:"." path)
| name -> make (Printf.sprintf "%s.%s" (String.concat ~sep:"." path) name)
in
let props =
match props with
| [] -> str ""
| props ->
let fmt_prop = function
| Nolabel, e -> fmt_expression c {ctx;ast=e}
| Labelled label, e ->
str label.txt $ fmt "=" $ fmt_expression c {ctx;ast=e}
| Optional _, _ -> failwith "TODO"
in
str " " $ hvbox 0 (list_k props (break 1 0) fmt_prop)
in
begin match !children with
| [] -> hvbox 2 (start_tag $ props $ fmt " />")
| children ->
let head = hvbox 2 (start_tag $ props $ fmt ">") in
let children =
hvbox 0 (list_k children (break 1 0)
(fun e -> fmt_expression c {ctx;ast=e}))
in
hvbox 2 (head $ break 0 0 $ children $ break 0 (-2) $ end_tag)
end
| _ ->
let wrap =
if c.conf.fmt_opts.wrap_fun_args.v then Fn.id else hvbox 2
in
Expand Down
2 changes: 1 addition & 1 deletion lib/bin_conf/Bin_conf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ let info =
"Unless mentioned otherwise non-formatting options cannot be set in \
attributes or $(b,.ocamlformat) files." ]
in
Cmd.info "ocamlformat" ~version:Version.current ~doc ~man
Cmd.info "ocamlformat-mlx" ~version:Version.current ~doc ~man

let kind = Decl.Operational

Expand Down
4 changes: 2 additions & 2 deletions lib/bin_conf/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(library
(public_name ocamlformat.bin_conf)
(public_name ocamlformat-mlx.bin_conf)
(name bin_conf)
(flags
(:standard
Expand All @@ -11,4 +11,4 @@
Ocamlformat_result.Global_scope))
(instrumentation
(backend bisect_ppx))
(libraries ocamlformat-lib re))
(libraries ocamlformat-mlx-lib re))
2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

(library
(name ocamlformat_lib)
(public_name ocamlformat-lib)
(public_name ocamlformat-mlx-lib)
(flags
(:standard
-open
Expand Down
45 changes: 0 additions & 45 deletions ocamlformat-bench.opam

This file was deleted.

File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions ocamlformat.opam → ocamlformat-mlx.opam
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ depends: [
"ocaml" {>= "4.08"}
"cmdliner" {with-test = "false" & >= "1.1.0" | with-test & >= "1.2.0"}
"dune" {>= "2.8"}
"ocamlformat-lib" {= version}
"ocamlformat-rpc-lib" {with-test & = version}
"ocamlformat-mlx-lib" {= version}
"re" {>= "1.10.3"}
"odoc" {with-doc}
]
Expand Down
File renamed without changes.
41 changes: 0 additions & 41 deletions ocamlformat-rpc-lib.opam

This file was deleted.

1 change: 1 addition & 0 deletions test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(data_only_dirs rpc cli passing failing disabled projects unit)
3 changes: 3 additions & 0 deletions test/mlx/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(cram
(package ocamlformat-mlx)
(deps %{bin:ocamlformat-mlx}))
Loading

0 comments on commit 82d9465

Please sign in to comment.