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: better printing of Pexp_extension #2814

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: better printing of Pexp_extension
  • Loading branch information
anmonteiro committed Nov 25, 2024
commit 759e85ef5bb2d2fcd8c379f96f78550adbbe22f0
11 changes: 4 additions & 7 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7928,14 +7928,11 @@ let createFormatter () =
| None, [] -> toThis
| Some id, _ ->
makeList
~wrap:("[", "]")
~postSpace:true
~indent:0
~wrap:("[%" ^ id.txt, "]")
~indent:1
~pad:(true, false)
~break:Layout.IfNeed
~inline:(true, true)
([ atom ("%" ^ id.txt) ]
@ List.map self#item_attribute l
@ [ toThis ])
(List.map self#item_attribute l @ [ toThis ])
| None, _ ->
makeList
~postSpace:true
Expand Down
37 changes: 37 additions & 0 deletions test/extension-exprs.t/input.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Extension with pexp_apply */

[%defer
cleanup();
];

/* Extension with comment with pexp_apply */

[%defer
/* 2. comment attached to expr in extension */
cleanup();
];

/* Let sequence + extension with pexp_apply */

let () = {
/* random let binding */
let x = 1;
/* 1. comment attached to extension */
[%defer cleanup()];
/* 3. comment attached to next expr */
something_else();
};

/* Let sequence + extension with comment with pexp_apply */

let () = {
/* random let binding */
let x = 1;
/* 1. comment attached to extension */
[%defer
/* 2. comment attached to expr in extension */
cleanup()
];
/* 3. comment attached to next expr */
something_else();
};
38 changes: 38 additions & 0 deletions test/extension-exprs.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

Format extensions
$ refmt ./input.re
/* Extension with pexp_apply */

[%defer cleanup()];

/* Extension with comment with pexp_apply */

[%defer
/* 2. comment attached to expr in extension */
cleanup()
];

/* Let sequence + extension with pexp_apply */

let () = {
/* random let binding */
let x = 1;
/* 1. comment attached to extension */
[%defer cleanup()];
/* 3. comment attached to next expr */
something_else();
};

/* Let sequence + extension with comment with pexp_apply */

let () = {
/* random let binding */
let x = 1;
/* 1. comment attached to extension */
[%defer
/* 2. comment attached to expr in extension */
cleanup()
];
/* 3. comment attached to next expr */
something_else();
};
66 changes: 44 additions & 22 deletions test/extensions.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ Format extensions
[%extend1
try%extend2() {
| _ => ()
}];
}
];
};

let x = {
[%extend1
switch%extend2 () {
| _ => ()
}];
}
];
};

let x = {
Expand All @@ -131,14 +133,16 @@ Format extensions
[%extend1
for%extend2 (i in 1 to 10) {
();
}];
}
];
};

let x = {
[%extend1
while%extend2 (false) {
();
}];
}
];
};

let x = {
Expand All @@ -149,7 +153,8 @@ Format extensions
[%extend1
fun%extend2
| None => ()
| Some(1) => ()];
| Some(1) => ()
];
};

/* With two extensions, first in sequence */
Expand All @@ -158,7 +163,8 @@ Format extensions
[%extend1
try%extend2() {
| _ => ()
}];
}
];
ignore();
};

Expand All @@ -167,7 +173,8 @@ Format extensions
[%extend1
switch%extend2 () {
| _ => ()
}];
}
];
ignore();
};

Expand All @@ -182,7 +189,8 @@ Format extensions
[%extend1
for%extend2 (i in 1 to 10) {
();
}];
}
];
ignore();
};

Expand All @@ -191,7 +199,8 @@ Format extensions
[%extend1
while%extend2 (false) {
();
}];
}
];
ignore();
};

Expand All @@ -206,7 +215,8 @@ Format extensions
[%extend1
fun%extend2
| None => ()
| Some(1) => ()];
| Some(1) => ()
];
};

/* With two extensions, in sequence */
Expand All @@ -216,7 +226,8 @@ Format extensions
[%extend1
try%extend2() {
| _ => ()
}];
}
];
ignore();
};

Expand All @@ -225,7 +236,8 @@ Format extensions
[%extend1
switch%extend2 () {
| _ => ()
}];
}
];
ignore();
};

Expand All @@ -240,7 +252,8 @@ Format extensions
[%extend1
for%extend2 (i in 1 to 10) {
();
}];
}
];
ignore();
};

Expand All @@ -249,7 +262,8 @@ Format extensions
[%extend1
while%extend2 (false) {
();
}];
}
];
ignore();
};

Expand All @@ -264,7 +278,8 @@ Format extensions
[%extend1
fun%extend2
| None => ()
| Some(1) => ()];
| Some(1) => ()
];
ignore();
};

Expand All @@ -275,15 +290,17 @@ Format extensions
[%extend1
try%extend2() {
| _ => ()
}];
}
];
};

let x = {
ignore();
[%extend1
switch%extend2 () {
| _ => ()
}];
}
];
};

let x = {
Expand All @@ -296,15 +313,17 @@ Format extensions
[%extend1
for%extend2 (i in 1 to 10) {
();
}];
}
];
};

let x = {
ignore();
[%extend1
while%extend2 (false) {
();
}];
}
];
};

let x = {
Expand All @@ -317,7 +336,8 @@ Format extensions
[%extend1
fun%extend2
| None => ()
| Some(1) => ()];
| Some(1) => ()
];
};

let _ =
Expand All @@ -333,7 +353,8 @@ Format extensions
/* 1. comment attached to extension */
[%defer
/* 2. comment attached to expr in extension */
cleanup()];
cleanup()
];
/* 3. comment attached to next expr */
something_else();
};
Expand All @@ -345,7 +366,8 @@ Format extensions
/* 1. comment attached to extension */
[%defer
/* 2. comment attached to expr in extension */
cleanup()];
cleanup()
];
/* 3. comment attached to next expr */
something_else();
};
Expand Down