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

Commit 32ed878

Browse files
authored
Clean up end-of-file/printing newline logic (#142)
* Clean up new line logic * Check in snapshots from previous commit * Shrink minibuffer api thanks to previous 2 commits
1 parent 62f6b16 commit 32ed878

File tree

16 files changed

+85
-28
lines changed

16 files changed

+85
-28
lines changed

src/res_diagnostics.ml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,24 @@ let make ~startPos ~endPos category = {
142142
}
143143

144144
let printReport diagnostics src =
145+
let rec print diagnostics src =
146+
match diagnostics with
147+
| [] -> ()
148+
| d::rest ->
149+
Res_diagnostics_printing_utils.Super_location.super_error_reporter
150+
Format.err_formatter
151+
~src
152+
~startPos:d.startPos
153+
~endPos:d.endPos
154+
~msg:(explain d);
155+
begin match rest with
156+
| [] -> ()
157+
| _ -> Format.fprintf Format.err_formatter "@."
158+
end;
159+
print rest src
160+
in
145161
Format.fprintf Format.err_formatter "@[<v>";
146-
List.rev diagnostics |> List.iter (fun d ->
147-
Res_diagnostics_printing_utils.Super_location.super_error_reporter
148-
Format.err_formatter
149-
~src
150-
~startPos:d.startPos
151-
~endPos:d.endPos
152-
~msg:(explain d)
153-
);
162+
print (List.rev diagnostics) src;
154163
Format.fprintf Format.err_formatter "@]@."
155164

156165
let unexpected token context =

src/res_doc.ml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@ let toString ~width doc =
209209
end
210210
in
211211
process ~pos:0 [] [0, Flat, doc];
212-
213-
let len = MiniBuffer.length buffer in
214-
if len > 0 && MiniBuffer.unsafe_get buffer (len - 1) != '\n' then
215-
MiniBuffer.add_char buffer '\n';
216212
MiniBuffer.contents buffer
217213

218214

src/res_minibuffer.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ let create n =
1111

1212
let contents b = Bytes.sub_string b.buffer 0 b.position
1313

14-
let unsafe_get b ofs =
15-
Bytes.unsafe_get b.buffer ofs
16-
17-
let length b = b.position
18-
1914
(* Can't be called directly, don't add to the interface *)
2015
let resize_internal b more =
2116
let len = b.length in
@@ -52,4 +47,4 @@ let flush_newline b =
5247
position := !position - 1;
5348
done;
5449
b.position <- !position;
55-
add_char b '\n'
50+
add_char b '\n'

src/res_minibuffer.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
type t
2-
val add_char : t -> char -> unit
32
val add_string : t -> string -> unit
43
val contents : t -> string
54
val create : int -> t
65
val flush_newline : t -> unit
7-
val length : t -> int
8-
val unsafe_get : t -> int -> char

src/res_printer.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5004,9 +5004,9 @@ let printImplementation ~width (s: Parsetree.structure) ~comments =
50045004
(* CommentTable.log cmtTbl; *)
50055005
let doc = printStructure s cmtTbl in
50065006
(* Doc.debug doc; *)
5007-
Doc.toString ~width doc
5007+
Doc.toString ~width doc ^ "\n"
50085008

50095009
let printInterface ~width (s: Parsetree.signature) ~comments =
50105010
let cmtTbl = CommentTable.make () in
50115011
CommentTable.walkSignature s cmtTbl comments;
5012-
Doc.toString ~width (printSignature s cmtTbl)
5012+
Doc.toString ~width (printSignature s cmtTbl) ^ "\n"

tests/oprint/oprint.res.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ type t21 = [#\"va r ia nt"]
142142
type t22 = [#\"Variant ⛰"]
143143
type \"let" = int
144144
type \"type" = [#\"Point🗿"(\"let", float)]
145-
type exoticUser = {\"let": string, \"type": float}
145+
type exoticUser = {\"let": string, \"type": float}

tests/parsing/errors/expressions/__snapshots__/parse.spec.js.snap

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ let x = ((let a = 1 in let b = 2 in fun pattern -> (\\"test\\" : int))
3131
2) (pattern: int) => \\"test\\"
3232
3333
34-
3534
========================================================"
3635
`;
3736

@@ -108,6 +107,7 @@ let pipeline =
108107
109108
Did you forget a \`)\` here?
110109
110+
111111
Syntax error!
112112
parsing/errors/expressions/block.js:18:25-19:1
113113
16 │
@@ -119,6 +119,7 @@ let pipeline =
119119
120120
Did you forget a \`)\` here?
121121
122+
122123
Syntax error!
123124
parsing/errors/expressions/block.js:22:11-23:1
124125
20
@@ -130,6 +131,7 @@ let pipeline =
130131
131132
Looks like there might be an expression missing here
132133
134+
133135
Syntax error!
134136
parsing/errors/expressions/block.js:26:7-27:1
135137
24 │ | Join(doc1, doc2) =>
@@ -141,6 +143,7 @@ let pipeline =
141143
142144
Did you forget a \`)\` here?
143145
146+
144147
Syntax error!
145148
parsing/errors/expressions/block.js:30:10-31:1
146149
28
@@ -173,6 +176,7 @@ let () = ((let open Foo in let exception End in x ())[@ns.braces ])
173176
174177
consecutive statements on a line must be separated by ';' or a newline
175178
179+
176180
Syntax error!
177181
parsing/errors/expressions/consecutive.res:4:7
178182
2
@@ -183,6 +187,7 @@ let () = ((let open Foo in let exception End in x ())[@ns.braces ])
183187
184188
consecutive expressions on a line must be separated by ';' or a newline
185189
190+
186191
Syntax error!
187192
parsing/errors/expressions/consecutive.res:8:16-27
188193
6
@@ -193,6 +198,7 @@ let () = ((let open Foo in let exception End in x ())[@ns.braces ])
193198
194199
consecutive expressions on a line must be separated by ';' or a newline
195200
201+
196202
Syntax error!
197203
parsing/errors/expressions/consecutive.res:12:11-20
198204
10
@@ -221,6 +227,7 @@ let f a b = ((())[@ns.braces ])
221227
222228
This let-binding misses an expression
223229
230+
224231
Syntax error!
225232
parsing/errors/expressions/emptyBlock.js:3:20
226233
1 │ let x = {}
@@ -299,7 +306,6 @@ switch result {
299306
}
300307
301308
302-
303309
========================================================"
304310
`;
305311
@@ -351,6 +357,7 @@ let x =
351357
352358
I'm not sure what to parse here when looking at \\"-\\".
353359
360+
354361
Syntax error!
355362
parsing/errors/expressions/jsx.js:2:20
356363
1 │ let x = <di-v />
@@ -360,6 +367,7 @@ let x =
360367
361368
Did you forget a \`</\` here?
362369
370+
363371
Syntax error!
364372
parsing/errors/expressions/jsx.js:3:9-28
365373
1 │ let x = <di-v />
@@ -370,6 +378,7 @@ let x =
370378
371379
Missing </Foo.Bar>
372380
381+
373382
Syntax error!
374383
parsing/errors/expressions/jsx.js:4:9-34
375384
2 │ let x = <Unclosed >;
@@ -380,6 +389,7 @@ let x =
380389
381390
Missing </Foo.Bar.Baz>
382391
392+
383393
Syntax error!
384394
parsing/errors/expressions/jsx.js:5:9-27
385395
3 │ let x = <Foo.Bar></Free.Will>;
@@ -390,6 +400,7 @@ let x =
390400
391401
Missing </Foo.bar>
392402
403+
393404
Syntax error!
394405
parsing/errors/expressions/jsx.js:6:17
395406
4 │ let x = <Foo.Bar.Baz></Foo.Bar.Boo>
@@ -436,6 +447,7 @@ let record = { field = ([%rescript.exprhole ]) }
436447
437448
Did you forget a \`,\` here?
438449
450+
439451
Syntax error!
440452
parsing/errors/expressions/record.js:8:10-18
441453
6 │
@@ -446,6 +458,7 @@ let record = { field = ([%rescript.exprhole ]) }
446458
447459
Did you forget a \`:\` here?
448460
461+
449462
Syntax error!
450463
parsing/errors/expressions/record.js:13:9-17:0
451464
11 │
@@ -547,6 +560,5 @@ let x = (\\"hi\\" : string)
547560
(\\"hi\\": string)
548561
549562
550-
551563
========================================================"
552564
`;

tests/parsing/errors/other/__snapshots__/parse.spec.js.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type nonrec 'extraInfo student =
3838
3939
Did you forget a \`,\` here?
4040
41+
4142
Syntax error!
4243
parsing/errors/other/regionMissingComma.res:8:11-9:11
4344
6 │ type student<'extraInfo> = {
@@ -71,6 +72,7 @@ let x::y = myList
7172
7273
Arrays can't use the \`...\` spread currently. Please use \`concat\` or other Array helpers.
7374
75+
7476
Syntax error!
7577
parsing/errors/other/spread.js:2:6-8
7678
1 │ let arr = [...x, ...y]
@@ -82,6 +84,7 @@ let x::y = myList
8284
Explanation: such spread would create a subarray; out of performance concern, our pattern matching currently guarantees to never create new intermediate data.
8385
Solution: if it's to validate the first few elements, use a \`when\` clause + Array size check + \`get\` checks on the current pattern. If it's to obtain a subarray, use \`Array.sub\` or \`Belt.Array.slice\`.
8486
87+
8588
Syntax error!
8689
parsing/errors/other/spread.js:4:21-23
8790
2 │ let [...arr, _] = [1, 2, 3]
@@ -93,6 +96,7 @@ Solution: if it's to validate the first few elements, use a \`when\` clause + Ar
9396
Records can only have one \`...\` spread, at the beginning.
9497
Explanation: since records have a known, fixed shape, a spread like \`{a, ...b}\` wouldn't make sense, as \`b\` would override every field of \`a\` anyway.
9598
99+
96100
Syntax error!
97101
parsing/errors/other/spread.js:5:15-18
98102
3 │
@@ -105,6 +109,7 @@ Explanation: since records have a known, fixed shape, a spread like \`{a, ...b}\
105109
Explanation: you can't collect a subset of a record's field into its own record, since a record needs an explicit declaration and that subset wouldn't have one.
106110
Solution: you need to pull out each field you want explicitly.
107111
112+
108113
Syntax error!
109114
parsing/errors/other/spread.js:8:13-22
110115
6 │

tests/parsing/errors/pattern/__snapshots__/parse.spec.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let 4 = for [%rescript.patternhole ] = 0 to 10 do Js.log \\"for\\" done
1515
1616
I was expecting a name for this let-binding. Example: \`let message = \\"hello\\"\`
1717
18+
1819
Syntax error!
1920
parsing/errors/pattern/missing.res:2:5
2021
1 │ let = 2
@@ -24,6 +25,7 @@ let 4 = for [%rescript.patternhole ] = 0 to 10 do Js.log \\"for\\" done
2425
2526
I was expecting a name for this let-binding. Example: \`let message = \\"hello\\"\`
2627
28+
2729
Syntax error!
2830
parsing/errors/pattern/missing.res:4:5-6
2931
2 │ let = 4
@@ -34,6 +36,7 @@ let 4 = for [%rescript.patternhole ] = 0 to 10 do Js.log \\"for\\" done
3436
3537
A for-loop has the following form: \`for i in 0 to 10\`. Did you forget to supply a name before \`in\`?
3638
39+
3740
Syntax error!
3841
parsing/errors/pattern/missing.res:9:3-4
3942
7 │

tests/parsing/errors/scanner/__snapshots__/parse.spec.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let x = \\"\\\\\\\\0AAA\\"
1414
1515
unknown escape sequence
1616
17+
1718
Syntax error!
1819
parsing/errors/scanner/escapeSequence.js:3:10-11
1920
1 │ let x = \\"\\\\0\\"
@@ -42,6 +43,7 @@ let newX = x +. (newVelocity *. secondPerFrame)
4243
Hmm, not sure what I should do here with this character.
4344
If you're trying to deref an expression, use \`foo.contents\` instead.
4445
46+
4547
Syntax error!
4648
parsing/errors/scanner/oldDerefOp.js:2:46
4749
1 │ let newVelocity = velocity +. a *. secondPerFrame^;

0 commit comments

Comments
 (0)