Skip to content

Commit 37a1388

Browse files
committed
remove redundant function name from signature help output
1 parent 6c0b654 commit 37a1388

File tree

2 files changed

+58
-43
lines changed

2 files changed

+58
-43
lines changed

analysis/src/SignatureHelp.ml

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ let findFunctionType ~currentFile ~debug ~path ~pos =
3030
file )))
3131
in
3232
match completables with
33-
| Some ({kind = Value type_expr; name; docstring} :: _, env, package, file) ->
33+
| Some ({kind = Value type_expr; docstring} :: _, env, package, file) ->
3434
let args, _ =
3535
CompletionBackEnd.extractFunctionType type_expr ~env ~package
3636
in
37-
Some (args, name, docstring, type_expr, package, env, file)
37+
Some (args, docstring, type_expr, package, env, file)
3838
| _ -> None
3939

4040
(* Extracts all parameters from a parsed function signature *)
41-
let extractParameters ~signature ~label =
41+
let extractParameters ~signature ~typeStrForParser ~labelPrefixLen =
4242
match signature with
4343
| [
4444
{
@@ -55,10 +55,13 @@ let extractParameters ~signature ~label =
5555
ptyp_loc;
5656
} ->
5757
let startOffset =
58-
ptyp_loc |> Loc.start |> Pos.positionToOffset label |> Option.get
58+
ptyp_loc |> Loc.start
59+
|> Pos.positionToOffset typeStrForParser
60+
|> Option.get
5961
in
6062
let endOffset =
61-
argumentTypeExpr.ptyp_loc |> Loc.end_ |> Pos.positionToOffset label
63+
argumentTypeExpr.ptyp_loc |> Loc.end_
64+
|> Pos.positionToOffset typeStrForParser
6265
|> Option.get
6366
in
6467
(* 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 =
6871
| _ -> endOffset
6972
in
7073
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+
])
7282
| _ -> params
7383
in
7484
extractParams expr []
@@ -283,7 +293,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
283293
(* Not looking for the cursor position after this, but rather the target function expression's loc. *)
284294
let pos = exp.pexp_loc |> Loc.end_ in
285295
match findFunctionType ~currentFile ~debug ~path ~pos with
286-
| Some (args, name, docstring, type_expr, package, _env, file) ->
296+
| Some (args, docstring, type_expr, package, _env, file) ->
287297
if debug then
288298
Printf.printf "argAtCursor: %s\n"
289299
(match argAtCursor with
@@ -296,19 +306,24 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
296306
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
297307
offsets from the parser. *)
298308

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
301314
let {Res_driver.parsetree = signature} =
302315
Res_driver.parseInterfaceFromSource ~forPrinter:false
303-
~displayFilename:"<missing-file>" ~source:label
316+
~displayFilename:"<missing-file>" ~source:typeStrForParser
304317
in
305318

306-
let parameters = extractParameters ~signature ~label in
319+
let parameters =
320+
extractParameters ~signature ~typeStrForParser ~labelPrefixLen
321+
in
307322
if debug then
308323
Printf.printf "extracted params: \n%s\n"
309324
(parameters
310325
|> List.map (fun (_, start, end_) ->
311-
String.sub label start (end_ - start))
326+
String.sub fnTypeStr start (end_ - start))
312327
|> list);
313328

314329
(* Figure out the active parameter *)
@@ -318,7 +333,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
318333
Protocol.signatures =
319334
[
320335
{
321-
label;
336+
label = fnTypeStr;
322337
parameters =
323338
parameters
324339
|> List.map (fun (argLabel, start, end_) ->

0 commit comments

Comments
 (0)