Skip to content

Commit 951da01

Browse files
committed
print function templates as uncurried when wanted
1 parent 4974b5e commit 951da01

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

analysis/src/CompletionBackEnd.ml

+8-4
Original file line numberDiff line numberDiff line change
@@ -1354,15 +1354,19 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
13541354
~env ();
13551355
]
13561356
else []
1357-
| Tfunction {env; typ; args} when prefix = "" && mode = Expression ->
1357+
| Tfunction {env; typ; args; uncurried} when prefix = "" && mode = Expression
1358+
->
1359+
let shouldPrintAsUncurried = uncurried && !Config.uncurried <> Uncurried in
13581360
let mkFnArgs ~asSnippet =
13591361
match args with
1360-
| [(Nolabel, argTyp)] when TypeUtils.typeIsUnit argTyp -> "()"
1362+
| [(Nolabel, argTyp)] when TypeUtils.typeIsUnit argTyp ->
1363+
if shouldPrintAsUncurried then "(. )" else "()"
13611364
| [(Nolabel, argTyp)] ->
13621365
let varName =
13631366
CompletionExpressions.prettyPrintFnTemplateArgName ~env ~full argTyp
13641367
in
1365-
if asSnippet then "${1:" ^ varName ^ "}" else varName
1368+
let argsText = if asSnippet then "${1:" ^ varName ^ "}" else varName in
1369+
if shouldPrintAsUncurried then "(. " ^ argsText ^ ")" else argsText
13661370
| _ ->
13671371
let currentUnlabelledIndex = ref 0 in
13681372
let argsText =
@@ -1385,7 +1389,7 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
13851389
else varName))
13861390
|> String.concat ", "
13871391
in
1388-
"(" ^ argsText ^ ")"
1392+
"(" ^ if shouldPrintAsUncurried then ". " else "" ^ argsText ^ ")"
13891393
in
13901394
[
13911395
Completion.createWithSnippet

analysis/src/SharedTypes.ml

+6-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,12 @@ and completionType =
335335
(** When we have the full type expr from the compiler. *) ];
336336
}
337337
| TinlineRecord of {env: QueryEnv.t; fields: field list}
338-
| Tfunction of {env: QueryEnv.t; args: typedFnArg list; typ: Types.type_expr}
338+
| Tfunction of {
339+
env: QueryEnv.t;
340+
args: typedFnArg list;
341+
typ: Types.type_expr;
342+
uncurried: bool;
343+
}
339344

340345
module Env = struct
341346
type t = {stamps: Stamps.t; modulePath: ModulePath.t}

analysis/src/TypeUtils.ml

+8-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ let rec extractType ~env ~package (t : Types.type_expr) =
120120
| Tconstr (Path.Pident {name = "bool"}, [], _) -> Some (Tbool env)
121121
| Tconstr (Path.Pident {name = "string"}, [], _) -> Some (Tstring env)
122122
| Tconstr (Path.Pident {name = "exn"}, [], _) -> Some (Texn env)
123+
| Tconstr (Pident {name = "function$"}, [t; _], _) -> (
124+
(* Uncurried functions. *)
125+
match extractFunctionType t ~env ~package with
126+
| args, _tRet when args <> [] ->
127+
Some (Tfunction {env; args; typ = t; uncurried = true})
128+
| _args, _tRet -> None)
123129
| Tconstr (path, _, _) -> (
124130
match References.digConstructor ~env ~package path with
125131
| Some (env, {item = {decl = {type_manifest = Some t1}}}) ->
@@ -151,7 +157,8 @@ let rec extractType ~env ~package (t : Types.type_expr) =
151157
Some (Tpolyvariant {env; constructors; typeExpr = t})
152158
| Tarrow _ -> (
153159
match extractFunctionType t ~env ~package with
154-
| args, _tRet when args <> [] -> Some (Tfunction {env; args; typ = t})
160+
| args, _tRet when args <> [] ->
161+
Some (Tfunction {env; args; typ = t; uncurried = false})
155162
| _args, _tRet -> None)
156163
| _ -> None
157164

analysis/tests/src/Completion.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,4 @@ type withUncurried = {
461461
}
462462

463463
// let f: withUncurried = {fn: }
464-
// ^com
464+
// ^com

analysis/tests/src/expected/Completion.res.txt

+12-3
Original file line numberDiff line numberDiff line change
@@ -1944,8 +1944,8 @@ Path red
19441944
}]
19451945

19461946
Complete src/Completion.res 405:22
1947-
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->464:0]
1948-
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->423:17], ...[428:0->464:0])
1947+
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->465:0]
1948+
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->423:17], ...[428:0->465:0])
19491949
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->423:17]
19501950
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->405:19], ...[405:21->423:17])
19511951
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:21->423:17]
@@ -2189,5 +2189,14 @@ Package opens Pervasives.JsxModules.place holder
21892189
Resolved opens 3 Completion.res Completion.res pervasives
21902190
ContextPath Type[withUncurried]
21912191
Path withUncurried
2192-
[]
2192+
[{
2193+
"label": "(. v) => {}",
2194+
"kind": 12,
2195+
"tags": [],
2196+
"detail": "int => unit",
2197+
"documentation": null,
2198+
"sortText": "A",
2199+
"insertText": "(. ${1:v}) => {$0}",
2200+
"insertTextFormat": 2
2201+
}]
21932202

0 commit comments

Comments
 (0)