Skip to content

Commit

Permalink
fix: better printing of Pexp_extension (#2814)
Browse files Browse the repository at this point in the history
* fix: better printing of `Pexp_extension`

* chore: add changelog entry
  • Loading branch information
anmonteiro authored Nov 29, 2024
1 parent a83a096 commit 20bd5ca
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[#2800](https://github.com/reasonml/reason/pull/2800))
- Fix: don't print all extension strings as quoted extensions (@anmonteiro,
[#2809](https://github.com/reasonml/reason/pull/2809))
- Fix: unify printing of extensions across structure items / expressions
(@anmonteiro, [#2814](https://github.com/reasonml/reason/pull/2814))

## 3.13.0

Expand Down
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

0 comments on commit 20bd5ca

Please sign in to comment.