Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Commit 73572ff

Browse files
committed
Sync outcome printer.
1 parent a9fa49f commit 73572ff

File tree

3 files changed

+75
-90
lines changed

3 files changed

+75
-90
lines changed

src/rescript-editor-support/vendor/res_outcome_printer/res_doc.ml

+34-33
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type t =
1515
| Text of string
1616
| Concat of t list
1717
| Indent of t
18-
| IfBreaks of {yes: t; no: t}
18+
| IfBreaks of {yes: t; no: t; mutable broken: bool} (* when broken is true, treat as the yes branch *)
1919
| LineSuffix of t
2020
| LineBreak of lineStyle
21-
| Group of {shouldBreak: bool; doc: t}
21+
| Group of {mutable shouldBreak: bool; doc: t}
2222
| CustomLayout of t list
2323
| BreakParent
2424

@@ -43,7 +43,7 @@ let rec _concat acc l =
4343
let concat l = Concat(_concat [] l)
4444

4545
let indent d = Indent d
46-
let ifBreaks t f = IfBreaks {yes = t; no = f}
46+
let ifBreaks t f = IfBreaks {yes = t; no = f; broken = false}
4747
let lineSuffix d = LineSuffix d
4848
let group d = Group {shouldBreak = false; doc = d}
4949
let breakableGroup ~forceBreak d = Group {shouldBreak = forceBreak; doc = d}
@@ -66,55 +66,52 @@ let rbracket = Text "]"
6666
let question = Text "?"
6767
let tilde = Text "~"
6868
let equal = Text "="
69-
let trailingComma = IfBreaks {yes = comma; no = nil}
69+
let trailingComma = ifBreaks comma nil
7070
let doubleQuote = Text "\""
7171

