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

Implement intelligent breaking for poly-var type expressions #246

Merged
merged 2 commits into from
Jan 22, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

* Implement intelligent breaking for poly-var type expressions in [#246](https://github.com/rescript-lang/syntax/pull/246)
* Improve indentation of fast pipe chain in let binding in [#244](https://github.com/rescript-lang/syntax/pull/244)
* Improve printing of non-callback arguments in call expressions with callback in [#241](https://github.com/rescript-lang/syntax/pull/241/files)
* Fix printing of constrained expressions in rhs of js objects [#240](https://github.com/rescript-lang/syntax/pull/240)
Expand Down
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]