Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds [%derive.pp] expressions. #287

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src_plugins/show/ppx_deriving_show.ml
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,14 @@ let deriving: Deriving.t =
~str_type_decl:impl_generator
~sig_type_decl:intf_generator

(* custom extension such that "derive"-prefixed also works *)
let derive_extension =
(* custom extensions such that "derive"-prefixed also works *)
let derive_pp_extension =
Extension.V3.declare "derive.pp" Extension.Context.expression
Ast_pattern.(ptyp __) (fun ~ctxt ->
let loc = Expansion_context.Extension.extension_point_loc ctxt in
Ppx_deriving.with_quoter (fun quoter typ ->
[%expr fun fmt -> [%e expr_of_typ quoter typ]]))
let derive_show_extension =
Extension.V3.declare "derive.show" Extension.Context.expression
Ast_pattern.(ptyp __) (fun ~ctxt ->
let loc = Expansion_context.Extension.extension_point_loc ctxt in
Expand All @@ -351,4 +357,7 @@ let derive_extension =
let derive_transformation =
Driver.register_transformation
deriver
~rules:[Context_free.Rule.extension derive_extension]
~rules:[
Context_free.Rule.extension derive_pp_extension;
Context_free.Rule.extension derive_show_extension;
]
10 changes: 10 additions & 0 deletions src_test/show/test_deriving_show.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ let test_result_result ctxt =
assert_equal ~printer "(Test_deriving_show.I_has (Error \"err\"))"
(show_i_has_result_result (I_has (Error "err")))

let test_expr_pp ctxt =
let buf = Buffer.create 16 in
Format.fprintf (Format.formatter_of_buffer buf) "- %a -@?" [%pp: int] 42;
assert_equal ~printer "- 42 -" (Buffer.contents buf)

let test_expr_show ctxt =
assert_equal ~printer "42" ([%show: int] 42)

type es =
| ESBool of (bool [@nobuiltin])
| ESString of (string [@nobuiltin])
Expand Down Expand Up @@ -267,6 +275,8 @@ let suite = "Test deriving(show)" >::: [
"test_paths" >:: test_paths_printer;
"test_result" >:: test_result;
"test_result_result" >:: test_result_result;
"test_expr_pp" >:: test_expr_pp;
"test_expr_show" >:: test_expr_show;
]

let _ = run_test_tt_main suite