Skip to content

Commit bb4e36d

Browse files
authored
enable signature help for option and result (#955)
1 parent 69bfb26 commit bb4e36d

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

analysis/src/SignatureHelp.ml

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ let findActiveParameter ~argAtCursor ~args =
192192
index := !index + 1;
193193
None)
194194

195+
type constructorInfo = {
196+
docstring: string list;
197+
name: string;
198+
args: constructorArgs;
199+
}
200+
195201
let findConstructorArgs ~full ~env ~constructorName loc =
196202
match
197203
References.getLocItem ~debug:false ~full
@@ -200,10 +206,51 @@ let findConstructorArgs ~full ~env ~constructorName loc =
200206
| None -> None
201207
| Some {locType = Typed (_, typExpr, _)} -> (
202208
match TypeUtils.extractType ~env ~package:full.package typExpr with
209+
| Some ((Toption (_, TypeExpr t) as extractedType), _) -> (
210+
match constructorName with
211+
| "Some" ->
212+
Some
213+
{
214+
name = "Some";
215+
docstring =
216+
[
217+
Markdown.codeBlock
218+
(TypeUtils.extractedTypeToString extractedType);
219+
];
220+
args = Args [(t, Location.none)];
221+
}
222+
| _ -> None)
223+
| Some ((Tresult {okType; errorType} as extractedType), _) -> (
224+
match constructorName with
225+
| "Ok" ->
226+
Some
227+
{
228+
name = "Ok";
229+
docstring =
230+
[
231+
Markdown.codeBlock
232+
(TypeUtils.extractedTypeToString extractedType);
233+
];
234+
args = Args [(okType, Location.none)];
235+
}
236+
| "Error" ->
237+
Some
238+
{
239+
name = "Error";
240+
docstring =
241+
[
242+
Markdown.codeBlock
243+
(TypeUtils.extractedTypeToString extractedType);
244+
];
245+
args = Args [(errorType, Location.none)];
246+
}
247+
| _ -> None)
203248
| Some (Tvariant {constructors}, _) ->
204249
constructors
205250
|> List.find_opt (fun (c : Constructor.t) ->
206251
c.cname.txt = constructorName)
252+
|> Option.map (fun (c : Constructor.t) ->
253+
{docstring = c.docstring; name = c.cname.txt; args = c.args})
207254
| _ -> None)
208255
| _ -> None
209256

@@ -554,7 +601,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
554601
(startOffset, endOffset) ))))
555602
in
556603
let label =
557-
constructor.cname.txt ^ "("
604+
constructor.name ^ "("
558605
^ (match argParts with
559606
| None -> ""
560607
| Some (`InlineRecord fields) ->
@@ -659,7 +706,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
659706
| _ -> -1
660707
in
661708

662-
let constructorNameLength = String.length constructor.cname.txt in
709+
let constructorNameLength = String.length constructor.name in
663710
let params =
664711
match argParts with
665712
| None -> []

analysis/tests/src/SignatureHelp.res

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,12 @@ let _ = switch _one {
152152
| Three(_, _b) => ""
153153
// ^she
154154
}
155+
156+
let _bb = Ok(true)
157+
// ^she
158+
159+
let _bbb = Error("err")
160+
// ^she
161+
162+
let _cc = Some(true)
163+
// ^she

analysis/tests/src/expected/SignatureHelp.res.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,36 @@ Signature help src/SignatureHelp.res 151:12
560560
"activeParameter": 1
561561
}
562562

563+
Signature help src/SignatureHelp.res 155:14
564+
{
565+
"signatures": [{
566+
"label": "Ok(bool)",
567+
"parameters": [{"label": [3, 7], "documentation": {"kind": "markdown", "value": ""}}],
568+
"documentation": {"kind": "markdown", "value": "```rescript\nresult<bool, 'a>\n```"}
569+
}],
570+
"activeSignature": 0,
571+
"activeParameter": 0
572+
}
573+
574+
Signature help src/SignatureHelp.res 158:19
575+
{
576+
"signatures": [{
577+
"label": "Error(string)",
578+
"parameters": [{"label": [6, 12], "documentation": {"kind": "markdown", "value": ""}}],
579+
"documentation": {"kind": "markdown", "value": "```rescript\nresult<'a, string>\n```"}
580+
}],
581+
"activeSignature": 0,
582+
"activeParameter": 0
583+
}
584+
585+
Signature help src/SignatureHelp.res 161:16
586+
{
587+
"signatures": [{
588+
"label": "Some(bool)",
589+
"parameters": [{"label": [5, 9], "documentation": {"kind": "markdown", "value": ""}}],
590+
"documentation": {"kind": "markdown", "value": "```rescript\noption<bool>\n```"}
591+
}],
592+
"activeSignature": 0,
593+
"activeParameter": 0
594+
}
595+

0 commit comments

Comments
 (0)