Skip to content

Commit 9fc87db

Browse files
committed
use value binding to check for destructures rather than just structure items
1 parent e78091a commit 9fc87db

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

analysis/src/CompletionFrontEnd.ml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,26 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
317317
in
318318
let scopeValueBinding (vb : Parsetree.value_binding) =
319319
scopePattern vb.pvb_pat;
320-
completePattern vb.pvb_pat
320+
(* Identify relevant destructures for completion, like `let {<com>} = someVar` or `let (true, false) = someFn()`. *)
321+
match vb with
322+
| {pvb_pat; pvb_expr} when locHasCursor pvb_pat.ppat_loc -> (
323+
match
324+
( pvb_pat
325+
|> CompletionPatterns.traversePattern ~patternPath:[] ~locHasCursor
326+
~firstCharBeforeCursorNoWhite ~posBeforeCursor,
327+
exprToContextPath pvb_expr )
328+
with
329+
| Some (prefix, nestedPattern), Some ctxPath ->
330+
setResult
331+
(Completable.Cpattern
332+
{
333+
contextPath = ctxPath;
334+
prefix;
335+
nested = List.rev nestedPattern;
336+
fallback = None;
337+
})
338+
| _ -> ())
339+
| _ -> ()
321340
in
322341
let scopeTypeKind (tk : Parsetree.type_kind) =
323342
match tk with
@@ -446,13 +465,6 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
446465
scope := !scope |> Scope.addOpen ~lid:popen_lid.txt
447466
| Pstr_primitive vd -> scopeValueDescription vd
448467
| Pstr_value (recFlag, bindings) ->
449-
(* Identify relevant destructures for completion, like `let {<com>} = someVar` or `let (true, false) = someFn()`. *)
450-
(match bindings with
451-
| [{pvb_pat = {ppat_desc = Ppat_record _ | Ppat_tuple _}; pvb_expr}] -> (
452-
match exprToContextPath pvb_expr with
453-
| None -> ()
454-
| Some ctxPath -> setLookingForPat ctxPath)
455-
| _ -> ());
456468
if recFlag = Recursive then bindings |> List.iter scopeValueBinding;
457469
bindings |> List.iter (fun vb -> iterator.value_binding iterator vb);
458470
if recFlag = Nonrecursive then bindings |> List.iter scopeValueBinding;

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,31 @@ Complete src/Destructuring.res 11:13
3030
posCursor:[11:13] posNoWhite:[11:11] Found expr:[10:8->14:1]
3131
posCursor:[11:13] posNoWhite:[11:11] Found expr:[11:2->13:6]
3232
posCursor:[11:13] posNoWhite:[11:11] Found pattern:[11:6->11:14]
33-
[]
33+
Completable: Cpattern Value[x]->recordBody
34+
[{
35+
"label": "age",
36+
"kind": 5,
37+
"tags": [],
38+
"detail": "age: int\n\nx",
39+
"documentation": null
40+
}]
3441

3542
Complete src/Destructuring.res 17:10
3643
posCursor:[17:10] posNoWhite:[17:9] Found expr:[16:9->20:1]
3744
posCursor:[17:10] posNoWhite:[17:9] Found expr:[17:5->19:11]
3845
posCursor:[17:10] posNoWhite:[17:9] Found pattern:[17:9->17:11]
39-
[]
46+
Completable: Cpattern Value[x]->recordBody
47+
[{
48+
"label": "name",
49+
"kind": 5,
50+
"tags": [],
51+
"detail": "name: string\n\nx",
52+
"documentation": null
53+
}, {
54+
"label": "age",
55+
"kind": 5,
56+
"tags": [],
57+
"detail": "age: int\n\nx",
58+
"documentation": null
59+
}]
4060

0 commit comments

Comments
 (0)