7272
let propagateForcedBreaks doc =
7373
let rec walk doc = match doc with
7474
| Text _ | Nil | LineSuffix _ ->
75-
(false, doc)
75+
false
7676
| BreakParent ->
77-
(true, Nil)
77+
true
7878
| LineBreak (Hard | Literal) ->
79-
(true, doc)
79+
true
8080
| LineBreak (Classic | Soft) ->
81-
(false, doc)
81+
false
8282
| Indent children ->
83-
let (childForcesBreak, newChildren) = walk children in
84-
(childForcesBreak, Indent newChildren)
85-
| IfBreaks {yes = trueDoc; no = falseDoc} ->
86-
let (falseForceBreak, falseDoc) = walk falseDoc in
83+
let childForcesBreak = walk children in
84+
childForcesBreak
85+
| IfBreaks ({yes = trueDoc; no = falseDoc} as ib) ->
86+
let falseForceBreak = walk falseDoc in
8787
if falseForceBreak then
88-
let (_, trueDoc) = walk trueDoc in
89-
(true, trueDoc)
88+
let _ = walk trueDoc in
89+
ib.broken <- true;
90+
true
9091
else
91-
let forceBreak, trueDoc = walk trueDoc in
92-
(forceBreak, IfBreaks {yes = trueDoc; no = falseDoc})
93-
| Group {shouldBreak = forceBreak; doc = children} ->
94-
let (childForcesBreak, newChildren) = walk children in
92+
let forceBreak = walk trueDoc in
93+
forceBreak
94+
| Group ({shouldBreak = forceBreak; doc = children} as gr) ->
95+
let childForcesBreak = walk children in
9596
let shouldBreak = forceBreak || childForcesBreak in
96-
(shouldBreak, Group {shouldBreak; doc = newChildren})
97+
gr.shouldBreak <- shouldBreak;
98+
shouldBreak
9799
| Concat children ->
98-
let (forceBreak, newChildren) = List.fold_left (fun (forceBreak, newChildren) child ->
99-
let (childForcesBreak, newChild) = walk child in
100-
(forceBreak || childForcesBreak, newChild::newChildren)
101-
) (false, []) children
102-
in
103-
(forceBreak, Concat (List.rev newChildren))
100+
List.fold_left (fun forceBreak child ->
101+
let childForcesBreak = walk child in
102+
forceBreak || childForcesBreak
103+
) false children
104104
| CustomLayout children ->
105105
(* When using CustomLayout, we don't want to propagate forced breaks
106106
* from the children up. By definition it picks the first layout that fits
107107
* otherwise it takes the last of the list.
108108
* However we do want to propagate forced breaks in the sublayouts. They
109109
* might need to be broken. We just don't propagate them any higher here *)
110-
let children = match walk (Concat children) with
111-
| (_, Concat children) -> children
112-
| _ -> assert false
113-
in
114-
(false, CustomLayout children)
110+
let _ = walk (Concat children) in
111+
false
115112
in
116-
let (_, processedDoc) = walk doc in
117-
processedDoc
113+
let _ = walk doc in
114+
()
118115

119116
(* See documentation in interface file *)
120117
let rec willBreak doc = match doc with
@@ -153,6 +150,7 @@ let fits w stack =
153150
| Break, LineBreak _ -> result := Some true
154151
| _, Group {shouldBreak = true; doc} -> calculate indent Break doc
155152
| _, Group {doc} -> calculate indent mode doc
153+
| _, IfBreaks {yes = breakDoc; broken = true} -> calculate indent mode breakDoc
156154
| Break, IfBreaks {yes = breakDoc} -> calculate indent mode breakDoc
157155
| Flat, IfBreaks {no = flatDoc} -> calculate indent mode flatDoc
158156
| _, Concat docs -> calculateConcat indent mode docs
@@ -180,7 +178,7 @@ let fits w stack =
180178
calculateAll stack
181179

182180
let toString ~width doc =
183-
let doc = propagateForcedBreaks doc in
181+
propagateForcedBreaks doc;
184182
let buffer = MiniBuffer.create 1000 in
185183

186184
let rec process ~pos lineSuffices stack =
@@ -199,6 +197,8 @@ let toString ~width doc =
199197
process ~pos lineSuffices (List.append ops rest)
200198
| Indent doc ->
201199
process ~pos lineSuffices ((ind + 2, mode, doc)::rest)
200+
| IfBreaks {yes = breakDoc; broken = true} ->
201+
process ~pos lineSuffices ((ind, mode, breakDoc)::rest)
202202
| IfBreaks {yes = breakDoc; no = flatDoc} ->
203203
if mode = Break then
204204
process ~pos lineSuffices ((ind, mode, breakDoc)::rest)
@@ -309,6 +309,7 @@ let debug t =
309309
softLine;
310310
text ")";
311311
]
312+
| IfBreaks {yes = trueDoc; broken = true} -> toDoc trueDoc
312313
| IfBreaks {yes = trueDoc; no = falseDoc} ->
313314
group(
314315
concat [

src/rescript-editor-support/vendor/res_outcome_printer/res_outcome_printer.ml

+17-24
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ let printPolyVarIdent txt =
435435
)
436436
]
437437
);
438-
Doc.softLine;
439438
Doc.trailingComma;
439+
Doc.softLine;
440440
Doc.rbrace;
441441
]
442442
)
@@ -678,29 +678,22 @@ let printPolyVarIdent txt =
678678
let constraints = match outTypeDecl.otype_cstrs with
679679
| [] -> Doc.nil
680680
| _ -> Doc.group (
681-
Doc.concat [
682-
Doc.line;
683-
Doc.indent (
684-
Doc.concat [
685-
Doc.hardLine;
686-
Doc.join ~sep:Doc.line (List.map (fun (typ1, typ2) ->
687-
Doc.group (
688-
Doc.concat [
689-
Doc.text "constraint ";
690-
printOutTypeDoc typ1;
691-
Doc.text " =";
692-
Doc.indent (
693-
Doc.concat [
694-
Doc.line;
695-
printOutTypeDoc typ2;
696-
]
697-
)
698-
]
699-
)
700-
) outTypeDecl.otype_cstrs)
701-
]
702-
)
703-
]
681+
Doc.indent (
682+
Doc.concat [
683+
Doc.hardLine;
684+
Doc.join ~sep:Doc.line (List.map (fun (typ1, typ2) ->
685+
Doc.group (
686+
Doc.concat [
687+
Doc.text "constraint ";
688+
printOutTypeDoc typ1;
689+
Doc.text " =";
690+
Doc.space;
691+
printOutTypeDoc typ2;
692+
]
693+
)
694+
) outTypeDecl.otype_cstrs)
695+
]
696+
)
704697
) in
705698
Doc.group (
706699
Doc.concat [

src/rescript-editor-support/vendor/res_outcome_printer/res_token.ml

+24-33
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type t =
4444
| Lazy
4545
| Tilde
4646
| Question
47-
| If | Else | For | In | To | Downto | While | Switch
47+
| If | Else | For | In | While | Switch
4848
| When
4949
| EqualGreater | MinusGreater
5050
| External
@@ -55,7 +55,6 @@ type t =
5555
| Include
5656
| Module
5757
| Of
58-
| With
5958
| Land | Lor
6059
| Band (* Bitwise and: & *)
6160
| BangEqual | BangEqualEqual
@@ -131,8 +130,6 @@ let toString = function
131130
| Else -> "else"
132131
| For -> "for"
133132
| In -> "in"
134-
| To -> "to"
135-
| Downto -> "downto"
136133
| While -> "while"
137134
| Switch -> "switch"
138135
| When -> "when"
@@ -145,7 +142,6 @@ let toString = function
145142
| Include -> "include"
146143
| Module -> "module"
147144
| Of -> "of"
148-
| With -> "with"
149145
| Lor -> "||"
150146
| Band -> "&" | Land -> "&&"
151147
| BangEqual -> "!=" | BangEqualEqual -> "!=="
@@ -164,48 +160,43 @@ let toString = function
164160
| Export -> "export"
165161

166162
let keywordTable = function
167-
| "true" -> True
168-
| "false" -> False
169-
| "open" -> Open
170-
| "let" -> Let
171-
| "rec" -> Rec
172163
| "and" -> And
173164
| "as" -> As
174-
| "exception" -> Exception
175165
| "assert" -> Assert
176-
| "lazy" -> Lazy
177-
| "if" -> If
166+
| "constraint" -> Constraint
178167
| "else" -> Else
168+
| "exception" -> Exception
169+
| "export" -> Export
170+
| "external" -> External
171+
| "false" -> False
179172
| "for" -> For
173+
| "if" -> If
174+
| "import" -> Import
180175
| "in" -> In
181-
| "to" -> To
182-
| "downto" -> Downto
183-
| "while" -> While
184-
| "switch" -> Switch
185-
| "when" -> When
186-
| "external" -> External
187-
| "type" -> Typ
188-
| "private" -> Private
189-
| "mutable" -> Mutable
190-
| "constraint" -> Constraint
191176
| "include" -> Include
177+
| "lazy" -> Lazy
178+
| "let" -> Let
179+
| "list{" -> List
192180
| "module" -> Module
181+
| "mutable" -> Mutable
193182
| "of" -> Of
194-
| "list{" -> List
195-
| "with" -> With
183+
| "open" -> Open
184+
| "private" -> Private
185+
| "rec" -> Rec
186+
| "switch" -> Switch
187+
| "true" -> True
196188
| "try" -> Try
197-
| "import" -> Import
198-
| "export" -> Export
189+
| "type" -> Typ
190+
| "when" -> When
191+
| "while" -> While
199192
| _ -> raise Not_found
200193
[@@raises Not_found]
201194

202195
let isKeyword = function
203-
| True | False | Open | Let | Rec | And | As
204-
| Exception | Assert | Lazy | If | Else | For | In | To
205-
| Downto | While | Switch | When | External | Typ | Private
206-
| Mutable | Constraint | Include | Module | Of
207-
| Land | Lor | List | With
208-
| Try | Import | Export -> true
196+
| And | As | Assert | Constraint | Else | Exception | Export
197+
| External | False | For | If | Import | In | Include | Land | Lazy
198+
| Let | List | Lor | Module | Mutable | Of | Open | Private | Rec
199+
| Switch | True | Try | Typ | When | While -> true
209200
| _ -> false
210201

211202
let lookupKeyword str =

0 commit comments

Comments
 (0)