Skip to content

Add broken test for record destructuring #705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,26 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
in
let scopeValueBinding (vb : Parsetree.value_binding) =
scopePattern vb.pvb_pat;
completePattern vb.pvb_pat
(* Identify relevant destructures for completion, like `let {<com>} = someVar` or `let (true, false) = someFn()`. *)
match vb with
| {pvb_pat; pvb_expr} when locHasCursor pvb_pat.ppat_loc -> (
match
( pvb_pat
|> CompletionPatterns.traversePattern ~patternPath:[] ~locHasCursor
~firstCharBeforeCursorNoWhite ~posBeforeCursor,
exprToContextPath pvb_expr )
with
| Some (prefix, nestedPattern), Some ctxPath ->
setResult
(Completable.Cpattern
{
contextPath = ctxPath;
prefix;
nested = List.rev nestedPattern;
fallback = None;
})
| _ -> ())
| _ -> ()
in
let scopeTypeKind (tk : Parsetree.type_kind) =
match tk with
Expand Down Expand Up @@ -446,13 +465,6 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
scope := !scope |> Scope.addOpen ~lid:popen_lid.txt
| Pstr_primitive vd -> scopeValueDescription vd
| Pstr_value (recFlag, bindings) ->
(* Identify relevant destructures for completion, like `let {<com>} = someVar` or `let (true, false) = someFn()`. *)
(match bindings with
| [{pvb_pat = {ppat_desc = Ppat_record _ | Ppat_tuple _}; pvb_expr}] -> (
match exprToContextPath pvb_expr with
| None -> ()
| Some ctxPath -> setLookingForPat ctxPath)
| _ -> ());
if recFlag = Recursive then bindings |> List.iter scopeValueBinding;
bindings |> List.iter (fun vb -> iterator.value_binding iterator vb);
if recFlag = Nonrecursive then bindings |> List.iter scopeValueBinding;
Expand Down
21 changes: 21 additions & 0 deletions analysis/tests/src/Destructuring.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type x = {name: string, age: int}

let x = {name: "123", age: 12}

let {name, } = x
// ^com

// let {} = x
// ^com

let f = (x: x) => {
let {name, } = x
// ^com
name
}

let f2 = (x: x) => {
// let {} = x
// ^com
ignore(x)
}
60 changes: 60 additions & 0 deletions analysis/tests/src/expected/Destructuring.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Complete src/Destructuring.res 4:11
posCursor:[4:11] posNoWhite:[4:9] Found pattern:[4:4->4:12]
Completable: Cpattern Value[x]->recordBody
[{
"label": "age",
"kind": 5,
"tags": [],
"detail": "age: int\n\nx",
"documentation": null
}]

Complete src/Destructuring.res 7:8
posCursor:[7:8] posNoWhite:[7:7] Found pattern:[7:7->7:9]
Completable: Cpattern Value[x]->recordBody
[{
"label": "name",
"kind": 5,
"tags": [],
"detail": "name: string\n\nx",
"documentation": null
}, {
"label": "age",
"kind": 5,
"tags": [],
"detail": "age: int\n\nx",
"documentation": null
}]

Complete src/Destructuring.res 11:13
posCursor:[11:13] posNoWhite:[11:11] Found expr:[10:8->14:1]
posCursor:[11:13] posNoWhite:[11:11] Found expr:[11:2->13:6]
posCursor:[11:13] posNoWhite:[11:11] Found pattern:[11:6->11:14]
Completable: Cpattern Value[x]->recordBody
[{
"label": "age",
"kind": 5,
"tags": [],
"detail": "age: int\n\nx",
"documentation": null
}]

Complete src/Destructuring.res 17:10
posCursor:[17:10] posNoWhite:[17:9] Found expr:[16:9->20:1]
posCursor:[17:10] posNoWhite:[17:9] Found expr:[17:5->19:11]
posCursor:[17:10] posNoWhite:[17:9] Found pattern:[17:9->17:11]
Completable: Cpattern Value[x]->recordBody
[{
"label": "name",
"kind": 5,
"tags": [],
"detail": "name: string\n\nx",
"documentation": null
}, {
"label": "age",
"kind": 5,
"tags": [],
"detail": "age: int\n\nx",
"documentation": null
}]