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

Fix printing of externals that happen to have newlines/quotes in them. #2593

Merged
merged 1 commit into from
Jun 18, 2020
Merged
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
29 changes: 28 additions & 1 deletion formatTest/unit_tests/expected_output/externals.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,31 @@
external foo: type_ = "%caml_something_or_other";

external multilineStringExtern: int => int =
"\n Did you know you can put whatver you want inside\n of an extern? Good luck with the linker though!\n";
{|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};

module Nested = {
external multilineStringExtern: int => int =
{|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};
external multilineStringExternWithTag:
int => int =
{|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};
external multilineStringExtern: int => int =
{|
And this has a newline in it, so will be formatted with { | | } style string|};
external containsQuote: int => int =
{|This has a quote in it " so will be formatted as { | | } style string|};
external noIndentation: int => int =
{|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};
};
18 changes: 18 additions & 0 deletions formatTest/unit_tests/input/externals.re
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,21 @@ external multilineStringExtern : int => int = {|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};

module Nested = {
external multilineStringExtern : int => int = {|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};
external multilineStringExternWithTag : int => int = {js|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|js};
external multilineStringExtern : int => int = "
And this has a newline in it, so will be formatted with { | | } style string";
external containsQuote : int => int = "This has a quote in it \" so will be formatted as { | | } style string";
external noIndentation : int => int = {|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};
};
13 changes: 9 additions & 4 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1991,8 +1991,12 @@ let paren b fu ppf x =
then Format.fprintf ppf "(%a)" fu x
else fu ppf x

let constant_string ppf s =
Format.fprintf ppf "%S" s
let constant_string_for_primitive ppf s =
let hasQuote = try String.index s '"' with Not_found -> -1 in
let hasNewline = try String.index s '\n' with Not_found -> -1 in
if hasQuote > -1 || hasNewline > -1 then
Format.fprintf ppf "{|%s|}" s
else Format.fprintf ppf "%S" s

let tyvar ppf str =
Format.fprintf ppf "'%s" str
Expand Down Expand Up @@ -2430,7 +2434,7 @@ let printer = object(self:'self)
method constant ?raw_literal ?(parens=true) =
wrap (constant ?raw_literal ~parens)

method constant_string = wrap constant_string
method constant_string_for_primitive = wrap constant_string_for_primitive
method tyvar = wrap tyvar

(* c ['a,'b] *)
Expand Down Expand Up @@ -6684,7 +6688,8 @@ let printer = object(self:'self)
| [""] -> lblBefore
| _ ->
let frstHalf = makeList ~postSpace:true [lblBefore; atom "="] in
let sndHalf = makeSpacedBreakableInlineList (List.map self#constant_string vd.pval_prim) in
let sndHalf =
makeSpacedBreakableInlineList (List.map self#constant_string_for_primitive vd.pval_prim) in
label ~space:true frstHalf sndHalf
in
match vd.pval_attributes with
Expand Down