Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
Implement intelligent breaking for poly-var type expressions
Browse files Browse the repository at this point in the history
Fixes #245

Given:
```rescript
type currencyPoly = [#USD
  | #CAD | #EUR]

type currencyPoly = [#USD | #CAD | #EUR]
```
we have two different layouts:

In the first example the user wrote them over multiple lines.
The second has them on one line.
This commit implements a new strategy to print these two examples.
We now look at the style of the author: did he write it over multiple lines or not?

If it is on one line, keep it on one line (unless it breaks the column width).
If it is written over multiple lines, force break it over multiple lines.
This is also consistent with how we print variant declaration.
  • Loading branch information
Iwan committed Jan 22, 2021
1 parent 7415b09 commit 0f72e8c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,7 @@ and printTypExpr (typExpr : Parsetree.core_type) cmtTbl =
| Ptyp_class _ ->
Doc.text "classes are not supported in types"
| Ptyp_variant (rowFields, closedFlag, labelsOpt) ->
let forceBreak = typExpr.ptyp_loc.Location.loc_start.pos_lnum < typExpr.ptyp_loc.loc_end.pos_lnum in
let printRowField = function
| Parsetree.Rtag ({txt}, attrs, true, []) ->
Doc.group (
Expand Down Expand Up @@ -1615,7 +1616,10 @@ and printTypExpr (typExpr : Parsetree.core_type) cmtTbl =
in
let docs = List.map printRowField rowFields in
let cases = Doc.join ~sep:(Doc.concat [Doc.line; Doc.text "| "]) docs in
let cases = if docs = [] then cases else Doc.concat [Doc.ifBreaks (Doc.text "| ") Doc.nil; cases] in
let cases =
if docs = [] then cases
else Doc.concat [Doc.ifBreaks (Doc.text "| ") Doc.nil; cases]
in
let openingSymbol =
if closedFlag = Open
then Doc.concat [Doc.greaterThan; Doc.line]
Expand All @@ -1628,10 +1632,14 @@ and printTypExpr (typExpr : Parsetree.core_type) cmtTbl =
| Some([]) ->
Doc.nil
| Some(labels) ->
Doc.concat (List.map (fun label -> Doc.concat [Doc.line; Doc.text "#" ; printPolyVarIdent label] ) labels)
Doc.concat (
List.map (fun label ->
Doc.concat [Doc.line; Doc.text "#" ; printPolyVarIdent label]
) labels
)
in
let closingSymbol = if hasLabels then Doc.text " >" else Doc.nil in
Doc.group (
Doc.breakableGroup ~forceBreak (
Doc.concat [
Doc.lbracket;
Doc.indent (
Expand Down
28 changes: 26 additions & 2 deletions tests/printer/typexpr/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,10 @@ let id = (x: [> #Red | #Green | #Blue]) => x
let upper = (x: [< #Red | #Green]) => true
type point = [#Point(float, float)]
type \\\\\\"type\\" = [#\\"Point🗿\\"(\\\\\\"let\\", float)]
type shape = [#Rectangle(point, point) | #Circle(point, float)]
type shape = [
| #Rectangle(point, point)
| #Circle(point, float)
]
type madness = [< #\\"type\\" & (\\\\\\"let\\") & (\\\\\\"Super exotic\\") | #\\"Bad Idea\\"]
Expand Down Expand Up @@ -648,7 +651,11 @@ type x = [
| #Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz
]
type animation = [#\\"ease-in\\" | #\\"ease-out\\" | #\\"never ease ✍️\\"]
type animation = [
| #\\"ease-in\\"
| #\\"ease-out\\"
| #\\"never ease ✍️\\"
]
module type Conjunctive = {
type u1 = [#A | #B]
Expand All @@ -663,5 +670,22 @@ module type Conjunctive = {
& ([< #\\"Exotic-u1+++\\"])
] => unit
}
// should break because user wrote it over serveral lines
type currencyPoly = [
| #USD
| #CAD
| #EUR
]
// should not break, user wrote it on one line
type currencyPoly = [#USD | #CAD | #EUR]
//
// should break, line length exceeded
type currencyPoly = [
| #UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUSD
| #CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD
| #EUUUUUUUUUUUUUUUUUUUR
]
"
`;
11 changes: 11 additions & 0 deletions tests/printer/typexpr/variant.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ module type Conjunctive = {
let g: [< | #S&([< u2]) & ([< u2]) & ([< u1])] => unit
let g: [< | #"Exotic-S+"&([< #"Exotic-u2+"]) & ([< #"Exotic-u2-"]) & ([< #"Exotic-u1+++"])] => unit
};


// should break because user wrote it over serveral lines
type currencyPoly = [#USD
| #CAD | #EUR]

// should not break, user wrote it on one line
type currencyPoly = [#USD | #CAD | #EUR]
//
// should break, line length exceeded
type currencyPoly = [#UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUSD | #CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD | #EUUUUUUUUUUUUUUUUUUUR]

0 comments on commit 0f72e8c

Please sign in to comment.