Skip to content

Enable signature help for option and result #955

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 1 commit into from
Mar 13, 2024
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
51 changes: 49 additions & 2 deletions analysis/src/SignatureHelp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ let findActiveParameter ~argAtCursor ~args =
index := !index + 1;
None)

type constructorInfo = {
docstring: string list;
name: string;
args: constructorArgs;
}

let findConstructorArgs ~full ~env ~constructorName loc =
match
References.getLocItem ~debug:false ~full
Expand All @@ -200,10 +206,51 @@ let findConstructorArgs ~full ~env ~constructorName loc =
| None -> None
| Some {locType = Typed (_, typExpr, _)} -> (
match TypeUtils.extractType ~env ~package:full.package typExpr with
| Some ((Toption (_, TypeExpr t) as extractedType), _) -> (
match constructorName with
| "Some" ->
Some
{
name = "Some";
docstring =
[
Markdown.codeBlock
(TypeUtils.extractedTypeToString extractedType);
];
args = Args [(t, Location.none)];
}
| _ -> None)
| Some ((Tresult {okType; errorType} as extractedType), _) -> (
match constructorName with
| "Ok" ->
Some
{
name = "Ok";
docstring =
[
Markdown.codeBlock
(TypeUtils.extractedTypeToString extractedType);
];
args = Args [(okType, Location.none)];
}
| "Error" ->
Some
{
name = "Error";
docstring =
[
Markdown.codeBlock
(TypeUtils.extractedTypeToString extractedType);
];
args = Args [(errorType, Location.none)];
}
| _ -> None)
| Some (Tvariant {constructors}, _) ->
constructors
|> List.find_opt (fun (c : Constructor.t) ->
c.cname.txt = constructorName)
|> Option.map (fun (c : Constructor.t) ->
{docstring = c.docstring; name = c.cname.txt; args = c.args})
| _ -> None)
| _ -> None

Expand Down Expand Up @@ -554,7 +601,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
(startOffset, endOffset) ))))
in
let label =
constructor.cname.txt ^ "("
constructor.name ^ "("
^ (match argParts with
| None -> ""
| Some (`InlineRecord fields) ->
Expand Down Expand Up @@ -659,7 +706,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
| _ -> -1
in

let constructorNameLength = String.length constructor.cname.txt in
let constructorNameLength = String.length constructor.name in
let params =
match argParts with
| None -> []
Expand Down
9 changes: 9 additions & 0 deletions analysis/tests/src/SignatureHelp.res
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,12 @@ let _ = switch _one {
| Three(_, _b) => ""
// ^she
}

let _bb = Ok(true)
// ^she

let _bbb = Error("err")
// ^she

let _cc = Some(true)
// ^she
33 changes: 33 additions & 0 deletions analysis/tests/src/expected/SignatureHelp.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,36 @@ Signature help src/SignatureHelp.res 151:12
"activeParameter": 1
}

Signature help src/SignatureHelp.res 155:14
{
"signatures": [{
"label": "Ok(bool)",
"parameters": [{"label": [3, 7], "documentation": {"kind": "markdown", "value": ""}}],
"documentation": {"kind": "markdown", "value": "```rescript\nresult<bool, 'a>\n```"}
}],
"activeSignature": 0,
"activeParameter": 0
}

Signature help src/SignatureHelp.res 158:19
{
"signatures": [{
"label": "Error(string)",
"parameters": [{"label": [6, 12], "documentation": {"kind": "markdown", "value": ""}}],
"documentation": {"kind": "markdown", "value": "```rescript\nresult<'a, string>\n```"}
}],
"activeSignature": 0,
"activeParameter": 0
}

Signature help src/SignatureHelp.res 161:16
{
"signatures": [{
"label": "Some(bool)",
"parameters": [{"label": [5, 9], "documentation": {"kind": "markdown", "value": ""}}],
"documentation": {"kind": "markdown", "value": "```rescript\noption<bool>\n```"}
}],
"activeSignature": 0,
"activeParameter": 0
}