Skip to content

Signature help - handle pipes #624

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

Merged
merged 3 commits into from
Nov 8, 2022
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
97 changes: 61 additions & 36 deletions analysis/src/SignatureHelp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,68 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
let posBeforeCursor = Pos.posBeforeCursor pos in
let supportsMarkdownLinks = true in
let foundFunctionApplicationExpr = ref None in
let setFound r = foundFunctionApplicationExpr := Some r in
let setFound r =
if !foundFunctionApplicationExpr = None then
foundFunctionApplicationExpr := Some r
in
let searchForArgWithCursor ~isPipeExpr ~args ~exp =
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole function body is just moved in here from below, with very minor changes.

let extractedArgs = extractExpApplyArgs ~args in
let argAtCursor =
let unlabelledArgCount = ref (if isPipeExpr then 1 else 0) in
extractedArgs
|> List.find_map (fun arg ->
match arg.label with
| None ->
let currentUnlabelledArgCount = !unlabelledArgCount in
unlabelledArgCount := currentUnlabelledArgCount + 1;
(* An argument without a label is just the expression, so we can use that. *)
if arg.exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then
Some (Unlabelled currentUnlabelledArgCount)
else None
| Some {name; posStart; posEnd} -> (
(* Check for the label identifier itself having the cursor *)
match
pos |> CursorPosition.classifyPositions ~posStart ~posEnd
with
| HasCursor -> Some (Labelled name)
| NoCursor | EmptyLoc -> (
(* If we're not in the label, check the exp. Either the exp
exists and has the cursor. Or the exp is a parser recovery
node, in which case we assume that the parser recovery
indicates that the cursor was here. *)
match
( arg.exp.pexp_desc,
arg.exp.pexp_loc
|> CursorPosition.classifyLoc ~pos:posBeforeCursor )
with
| Pexp_extension ({txt = "rescript.exprhole"}, _), _
| _, HasCursor ->
Some (Labelled name)
| _ -> None)))
in
setFound (argAtCursor, exp, extractedArgs)
in
let expr (iterator : Ast_iterator.iterator) (expr : Parsetree.expression) =
(match expr with
(* Handle pipes, like someVar->someFunc(... *)
| {
pexp_desc =
Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
[
_;
( _,
{
pexp_desc =
Pexp_apply (({pexp_desc = Pexp_ident _} as exp), args);
pexp_loc;
} );
] );
}
when pexp_loc
|> CursorPosition.classifyLoc ~pos:posBeforeCursor
== HasCursor ->
searchForArgWithCursor ~isPipeExpr:true ~args ~exp
(* Look for applying idents, like someIdent(...) *)
| {
pexp_desc = Pexp_apply (({pexp_desc = Pexp_ident _} as exp), args);
Expand All @@ -165,41 +224,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
when pexp_loc
|> CursorPosition.classifyLoc ~pos:posBeforeCursor
== HasCursor ->
let extractedArgs = extractExpApplyArgs ~args in
let argAtCursor =
let unlabelledArgCount = ref 0 in
extractedArgs
|> List.find_map (fun arg ->
match arg.label with
| None ->
let currentUnlabelledArgCount = !unlabelledArgCount in
unlabelledArgCount := currentUnlabelledArgCount + 1;
(* An argument without a label is just the expression, so we can use that. *)
if arg.exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then
Some (Unlabelled currentUnlabelledArgCount)
else None
| Some {name; posStart; posEnd} -> (
(* Check for the label identifier itself having the cursor *)
match
pos |> CursorPosition.classifyPositions ~posStart ~posEnd
with
| HasCursor -> Some (Labelled name)
| NoCursor | EmptyLoc -> (
(* If we're not in the label, check the exp. Either the exp
exists and has the cursor. Or the exp is a parser recovery
node, in which case we assume that the parser recovery
indicates that the cursor was here. *)
match
( arg.exp.pexp_desc,
arg.exp.pexp_loc
|> CursorPosition.classifyLoc ~pos:posBeforeCursor )
with
| Pexp_extension ({txt = "rescript.exprhole"}, _), _
| _, HasCursor ->
Some (Labelled name)
| _ -> None)))
in
setFound (argAtCursor, exp, extractedArgs)
searchForArgWithCursor ~isPipeExpr:false ~args ~exp
| _ -> ());
Ast_iterator.default_iterator.expr iterator expr
in
Expand Down
3 changes: 3 additions & 0 deletions analysis/tests/src/SignatureHelp.res
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ let iAmSoSpecial = (iJustHaveOneArg: string) => {

// let _ = iAmSoSpecial(
// ^she

// let _ = "hello"->otherFunc(1
// ^she
20 changes: 19 additions & 1 deletion analysis/tests/src/expected/SignatureHelp.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ extracted params:

Signature help src/SignatureHelp.res 50:24
posCursor:[50:23] posNoWhite:[50:22] Found expr:[50:11->50:24]
Pexp_apply ...[50:11->50:23] (...[53:0->50:24])
Pexp_apply ...[50:11->50:23] (...[56:0->50:24])
posCursor:[50:23] posNoWhite:[50:22] Found expr:[50:11->50:23]
Pexp_ident iAmSoSpecial:[50:11->50:23]
argAtCursor: none
Expand All @@ -197,3 +197,21 @@ extracted params:
"activeParameter": 0
}

Signature help src/SignatureHelp.res 53:31
posCursor:[53:29] posNoWhite:[53:28] Found expr:[53:11->53:31]
posCursor:[53:29] posNoWhite:[53:28] Found expr:[53:20->53:31]
Pexp_apply ...[53:20->53:29] (...[53:30->53:31])
posCursor:[53:29] posNoWhite:[53:28] Found expr:[53:20->53:29]
Pexp_ident otherFunc:[53:20->53:29]
argAtCursor: unlabelled<1>
extracted params:
[(string, int, float]
{
"signatures": [{
"label": "let otherFunc: (string, int, float) => unit",
"parameters": [{"label": [15, 22], "documentation": {"kind": "markdown", "value": "```rescript\nstring\n```"}}, {"label": [24, 27], "documentation": {"kind": "markdown", "value": "```rescript\nstring\n```"}}, {"label": [29, 34], "documentation": {"kind": "markdown", "value": "```rescript\nstring\n```"}}]
}],
"activeSignature": 0,
"activeParameter": 1
}