Skip to content

Commit bf3d9dd

Browse files
committed
add Some(fieldName) to completions of optional values on field names
1 parent 5b504d4 commit bf3d9dd

File tree

4 files changed

+60
-29
lines changed

4 files changed

+60
-29
lines changed

analysis/src/CompletionBackEnd.ml

+21-8
Original file line numberDiff line numberDiff line change
@@ -1235,13 +1235,26 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
12351235
| Some insertText -> Some ("Some(" ^ insertText ^ ")"));
12361236
})
12371237
in
1238-
[
1239-
Completion.create "None" ~kind:(kindFromInnerType t) ~env;
1238+
let noneCase = Completion.create "None" ~kind:(kindFromInnerType t) ~env in
1239+
let someAnyCase =
12401240
Completion.createWithSnippet ~name:"Some(_)" ~kind:(kindFromInnerType t)
1241-
~env ~insertText:"Some(${1:_})" ();
1242-
]
1243-
@ expandedCompletions
1244-
|> filterItems ~prefix
1241+
~env ~insertText:"Some(${1:_})" ()
1242+
in
1243+
let completions =
1244+
match completionContext with
1245+
| Some (Completable.CameFromRecordField fieldName) ->
1246+
[
1247+
Completion.createWithSnippet
1248+
~name:("Some(" ^ fieldName ^ ")")
1249+
~kind:(kindFromInnerType t) ~env
1250+
~insertText:("Some(${1:" ^ fieldName ^ "})")
1251+
();
1252+
someAnyCase;
1253+
noneCase;
1254+
]
1255+
| _ -> [noneCase; someAnyCase]
1256+
in
1257+
completions @ expandedCompletions |> filterItems ~prefix
12451258
| Tuple (env, exprs, typ) ->
12461259
let numExprs = List.length exprs in
12471260
[
@@ -1279,7 +1292,7 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
12791292
(Field (field, TypeUtils.extractedTypeToString extractedType))
12801293
~env)
12811294
|> filterItems ~prefix
1282-
| None ->
1295+
| _ ->
12831296
if prefix = "" then
12841297
[
12851298
Completion.createWithSnippet ~name:"{}"
@@ -1304,7 +1317,7 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
13041317
Completion.create field.fname.txt ~kind:(Label "Inline record")
13051318
?deprecated:field.deprecated ~env)
13061319
|> filterItems ~prefix
1307-
| None ->
1320+
| _ ->
13081321
if prefix = "" then
13091322
[
13101323
Completion.createWithSnippet ~name:"{}"

analysis/src/SharedTypes.ml

+6-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,12 @@ module Completable = struct
529529
| Optional of string
530530

531531
(** Additional context for nested completion where needed. *)
532-
type nestedContext = RecordField of {seenFields: string list}
532+
type nestedContext =
533+
| RecordField of {seenFields: string list}
534+
(** Completing for a record field, and we already saw the following fields... *)
535+
| CameFromRecordField of string
536+
(** We just came from this field (we leverage use this for better
537+
completion names etc) *)
533538

534539
type nestedPath =
535540
| NTupleItem of {itemNum: int}

analysis/src/TypeUtils.ml

+13-3
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,20 @@ let extractTypeFromResolvedType (typ : Type.t) ~env ~full =
257257
| None -> None
258258
| Some t -> t |> extractType ~env ~package:full.package)
259259

260+
(** The context we just came from as we resolve the nested structure. *)
261+
type ctx = Rfield of string (** A record field of name *)
262+
260263
(** This moves through a nested path via a set of instructions, trying to resolve the type at the end of the path. *)
261-
let rec resolveNested (typ : completionType) ~env ~full ~nested =
264+
let rec resolveNested ~env ~full ~nested ?ctx (typ : completionType) =
262265
match nested with
263-
| [] -> Some (typ, env, None)
266+
| [] ->
267+
Some
268+
( typ,
269+
env,
270+
match ctx with
271+
| None -> None
272+
| Some (Rfield fieldName) ->
273+
Some (Completable.CameFromRecordField fieldName) )
264274
| patternPath :: nested -> (
265275
match (patternPath, typ) with
266276
| Completable.NTupleItem {itemNum}, Tuple (env, tupleItems, _) -> (
@@ -283,7 +293,7 @@ let rec resolveNested (typ : completionType) ~env ~full ~nested =
283293
typ
284294
|> extractType ~env ~package:full.package
285295
|> Utils.Option.flatMap (fun typ ->
286-
typ |> resolveNested ~env ~full ~nested))
296+
typ |> resolveNested ~ctx:(Rfield fieldName) ~env ~full ~nested))
287297
| NRecordBody {seenFields}, Trecord {env; definition = `TypeExpr typeExpr}
288298
->
289299
typeExpr

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

+20-17
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,13 @@ ContextPath CArgument Value[fnTakingRecord]($0)
191191
ContextPath Value[fnTakingRecord]
192192
Path fnTakingRecord
193193
[{
194-
"label": "None",
194+
"label": "Some(nested)",
195195
"kind": 12,
196196
"tags": [],
197197
"detail": "otherRecord",
198-
"documentation": null
198+
"documentation": null,
199+
"insertText": "Some(${1:nested})",
200+
"insertTextFormat": 2
199201
}, {
200202
"label": "Some(_)",
201203
"kind": 12,
@@ -204,6 +206,12 @@ Path fnTakingRecord
204206
"documentation": null,
205207
"insertText": "Some(${1:_})",
206208
"insertTextFormat": 2
209+
}, {
210+
"label": "None",
211+
"kind": 12,
212+
"tags": [],
213+
"detail": "otherRecord",
214+
"documentation": null
207215
}, {
208216
"label": "Some({})",
209217
"kind": 12,
@@ -298,21 +306,8 @@ Path fnTakingRecord
298306
"tags": [],
299307
"detail": "One\n\ntype someVariant = One | Two | Three(int, string)",
300308
"documentation": null,
301-
"sortText": "A One",
302309
"insertText": "One",
303310
"insertTextFormat": 2
304-
}, {
305-
"label": "Obj",
306-
"kind": 9,
307-
"tags": [],
308-
"detail": "file module",
309-
"documentation": null
310-
}, {
311-
"label": "Objects",
312-
"kind": 9,
313-
"tags": [],
314-
"detail": "file module",
315-
"documentation": null
316311
}]
317312

318313
Complete src/CompletionExpressions.res 56:57
@@ -616,11 +611,13 @@ ContextPath CArgument Value[fnTakingRecordWithOptVariant]($0)
616611
ContextPath Value[fnTakingRecordWithOptVariant]
617612
Path fnTakingRecordWithOptVariant
618613
[{
619-
"label": "None",
614+
"label": "Some(someVariant)",
620615
"kind": 12,
621616
"tags": [],
622617
"detail": "someVariant",
623-
"documentation": null
618+
"documentation": null,
619+
"insertText": "Some(${1:someVariant})",
620+
"insertTextFormat": 2
624621
}, {
625622
"label": "Some(_)",
626623
"kind": 12,
@@ -629,6 +626,12 @@ Path fnTakingRecordWithOptVariant
629626
"documentation": null,
630627
"insertText": "Some(${1:_})",
631628
"insertTextFormat": 2
629+
}, {
630+
"label": "None",
631+
"kind": 12,
632+
"tags": [],
633+
"detail": "someVariant",
634+
"documentation": null
632635
}, {
633636
"label": "Some(One)",
634637
"kind": 4,

0 commit comments

Comments
 (0)