Skip to content

Commit b26a607

Browse files
committed
identify and handle completing destructuring patterns
1 parent e378f57 commit b26a607

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

analysis/src/CompletionFrontEnd.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,12 +572,20 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
572572
in
573573
let structure_item (iterator : Ast_iterator.iterator)
574574
(item : Parsetree.structure_item) =
575+
unsetLookingForPat;
575576
let processed = ref false in
576577
(match item.pstr_desc with
577578
| Pstr_open {popen_lid} ->
578579
scope := !scope |> Scope.addOpen ~lid:popen_lid.txt
579580
| Pstr_primitive vd -> scopeValueDescription vd
580581
| Pstr_value (recFlag, bindings) ->
582+
(* Identify relevant destructures for completion, like `let {<com>} = someVar` or `let (true, false) = someFn()`. *)
583+
(match bindings with
584+
| [{pvb_pat = {ppat_desc = Ppat_record _ | Ppat_tuple _}; pvb_expr}] -> (
585+
match exprToContextPath pvb_expr with
586+
| None -> ()
587+
| Some ctxPath -> setLookingForPat ctxPath)
588+
| _ -> ());
581589
if recFlag = Recursive then bindings |> List.iter scopeValueBinding;
582590
bindings |> List.iter (fun vb -> iterator.value_binding iterator vb);
583591
if recFlag = Nonrecursive then bindings |> List.iter scopeValueBinding;

analysis/tests/src/CompletionPattern.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,6 @@ let _ = switch f {
7272
nest.nested
7373
| _ => false
7474
}
75+
76+
// let {} = f
77+
// ^com

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,33 @@ Completable: Cpattern Value[nest]->recordBody
214214
"documentation": null
215215
}]
216216

217+
Complete src/CompletionPattern.res 75:8
218+
looking for: Cpath Value[f]
219+
posCursor:[75:8] posNoWhite:[75:7] Found pattern:[75:7->75:9]
220+
Completable: Cpattern Value[f]->recordBody
221+
[{
222+
"label": "first",
223+
"kind": 5,
224+
"tags": [],
225+
"detail": "first: int\n\nsomeRecord",
226+
"documentation": null
227+
}, {
228+
"label": "second",
229+
"kind": 5,
230+
"tags": [],
231+
"detail": "second: (bool, option<someRecord>)\n\nsomeRecord",
232+
"documentation": null
233+
}, {
234+
"label": "optThird",
235+
"kind": 5,
236+
"tags": [],
237+
"detail": "optThird: option<[#second(someRecord) | #first]>\n\nsomeRecord",
238+
"documentation": null
239+
}, {
240+
"label": "nest",
241+
"kind": 5,
242+
"tags": [],
243+
"detail": "nest: nestedRecord\n\nsomeRecord",
244+
"documentation": null
245+
}]
246+

0 commit comments

Comments
 (0)