@@ -30,15 +30,15 @@ let findFunctionType ~currentFile ~debug ~path ~pos =
30
30
file )))
31
31
in
32
32
match completables with
33
- | Some ({kind = Value type_expr ; name; docstring} :: _ , env , package , file ) ->
33
+ | Some ({kind = Value type_expr ; docstring} :: _ , env , package , file ) ->
34
34
let args, _ =
35
35
CompletionBackEnd. extractFunctionType type_expr ~env ~package
36
36
in
37
- Some (args, name, docstring, type_expr, package, env, file)
37
+ Some (args, docstring, type_expr, package, env, file)
38
38
| _ -> None
39
39
40
40
(* Extracts all parameters from a parsed function signature *)
41
- let extractParameters ~signature ~label =
41
+ let extractParameters ~signature ~typeStrForParser ~ labelPrefixLen =
42
42
match signature with
43
43
| [
44
44
{
@@ -55,10 +55,13 @@ let extractParameters ~signature ~label =
55
55
ptyp_loc;
56
56
} ->
57
57
let startOffset =
58
- ptyp_loc |> Loc. start |> Pos. positionToOffset label |> Option. get
58
+ ptyp_loc |> Loc. start
59
+ |> Pos. positionToOffset typeStrForParser
60
+ |> Option. get
59
61
in
60
62
let endOffset =
61
- argumentTypeExpr.ptyp_loc |> Loc. end_ |> Pos. positionToOffset label
63
+ argumentTypeExpr.ptyp_loc |> Loc. end_
64
+ |> Pos. positionToOffset typeStrForParser
62
65
|> Option. get
63
66
in
64
67
(* The AST locations does not account for "=?" of optional arguments, so add that to the offset here if needed. *)
@@ -68,7 +71,14 @@ let extractParameters ~signature ~label =
68
71
| _ -> endOffset
69
72
in
70
73
extractParams nextFunctionExpr
71
- (params @ [(argumentLabel, startOffset, endOffset)])
74
+ (params
75
+ @ [
76
+ ( argumentLabel,
77
+ (* Remove the label prefix offset here, since we're not showing
78
+ that to the end user. *)
79
+ startOffset - labelPrefixLen,
80
+ endOffset - labelPrefixLen );
81
+ ])
72
82
| _ -> params
73
83
in
74
84
extractParams expr []
@@ -283,7 +293,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
283
293
(* Not looking for the cursor position after this, but rather the target function expression's loc. *)
284
294
let pos = exp.pexp_loc |> Loc. end_ in
285
295
match findFunctionType ~current File ~debug ~path ~pos with
286
- | Some (args , name , docstring , type_expr , package , _env , file ) ->
296
+ | Some (args , docstring , type_expr , package , _env , file ) ->
287
297
if debug then
288
298
Printf. printf " argAtCursor: %s\n "
289
299
(match argAtCursor with
@@ -296,19 +306,24 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
296
306
in the form of a list of start/end character offsets. We leverage the parser to figure the offsets out by parsing the label, and extract the
297
307
offsets from the parser. *)
298
308
299
- (* Put together a label here that both makes sense to show to the end user in the signature help, but also can be passed to the parser. *)
300
- let label = " let " ^ name ^ " : " ^ Shared. typeToString type_expr in
309
+ (* A full let binding with the type text is needed for the parser to be able to parse it. *)
310
+ let labelPrefix = " let fn: " in
311
+ let labelPrefixLen = String. length labelPrefix in
312
+ let fnTypeStr = Shared. typeToString type_expr in
313
+ let typeStrForParser = labelPrefix ^ fnTypeStr in
301
314
let {Res_driver. parsetree = signature} =
302
315
Res_driver. parseInterfaceFromSource ~for Printer:false
303
- ~display Filename:" <missing-file>" ~source: label
316
+ ~display Filename:" <missing-file>" ~source: typeStrForParser
304
317
in
305
318
306
- let parameters = extractParameters ~signature ~label in
319
+ let parameters =
320
+ extractParameters ~signature ~type StrForParser ~label PrefixLen
321
+ in
307
322
if debug then
308
323
Printf. printf " extracted params: \n %s\n "
309
324
(parameters
310
325
|> List. map (fun (_ , start , end_ ) ->
311
- String. sub label start (end_ - start))
326
+ String. sub fnTypeStr start (end_ - start))
312
327
|> list );
313
328
314
329
(* Figure out the active parameter *)
@@ -318,7 +333,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
318
333
Protocol. signatures =
319
334
[
320
335
{
321
- label;
336
+ label = fnTypeStr ;
322
337
parameters =
323
338
parameters
324
339
|> List. map (fun (argLabel , start , end_ ) ->
0 commit comments