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

Commit 51773c6

Browse files
committed
Sync with syntax repo to print uncurried types.
1 parent 0eb380a commit 51773c6

File tree

4 files changed

+74
-43
lines changed

4 files changed

+74
-43
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## master
22
- Add support for doc strings when hovering on modules.
3+
- Add support for printing uncurried function types in hover.
34

45
## Release 1.0.3 of rescript-vscode
56
This [commit](https://github.com/rescript-lang/rescript-editor-support/commit/214d220d8573f9f0c8d54e623c163e01617bf124) is vendored in [rescript-vscode 1.0.3](https://github.com/rescript-lang/rescript-vscode/releases/tag/1.0.3).

examples/example-project/src/ZZ.res

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ type vr = | V1 | V2
8383

8484
let v1 = V1
8585

86-
module DoubleNested = ModuleWithDocComment.Nested.NestedAgain
86+
module DoubleNested = ModuleWithDocComment.Nested.NestedAgain
87+
88+
let uncurried = (. x) => x+1;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ let debug t =
234234
let rec toDoc = function
235235
| Nil -> text "nil"
236236
| BreakParent -> text "breakparent"
237-
| Text txt -> text ("text(" ^ txt ^ ")")
237+
| Text txt -> text ("text(\"" ^ txt ^ "\")")
238238
| LineSuffix doc -> group(
239239
concat [
240240
text "linesuffix(";
@@ -245,6 +245,7 @@ let debug t =
245245
text ")"
246246
]
247247
)
248+
| Concat [] -> text "concat()"
248249
| Concat docs -> group(
249250
concat [
250251
text "concat(";
@@ -312,7 +313,7 @@ let debug t =
312313
indent (
313314
concat [
314315
line;
315-
text ("shouldBreak: " ^ (string_of_bool shouldBreak));
316+
text ("{shouldBreak: " ^ (string_of_bool shouldBreak) ^ "}");
316317
concat [text ","; line];
317318
toDoc doc;
318319
]

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

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
module Doc = Res_doc
1111
module Token = Res_token
1212

13+
(* checks if ident contains "arity", like in "arity1", "arity2", "arity3" etc. *)
14+
let isArityIdent ident =
15+
if String.length ident >= 6 then
16+
(String.sub [@doesNotRaise]) ident 0 5 = "arity"
17+
else
18+
false
19+
1320
type identifierStyle =
1421
| ExoticIdent
1522
| NormalIdent
@@ -195,6 +202,21 @@ let printIdentLike ~allowUident txt =
195202
Doc.text " as '";
196203
Doc.text aliasTxt
197204
]
205+
| Otyp_constr (
206+
Oide_dot (Oide_dot (Oide_ident "Js", "Fn") , "arity0"), (* Js.Fn.arity0 *)
207+
[Otyp_constr (Oide_ident ident, [])] (* int or unit or string *)
208+
) ->
209+
(* Js.Fn.arity0<int> -> (.) => int*)
210+
Doc.concat [
211+
Doc.text "(.) => ";
212+
Doc.text ident;
213+
]
214+
| Otyp_constr (
215+
Oide_dot (Oide_dot (Oide_ident "Js", "Fn") , ident), (* Js.Fn.arity2 *)
216+
[(Otyp_arrow _) as arrowType] (* (int, int) => int *)
217+
) when isArityIdent ident ->
218+
(* Js.Fn.arity2<(int, int) => int> -> (. int, int) => int*)
219+
printOutArrowType ~uncurried:true arrowType
198220
| Otyp_constr (outIdent, []) ->
199221
printOutIdentDoc ~allowUident:false outIdent
200222
| Otyp_manifest (typ1, typ2) ->
@@ -283,51 +305,56 @@ let printIdentLike ~allowUident txt =
283305
]
284306
)
285307
| Otyp_arrow _ as typ ->
286-
let (typArgs, typ) = collectArrowArgs typ [] in
287-
let args = Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
288-
List.map (fun (lbl, typ) ->
289-
if lbl = "" then
290-
printOutTypeDoc typ
291-
else
292-
Doc.group (
293-
Doc.concat [
294-
Doc.text ("~" ^ lbl ^ ": ");
295-
printOutTypeDoc typ
296-
]
297-
)
298-
) typArgs
299-
) in
300-
let argsDoc =
301-
let needsParens = match typArgs with
302-
| [_, (Otyp_tuple _ | Otyp_arrow _)] -> true
303-
(* single argument should not be wrapped *)
304-
| ["", _] -> false
305-
| _ -> true
306-
in
307-
if needsParens then
308+
printOutArrowType ~uncurried:false typ
309+
| Otyp_module (_modName, _stringList, _outTypes) ->
310+
Doc.nil
311+
312+
and printOutArrowType ~uncurried typ =
313+
let (typArgs, typ) = collectArrowArgs typ [] in
314+
let args = Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
315+
List.map (fun (lbl, typ) ->
316+
if lbl = "" then
317+
printOutTypeDoc typ
318+
else
308319
Doc.group (
309320
Doc.concat [
310-
Doc.lparen;
311-
Doc.indent (
312-
Doc.concat [
313-
Doc.softLine;
314-
args;
315-
]
316-
);
317-
Doc.trailingComma;
318-
Doc.softLine;
319-
Doc.rparen;
321+
Doc.text ("~" ^ lbl ^ ": ");
322+
printOutTypeDoc typ
320323
]
321324
)
322-
else args
325+
) typArgs
326+
) in
327+
let argsDoc =
328+
let needsParens = match typArgs with
329+
| _ when uncurried -> true
330+
| [_, (Otyp_tuple _ | Otyp_arrow _)] -> true
331+
(* single argument should not be wrapped *)
332+
| ["", _] -> false
333+
| _ -> true
323334
in
324-
Doc.concat [
325-
argsDoc;
326-
Doc.text " => ";
327-
printOutTypeDoc typ;
328-
]
329-
| Otyp_module (_modName, _stringList, _outTypes) ->
330-
Doc.nil
335+
if needsParens then
336+
Doc.group (
337+
Doc.concat [
338+
if uncurried then Doc.text "(. " else Doc.lparen;
339+
Doc.indent (
340+
Doc.concat [
341+
Doc.softLine;
342+
args;
343+
]
344+
);
345+
Doc.trailingComma;
346+
Doc.softLine;
347+
Doc.rparen;
348+
]
349+
)
350+
else args
351+
in
352+
Doc.concat [
353+
argsDoc;
354+
Doc.text " => ";
355+
printOutTypeDoc typ;
356+
]
357+
331358

332359
and printOutVariant variant = match variant with
333360
| Ovar_fields fields -> (* (string * bool * out_type list) list *)

0 commit comments

Comments
 (0)