Skip to content

Commit d6a67fb

Browse files
committed
handle options with annotations
1 parent 7ee892e commit d6a67fb

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,19 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
630630
(Completion.Value
631631
(Ctype.newconstr (Path.Pident (Ident.create "array")) []));
632632
]
633+
| CPOption cp -> (
634+
match
635+
cp
636+
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
637+
~exact:true ~scope
638+
|> completionsGetCompletionType ~full
639+
with
640+
| None -> []
641+
| Some (typ, env) ->
642+
[
643+
Completion.create "dummy" ~env
644+
~kind:(Completion.ExtractedType (Toption (env, typ), `Type));
645+
])
633646
| CPId (path, completionContext) ->
634647
path
635648
|> getCompletionsForPath ~package ~opens ~allFiles ~pos ~exact

analysis/src/SharedTypes.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ module Completable = struct
589589
| CPArray
590590
| CPInt
591591
| CPFloat
592+
| CPOption of contextPath
592593
| CPApply of contextPath * Asttypes.arg_label list
593594
| CPId of string list * completionContext
594595
| CPField of contextPath * string
@@ -662,6 +663,7 @@ module Completable = struct
662663
| CPString -> "string"
663664
| CPInt -> "int"
664665
| CPFloat -> "float"
666+
| CPOption ctxPath -> "option<" ^ contextPathToString ctxPath ^ ">"
665667
| CPApply (cp, labels) ->
666668
contextPathToString cp ^ "("
667669
^ (labels

analysis/src/TypeUtils.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,13 @@ let typeIsUnit (typ : Types.type_expr) =
379379
true
380380
| _ -> false
381381

382-
let contextPathFromCoreType (coreType : Parsetree.core_type) =
382+
let rec contextPathFromCoreType (coreType : Parsetree.core_type) =
383383
match coreType.ptyp_desc with
384-
| Ptyp_constr (lid, []) ->
385-
Some (Completable.CPId (lid.txt |> Utils.flattenLongIdent, Type))
384+
| Ptyp_constr ({txt = Lident "option"}, [innerTyp]) ->
385+
innerTyp |> contextPathFromCoreType
386+
|> Option.map (fun innerTyp -> Completable.CPOption innerTyp)
387+
| Ptyp_constr (lid, _) ->
388+
Some (CPId (lid.txt |> Utils.flattenLongIdent, Type))
386389
| _ -> None
387390

388391
let printRecordFromFields ?name (fields : field list) =

analysis/tests/src/CompletionTypeAnnotation.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ type someTuple = (bool, option<bool>)
3737

3838
// let x: someTuple = (true, )
3939
// ^com
40+
41+
// let x: option<someVariant> =
42+
// ^com

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,38 @@ Completable: Cexpression Type[someTuple]->tuple($1)
168168
"documentation": null
169169
}]
170170

171+
Complete src/CompletionTypeAnnotation.res 40:31
172+
XXX Not found!
173+
Completable: Cexpression option<Type[someVariant]>
174+
[{
175+
"label": "None",
176+
"kind": 4,
177+
"tags": [],
178+
"detail": "type someVariant = One | Two(bool)",
179+
"documentation": null
180+
}, {
181+
"label": "Some(_)",
182+
"kind": 4,
183+
"tags": [],
184+
"detail": "type someVariant = One | Two(bool)",
185+
"documentation": null,
186+
"insertText": "Some(${1:_})",
187+
"insertTextFormat": 2
188+
}, {
189+
"label": "Some(One)",
190+
"kind": 4,
191+
"tags": [],
192+
"detail": "One\n\ntype someVariant = One | Two(bool)",
193+
"documentation": null,
194+
"insertText": "Some(One)",
195+
"insertTextFormat": 2
196+
}, {
197+
"label": "Some(Two(_))",
198+
"kind": 4,
199+
"tags": [],
200+
"detail": "Two(bool)\n\ntype someVariant = One | Two(bool)",
201+
"documentation": null,
202+
"insertText": "Some(Two(${1:_}))",
203+
"insertTextFormat": 2
204+
}]
205+

0 commit comments

Comments
 (0)