Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove redundant function name from signature help output #678

Merged
merged 2 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

- Prefer opened `Belt` modules in autocomplete when `-open Belt` is detected in `bsconfig`. https://github.com/rescript-lang/rescript-vscode/pull/673
- Improve precision in signature help. You now do not need to type anything into the argument for it to highlight. https://github.com/rescript-lang/rescript-vscode/pull/675
- Remove redundant function name in signature help, to clean up what's shown to the user some. https://github.com/rescript-lang/rescript-vscode/pull/678

#### :bug: Bug Fix

Expand Down
41 changes: 28 additions & 13 deletions analysis/src/SignatureHelp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ let findFunctionType ~currentFile ~debug ~path ~pos =
file )))
in
match completables with
| Some ({kind = Value type_expr; name; docstring} :: _, env, package, file) ->
| Some ({kind = Value type_expr; docstring} :: _, env, package, file) ->
let args, _ =
CompletionBackEnd.extractFunctionType type_expr ~env ~package
in
Some (args, name, docstring, type_expr, package, env, file)
Some (args, docstring, type_expr, package, env, file)
| _ -> None

(* Extracts all parameters from a parsed function signature *)
let extractParameters ~signature ~label =
let extractParameters ~signature ~typeStrForParser ~labelPrefixLen =
match signature with
| [
{
Expand All @@ -55,10 +55,13 @@ let extractParameters ~signature ~label =
ptyp_loc;
} ->
let startOffset =
ptyp_loc |> Loc.start |> Pos.positionToOffset label |> Option.get
ptyp_loc |> Loc.start
|> Pos.positionToOffset typeStrForParser
|> Option.get
in
let endOffset =
argumentTypeExpr.ptyp_loc |> Loc.end_ |> Pos.positionToOffset label
argumentTypeExpr.ptyp_loc |> Loc.end_
|> Pos.positionToOffset typeStrForParser
|> Option.get
in
(* The AST locations does not account for "=?" of optional arguments, so add that to the offset here if needed. *)
Expand All @@ -68,7 +71,14 @@ let extractParameters ~signature ~label =
| _ -> endOffset
in
extractParams nextFunctionExpr
(params @ [(argumentLabel, startOffset, endOffset)])
(params
@ [
( argumentLabel,
(* Remove the label prefix offset here, since we're not showing
that to the end user. *)
startOffset - labelPrefixLen,
endOffset - labelPrefixLen );
])
| _ -> params
in
extractParams expr []
Expand Down Expand Up @@ -283,7 +293,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
(* Not looking for the cursor position after this, but rather the target function expression's loc. *)
let pos = exp.pexp_loc |> Loc.end_ in
match findFunctionType ~currentFile ~debug ~path ~pos with
| Some (args, name, docstring, type_expr, package, _env, file) ->
| Some (args, docstring, type_expr, package, _env, file) ->
if debug then
Printf.printf "argAtCursor: %s\n"
(match argAtCursor with
Expand All @@ -296,19 +306,24 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
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
offsets from the parser. *)

(* 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. *)
let label = "let " ^ name ^ ": " ^ Shared.typeToString type_expr in
(* A full let binding with the type text is needed for the parser to be able to parse it. *)
let labelPrefix = "let fn: " in
let labelPrefixLen = String.length labelPrefix in
let fnTypeStr = Shared.typeToString type_expr in
let typeStrForParser = labelPrefix ^ fnTypeStr in
let {Res_driver.parsetree = signature} =
Res_driver.parseInterfaceFromSource ~forPrinter:false
~displayFilename:"<missing-file>" ~source:label
~displayFilename:"<missing-file>" ~source:typeStrForParser
in

let parameters = extractParameters ~signature ~label in
let parameters =
extractParameters ~signature ~typeStrForParser ~labelPrefixLen
in
if debug then
Printf.printf "extracted params: \n%s\n"
(parameters
|> List.map (fun (_, start, end_) ->
String.sub label start (end_ - start))
String.sub fnTypeStr start (end_ - start))
|> list);

(* Figure out the active parameter *)
Expand All @@ -318,7 +333,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
Protocol.signatures =
[
{
label;
label = fnTypeStr;
parameters =
parameters
|> List.map (fun (argLabel, start, end_) ->
Expand Down
Loading