Skip to content

Commit

Permalink
[@@deriving show]: add with_path option, to skip module path.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kakadu authored and whitequark committed Feb 16, 2017
1 parent dd91e6f commit 14df45f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ It is also possible to use `[@polyprinter]`. The difference is that for a type `

The function `fprintf` is locally defined in the printer.

By default all constructors are printed with prefix which is dot-separated filename and module path. For example
``` ocaml
# module X = struct type t = C [@@deriving show] end;;
...
# X.(show C);;
- : Ppx_deriving_runtime.string = "X.C"
```

This code will create printers which return the string `X.C`, `X` is a module path and `C` is a constructor name. File's name is omitted in the toplevel. To skip all module paths the one needs to derive show with option `with_path` (which defaults to `true`)

``` ocaml
# module X = struct type t = C [@@deriving show { with_path = false }] end;;
...
# X.(show C);;
- : Ppx_deriving_runtime.string = "C"
```


Plugins: eq and ord
-------------------

Expand Down
21 changes: 15 additions & 6 deletions src_plugins/ppx_deriving_show.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ open Ast_convenience
let deriver = "show"
let raise_errorf = Ppx_deriving.raise_errorf

type options = { with_path : bool }

let parse_options options =
let with_path = ref true in
options |> List.iter (fun (name, expr) ->
match name with
| _ -> raise_errorf ~loc:expr.pexp_loc "%s does not support option %s" deriver name)
| "with_path" -> with_path := Ppx_deriving.Arg.(get_expr ~deriver bool) expr
| _ -> raise_errorf ~loc:expr.pexp_loc "%s does not support option %s" deriver name);
{ with_path = !with_path }

let attr_nobuiltin attrs =
Ppx_deriving.(attrs |> attr ~deriver "nobuiltin" |> Arg.get_flag ~deriver)
Expand All @@ -42,23 +47,23 @@ let wrap_printer quoter printer =
[%expr (let fprintf = Format.fprintf in [%e printer]) [@ocaml.warning "-26"]]

let pp_type_of_decl ~options ~path type_decl =
parse_options options;
let _ = parse_options options in
let typ = Ppx_deriving.core_type_of_type_decl type_decl in
Ppx_deriving.poly_arrow_of_type_decl
(fun var -> [%type: Format.formatter -> [%t var] -> Ppx_deriving_runtime.unit])
type_decl
[%type: Format.formatter -> [%t typ] -> Ppx_deriving_runtime.unit]

let show_type_of_decl ~options ~path type_decl =
parse_options options;
let _ = parse_options options in
let typ = Ppx_deriving.core_type_of_type_decl type_decl in
Ppx_deriving.poly_arrow_of_type_decl
(fun var -> [%type: Format.formatter -> [%t var] -> Ppx_deriving_runtime.unit])
type_decl
[%type: [%t typ] -> Ppx_deriving_runtime.string]

let sig_of_type ~options ~path type_decl =
parse_options options;
let _ = parse_options options in
[Sig.value (Val.mk (mknoloc (Ppx_deriving.mangle_type_decl (`Prefix "pp") type_decl))
(pp_type_of_decl ~options ~path type_decl));
Sig.value (Val.mk (mknoloc (Ppx_deriving.mangle_type_decl (`Prefix "show") type_decl))
Expand Down Expand Up @@ -182,7 +187,7 @@ let rec expr_of_typ quoter typ =
deriver (Ppx_deriving.string_of_core_type typ)

let str_of_type ~options ~path ({ ptype_loc = loc } as type_decl) =
parse_options options;
let show_opts = parse_options options in
let quoter = Ppx_deriving.create_quoter () in
let path = Ppx_deriving.path_of_type_decl ~path type_decl in
let prettyprinter =
Expand All @@ -192,7 +197,11 @@ let str_of_type ~options ~path ({ ptype_loc = loc } as type_decl) =
| Ptype_variant constrs, _ ->
let cases =
constrs |> List.map (fun { pcd_name = { txt = name' }; pcd_args; pcd_attributes } ->
let constr_name = Ppx_deriving.expand_path ~path name' in
let constr_name =
let path = if show_opts.with_path then path else [] in
Ppx_deriving.expand_path ~path name'
in

match attr_printer pcd_attributes, pcd_args with
| Some printer, Pcstr_tuple(args) ->
let rec range from_idx to_idx =
Expand Down
12 changes: 12 additions & 0 deletions src_test/test_deriving_show.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ let test_variant_printer ctxt =
assert_equal ~printer
"fourth: 8 4" (show_variant_printer (Fourth(8,4)))

type no_full = NoFull of int [@@deriving show { with_path = false }]
type with_full = WithFull of int [@@deriving show { with_path = true }]
module WithFull = struct
type t = A of int [@@deriving show ]
end
let test_paths_printer ctxt =
assert_equal ~printer "(NoFull 1)" (show_no_full (NoFull 1));
assert_equal ~printer "(Test_deriving_show.WithFull 1)" (show_with_full (WithFull 1));
assert_equal ~printer "(Test_deriving_show.WithFull.A 1)" (WithFull.show (WithFull.A 1));
()

let suite = "Test deriving(show)" >::: [
"test_alias" >:: test_alias;
"test_variant" >:: test_variant;
Expand All @@ -232,5 +243,6 @@ let suite = "Test deriving(show)" >::: [
"test_std_shadowing" >:: test_std_shadowing;
"test_poly_app" >:: test_poly_app;
"test_variant_printer" >:: test_variant_printer;
"test_paths" >:: test_paths_printer;
"test_result" >:: test_result
]

0 comments on commit 14df45f

Please sign in to comment.