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

Commit 33287cf

Browse files
authored
Sync up outcome printer (#70)
No new changes really. Tracking rescript-lang/syntax@9907b82
1 parent 0ace496 commit 33287cf

File tree

7 files changed

+85
-209
lines changed

7 files changed

+85
-209
lines changed

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

-160
This file was deleted.

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,16 @@ let trimSpaces s =
5757
let len = String.length s in
5858
if len = 0 then s
5959
else if String.unsafe_get s 0 = ' ' || String.unsafe_get s (len - 1) = ' ' then (
60-
let b = Bytes.of_string s in
6160
let i = ref 0 in
62-
while !i < len && (Bytes.unsafe_get b !i) = ' ' do
61+
while !i < len && (String.unsafe_get s !i) = ' ' do
6362
incr i
6463
done;
6564
let j = ref (len - 1) in
66-
while !j >= !i && (Bytes.unsafe_get b !j) = ' ' do
65+
while !j >= !i && (String.unsafe_get s !j) = ' ' do
6766
decr j
6867
done;
6968
if !j >= !i then
70-
(Bytes.sub [@doesNotRaise]) b !i (!j - !i + 1) |> Bytes.to_string
69+
(String.sub [@doesNotRaise]) s !i (!j - !i + 1)
7170
else
7271
""
7372
) else s

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

+61-34
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,20 @@ let hardLine = LineBreak Hard
2828
let softLine = LineBreak Soft
2929
let literalLine = LineBreak Literal
3030
let text s = Text s
31-
let concat l = Concat l
31+
32+
(* Optimization. We eagerly collapse and reduce whatever allocation we can *)
33+
let rec _concat acc l =
34+
match l with
35+
| Text s1 :: Text s2 :: rest -> Text (s1 ^ s2) :: _concat acc rest
36+
| Nil :: rest -> _concat acc rest
37+
| Concat l2 :: rest -> _concat (_concat acc rest) l2 (* notice the order here *)
38+
| x :: rest ->
39+
let rest1 = _concat acc rest in
40+
if rest1 == rest then l else x :: rest1
41+
| [] -> acc
42+
43+
let concat l = Concat(_concat [] l)
44+
3245
let indent d = Indent d
3346
let ifBreaks t f = IfBreaks {yes = t; no = f}
3447
let lineSuffix d = LineSuffix d
@@ -118,39 +131,53 @@ let join ~sep docs =
118131
| [x] -> List.rev (x::acc)
119132
| x::xs -> loop (sep::x::acc) sep xs
120133
in
121-
Concat(loop [] sep docs)
134+
concat(loop [] sep docs)
135+
136+
let fits w stack =
137+
let width = ref w in
138+
let result = ref None in
122139

123-
let rec fits w doc = match doc with
124-
| _ when w < 0 -> false
125-
| [] -> true
126-
| (_ind, _mode, Text txt)::rest -> fits (w - String.length txt) rest
127-
| (ind, mode, Indent doc)::rest -> fits w ((ind + 2, mode, doc)::rest)
128-
| (_ind, Flat, LineBreak break)::rest ->
129-
if break = Hard || break = Literal then true
130-
else
131-
let w = if break = Classic then w - 1 else w in
132-
fits w rest
133-
| (_ind, _mode, Nil)::rest -> fits w rest
134-
| (_ind, Break, LineBreak _break)::_rest -> true
135-
| (ind, mode, Group {shouldBreak = forceBreak; doc})::rest ->
136-
let mode = if forceBreak then Break else mode in
137-
fits w ((ind, mode, doc)::rest)
138-
| (ind, mode, IfBreaks {yes = breakDoc; no = flatDoc})::rest ->
139-
if mode = Break then
140-
fits w ((ind, mode, breakDoc)::rest)
141-
else
142-
fits w ((ind, mode, flatDoc)::rest)
143-
| (ind, mode, Concat docs)::rest ->
144-
let ops = List.map (fun doc -> (ind, mode, doc)) docs in
145-
fits w (List.append ops rest)
146-
(* | (_ind, _mode, Cursor)::rest -> fits w rest *)
147-
| (_ind, _mode, LineSuffix _)::rest -> fits w rest
148-
| (_ind, _mode, BreakParent)::rest -> fits w rest
149-
| (ind, mode, CustomLayout (hd::_))::rest ->
150-
(* TODO: if we have nested custom layouts, what we should do here? *)
151-
fits w ((ind, mode, hd)::rest)
152-
| (_ind, _mode, CustomLayout _)::rest ->
153-
fits w rest
140+
let rec calculate indent mode doc =
141+
match mode, doc with
142+
| _ when result.contents != None -> ()
143+
| _ when width.contents < 0 -> result := Some false
144+
| _, Nil
145+
| _, LineSuffix _
146+
| _, BreakParent -> ()
147+
| _, Text txt -> width := width.contents - (String.length txt)
148+
| _, Indent doc -> calculate (indent + 2) mode doc
149+
| Flat, LineBreak Hard
150+
| Flat, LineBreak Literal -> result := Some true
151+
| Flat, LineBreak Classic -> width := width.contents - 1
152+
| Flat, LineBreak Soft -> ()
153+
| Break, LineBreak _ -> result := Some true
154+
| _, Group {shouldBreak = true; doc} -> calculate indent Break doc
155+
| _, Group {doc} -> calculate indent mode doc
156+
| Break, IfBreaks {yes = breakDoc} -> calculate indent mode breakDoc
157+
| Flat, IfBreaks {no = flatDoc} -> calculate indent mode flatDoc
158+
| _, Concat docs -> calculateConcat indent mode docs
159+
| _, CustomLayout (hd::_) ->
160+
(* TODO: if we have nested custom layouts, what we should do here? *)
161+
calculate indent mode hd
162+
| _, CustomLayout [] -> ()
163+
and calculateConcat indent mode docs =
164+
if result.contents == None then (
165+
match docs with
166+
| [] -> ()
167+
| doc::rest ->
168+
calculate indent mode doc;
169+
calculateConcat indent mode rest
170+
)
171+
in
172+
let rec calculateAll stack =
173+
match result.contents, stack with
174+
| Some r, _ -> r
175+
| None, [] -> !width >= 0
176+
| None, (indent, mode, doc)::rest ->
177+
calculate indent mode doc;
178+
calculateAll rest
179+
in
180+
calculateAll stack
154181

155182
let toString ~width doc =
156183
let doc = propagateForcedBreaks doc in
@@ -226,7 +253,7 @@ let toString ~width doc =
226253
process ~pos:0 [] (List.rev suffices)
227254
end
228255
in
229-
process ~pos:0 [] [0, Flat, doc];
256+
process ~pos:0 [] [(0, Flat, doc)];
230257
MiniBuffer.contents buffer
231258

232259

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ val question: t
3636
val tilde: t
3737
val equal: t
3838
val trailingComma: t
39-
val doubleQuote: t
39+
val doubleQuote: t [@@live]
4040

4141
(*
4242
* `willBreak doc` checks whether `doc` contains forced line breaks.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let create n =
99
let s = (Bytes.create [@doesNotRaise]) n in
1010
{buffer = s; position = 0; length = n}
1111

12-
let contents b = Bytes.sub_string b.buffer 0 b.position
12+
let contents b = (Bytes.sub_string [@doesNotRaise]) b.buffer 0 b.position
1313

1414
(* Can't be called directly, don't add to the interface *)
1515
let resize_internal b more =

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

+15-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let classifyIdentContent ~allowUident txt =
2929
let c = String.unsafe_get txt i in
3030
if i == 0 && not (
3131
(allowUident && (c >= 'A' && c <= 'Z')) ||
32-
(c >= 'a' && c <= 'z') || c = '_' || (c >= '0' && c <= '9')) then
32+
(c >= 'a' && c <= 'z') || c = '_') then
3333
ExoticIdent
3434
else if not (
3535
(c >= 'a' && c <= 'z')
@@ -56,6 +56,15 @@ let printIdentLike ~allowUident txt =
5656
]
5757
| NormalIdent -> Doc.text txt
5858

59+
let printPolyVarIdent txt =
60+
match classifyIdentContent ~allowUident:true txt with
61+
| ExoticIdent -> Doc.concat [
62+
Doc.text "\"";
63+
Doc.text txt;
64+
Doc.text"\""
65+
]
66+
| NormalIdent -> Doc.text txt
67+
5968
(* ReScript doesn't have parenthesized identifiers.
6069
* We don't support custom operators. *)
6170
let parenthesized_ident _name = true
@@ -301,6 +310,8 @@ let printIdentLike ~allowUident txt =
301310
Doc.join ~sep:Doc.space (
302311
List.map (fun var -> Doc.text ("'" ^ var)) vars
303312
);
313+
Doc.dot;
314+
Doc.space;
304315
printOutTypeDoc outType;
305316
]
306317
)
@@ -376,7 +387,7 @@ let printIdentLike ~allowUident txt =
376387
Doc.group (
377388
Doc.concat [
378389
Doc.text "#";
379-
printIdentLike ~allowUident:true name;
390+
printPolyVarIdent name;
380391
match types with
381392
| [] -> Doc.nil
382393
| types ->
@@ -1023,7 +1034,7 @@ let printIdentLike ~allowUident txt =
10231034
Doc.rparen;
10241035
]
10251036
)
1026-
(* Not supported by NapkinScript *)
1037+
(* Not supported by ReScript *)
10271038
| Oval_variant _ -> Doc.nil
10281039

10291040
let printOutExceptionDoc exc outValue =
@@ -1130,7 +1141,7 @@ let printIdentLike ~allowUident txt =
11301141

11311142

11321143

1133-
(* Not supported in Napkin *)
1144+
(* Not supported in ReScript *)
11341145
(* Oprint.out_class_type *)
11351146
let setup = lazy begin
11361147
Oprint.out_value := printOutValue;

0 commit comments

Comments
 (0)